<commit_before># -*-coding:Utf-8 -*-

# Compatibility 2.7-3.4
from __future__ import absolute_import
from __future__ import unicode_literals

from flask import request
from sqlalchemy import exc
from sqlalchemy.orm import sessionmaker
from app import app, db
from app.models_mod import user, target, usergroup, targetgroup
from . import api

from .. import utilities as utils


@app.route("/targetgroup/list")
def targetgroup_list():
    """Return the targetgroup list of database"""
    result = []
    query = db.session.query(
        targetgroup.Targetgroup.name).order_by(
        targetgroup.Targetgroup.name)

    for row in query.all():
        result.append(str(row[0]))

    if not result:
        return  utils.response("No targetgroup in database.", 200)

    return utils.response("\n".join(result), 200)


@app.route("/targetgroup/search/<pattern>")
def targetgroup_search(pattern):
    """Return a list of targetgroups that match the given pattern"""
    result = []
    query = db.session.query(
        targetgroup.Targetgroup.name).filter(
        targetgroup.Targetgroup.name.like("%" + pattern + "%"))

    for row in query.all():
        result.append(str(row[0]))

    if not result:
        return utils.response('No targetgroup matching the pattern "' + \
                              pattern + '" found.', 200)

    return utils.response("\n".join(result), 200)


@app.route("/targetgroup/show/<name>")
def targetgroup_show(name):
    """Return all data about a targetgroup"""
    # Check for required fields
    if not name:
        return utils.response("ERROR: The name is required ", 417)

    targetgroup_data = targetgroup.Targetgroup.query.filter_by(
        name=name).first()

    if targetgroup_data is None:
        return utils.response('ERROR: No targetgroup with the name "' + \
                              name + '" in the database.', 417)

    return utils.response(targetgroup_data.__repr__(), 200)


@app.route("/targetgroup/memberof/<obj>/<name>")
def targetgroup_memberof(obj, name):
    """Return the list of obj this targetgroup is member of"""
    # Check for required fields
    if not name:
        return utils.response("ERROR: The name is required ", 417)

    data = targetgroup.Targetgroup.query.filter_by(name=name).first()

    if data is None:
        return utils.response('ERROR: No targetgroup with the name "' + name + \
                              '" in the database.', 417)

    return utils.response(str(data.memberof(obj)), 200)


@app.route("/targetgroup/create", methods=["POST"])
def targetgroup_create():
    """Add a targetgroup in the database"""
    # Only POST data are handled
    if request.method != "POST":
        return utils.response("ERROR: POST method is required ", 405)

    # Simplification for the reading
    name = request.form["name"]
    comment = request.form["comment"]

    # Check for required fields
    if not name:
        return utils.response("ERROR: The name is required ", 417)

    # Check unicity for name
    query = db.session.query(targetgroup.Targetgroup.name)\
        .filter_by(name=name).first()

    if query is not None:
        return utils.response('ERROR: The name "' + name + \
                              '" is already used by another targetgroup ', 417)

    t = targetgroup.Targetgroup(
        name=name,
        comment=comment)
    db.session.add(t)

    # Try to add the targetgroup in the database
    try:
        db.session.commit()
    except exc.SQLAlchemyError as e:
        return utils.response('ERROR: "' + name + '" -> ' + \
                              e.message, 409)

    return utils.response('OK: "' + name + '" -> created', 200)


