<commit_before>from maproulette import app
from flask.ext.restful import reqparse, fields, marshal, \
    marshal_with, Api, Resource
from flask.ext.restful.fields import Raw
from flask import session, request, abort, url_for
from maproulette.helpers import get_random_task,\
    get_challenge_or_404, get_task_or_404,\
    require_signedin, osmerror, challenge_exists,\
    parse_task_json
from maproulette.models import User, Challenge, Task, Action, db
from sqlalchemy.sql import func
from sqlalchemy.sql.expression import cast
from geoalchemy2.functions import ST_Buffer, ST_DWithin
from geoalchemy2.shape import to_shape, from_shape
from geoalchemy2.types import Geography
from shapely.geometry import Point
import geojson
import json
import markdown
import requests


class ProtectedResource(Resource):

    """A Resource that requires the caller to be authenticated against OSM"""
    method_decorators = [require_signedin]


class PointField(Raw):

    """An encoded point"""

    def format(self, geometry):
        return '%f|%f' % to_shape(geometry).coords[0]


class MarkdownField(Raw):

    """Markdown text"""

    def format(self, text):
        return markdown.markdown(text)

challenge_summary = {
    'slug': fields.String,
    'title': fields.String,
    'difficulty': fields.Integer,
    'description': fields.String,
    'help': MarkdownField,
    'blurb': fields.String,
    'islocal': fields.Boolean
}

task_fields = {
    'identifier': fields.String(attribute='identifier'),
    'instruction': fields.String(attribute='instruction'),
    'location': PointField,
    'status': fields.String
}

me_fields = {
    'username': fields.String(attribute='display_name'),
    'osm_id': fields.String,
    'lat': fields.Float,
    'lon': fields.Float,
    'radius': fields.Integer
}

action_fields = {
    'task': fields.String(attribute='task_id'),
    'timestamp': fields.DateTime,
    'status': fields.String,
    'user': fields.String(attribute='user_id')
}

api = Api(app)

# override the default JSON representation to support the geo objects


def user_has_defined_area():
    return 'lon' and 'lat' and 'radius' in session


def refine_with_user_area(query):
    if 'lon' and 'lat' and 'radius' in session:
        return query.filter(ST_DWithin(
            cast(Task.location, Geography),
            cast(from_shape(Point(session["lon"], session["lat"])), Geography),
            session["radius"]))
    else:
        return query


class ApiPing(Resource):

    """a simple ping endpoint"""

    def get(self):
        return ["I am alive"]


class ApiStatsMe(ProtectedResource):

    def get(self):
        me = {}
        challenges = {}
        # select min(a.timestamp) firsttime, count(1), a.status, c.slug from
        # actions a, tasks t, challenges c where a.task_id = t.id and
        # t.challenge_slug = c.slug and a.user_id = 437<commit_msg>removing obsolete function<commit_after>from maproulette import app
from flask.ext.restful import reqparse, fields, marshal, \
    marshal_with, Api, Resource
from flask.ext.restful.fields import Raw
from flask import session, request, abort, url_for
from maproulette.helpers import get_random_task,\
    get_challenge_or_404, get_task_or_404,\
    require_signedin, osmerror, challenge_exists,\
    parse_task_json
from maproulette.models import User, Challenge, Task, Action, db
from sqlalchemy.sql import func
from sqlalchemy.sql.expression import cast
from geoalchemy2.functions import ST_Buffer, ST_DWithin
from geoalchemy2.shape import to_shape, from_shape
from geoalchemy2.types import Geography
from shapely.geometry import Point
import geojson
import json
import markdown
import requests


class ProtectedResource(Resource):

    """A Resource that requires the caller to be authenticated against OSM"""
    method_decorators = [require_signedin]


class PointField(Raw):

    """An encoded point"""

    def format(self, geometry):
        return '%f|%f' % to_shape(geometry).coords[0]


class MarkdownField(Raw):

    """Markdown text"""

    def format(self, text):
        return markdown.markdown(text)

challenge_summary = {
    'slug': fields.String,
    'title': fields.String,
    'difficulty': fields.Integer,
    'description': fields.String,
    'help': MarkdownField,
    'blurb': fields.String,
    'islocal': fields.Boolean
}

task_fields = {
    'identifier': fields.String(attribute='identifier'),
    'instruction': fields.String(attribute='instruction'),
    'location': PointField,
    'status': fields.String
}

me_fields = {
    'username': fields.String(attribute='display_name'),
    'osm_id': fields.String,
    'lat': fields.Float,
    'lon': fields.Float,
    'radius': fields.Integer
}

action_fields = {
    'task': fields.String(attribute='task_id'),
    'timestamp': fields.DateTime,
    'status': fields.String,
    'user': fields.String(attribute='user_id')
}

api = Api(app)

# override the default JSON representation to support the geo objects


def refine_with_user_area(query):
    if 'lon' and 'lat' and 'radius' in session:
        return query.filter(ST_DWithin(
            cast(Task.location, Geography),
            cast(from_shape(Point(session["lon"], session["lat"])), Geography),
            session["radius"]))
    else:
        return query


class ApiPing(Resource):

    """a simple ping endpoint"""

    def get(self):
        return ["I am alive"]


class ApiStatsMe(ProtectedResource):

    def get(self):
        me = {}
        challenges = {}
        # select min(a.timestamp) firsttime, count(1), a.status, c.slug from
        # actions a, tasks t, challenges c where a.task_id = t.id and
        # t.challenge_slug = c.slug and a.user_id = 437
{'fraction_non_alphanumeric': 0.1143509573160021, 'fraction_numerical': 0.004918320744774284, 'mean_word_length': 3.796967144060657, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '895765', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1710, 'n_words': 467}