<commit_before># coding: utf-8
from rest_framework import status, exceptions
from rest_framework.response import Response
from rest_framework.reverse import reverse

from kpi.models import ExportTask
from kpi.models.import_export_task import _resolve_url_to_asset_or_collection
from kpi.model_utils import remove_string_prefix
from kpi.serializers import ExportTaskSerializer
from kpi.tasks import export_in_background
from kpi.views.no_update_model import NoUpdateModelViewSet


class ExportTaskViewSet(NoUpdateModelViewSet):
    queryset = ExportTask.objects.all()
    serializer_class = ExportTaskSerializer
    lookup_field = 'uid'

    def get_queryset(self, *args, **kwargs):
        if self.request.user.is_anonymous:
            return ExportTask.objects.none()

        queryset = ExportTask.objects.filter(
            user=self.request.user).order_by('date_created')

        # Ultra-basic filtering by:
        # * source URL or UID if `q=source:[URL|UID]` was provided;
        # * comma-separated list of `ExportTask` UIDs if
        #   `q=uid__in:[UID],[UID],...` was provided
        q = self.request.query_params.get('q', False)
        if not q:
            # No filter requested
            return queryset
        if q.startswith('source:'):
            q = remove_string_prefix(q, 'source:')
            queryset = queryset.filter(data__source=q)
        elif q.startswith('uid__in:'):
            q = remove_string_prefix(q, 'uid__in:')
            uids = [uid.strip() for uid in q.split(',')]
            queryset = queryset.filter(uid__in=uids)
        else:
            # Filter requested that we don't understand; make it obvious by
            # returning nothing
            return ExportTask.objects.none()
        return queryset

    def create(self, request, *args, **kwargs):
        if self.request.user.is_anonymous:
            raise exceptions.NotAuthenticated()

        # Read valid options from POST data
        valid_options = (<commit_msg>Fix JSONB export uid filtering issue<commit_after># coding: utf-8
from django.db.models import TextField
from django.db.models.functions import Cast
from rest_framework import status, exceptions
from rest_framework.response import Response
from rest_framework.reverse import reverse

from kpi.models import ExportTask
from kpi.models.import_export_task import _resolve_url_to_asset_or_collection
from kpi.model_utils import remove_string_prefix
from kpi.serializers import ExportTaskSerializer
from kpi.tasks import export_in_background
from kpi.views.no_update_model import NoUpdateModelViewSet


class ExportTaskViewSet(NoUpdateModelViewSet):
    queryset = ExportTask.objects.all()
    serializer_class = ExportTaskSerializer
    lookup_field = 'uid'

    def get_queryset(self, *args, **kwargs):
        if self.request.user.is_anonymous:
            return ExportTask.objects.none()

        queryset = ExportTask.objects.filter(
            user=self.request.user).order_by('date_created')

        # Ultra-basic filtering by:
        # * source URL or UID if `q=source:[URL|UID]` was provided;
        # * comma-separated list of `ExportTask` UIDs if
        #   `q=uid__in:[UID],[UID],...` was provided
        q = self.request.query_params.get('q', False)
        if not q:
            # No filter requested
            return queryset
        if q.startswith('source:'):
            q = remove_string_prefix(q, 'source:')
            # Crude, but `data__source` is a URL. Cast `data__source` to a
            # `TextField` to avoid the special behavior of `__contains` for
            # `JSONField`s. See
            # https://docs.djangoproject.com/en/2.2/ref/contrib/postgres/fields/#std:fieldlookup-hstorefield.contains
            queryset = queryset.annotate(
                source_str=Cast('data__source', output_field=TextField())
            ).filter(source_str__contains=q)
        elif q.startswith('uid__in:'):
            q = remove_string_prefix(q, 'uid__in:')
            uids = [uid.strip() for uid in q.split(',')]
            queryset = queryset.filter(uid__in=uids)
        else:
            # Filter requested that we don't understand; make it obvious by
            # returning nothing
            return ExportTask.objects.none()
        return queryset

    def create(self, request, *args, **kwargs):
        if self.request.user.is_anonymous:
            raise exceptions.NotAuthenticated()

        # Read valid options from POST data
        valid_options = (
{'fraction_non_alphanumeric': 0.09131502567537397, 'fraction_numerical': 0.0008930564858227283, 'mean_word_length': 2.8389031705227077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '939769', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1252, 'n_words': 324}