<commit_before>from flask import abort, jsonify, request

from dssg.Machine import Machine
from dssg.model import Category
from dssg.model import Deployment
from dssg.model import Message
from dssg.model import Report
from dssg.webapp import app


@app.route('/v1/language', methods=['POST'])
def detect_language():
    """Given some text, returns a ranked list of likey natural languages
    the given content is in
    
    Input parameters:
        text: string
    """
    if not request.json or not 'text' in request.json:
        abort(400)
    language = Machine.guess_language(request.json['text'])[0]
    
    return jsonify({'language': language[0], "confidence": language[1]})

@app.route('/v1/deplyoments', methods=['POST'])
def add_deployment():
    """Registers a new Ushahidi deployment. The following information
    should be included in the request
        - deployment name
        - list of categories; <id, name> for each category
    """
    if not requst.json and \
        ('name' not in request.json or 'url' not in request.json):
        abort(400)
    name, url = request.json['name'], request.json['url']

    # Is there a deployment that's been registered with the specified url
    deployment = Deployment.by_url(url)
    if deployment is None:
        deployment = Deployment(name=name, url=url)
        deployment.save()
    else:
        return jsonify(deployment.as_dict())
    
    # Add the categories
    _post = request.json
    if 'categories' in _post:
        categories = []
        for cat in _post['categories']:
            category = Category(deployment_id=deployment.id,
                                origin_category_id=cat['origin_category_id'],
                                origin_parent_id=cat['origin_parent_id'],
                                title=cat['title'])
            categories.append(category)

        # Save the categories in bulk
        Category.create_all(categories)

    return jsonify(deployment.as_dict())

@app.route('/v1/deployments/<int:deployment_id>/category', methods=['POST'])
def suggest_categories(deployment_id):
    """Given a message/report, suggests the possible categories
    that the message could fall into
    
    :param deployment_id: the id of the deployment
    """
    # if not request.json:
    #     abort(400)
    # # Does the deployment exist
    # deployment = Deployment.by_id(deployment_id)
    # if not deployment:
    #     abort(404)
    # pass

@app.route('/v1/deployments/<int:deployment_id>/messages', methods=['POST'])
def add_message(deployment_id):
    """Adds a new message for the deployment in :deployment_id
    
    The input parameters are:
        message: string
        
    :param deployment_id: the id of the deployment
    """
    if 'origin_message_id' not in request.json and \
        'content' not in request.json:
        abort(400)

    # Does the deployment exist
    deployment = Deployment.by_id(deployment_id)
    if deployment is None:
        abort(404)
    message = Message(deployment_id=deployment_id,
                      origin_message_id=request.json['origin_message_id'],
                      content=request.json['content'])
    message.save()
    return jsonify(message.as_dict())

@app.route('/v1/deployments/<int:deployment_id>/messages/<int:message_id>',
            methods=['DELETE'])
def delete_message(deployment_id, message_id):
    """Deletes the message with the specified :message_id from
    the deployment specified by the ``deployment_id`` parameter
    
    :param deployment_id: the id of the deployment
    :param message_id: the id of the message
    """
    message = db.session.query(Message).\
        filter(Message.deployment_id==deployment_id).\
        filter(Message.origin_message_id==message_id)
    if message is None:
        abort(404)
    message.delete()

@app.route('/v1/deployments/<int:deployment_id>/similar', methods=['POST'])
def similar_messages(deployment_id):
    """
    Given text, finds the near duplicate messages. The duplicate messages
    are specific to the deployment specified in ``deployment_id``
    
    input: text
    output: list. made up of tuples of (id, message text).
    [todo: does this only return reports? or unannotated messages, too?
    should be any message for completeness, and then the front-end can decide
    what should be hidden from the user.]
    
    :param deployment_id: the id of the deployment
    """
    pass

@app.route('/v1/deployments/<int:deployment_id>/reports',
           methods=['POST'])
def add_report(deployment_id):
    """Adds a new report to the deployment specified by the ``deployment_id``
    parameter
    
    Input parameters:
        description: string - Description of the report
        categories: array of integers - category ids
    
    :param deployment_id: the id of the deployment
    """
    if Deployment.by_id(deployment_id) is None:
        abort(404)
    
    if 'title' not in request.json and 'description' not in request.json:
        abort(400)
    _post = request.json
    report=Report(deployment_id=deployment_id,
                  origin_report_id=_post['origin_report_id'],
                  title=_post['title'],
                  description=_post['description'])
    report.save()
    return jsonify(report.as_dict())

@app.route('/v1/deployments/<int:deployment_id>/reports/<int:report_id>',
            methods=['DELETE'])
def delete_report(deployment_id, report_id):
    """Deletes the report with the specified ``report_id`` from the
    deployment referenced by the :deployment_id parameter
    
    :param deployment_id: the id of the deployment
    :param report_id: the id of the report
    """
    report = db.session.query(Report).\
        filter(Report.deployment_id==deployment_id).\
        filter(Report.origin_report_id=report_id)<commit_msg>Specify all criteria in filter()<commit_after>from flask import abort, jsonify, request

