<commit_before>import django_rq
from django.http import FileResponse
from rest_framework import viewsets, permissions
from rest_framework.decorators import detail_route

from conversion_job.models import Extent, ConversionJob, GISFormat
from conversion_job.serializers import ExtentSerializer, ConversionJobSerializer, ConversionJobStatusSerializer, \
    GISFormatStatusSerializer
from shared import rq_job_status_mapping


class ExtentViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.
    """
    queryset = Extent.objects.all()
    serializer_class = ExtentSerializer
    permission_classes = (
        permissions.IsAuthenticatedOrReadOnly,
    )


class ConversionJobViewSet(viewsets.ModelViewSet):
    queryset = ConversionJob.objects.all()
    serializer_class = ConversionJobSerializer
    permission_classes = (
        permissions.IsAuthenticatedOrReadOnly,
    )


class ConversionJobStatusViewSet(viewsets.mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    lookup_field = 'rq_job_id'
    lookup_value_regex = '[0-9a-f-]{36}'

    queryset = ConversionJob.objects.all()
    serializer_class = ConversionJobStatusSerializer
    permission_classes = (
        permissions.IsAuthenticatedOrReadOnly,
    )

    def retrieve(self, request, *args, **kwargs):
        self._update_status_from_rq()
        return super().retrieve(request, *args, **kwargs)

    def _update_status_from_rq(self):
        conversion_job = self.get_object()
        rq_job = django_rq.get_queue().fetch_job(job_id=conversion_job.rq_job_id)

        # only do work if the job is not yet deleted
        if rq_job:
            conversion_job.status = rq_job_status_mapping[rq_job.status].value

            progress = rq_job.meta.get('progress', None)
            if progress:<commit_msg>only allow authenticated users to access the API<commit_after>import django_rq
from django.http import FileResponse
from rest_framework import viewsets, permissions
from rest_framework.decorators import detail_route

from conversion_job.models import Extent, ConversionJob, GISFormat
from conversion_job.serializers import ExtentSerializer, ConversionJobSerializer, ConversionJobStatusSerializer, \
    GISFormatStatusSerializer
from shared import rq_job_status_mapping


class ExtentViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.
    """
    queryset = Extent.objects.all()
    serializer_class = ExtentSerializer
    permission_classes = (
        permissions.IsAuthenticated,
    )


class ConversionJobViewSet(viewsets.ModelViewSet):
    queryset = ConversionJob.objects.all()
    serializer_class = ConversionJobSerializer
    permission_classes = (
        permissions.IsAuthenticated,
    )


class ConversionJobStatusViewSet(viewsets.mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    lookup_field = 'rq_job_id'
    lookup_value_regex = '[0-9a-f-]{36}'

    queryset = ConversionJob.objects.all()
    serializer_class = ConversionJobStatusSerializer
    permission_classes = (
        permissions.IsAuthenticated,
    )

    def retrieve(self, request, *args, **kwargs):
        self._update_status_from_rq()
        return super().retrieve(request, *args, **kwargs)

    def _update_status_from_rq(self):
        conversion_job = self.get_object()
        rq_job = django_rq.get_queue().fetch_job(job_id=conversion_job.rq_job_id)

        # only do work if the job is not yet deleted
        if rq_job:
            conversion_job.status = rq_job_status_mapping[rq_job.status].value

            progress = rq_job.meta.get('progress', None)
            if progress:
{'fraction_non_alphanumeric': 0.07997867235403892, 'fraction_numerical': 0.0021327645961077045, 'mean_word_length': 4.029490616621984, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '4635345', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1075, 'n_words': 220}