<commit_before># Amara, universalsubtitles.org
#
# Copyright (C) 2015 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program.  If not, see http://www.gnu.org/licenses/agpl-3.0.html.

"""
Team Resource
^^^^^^^^^^^^^

Get a list of teams
+++++++++++++++++++

.. http:get:: /api2/partners/teams/

    :>json name: Name of the team
    :>json slug: Machine name for the team slug (used in URLs)
    :>json description: Team description
    :>json is_visible: Should this team's videos be publicly visible?
    :>json membership_policy: See below for possible values
    :>json video_policy: See below for possible values

Get a list of teams
+++++++++++++++++++
.. http:get:: /api2/partners/teams/[team-slug]/

    Returns one entry from the team list resource data.

Updating a team
+++++++++++++++

.. http:put:: /api2/partners/teams/[team-slug]:

    :<json name: (required) Name of the team
    :<json slug: (required) Manchine name for the team (used in URLs)
    :<json description: Team description
    :<json is_visible: Should this team be publicly visible?
    :<json membership_policy: See below for possible values
    :<json video_policy: See below for possible values

Policy values
+++++++++++++

Membership policy:

* ``Open``
* ``Application``
* ``Invitation by any team member``
* ``Invitation by manager``
* ``Invitation by admin``

Video policy:

* ``Any team member``
* ``Managers and admins``
* ``Admins only``

Team Member Resource
^^^^^^^^^^^^^^^^^^^^

Get info on a team member
+++++++++++++++++++++++++

.. http:get:: /api2/partners/teams/[team-slug]/members/[username]

    :<json username: username
    :<json role: One of: ``owner``, ``admin``, ``manager``, or ``contributor``

Litsing all team members
++++++++++++++++++++++++

.. http:get:: /api2/partners/teams/[team-slug]/members/

    Returns a list of team member data.  Each item is the same as above.

Add a new member to a team
++++++++++++++++++++++++++

.. http:post:: /api2/partners/teams/[team-slug]/members/

    :>json username: username of the user to add
    :>json role: One of: ``owner``, ``admin``, ``manager``, or ``contributor``


Change a team member's role
+++++++++++++++++++++++++++

.. http:put:: /api2/partners/teams/[team-slug]/members/[username]/

    :>json role: One of: ``owner``, ``admin``, ``manager``, or ``contributor``

Removing a user from a team
+++++++++++++++++++++++++++

.. http:delete:: /api2/partners/teams/[team-slug]/members/[username]/

Safe Team Member Resource
^^^^^^^^^^^^^^^^^^^^^^^^^

This resource behaves the same as the normal Team Member resource except with
couple differences for the POST action to add members

* An invitation is sent to the user to join the team instead of simply adding
  them
* If no user exists with the username, and an ``email`` field is included in
  the POST data, we will create a user and send an email to the email account.
"""

from __future__ import absolute_import
from django.core.exceptions import PermissionDenied
from django.db import IntegrityError
from django.shortcuts import get_object_or_404
from rest_framework import serializers
from rest_framework import status
from rest_framework import viewsets

from api.pagination import AmaraPaginationMixin
from auth.models import CustomUser as User
from teams.models import Team, TeamMember
import messages.tasks
import teams.permissions as team_permissions

class MappedChoiceField(serializers.ChoiceField):
    """Choice field that maps internal values to choices."""

    default_error_messages = {
        'unknown-choice': "Unknown choice: {choice}",
    }

    def __init__(self, choices, *args, **kwargs):<commit_msg>Write project API docs<commit_after># Amara, universalsubtitles.org
#
# Copyright (C) 2015 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program.  If not, see http://www.gnu.org/licenses/agpl-3.0.html.

