Blame pagure/api/group.py

Matt Prahl 1a51d9
# -*- coding: utf-8 -*-
Matt Prahl 1a51d9
Matt Prahl 1a51d9
"""
Matt Prahl 1a51d9
 (c) 2017 - Copyright Red Hat Inc
Matt Prahl 1a51d9
Matt Prahl 1a51d9
 Authors:
Matt Prahl 802ab8
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Matt Prahl 1a51d9
   Matt Prahl <mprahl@redhat.com></mprahl@redhat.com>
Matt Prahl 1a51d9
Matt Prahl 1a51d9
"""
Matt Prahl 1a51d9
Aurélien Bompard dcf6f6
from __future__ import unicode_literals
Aurélien Bompard dcf6f6
Matt Prahl 1a51d9
import flask
Matt Prahl 1a51d9
Matt Prahl 1a51d9
import pagure
Matt Prahl 1a51d9
import pagure.exceptions
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 75809f
from pagure.api import (
Pierre-Yves Chibon 9c2953
    API,
Pierre-Yves Chibon 9c2953
    APIERROR,
Pierre-Yves Chibon 9c2953
    api_method,
Pierre-Yves Chibon 9c2953
    api_login_optional,
Pierre-Yves Chibon 9c2953
    get_page,
Pierre-Yves Chibon 9c2953
    get_per_page,
Pierre-Yves Chibon 9c2953
)
Aurélien Bompard 619e2a
from pagure.utils import is_true
Matt Prahl 1a51d9
Matt Prahl 1a51d9
Pierre-Yves Chibon 9c2953
@API.route("/groups/")
Pierre-Yves Chibon 9c2953
@API.route("/groups")
Matt Prahl 802ab8
def api_groups():
Pierre-Yves Chibon 9c2953
    """
Matt Prahl 802ab8
    List groups
Matt Prahl 802ab8
    -----------
Matt Prahl 802ab8
    Retrieve groups on this Pagure instance.
Matt Prahl 802ab8
    This can then be used as input for autocompletion in some forms/fields.
Matt Prahl 802ab8
Matt Prahl 802ab8
    ::
Matt Prahl 802ab8
Matt Prahl 802ab8
        GET /api/0/groups
Matt Prahl 802ab8
Matt Prahl 802ab8
    Parameters
Matt Prahl 802ab8
    ^^^^^^^^^^
Matt Prahl 802ab8
Matt Prahl 802ab8
    +---------------+----------+---------------+--------------------------+
Matt Prahl 802ab8
    | Key           | Type     | Optionality   | Description              |
Matt Prahl 802ab8
    +===============+==========+===============+==========================+
Matt Prahl 802ab8
    | ``pattern``   | string   | Optional      | | Filters the starting   |
Matt Prahl 802ab8
    |               |          |               |   letters of the group   |
Matt Prahl 802ab8
    |               |          |               |   names                  |
Matt Prahl 802ab8
    +---------------+----------+---------------+--------------------------+
Pierre-Yves Chibon 75809f
    | ``page``      | int      | Optional      | | Specifies which        |
Pierre-Yves Chibon 75809f
    |               |          |               |   page to return         |
Pierre-Yves Chibon 75809f
    |               |          |               |   (defaults to: 1)       |
Pierre-Yves Chibon 75809f
    +---------------+----------+---------------+--------------------------+
Pierre-Yves Chibon 75809f
    | ``per_page``  | int      | Optional      | | The number of projects |
Pierre-Yves Chibon 75809f
    |               |          |               |   to return per page.    |
Pierre-Yves Chibon 75809f
    |               |          |               |   The maximum is 100.    |
Pierre-Yves Chibon 75809f
    +---------------+----------+---------------+--------------------------+
Matt Prahl 802ab8
Matt Prahl 802ab8
    Sample response
Matt Prahl 802ab8
    ^^^^^^^^^^^^^^^
Matt Prahl 802ab8
Matt Prahl 802ab8
    ::
Matt Prahl 802ab8
Matt Prahl 802ab8
        {
Matt Prahl 802ab8
          "total_groups": 2,
Pierre-Yves Chibon 75809f
          u'pagination': {
Pierre-Yves Chibon 75809f
            'first': 'http://localhost/api/0/groups?per_page=20&extended=1&page=1',
Pierre-Yves Chibon 75809f
            'last': 'http://localhost/api/0/groups?per_page=20&extended=1&page=1',
Karsten Hopp 46ce34
            'next': null,
Pierre-Yves Chibon 75809f
            'page': 1,
Pierre-Yves Chibon 75809f
            'pages': 1,
Pierre-Yves Chibon 75809f
            'per_page': 20,
Pierre-Yves Chibon 75809f
            'prev': None
Pierre-Yves Chibon 75809f
          },
Matt Prahl 802ab8
          "groups": ["group1", "group2"]
Matt Prahl 802ab8
        }
Matt Prahl 802ab8
Pierre-Yves Chibon 9c2953
    """  # noqa
Pierre-Yves Chibon 9c2953
    pattern = flask.request.args.get("pattern", None)