@app.route("/targetgroup/edit", methods=["POST"])
def targetgroup_edit():
    """Edit a targetgroup in the database"""
    # Only POST data are handled
    if request.method != "POST":
        return utils.response("ERROR: POST method is required ", 405)

    # Simplification for the reading
    name = request.form["name"]
    new_name = request.form["new_name"]
    new_comment = request.form["new_comment"]

    # Check required fields
    if not name:
        return utils.response("ERROR: The name is required ", 417)

    # Check if the name exists in the database
    query = db.session.query(targetgroup.Targetgroup.name).filter_by(
        name=name).first()

    if query is None:
        return utils.response('ERROR: No targetgroup with the name "' + \
                              name + '" in the database.', 417)

    to_update = db.session.query(
        targetgroup.Targetgroup).filter_by(
        name=name)

    # Let's modify only relevent fields
    if new_comment:
        # This specific string allows admins to remove old comments
        if new_comment == "PASSHPORTREMOVECOMMENT":
            new_comment = ""
        to_update.update({"comment": new_comment})
    if new_name:
        # Check unicity for name
        query = db.session.query(targetgroup.Targetgroup.name)\
            .filter_by(name=new_name).first()

        if query is not None and new_name != query.name:
            return utils.response('ERROR: The name "' + new_name + \
                                  '" is already used by another targetgroup.',\
                                  417)

        to_update.update({"name": new_name})

    try:
        db.session.commit()
    except exc.SQLAlchemyError as e:
        return utils.response('ERROR: "' + name + '" -> ' + \
                              e.message, 409)

    return utils.response('OK: "' + name + '" -> edited', 200)


@app.route("/targetgroup/delete/<name>")
def targetgroup_delete(name):
    """Delete a targetgroup in the database"""
    if not name:
        return utils.response("ERROR: The name is required ", 417)

    # Check if the name exists
    query = db.session.query(targetgroup.Targetgroup.name)\
        .filter_by(name=name).first()

    if query is None:
        return utils.response('ERROR: No targetgroup with the name "' + \
                              name + '" in the database.', 417)

    tg = db.session.query(
            targetgroup.Targetgroup).filter(
            targetgroup.Targetgroup.name == name)
    tg[0].prepare_delete()
    tg[0].prepare_delete()
    tg[0].prepare_delete()
    tg[0].prepare_delete()
    tg.delete()
<commit_msg>Correction on element deletion<commit_after># -*-coding:Utf-8 -*-

# Compatibility 2.7-3.4
from __future__ import absolute_import
from __future__ import unicode_literals

from flask import request
from sqlalchemy import exc
from sqlalchemy.orm import sessionmaker
from app import app, db
from app.models_mod import user, target, usergroup, targetgroup
from . import api

from .. import utilities as utils


@app.route("/targetgroup/list")
def targetgroup_list():
    """Return the targetgroup list of database"""
    result = []
    query = db.session.query(
        targetgroup.Targetgroup.name).order_by(
        targetgroup.Targetgroup.name)

    for row in query.all():
        result.append(str(row[0]))

    if not result:
        return  utils.response("No targetgroup in database.", 200)

    return utils.response("\n".join(result), 200)


@app.route("/targetgroup/search/<pattern>")
def targetgroup_search(pattern):
    """Return a list of targetgroups that match the given pattern"""
    result = []
    query = db.session.query(
        targetgroup.Targetgroup.name).filter(
        targetgroup.Targetgroup.name.like("%" + pattern + "%"))

    for row in query.all():
        result.append(str(row[0]))

    if not result:
        return utils.response('No targetgroup matching the pattern "' + \
                              pattern + '" found.', 200)

    return utils.response("\n".join(result), 200)


@app.route("/targetgroup/show/<name>")
def targetgroup_show(name):
    """Return all data about a targetgroup"""
    # Check for required fields
    if not name:
        return utils.response("ERROR: The name is required ", 417)

    targetgroup_data = targetgroup.Targetgroup.query.filter_by(
        name=name).first()

    if targetgroup_data is None:
        return utils.response('ERROR: No targetgroup with the name "' + \
                              name + '" in the database.', 417)

    return utils.response(targetgroup_data.__repr__(), 200)


