<commit_before>from django.shortcuts import get_object_or_404

from rest_framework import status
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from onadata.libs.filters import AnonUserProjectFilter, ProjectOwnerFilter
from onadata.libs.serializers.project_serializer import ProjectSerializer
from onadata.libs.serializers.share_project_serializer import \
    ShareProjectSerializer
from onadata.libs.serializers.xform_serializer import XFormSerializer
from onadata.apps.api.models import Project, ProjectXForm
from onadata.apps.api import tools as utils
from onadata.apps.api.permissions import ProjectPermissions
from onadata.apps.logger.models import XForm


class ProjectViewSet(ModelViewSet):
    """
List, Retrieve, Update, Create Project and Project Forms

Where:

- `pk` - is the project id
- `formid` - is the form id
- `owner` - is the username for the user or organization of the project

## Register a new Project
<pre class="prettyprint">
<b>POST</b> /api/v1/projects</pre>
> Example
>
>       {
>           "url": "https://ona.io/api/v1/projects/1",
>           "owner": "https://ona.io/api/v1/users/ona",
>           "name": "project 1",
>           "date_created": "2013-07-24T13:37:39Z",
>           "date_modified": "2013-07-24T13:37:39Z"
>       }

## List of Projects

<pre class="prettyprint"><b>GET</b> /api/v1/projects</pre>
> Example
>
>       curl -X GET https://ona.io/api/v1/projects

> Response
>
>       [
>           {
>               "url": "https://ona.io/api/v1/projects/1",
>               "owner": "https://ona.io/api/v1/users/ona",
>               "name": "project 1",
>               "date_created": "2013-07-24T13:37:39Z",
>               "date_modified": "2013-07-24T13:37:39Z"
>           },
>           {
>               "url": "https://ona.io/api/v1/projects/4",
>               "owner": "https://ona.io/api/v1/users/ona",
>               "name": "project 2",
>               "date_created": "2013-07-24T13:59:10Z",
>               "date_modified": "2013-07-24T13:59:10Z"
>           }, ...
>       ]

## List of Projects filter by owner/organization

<pre class="prettyprint">
<b>GET</b> /api/v1/projects?<code>owner</code>=<code>owner_username</code>
</pre>
> Example
>
>       curl -X GET https://ona.io/api/v1/projects?owner=ona

## Retrieve Project Information

<pre class="prettyprint">
<b>GET</b> /api/v1/projects/<code>{pk}</code></pre>
> Example
>
>       curl -X GET https://ona.io/api/v1/projects/1

> Response
>
>       {
>           "url": "https://ona.io/api/v1/projects/1",
>           "owner": "https://ona.io/api/v1/users/ona",
>           "name": "project 1",
>           "date_created": "2013-07-24T13:37:39Z",
>           "date_modified": "2013-07-24T13:37:39Z"
>       }

## Update Project Information

<pre class="prettyprint">
<b>PUT</b> /api/v1/projects/<code>{pk}</code> or \
<b>PATCH</b> /api/v1/projects/<code>{pk}</code></pre></pre>
> Example

>        curl -X PATCH -d '{metadata: { "description": "Lorem ipsum",\
"location": "Nakuru, Kenya",\
"category": "water"}} \
https://ona.io/api/v1/projects/1  "Content-Type: application/json"


> Response
>
>       {
>           "url": "https://ona.io/api/v1/projects/1",
>           "owner": "https://ona.io/api/v1/users/ona",
>           "name": "project 1",
>           "metadata": {
>                        "description": "Lorem ipsum",
>                        "location": "Nakuru, Kenya",
>                        "category": "water"
>                        }
>           "date_created": "2013-07-24T13:37:39Z",
>           "date_modified": "2013-07-24T13:37:39Z"
>       }

## Share a project with a specific user

You can share a project with a specific user by `POST` a payload with

- `username` of the user you want to share the form with and
- `role` you want the user to have on the form. Available roles are `readonly`,
`dataentry`, `editor`, `manager`.<commit_msg>PLD: use form param format<commit_after>from django.shortcuts import get_object_or_404

from rest_framework import status
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from onadata.libs.filters import AnonUserProjectFilter, ProjectOwnerFilter
from onadata.libs.serializers.project_serializer import ProjectSerializer
from onadata.libs.serializers.share_project_serializer import \
    ShareProjectSerializer
from onadata.libs.serializers.xform_serializer import XFormSerializer
from onadata.apps.api.models import Project, ProjectXForm
from onadata.apps.api import tools as utils
from onadata.apps.api.permissions import ProjectPermissions
from onadata.apps.logger.models import XForm


class ProjectViewSet(ModelViewSet):
    """