Pierre-Yves Chibon 9c2953
    extended = is_true(flask.request.args.get("extended", False))
Pierre-Yves Chibon 2231a1
Pierre-Yves Chibon 9c2953
    if pattern is not None and not pattern.endswith("*"):
Pierre-Yves Chibon 9c2953
        pattern += "*"
Matt Prahl 802ab8
Pierre-Yves Chibon 75809f
    page = get_page()
Pierre-Yves Chibon 75809f
    per_page = get_per_page()
Pierre-Yves Chibon 930073
    group_cnt = pagure.lib.query.search_groups(
Pierre-Yves Chibon 9c2953
        flask.g.session, pattern=pattern, count=True
Pierre-Yves Chibon 9c2953
    )
Pierre-Yves Chibon 930073
    pagination_metadata = pagure.lib.query.get_pagination_metadata(
Pierre-Yves Chibon 9c2953
        flask.request, page, per_page, group_cnt
Pierre-Yves Chibon 9c2953
    )
Pierre-Yves Chibon 75809f
    query_start = (page - 1) * per_page
Pierre-Yves Chibon 75809f
    query_limit = per_page
Pierre-Yves Chibon 75809f
Pierre-Yves Chibon 930073
    groups = pagure.lib.query.search_groups(
Pierre-Yves Chibon 9c2953
        flask.g.session, pattern=pattern, limit=query_limit, offset=query_start
Pierre-Yves Chibon 9c2953
    )
Matt Prahl 802ab8
Pierre-Yves Chibon 2231a1
    if extended:
Pierre-Yves Chibon 2231a1
        groups = [
Pierre-Yves Chibon 9c2953
            {"name": grp.group_name, "description": grp.description}
Pierre-Yves Chibon 2231a1
            for grp in groups
Pierre-Yves Chibon 2231a1
        ]
Pierre-Yves Chibon 2231a1
    else:
Pierre-Yves Chibon 2231a1
        groups = [group.group_name for group in groups]
Pierre-Yves Chibon 2231a1
Matt Prahl 802ab8
    return flask.jsonify(
Matt Prahl 802ab8
        {
Pierre-Yves Chibon 9c2953
            "total_groups": group_cnt,
Pierre-Yves Chibon 9c2953
            "groups": groups,
Pierre-Yves Chibon 9c2953
            "pagination": pagination_metadata,
Matt Prahl 802ab8
        }
Matt Prahl 802ab8
    )
