<commit_before>"""Group view methods"""
from flask import abort, Blueprint, jsonify
from flask import request
from flask_user import roles_required

from ..audit import auditable_event
from ..extensions import db, oauth
from ..models.group import Group
from ..models.role import ROLE
from ..models.user import current_user

group_api = Blueprint('group_api', __name__, url_prefix='/api/group')

@group_api.route('/')
@oauth.require_oauth()
def current_groups():
    """Returns simple JSON defining all current groups

    Returns a list of all known groups.  Groups are used to logically
    associate users, see `role` for access issues.

    ---
    tags:
      - Group
    operationId: current_groups
    produces:
      - application/json
    responses:
      200:
        description: Returns a list of all known groups.
        schema:
          id: groups
          required:
            - name
            - description
          properties:
            name:
              type: string
              description:
                Group name, always a lower case string with no white space.
            description:
              type: string
              description: Plain text describing the group.
      401:
        description: if missing valid OAuth token

    """
    results = [g.as_json() for g in Group.query.all()]
    return jsonify(groups=results)


@group_api.route('/<string:group_name>')
@oauth.require_oauth()
def group_by_name(group_name):
    """Returns simple JSON for the requested group

    ---
    tags:
      - Group
    operationId: group_by_name
    parameters:
      - name: group_name
        in: path
        description: Group name
        required: true
        type: string
    produces:
      - application/json
    responses:
      200:
        description: Returns simple JSON for the named group
        schema:
          id: groups
          required:
            - name
            - description
          properties:
            name:
              type: string
              description:
                Group name, always a lower case string with no white space.
            description:
              type: string
              description: Plain text describing the group.
      401:
        description: if missing valid OAuth token
      404:
        description: if named group doesn't exist

    """
    g = Group.query.filter_by(name=group_name).first()
    if not g:
        abort(404, "Group {n} not found".format(n=group_name))
    return jsonify(group=g.as_json())


@group_api.route('/', methods=('POST',))
@roles_required(ROLE.ADMIN)
def add_group():
    """Add new group<commit_msg>Allow service users to POST to /api/group/<commit_after>"""Group view methods"""
from flask import abort, Blueprint, jsonify
from flask import request
from flask_user import roles_required

from ..audit import auditable_event
from ..extensions import db, oauth
from ..models.group import Group
from ..models.role import ROLE
from ..models.user import current_user

group_api = Blueprint('group_api', __name__, url_prefix='/api/group')

@group_api.route('/')
@oauth.require_oauth()
def current_groups():
    """Returns simple JSON defining all current groups

    Returns a list of all known groups.  Groups are used to logically
    associate users, see `role` for access issues.

    ---
    tags:
      - Group
    operationId: current_groups
    produces:
      - application/json
    responses:
      200:
        description: Returns a list of all known groups.
        schema:
          id: groups
          required:
            - name
            - description
          properties:
            name:
              type: string
              description:
                Group name, always a lower case string with no white space.
            description:
              type: string
              description: Plain text describing the group.
      401:
        description: if missing valid OAuth token

    """
    results = [g.as_json() for g in Group.query.all()]
    return jsonify(groups=results)


@group_api.route('/<string:group_name>')
@oauth.require_oauth()
def group_by_name(group_name):
    """Returns simple JSON for the requested group

    ---
    tags:
      - Group
    operationId: group_by_name
    parameters:
      - name: group_name
        in: path
        description: Group name
        required: true
        type: string
    produces:
      - application/json
    responses:
      200:
        description: Returns simple JSON for the named group
        schema:
          id: groups
          required:
            - name
            - description
          properties:
            name:
              type: string
              description:
                Group name, always a lower case string with no white space.
            description:
              type: string
              description: Plain text describing the group.
      401:
        description: if missing valid OAuth token
      404:
        description: if named group doesn't exist

    """
    g = Group.query.filter_by(name=group_name).first()
    if not g:
        abort(404, "Group {n} not found".format(n=group_name))
    return jsonify(group=g.as_json())


@group_api.route('/', methods=('POST',))
@roles_required([ROLE.ADMIN, ROLE.SERVICE])
def add_group():
    """Add new group
{'fraction_non_alphanumeric': 0.08297433976400075, 'fraction_numerical': 0.006742835737029406, 'mean_word_length': 2.047945205479452, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '620895', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1392, 'n_words': 482}