@app.route("/targetgroup/memberof/<obj>/<name>")
def targetgroup_memberof(obj, name):
    """Return the list of obj this targetgroup is member of"""
    # Check for required fields
    if not name:
        return utils.response("ERROR: The name is required ", 417)

    data = targetgroup.Targetgroup.query.filter_by(name=name).first()

    if data is None:
        return utils.response('ERROR: No targetgroup with the name "' + name + \
                              '" in the database.', 417)

    return utils.response(str(data.memberof(obj)), 200)


@app.route("/targetgroup/create", methods=["POST"])
def targetgroup_create():
    """Add a targetgroup in the database"""
    # Only POST data are handled
    if request.method != "POST":
        return utils.response("ERROR: POST method is required ", 405)

    # Simplification for the reading
    name = request.form["name"]
    comment = request.form["comment"]

    # Check for required fields
    if not name:
        return utils.response("ERROR: The name is required ", 417)

    # Check unicity for name
    query = db.session.query(targetgroup.Targetgroup.name)\
        .filter_by(name=name).first()

    if query is not None:
        return utils.response('ERROR: The name "' + name + \
                              '" is already used by another targetgroup ', 417)

    t = targetgroup.Targetgroup(
        name=name,
        comment=comment)
    db.session.add(t)

    # Try to add the targetgroup in the database
    try:
        db.session.commit()
    except exc.SQLAlchemyError as e:
        return utils.response('ERROR: "' + name + '" -> ' + \
                              e.message, 409)

    return utils.response('OK: "' + name + '" -> created', 200)


@app.route("/targetgroup/edit", methods=["POST"])
def targetgroup_edit():
    """Edit a targetgroup in the database"""
    # Only POST data are handled
    if request.method != "POST":
        return utils.response("ERROR: POST method is required ", 405)

    # Simplification for the reading
    name = request.form["name"]
    new_name = request.form["new_name"]
    new_comment = request.form["new_comment"]

    # Check required fields
    if not name:
        return utils.response("ERROR: The name is required ", 417)

    # Check if the name exists in the database
    query = db.session.query(targetgroup.Targetgroup.name).filter_by(
        name=name).first()

    if query is None:
        return utils.response('ERROR: No targetgroup with the name "' + \
                              name + '" in the database.', 417)

    to_update = db.session.query(
        targetgroup.Targetgroup).filter_by(
        name=name)

    # Let's modify only relevent fields
    if new_comment:
        # This specific string allows admins to remove old comments
        if new_comment == "PASSHPORTREMOVECOMMENT":
            new_comment = ""
        to_update.update({"comment": new_comment})
    if new_name:
        # Check unicity for name
        query = db.session.query(targetgroup.Targetgroup.name)\
            .filter_by(name=new_name).first()

        if query is not None and new_name != query.name:
            return utils.response('ERROR: The name "' + new_name + \
                                  '" is already used by another targetgroup.',\
                                  417)

        to_update.update({"name": new_name})

    try:
        db.session.commit()
    except exc.SQLAlchemyError as e:
        return utils.response('ERROR: "' + name + '" -> ' + \
                              e.message, 409)

    return utils.response('OK: "' + name + '" -> edited', 200)


@app.route("/targetgroup/delete/<name>")
def targetgroup_delete(name):
    """Delete a targetgroup in the database"""
    if not name:
        return utils.response("ERROR: The name is required ", 417)

    # Check if the name exists
    query = db.session.query(targetgroup.Targetgroup.name)\
        .filter_by(name=name).first()

    if query is None:
        return utils.response('ERROR: No targetgroup with the name "' + \
                              name + '" in the database.', 417)

    tg = db.session.query(
            targetgroup.Targetgroup).filter(
            targetgroup.Targetgroup.name == name)
    tg[0].prepare_delete()
    tg.delete()

{'fraction_non_alphanumeric': 0.11758626172682461, 'fraction_numerical': 0.01248211162346955, 'mean_word_length': 2.469111969111969, 'pattern_counts': {'":': 10, '<': 13, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6641460', 'n_tokens_mistral': 3679, 'n_tokens_neox': 3480, 'n_words': 1018}