from dssg.Machine import Machine
from dssg.model import Category
from dssg.model import Deployment
from dssg.model import Message
from dssg.model import Report
from dssg.webapp import app


@app.route('/v1/language', methods=['POST'])
def detect_language():
    """Given some text, returns a ranked list of likey natural languages
    the given content is in
    
    Input parameters:
        text: string
    """
    if not request.json or not 'text' in request.json:
        abort(400)
    language = Machine.guess_language(request.json['text'])[0]
    
    return jsonify({'language': language[0], "confidence": language[1]})

@app.route('/v1/deplyoments', methods=['POST'])
def add_deployment():
    """Registers a new Ushahidi deployment. The following information
    should be included in the request
        - deployment name
        - list of categories; <id, name> for each category
    """
    if not requst.json and \
        ('name' not in request.json or 'url' not in request.json):
        abort(400)
    name, url = request.json['name'], request.json['url']

    # Is there a deployment that's been registered with the specified url
    deployment = Deployment.by_url(url)
    if deployment is None:
        deployment = Deployment(name=name, url=url)
        deployment.save()
    else:
        return jsonify(deployment.as_dict())
    
    # Add the categories
    _post = request.json
    if 'categories' in _post:
        categories = []
        for cat in _post['categories']:
            category = Category(deployment_id=deployment.id,
                                origin_category_id=cat['origin_category_id'],
                                origin_parent_id=cat['origin_parent_id'],
                                title=cat['title'])
            categories.append(category)

        # Save the categories in bulk
        Category.create_all(categories)

    return jsonify(deployment.as_dict())

@app.route('/v1/deployments/<int:deployment_id>/category', methods=['POST'])
def suggest_categories(deployment_id):
    """Given a message/report, suggests the possible categories
    that the message could fall into
    
    :param deployment_id: the id of the deployment
    """
    # if not request.json:
    #     abort(400)
    # # Does the deployment exist
    # deployment = Deployment.by_id(deployment_id)
    # if not deployment:
    #     abort(404)
    # pass

@app.route('/v1/deployments/<int:deployment_id>/messages', methods=['POST'])
def add_message(deployment_id):
    """Adds a new message for the deployment in :deployment_id
    
    The input parameters are:
        message: string
        
    :param deployment_id: the id of the deployment
    """
    if 'origin_message_id' not in request.json and \
        'content' not in request.json:
        abort(400)

    # Does the deployment exist
    deployment = Deployment.by_id(deployment_id)
    if deployment is None:
        abort(404)
    message = Message(deployment_id=deployment_id,
                      origin_message_id=request.json['origin_message_id'],
                      content=request.json['content'])
    message.save()
    return jsonify(message.as_dict())

@app.route('/v1/deployments/<int:deployment_id>/messages/<int:message_id>',
            methods=['DELETE'])
def delete_message(deployment_id, message_id):
    """Deletes the message with the specified :message_id from
    the deployment specified by the ``deployment_id`` parameter
    
    :param deployment_id: the id of the deployment
    :param message_id: the id of the message
    """
    message = db.session.query(Message).\
        filter(Message.deployment_id == deployment_id,
               Message.origin_message_id == message_id)
    if message is None:
        abort(404)
    message.delete()

@app.route('/v1/deployments/<int:deployment_id>/similar', methods=['POST'])
def similar_messages(deployment_id):
    """
    Given text, finds the near duplicate messages. The duplicate messages
    are specific to the deployment specified in ``deployment_id``
    
    input: text
    output: list. made up of tuples of (id, message text).
    [todo: does this only return reports? or unannotated messages, too?
    should be any message for completeness, and then the front-end can decide
    what should be hidden from the user.]
    
    :param deployment_id: the id of the deployment
    """
    pass

@app.route('/v1/deployments/<int:deployment_id>/reports',
           methods=['POST'])
def add_report(deployment_id):
    """Adds a new report to the deployment specified by the ``deployment_id``
    parameter
    
    Input parameters:
        description: string - Description of the report
        categories: array of integers - category ids
    
    :param deployment_id: the id of the deployment
    """
    if Deployment.by_id(deployment_id) is None:
        abort(404)
    
    if 'title' not in request.json and 'description' not in request.json:
        abort(400)
    _post = request.json
    report=Report(deployment_id=deployment_id,
                  origin_report_id=_post['origin_report_id'],
                  title=_post['title'],
                  description=_post['description'])
    report.save()
    return jsonify(report.as_dict())

@app.route('/v1/deployments/<int:deployment_id>/reports/<int:report_id>',
            methods=['DELETE'])
def delete_report(deployment_id, report_id):
    """Deletes the report with the specified ``report_id`` from the
    deployment referenced by the :deployment_id parameter
    
    :param deployment_id: the id of the deployment
    :param report_id: the id of the report
    """
    report = db.session.query(Report).\
        filter(Report.deployment_id == deployment_id,
               Report.origin_report_id == report_id)
{'fraction_non_alphanumeric': 0.10051238257899231, 'fraction_numerical': 0.006490179333902647, 'mean_word_length': 2.907574240907574, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 21, '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': '81578', 'n_tokens_mistral': 3371, 'n_tokens_neox': 3301, 'n_words': 961}