<commit_before># coding: utf-8
import copy
import json
from collections import defaultdict, OrderedDict
from operator import itemgetter
from hashlib import md5


from django.contrib.contenttypes.models import ContentType
from django.db.models import Count
from django.http import Http404
from django.shortcuts import get_object_or_404
from rest_framework import exceptions, renderers, status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework_extensions.mixins import NestedViewSetMixin

from kpi.constants import (
    ASSET_TYPES,
    ASSET_TYPE_ARG_NAME,
    ASSET_TYPE_SURVEY,
    ASSET_TYPE_TEMPLATE,
    CLONE_ARG_NAME,
    CLONE_COMPATIBLE_TYPES,
    CLONE_FROM_VERSION_ID_ARG_NAME,
)
from kpi.deployment_backends.backends import DEPLOYMENT_BACKENDS
from kpi.exceptions import BadAssetTypeException
from kpi.filters import (
    AssetOrderingFilter,
    KpiObjectPermissionsFilter,
    SearchFilter
)
from kpi.highlighters import highlight_xform
from kpi.models import Asset
from kpi.models.object_permission import (
    ObjectPermission,
    get_anonymous_user,
    get_objects_for_user
)
from kpi.models.asset import UserAssetSubscription
from kpi.paginators import AssetPagination
from kpi.permissions import IsOwnerOrReadOnly, PostMappedToChangePermission, \
    get_perm_name
from kpi.renderers import AssetJsonRenderer, SSJsonRenderer, XFormRenderer, \
    XlsRenderer
from kpi.serializers import DeploymentSerializer
from kpi.serializers.v2.asset import AssetListSerializer, AssetSerializer
from kpi.utils.strings import hashable_str
from kpi.utils.kobo_to_xlsform import to_xlsform_structure
from kpi.utils.ss_structure_to_mdtable import ss_structure_to_mdtable


class AssetViewSet(NestedViewSetMixin, viewsets.ModelViewSet):
    """
    * Assign a asset to a collection <span class='label label-warning'>partially implemented</span>
    * Run a partial update of a asset <span class='label label-danger'>TODO</span>

    <span class='label label-danger'>TODO</span> Complete documentation

    ## List of asset endpoints

    Lists the asset endpoints accessible to requesting user, for anonymous access
    a list of public data endpoints is returned.

    <pre class="prettyprint">
    <b>GET</b> /api/v2/assets/
    </pre>

    > Example
    >
    >       curl -X GET https://[kpi]/api/v2/assets/

    Search can be made with `q` parameter. Look at [README](https://github.com/kobotoolbox/kpi#searching-assets)
    for more details

    Get an hash of all `version_id`s of assets.
    Useful to detect any changes in assets with only one call to `API`

    <pre class="prettyprint">
    <b>GET</b> /api/v2/assets/hash/
    </pre>

    > Example
    >
    >       curl -X GET https://[kpi]/api/v2/assets/hash/

    ## CRUD

    * `uid` - is the unique identifier of a specific asset

    Retrieves current asset
    <pre class="prettyprint">
    <b>GET</b> /api/v2/assets/<code>{uid}</code>/
    </pre>


    > Example
    >
    >       curl -X GET https://[kpi]/api/v2/assets/aSAvYreNzVEkrWg5Gdcvg/

    Creates or clones an asset.<commit_msg>Updated AssetViewSet API doc<commit_after># coding: utf-8
import copy
import json
from collections import defaultdict, OrderedDict
from operator import itemgetter
from hashlib import md5


from django.contrib.contenttypes.models import ContentType
from django.db.models import Count
from django.http import Http404
from django.shortcuts import get_object_or_404
from rest_framework import exceptions, renderers, status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework_extensions.mixins import NestedViewSetMixin

from kpi.constants import (
    ASSET_TYPES,
    ASSET_TYPE_ARG_NAME,
    ASSET_TYPE_SURVEY,
    ASSET_TYPE_TEMPLATE,
    CLONE_ARG_NAME,
    CLONE_COMPATIBLE_TYPES,
    CLONE_FROM_VERSION_ID_ARG_NAME,
)
from kpi.deployment_backends.backends import DEPLOYMENT_BACKENDS
from kpi.exceptions import BadAssetTypeException
from kpi.filters import (
    AssetOrderingFilter,
    KpiObjectPermissionsFilter,
    SearchFilter
)
from kpi.highlighters import highlight_xform
from kpi.models import Asset
from kpi.models.object_permission import (
    ObjectPermission,
    get_anonymous_user,
    get_objects_for_user
)
from kpi.models.asset import UserAssetSubscription
from kpi.paginators import AssetPagination
from kpi.permissions import IsOwnerOrReadOnly, PostMappedToChangePermission, \
    get_perm_name
from kpi.renderers import AssetJsonRenderer, SSJsonRenderer, XFormRenderer, \
    XlsRenderer
from kpi.serializers import DeploymentSerializer
from kpi.serializers.v2.asset import AssetListSerializer, AssetSerializer
from kpi.utils.strings import hashable_str
from kpi.utils.kobo_to_xlsform import to_xlsform_structure
from kpi.utils.ss_structure_to_mdtable import ss_structure_to_mdtable


class AssetViewSet(NestedViewSetMixin, viewsets.ModelViewSet):
    """
    * Assign a asset to a collection <span class='label label-warning'>partially implemented</span>
    * Run a partial update of a asset <span class='label label-danger'>TODO</span>

    ## List of asset endpoints

    Lists the asset endpoints accessible to requesting user, for anonymous access
    a list of public data endpoints is returned.

    <pre class="prettyprint">
    <b>GET</b> /api/v2/assets/
    </pre>

    > Example
    >
    >       curl -X GET https://[kpi]/api/v2/assets/

    Search can be made with `q` parameter.
    Search filters can be returned with results by passing `metadata=on` to querystring.
    > Example
    >
    >       curl -X GET https://[kpi]/api/v2/assets/?metadata=on
    >       {
    >           "count": 0
    >           "next": ...
    >           "previous": ...
    >           "results": []
    >           "metadata": {
    >               "languages": [],
    >               "countries": [],
    >               "sectors": [],
    >               "organizations": []
    >           }
    >       }

    Look at [README](https://github.com/kobotoolbox/kpi#searching-assets)
    for more details.

    <hr>

    Get an hash of all `version_id`s of assets.
    Useful to detect any changes in assets with only one call to `API`

    <pre class="prettyprint">
    <b>GET</b> /api/v2/assets/hash/
    </pre>

    > Example
    >
    >       curl -X GET https://[kpi]/api/v2/assets/hash/

    ## CRUD

    * `uid` - is the unique identifier of a specific asset

    Retrieves current asset
    <pre class="prettyprint">
    <b>GET</b> /api/v2/assets/<code>{uid}</code>/
    </pre>


    > Example
    >
    >       curl -X GET https://[kpi]/api/v2/assets/aSAvYreNzVEkrWg5Gdcvg/

    Creates or clones an asset.
{'fraction_non_alphanumeric': 0.08175074183976261, 'fraction_numerical': 0.005044510385756677, 'mean_word_length': 3.467196819085487, 'pattern_counts': {'":': 9, '<': 42, '<?xml version=': 0, '>': 75, 'https://': 9, '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': '5485188', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2068, 'n_words': 561}