<commit_before>import json

from maproulette import app, models
from maproulette.models import Challenge, Task, Action
from flask import render_template, redirect, session, jsonify, abort, request
from flask.ext.sqlalchemy import get_debug_queries
from geoalchemy2.functions import ST_Contains, ST_Intersects, ST_Buffer, ST_AsText
from geoalchemy2.shape import to_shape
from sqlalchemy import and_
from shapely.wkt import dumps

# By default, send out the standard client
@app.route('/')
def index():
    "Display the index.html"
    return render_template('index.html')

### CLIENT API ###

@app.route('/api/c/challenges')
def challenges_api():
    """returns a list of challenges as json
    optional URL parameters are
    difficulty: the desired difficulty to filter on (1=easy, 2=medium, 3=hard)
    contains: the coordinate to filter on (as lon|lat, returns only 
    challenges whose bounding polygons contain this point)
    example: /get/challenges?contains=-100.22|40.45&difficulty=2
    """    
    difficulty = request.args.get('difficulty')
    contains = request.args.get('contains')
    if contains:
        try:
            coordWKT = 'POINT(%s %s)' % tuple(contains.split("|"))
        except:
            contains = None
    if difficulty and contains:
        challenges =  Challenge.query.filter(and_(
            Challenge.difficulty == difficulty,
            Challenge.polygon.ST_Contains(coordWKT))).all()
    elif difficulty:
        challenges = Challenge.query.filter(
            Challenge.difficulty == difficulty).all()
    elif contains:
        challenges = Challenge.query.filter(
            Challenge.polygon.ST_Contains(coordWKT)).all()
    else:
        challenges = Challenge.query.all()
    return jsonify(challenges =
        [i.id for i in challenges if i.active])

@app.route('/api/c/challenges/<challenge_id>')
    "Returns the metadata for a challenge"
    c = Challenge.query.filter(Challenge.id==challenge_id).first_or_404()
    return jsonify(challenge = {
            'slug': c.slug,
            'title': c.title,
            'description': c.description,
            'blurb': c.blurb,
            'help': c.help,
            'doneDlg': json.loads(c.done_dialog),
            'instruction': c.instruction})

@app.route('/api/c/challenges/<challenge_id>/stats')
def challenge_stats(challenge_id):
    "Returns stat data for a challenge"
    c = Challenge.query.filter(Challenge.id==challenge_id).first_or_404()
    #challenge_obj = models.types[c.type](c.id)
    tasks = Task.query.filter(challenge_id==c.id).all()
    total = len(tasks)
    available = len([t for t in tasks if c._get_task_available(t)])
    return jsonify(stats={'total': 100, 'available': available})

# THIS FUNCTION IS NOT COMPLETE!!! #
@app.route('/api/c/challenges/<challenge_id>/tasks')<commit_msg>Recommitting the code to make challenge availability checks work<commit_after>import json

from maproulette import app, models
from maproulette.models import Challenge, Task, Action
from flask import render_template, redirect, session, jsonify, abort, request
from flask.ext.sqlalchemy import get_debug_queries
from geoalchemy2.functions import ST_Contains, ST_Intersects, ST_Buffer, ST_AsText
from geoalchemy2.shape import to_shape
from sqlalchemy import and_
from shapely.wkt import dumps

# By default, send out the standard client
@app.route('/')
def index():
    "Display the index.html"
    return render_template('index.html')

### CLIENT API ###

@app.route('/api/c/challenges')
def challenges_api():
    """returns a list of challenges as json
    optional URL parameters are
    difficulty: the desired difficulty to filter on (1=easy, 2=medium, 3=hard)
    contains: the coordinate to filter on (as lon|lat, returns only 
    challenges whose bounding polygons contain this point)
    example: /get/challenges?contains=-100.22|40.45&difficulty=2
    """    
    difficulty = request.args.get('difficulty')
    contains = request.args.get('contains')
    if contains:
        try:
            coordWKT = 'POINT(%s %s)' % tuple(contains.split("|"))
        except:
            contains = None
    if difficulty and contains:
        challenges =  Challenge.query.filter(and_(
            Challenge.difficulty == difficulty,
            Challenge.polygon.ST_Contains(coordWKT))).all()
    elif difficulty:
        challenges = Challenge.query.filter(
            Challenge.difficulty == difficulty).all()
    elif contains:
        challenges = Challenge.query.filter(
            Challenge.polygon.ST_Contains(coordWKT)).all()
    else:
        challenges = Challenge.query.all()
    return jsonify(challenges =
        [i.id for i in challenges if i.active])

@app.route('/api/c/challenges/<challenge_id>')
    "Returns the metadata for a challenge"
    c = Challenge.query.filter(Challenge.id==challenge_id).first_or_404()
    return jsonify(challenge = {
            'slug': c.slug,
            'title': c.title,
            'description': c.description,
            'blurb': c.blurb,
            'help': c.help,
            'doneDlg': json.loads(c.done_dialog),
            'instruction': c.instruction})

@app.route('/api/c/challenges/<challenge_id>/stats')
def challenge_stats(challenge_id):
    "Returns stat data for a challenge"
    c = Challenge.query.filter(Challenge.id==challenge_id).first_or_404()
    # Here be dragons. If you do not understand c vs chal, do not touch this!
    chal = challenge_types[c.type]()
    tasks = Task.query.filter(challenge_id==c.id).all()
    total = len(tasks)
    available = len([t for t in tasks if chal._get_task_available(t)])
    rehyturn jsonify(stats={'total': 100, 'available': available})

# THIS FUNCTION IS NOT COMPLETE!!! #
@app.route('/api/c/challenges/<challenge_id>/tasks')
{'fraction_non_alphanumeric': 0.11677945514488981, 'fraction_numerical': 0.008328995314940135, 'mean_word_length': 3.31437125748503, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5962794', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1702, 'n_words': 446}