<commit_before>import json

from flask import request, session, url_for
from flask.ext.jsonpify import jsonify

from .. import preprocessors, responses
from ...database import IntegrityError, NotFoundError


def configure(bp, config, db):

    @bp.route("/campaigns/", methods=["GET"])
    def campaigns_index():
        """
        Returns a list of wikis that have active campaigns.
        """
        info = "Welcome to the campaigns module.  This module provides " + \
               "access to data using a heirachical strategy: 'wikis' " + \
               "have 'campaigns', 'campaigns' have 'worksets' and " + \
               "'worksets' have 'tasks'.  The URL structure looks like " + \
               "this: /campaigns/<wiki>/<campaign_id>/<workset_id>/<task_id>/"
        return jsonify({'info': info,
                        'wikis': db.campaigns.wikis()})

    @bp.route("/campaigns/<wiki>/", methods=["GET"])
    def get_wiki(wiki):
        """
        Returns a list of campaigns available for this particular wiki.
        """
        if 'create' in request.args:
            create_campaign(wiki, request.args['create'])
        else:
            stats = request.args.get('campaigns') == "stats"
            return jsonify(
                {
                    "wiki": wiki,
                    "campaigns": db.campaigns.for_wiki(wiki, stats)
                }
            )

    @bp.route("/campaigns/<wiki>/", methods=["POST"])
    @preprocessors.authenticated
    def create_campaign(wiki, campaign=None):
        """
        Creates a new campaign
        """
        campaign = json.loads(campaign or request.form.get('campaign'))

        return responses.not_implemented("Create campaign")

    @bp.route("/campaigns/<wiki>/<int:campaign_id>/", methods=["GET"])
    def get_campaign(wiki, campaign_id):
        """
        Returns metadata for a campaign or assign a new workset
        """

        doc = {}
        if 'tasks' in request.args:
            doc['tasks'] = db.tasks.for_campaign(campaign_id)

        if 'worksets' in request.args:
            stats = request.args.get('worksets') == "stats"
            doc['worksets'] = db.worksets.for_campaign(campaign_id, stats)

        try:
            stats = request.args.get('campaign') == "stats"
            doc['campaign'] = db.campaigns.get(campaign_id, stats)
            return jsonify(doc)
        except NotFoundError as e:
            return responses.not_found(str(e))

    @bp.route("/campaigns/<wiki>/<int:campaign_id>/", methods=["POST"])
    @preprocessors.authenticated
    def assign_workset(wiki, campaign_id):
        """
        Assigns and gathers metadata for a particular workset
        """
        user_id = session['user']['id']

        stats = request.form.get('workset') == "stats"
        try:
            doc = {'workset': db.worksets.assign(campaign_id, user_id, stats)}
        except IntegrityError as e:
            return responses.conflict(
                str(e) +
                "Please try again or check campaign status " +
                url_for("wikilabels.stats_wiki", wiki=wiki))
        except NotFoundError as e:
            return responses.not_found(str(e))

        if 'campaign' in request.form:
            stats = request.form.get('campaign') == "stats"
            try:
                doc['campaign'] = db.campaigns.get(campaign_id, stats)
            except NotFoundError as e:
                return responses.not_found(str(e))

        if 'tasks' in request.form:
            doc['tasks'] = db.tasks.for_workset(doc['workset']['id'])

        return jsonify(doc)

    @bp.route("/campaigns/<wiki>/<int:campaign_id>/<int:workset_id>/",
              methods=["GET"])
    def get_workset(wiki, campaign_id, workset_id):
        """
        Gathers metadata for a particular workset
        """

        doc = {}
        if 'campaign' in request.args:<commit_msg>Add space before start of the new message<commit_after>import json

from flask import request, session, url_for
from flask.ext.jsonpify import jsonify

from .. import preprocessors, responses
from ...database import IntegrityError, NotFoundError


def configure(bp, config, db):

    @bp.route("/campaigns/", methods=["GET"])
    def campaigns_index():
        """
        Returns a list of wikis that have active campaigns.
        """
        info = "Welcome to the campaigns module.  This module provides " + \
               "access to data using a heirachical strategy: 'wikis' " + \
               "have 'campaigns', 'campaigns' have 'worksets' and " + \
               "'worksets' have 'tasks'.  The URL structure looks like " + \
               "this: /campaigns/<wiki>/<campaign_id>/<workset_id>/<task_id>/"
        return jsonify({'info': info,
                        'wikis': db.campaigns.wikis()})

    @bp.route("/campaigns/<wiki>/", methods=["GET"])
    def get_wiki(wiki):
        """
        Returns a list of campaigns available for this particular wiki.
        """
        if 'create' in request.args:
            create_campaign(wiki, request.args['create'])
        else:
            stats = request.args.get('campaigns') == "stats"
            return jsonify(
                {
                    "wiki": wiki,
                    "campaigns": db.campaigns.for_wiki(wiki, stats)
                }
            )

    @bp.route("/campaigns/<wiki>/", methods=["POST"])
    @preprocessors.authenticated
    def create_campaign(wiki, campaign=None):
        """
        Creates a new campaign
        """
        campaign = json.loads(campaign or request.form.get('campaign'))

        return responses.not_implemented("Create campaign")

    @bp.route("/campaigns/<wiki>/<int:campaign_id>/", methods=["GET"])
    def get_campaign(wiki, campaign_id):
        """
        Returns metadata for a campaign or assign a new workset
        """

        doc = {}
        if 'tasks' in request.args:
            doc['tasks'] = db.tasks.for_campaign(campaign_id)

        if 'worksets' in request.args:
            stats = request.args.get('worksets') == "stats"
            doc['worksets'] = db.worksets.for_campaign(campaign_id, stats)

        try:
            stats = request.args.get('campaign') == "stats"
            doc['campaign'] = db.campaigns.get(campaign_id, stats)
            return jsonify(doc)
        except NotFoundError as e:
            return responses.not_found(str(e))

    @bp.route("/campaigns/<wiki>/<int:campaign_id>/", methods=["POST"])
    @preprocessors.authenticated
    def assign_workset(wiki, campaign_id):
        """
        Assigns and gathers metadata for a particular workset
        """
        user_id = session['user']['id']

        stats = request.form.get('workset') == "stats"
        try:
            doc = {'workset': db.worksets.assign(campaign_id, user_id, stats)}
        except IntegrityError as e:
            return responses.conflict(
                str(e) +
                " Please try again or check campaign status " +
                url_for("wikilabels.stats_wiki", wiki=wiki))
        except NotFoundError as e:
            return responses.not_found(str(e))

        if 'campaign' in request.form:
            stats = request.form.get('campaign') == "stats"
            try:
                doc['campaign'] = db.campaigns.get(campaign_id, stats)
            except NotFoundError as e:
                return responses.not_found(str(e))

        if 'tasks' in request.form:
            doc['tasks'] = db.tasks.for_workset(doc['workset']['id'])

        return jsonify(doc)

    @bp.route("/campaigns/<wiki>/<int:campaign_id>/<int:workset_id>/",
              methods=["GET"])
    def get_workset(wiki, campaign_id, workset_id):
        """
        Gathers metadata for a particular workset
        """

        doc = {}
        if 'campaign' in request.args:
{'fraction_non_alphanumeric': 0.12989664412402704, 'fraction_numerical': 0.0, 'mean_word_length': 2.0992487149070778, 'pattern_counts': {'":': 4, '<': 29, '<?xml version=': 0, '>': 29, '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': '230920', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2175, 'n_words': 526}