Matt Prahl 802ab8
Matt Prahl 802ab8
Pierre-Yves Chibon 9c2953
@API.route("/group/<group>")</group>
Matt Prahl 9264b1
@api_login_optional()
Matt Prahl 1a51d9
@api_method
Matt Prahl 1a51d9
def api_view_group(group):
Matt Prahl 1a51d9
    """
Matt Prahl 1a51d9
    Group information
Matt Prahl 1a51d9
    -----------------
Matt Prahl 1a51d9
    Use this endpoint to retrieve information about a specific group.
Matt Prahl 1a51d9
Matt Prahl 1a51d9
    ::
Matt Prahl 1a51d9
Matt Prahl 1a51d9
        GET /api/0/group/<group></group>
Matt Prahl 1a51d9
Matt Prahl 1a51d9
    ::
Matt Prahl 1a51d9
Matt Prahl 1a51d9
        GET /api/0/group/some_group_name
Matt Prahl 1a51d9
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
    ::
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
        GET /api/0/group/some_group_name?projects=1&acl=commit
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
    Input
Pierre-Yves Chibon 6fa1ec
    ^^^^^
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
    +------------------+---------+--------------+-----------------------------+
Pierre-Yves Chibon 6fa1ec
    | Key              | Type    | Optionality  | Description                 |
Pierre-Yves Chibon 6fa1ec
    +==================+=========+==============+=============================+
Pierre-Yves Chibon 6fa1ec
    | ``group name``   | str     | Mandatory    | The name of the group to    |
Pierre-Yves Chibon 6fa1ec
    |                  |         |              | retrieve information about. |
Pierre-Yves Chibon 6fa1ec
    +------------------+---------+--------------+-----------------------------+
Pierre-Yves Chibon 6fa1ec
    | ``projects``     | bool    | Optional     | Specifies whether to include|
Pierre-Yves Chibon 6fa1ec
    |                  |         |              | projects in the data        |
Pierre-Yves Chibon 6fa1ec
    |                  |         |              | returned.                   |
Pierre-Yves Chibon 6fa1ec
    +------------------+---------+--------------+-----------------------------+
Pierre-Yves Chibon 6fa1ec
    | ``acl``          | str     | Optional     | Filter the project returned |
Pierre-Yves Chibon 6fa1ec
    |                  |         |              | (if any) to those where the |
Pierre-Yves Chibon 6fa1ec
    |                  |         |              | has the specified ACL level.|
Pierre-Yves Chibon 6fa1ec
    |                  |         |              | Can be any of: ``admin``,   |
Pierre-Yves Chibon 6fa1ec
    |                  |         |              | ``commit`` or ``ticket``.   |
Pierre-Yves Chibon 6fa1ec
    +------------------+---------+--------------+-----------------------------+
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
Matt Prahl 1a51d9
    Sample response
Matt Prahl 1a51d9
    ^^^^^^^^^^^^^^^
Matt Prahl 1a51d9
Matt Prahl 1a51d9
    ::
Matt Prahl 1a51d9
Matt Prahl 1a51d9
        {
Matt Prahl 1a51d9
          "creator": {
Pierre-Yves Chibon 9602c6
            "default_email": "user1@example.com",
Matt Prahl 1a51d9
            "emails": [
Matt Prahl 1a51d9
              "user1@example.com"
Pierre-Yves Chibon 9602c6
            ],
Pierre-Yves Chibon 9602c6
            "fullname": "User1",
Matt Prahl 1a51d9
            "name": "user1"
Pierre-Yves Chibon 9602c6
          },
Pierre-Yves Chibon 9602c6
          "date_created": "1492011511",
Pierre-Yves Chibon 9602c6
          "description": "Some Group",
Pierre-Yves Chibon 9602c6
          "display_name": "Some Group",
Pierre-Yves Chibon 9602c6
          "group_type": "user",
Matt Prahl 1a51d9
          "members": [
Pierre-Yves Chibon 9602c6
            "user1",
Matt Prahl 1a51d9
            "user2"
Pierre-Yves Chibon 9602c6
          ],
Matt Prahl 1a51d9
          "name": "some_group_name"
Matt Prahl 1a51d9
        }
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
    ::
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
        {
Pierre-Yves Chibon 6fa1ec
          "creator": {
Pierre-Yves Chibon 6fa1ec
            "default_email": "user1@example.com",
Pierre-Yves Chibon 6fa1ec
            "emails": [
Pierre-Yves Chibon 6fa1ec
              "user1@example.com"
Pierre-Yves Chibon 6fa1ec
            ],
Pierre-Yves Chibon 6fa1ec
            "fullname": "User1",
Pierre-Yves Chibon 6fa1ec
            "name": "user1"
Pierre-Yves Chibon 6fa1ec
          },
Pierre-Yves Chibon 6fa1ec
          "date_created": "1492011511",
Pierre-Yves Chibon 6fa1ec
          "description": "Some Group",
Pierre-Yves Chibon 6fa1ec
          "display_name": "Some Group",
Pierre-Yves Chibon 6fa1ec
          "group_type": "user",
Pierre-Yves Chibon 6fa1ec
          "members": [
Pierre-Yves Chibon 6fa1ec
            "user1",
Pierre-Yves Chibon 6fa1ec
            "user2"
Pierre-Yves Chibon 6fa1ec
          ],
Pierre-Yves Chibon 6fa1ec
          "name": "some_group_name",
Pierre-Yves Chibon 6fa1ec
          "projects": [],
Pierre-Yves Chibon 6fa1ec
        }
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 9602c6
    """  # noqa
Pierre-Yves Chibon 9c2953
    projects = flask.request.values.get("projects", "").strip().lower() in [
Pierre-Yves Chibon 9c2953
        "1",
Pierre-Yves Chibon 9c2953
        "true",
Pierre-Yves Chibon 9c2953
    ]
Pierre-Yves Chibon 9c2953
    acl = flask.request.values.get("acl", "").strip().lower() or None
Pierre-Yves Chibon 9c2953
    if acl == "ticket":
Pierre-Yves Chibon 9c2953
        acl = ["admin", "commit", "ticket"]
Pierre-Yves Chibon 9c2953
    elif acl == "commit":
Pierre-Yves Chibon 9c2953
        acl = ["commit", "admin"]
Pierre-Yves Chibon 6fa1ec
    elif acl:
Pierre-Yves Chibon 6fa1ec
        acl = [acl]
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 930073
    group = pagure.lib.query.search_groups(flask.g.session, group_name=group)
Matt Prahl 1a51d9
    if not group:
Matt Prahl 1a51d9
        raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOGROUP)
Matt Prahl 1a51d9
Pierre-Yves Chibon b130e5
    output = group.to_json(public=(not pagure.utils.api_authenticated()))
Pierre-Yves Chibon 6fa1ec
    if projects and not acl:
Pierre-Yves Chibon 9c2953
        output["projects"] = [
Pierre-Yves Chibon 9c2953
            project.to_json(public=True) for project in group.projects
Pierre-Yves Chibon 6fa1ec
        ]
Pierre-Yves Chibon 6fa1ec
    elif projects and acl:
Pierre-Yves Chibon 9c2953
        output["projects"] = [
Pierre-Yves Chibon 6fa1ec
            pg.project.to_json(public=True)
Pierre-Yves Chibon 6fa1ec
            for pg in group.projects_groups
Pierre-Yves Chibon 6fa1ec
            if pg.access in acl
Pierre-Yves Chibon 6fa1ec
        ]
Pierre-Yves Chibon 6fa1ec
    jsonout = flask.jsonify(output)
Matt Prahl 1a51d9
    jsonout.status_code = 200
Matt Prahl 1a51d9
    return jsonout