"""
Team Resource
^^^^^^^^^^^^^

Get a list of teams
+++++++++++++++++++

.. http:get:: /api/teams/

    :>json name: Name of the team
    :>json slug: Machine name for the team slug (used in URLs)
    :>json description: Team description
    :>json is_visible: Should this team's videos be publicly visible?
    :>json membership_policy: See below for possible values
    :>json video_policy: See below for possible values

Get a list of teams
+++++++++++++++++++
.. http:get:: /api/teams/[team-slug]/

    Returns one entry from the team list resource data.

Updating a team
+++++++++++++++

.. http:put:: /api/teams/[team-slug]:

    :<json name: (required) Name of the team
    :<json slug: (required) Manchine name for the team (used in URLs)
    :<json description: Team description
    :<json is_visible: Should this team be publicly visible?
    :<json membership_policy: See below for possible values
    :<json video_policy: See below for possible values

Policy values
+++++++++++++

Membership policy:

* ``Open``
* ``Application``
* ``Invitation by any team member``
* ``Invitation by manager``
* ``Invitation by admin``

Video policy:

* ``Any team member``
* ``Managers and admins``
* ``Admins only``

Team Member Resource
^^^^^^^^^^^^^^^^^^^^

Get info on a team member
+++++++++++++++++++++++++

.. http:get:: /api/teams/[team-slug]/members/[username]

    :<json username: username
    :<json role: One of: ``owner``, ``admin``, ``manager``, or ``contributor``

Litsing all team members
++++++++++++++++++++++++

.. http:get:: /api/teams/[team-slug]/members/

    Returns a list of team member data.  Each item is the same as above.

Add a new member to a team
++++++++++++++++++++++++++

.. http:post:: /api/teams/[team-slug]/members/

    :>json username: username of the user to add
    :>json role: One of: ``owner``, ``admin``, ``manager``, or ``contributor``


Change a team member's role
+++++++++++++++++++++++++++

.. http:put:: /api/teams/[team-slug]/members/[username]/

    :>json role: One of: ``owner``, ``admin``, ``manager``, or ``contributor``

Removing a user from a team
+++++++++++++++++++++++++++

.. http:delete:: /api/teams/[team-slug]/members/[username]/

Safe Team Member Resource
^^^^^^^^^^^^^^^^^^^^^^^^^

This resource behaves the same as the normal Team Member resource except with
couple differences for the POST action to add members

* An invitation is sent to the user to join the team instead of simply adding
  them
* If no user exists with the username, and an ``email`` field is included in
  the POST data, we will create a user and send an email to the email account.

Project Resource
^^^^^^^^^^^^^^^^

List a team's projects
++++++++++++++++++++++

.. http:get:: /api/teams/[team-slug]/projects/

    :>jsonarr name: project name
    :>jsonarr slug: machine-name for the project
    :>jsonarr description: project description
    :>jsonarr guidelines: Project guidelines for users working on it
    :>jsonarr resource_uri: API URI for project details
    :>jsonarr created: datetime when the project was created
    :>jsonarr modified: datetime when the project was last changed
    :>jsonarr workflow_enabled: Are tasks enabled for this project?

Get details on a project
++++++++++++++++++++++++

.. http:get:: /api/teams/[team-slug]/projects/[project-slug]/

    Returns the same data as the project list resource


Create a new project
++++++++++++++++++++

.. http:post:: /api/teams/[team-slug]/projects/

    :<json name: project name
    :<json slug: machine-name for the project
    :<json description: project description *(optional)*
    :<json guidelines: Project guidelines for users working on it *(optional)*

Updating a project
++++++++++++++++++

.. http:put:: /api/teams/[team-slug]/projects/[project-slug]/

    Inputs the same data is the create project resource

Delete a project
++++++++++++++++

.. http:delete:: /api/teams/[team-slug]/projects/[project-slug]/

"""

from __future__ import absolute_import
from django.core.exceptions import PermissionDenied
from django.db import IntegrityError
from django.shortcuts import get_object_or_404
from rest_framework import serializers
from rest_framework import status
from rest_framework import viewsets

from api.pagination import AmaraPaginationMixin
from auth.models import CustomUser as User
from teams.models import Team, TeamMember
import messages.tasks
import teams.permissions as team_permissions

class MappedChoiceField(serializers.ChoiceField):
    """Choice field that maps internal values to choices."""

    default_error_messages = {
        'unknown-choice': "Unknown choice: {choice}",
    }

    def __init__(self, choices, *args, **kwargs):
{'fraction_non_alphanumeric': 0.1555647070935859, 'fraction_numerical': 0.0028827344795634715, 'mean_word_length': 4.322739726027398, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1514614', 'n_tokens_mistral': 2831, 'n_tokens_neox': 2699, 'n_words': 1111}