List, Retrieve, Update, Create Project and Project Forms

Where:

- `pk` - is the project id
- `formid` - is the form id
- `owner` - is the username for the user or organization of the project

## Register a new Project
<pre class="prettyprint">
<b>POST</b> /api/v1/projects</pre>
> Example
>
>       {
>           "url": "https://ona.io/api/v1/projects/1",
>           "owner": "https://ona.io/api/v1/users/ona",
>           "name": "project 1",
>           "date_created": "2013-07-24T13:37:39Z",
>           "date_modified": "2013-07-24T13:37:39Z"
>       }

## List of Projects

<pre class="prettyprint"><b>GET</b> /api/v1/projects</pre>
> Example
>
>       curl -X GET https://ona.io/api/v1/projects

> Response
>
>       [
>           {
>               "url": "https://ona.io/api/v1/projects/1",
>               "owner": "https://ona.io/api/v1/users/ona",
>               "name": "project 1",
>               "date_created": "2013-07-24T13:37:39Z",
>               "date_modified": "2013-07-24T13:37:39Z"
>           },
>           {
>               "url": "https://ona.io/api/v1/projects/4",
>               "owner": "https://ona.io/api/v1/users/ona",
>               "name": "project 2",
>               "date_created": "2013-07-24T13:59:10Z",
>               "date_modified": "2013-07-24T13:59:10Z"
>           }, ...
>       ]

## List of Projects filter by owner/organization

<pre class="prettyprint">
<b>GET</b> /api/v1/projects?<code>owner</code>=<code>owner_username</code>
</pre>
> Example
>
>       curl -X GET https://ona.io/api/v1/projects?owner=ona

## Retrieve Project Information

<pre class="prettyprint">
<b>GET</b> /api/v1/projects/<code>{pk}</code></pre>
> Example
>
>       curl -X GET https://ona.io/api/v1/projects/1

> Response
>
>       {
>           "url": "https://ona.io/api/v1/projects/1",
>           "owner": "https://ona.io/api/v1/users/ona",
>           "name": "project 1",
>           "date_created": "2013-07-24T13:37:39Z",
>           "date_modified": "2013-07-24T13:37:39Z"
>       }

## Update Project Information

<pre class="prettyprint">
<b>PUT</b> /api/v1/projects/<code>{pk}</code> or \
<b>PATCH</b> /api/v1/projects/<code>{pk}</code></pre></pre>
> Example

>        curl -X PATCH -d 'metadata={"description": "Lorem ipsum",\
"location": "Nakuru, Kenya",\
"category": "water"}' \
https://ona.io/api/v1/projects/1

> Response
>
>       {
>           "url": "https://ona.io/api/v1/projects/1",
>           "owner": "https://ona.io/api/v1/users/ona",
>           "name": "project 1",
>           "metadata": {
>                        "description": "Lorem ipsum",
>                        "location": "Nakuru, Kenya",
>                        "category": "water"
>                        }
>           "date_created": "2013-07-24T13:37:39Z",
>           "date_modified": "2013-07-24T13:37:39Z"
>       }

## Share a project with a specific user

You can share a project with a specific user by `POST` a payload with

- `username` of the user you want to share the form with and
- `role` you want the user to have on the form. Available roles are `readonly`,
`dataentry`, `editor`, `manager`.
{'fraction_non_alphanumeric': 0.16308311329058217, 'fraction_numerical': 0.04400855023261662, 'mean_word_length': 2.562024182713838, 'pattern_counts': {'":': 64, '<': 69, '<?xml version=': 0, '>': 191, 'https://': 28, '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': '4032670', 'n_tokens_mistral': 2867, 'n_tokens_neox': 2655, 'n_words': 562}