<commit_before>from rest_framework import generics, permissions as drf_permissions

from website.files.models import File
from website.files.models import FileVersion
from api.base.utils import get_object_or_error
from api.base.filters import ODMFilterMixin
from api.files.serializers import FileSerializer
from api.files.serializers import FileVersionSerializer


class FileMixin(object):
    """Mixin with convenience methods for retrieving the current file based on the
    current URL. By default, fetches the file based on the file_id kwarg.
    """

    serializer_class = FileSerializer
    file_lookup_url_kwarg = 'file_id'

    def get_file(self, check_permissions=True):
        key = self.kwargs[self.file_lookup_url_kwarg]

        obj = get_object_or_error(File, key)

        if check_permissions:
            # May raise a permission denied
            self.check_object_permissions(self.request, obj)
        return obj


class FileList(generics.ListAPIView, ODMFilterMixin):
    """Files that the OSF has metadata about

    You can filter on users by their id, name, path, provider
    """
    permission_classes = (
        drf_permissions.IsAuthenticatedOrReadOnly,
    )
    serializer_class = FileSerializer
    ordering = ('-last_touched', )

    # overrides ODMFilterMixin
    def get_default_odm_query(self):
        return File._filter()

    # overrides ListAPIView
    def get_queryset(self):
        # TODO: sort
        query = self.get_query_from_request()
        return File.find(query)


class FileDetail(generics.RetrieveAPIView, FileMixin):
    """Details about a specific file.
    """
    serializer_class = FileSerializer

    # overrides RetrieveAPIView<commit_msg>Allow folders to be looked up as well<commit_after>from rest_framework import generics, permissions as drf_permissions

from website.files.models import File
from website.files.models import FileNode
from website.files.models import FileVersion
from api.base.utils import get_object_or_error
from api.base.filters import ODMFilterMixin
from api.files.serializers import FileSerializer
from api.files.serializers import FileVersionSerializer


class FileMixin(object):
    """Mixin with convenience methods for retrieving the current file based on the
    current URL. By default, fetches the file based on the file_id kwarg.
    """

    serializer_class = FileSerializer
    file_lookup_url_kwarg = 'file_id'

    def get_file(self, check_permissions=True):
        key = self.kwargs[self.file_lookup_url_kwarg]

        obj = get_object_or_error(FileNode, key)

        if check_permissions:
            # May raise a permission denied
            self.check_object_permissions(self.request, obj)
        return obj


class FileList(generics.ListAPIView, ODMFilterMixin):
    """Files that the OSF has metadata about

    You can filter on users by their id, name, path, provider
    """
    permission_classes = (
        drf_permissions.IsAuthenticatedOrReadOnly,
    )
    serializer_class = FileSerializer
    ordering = ('-last_touched', )

    # overrides ODMFilterMixin
    def get_default_odm_query(self):
        return File._filter()

    # overrides ListAPIView
    def get_queryset(self):
        # TODO: sort
        query = self.get_query_from_request()
        return FileNode.find(query)


class FileDetail(generics.RetrieveAPIView, FileMixin):
    """Details about a specific file.
    """
    serializer_class = FileSerializer

    # overrides RetrieveAPIView
{'fraction_non_alphanumeric': 0.06601607347876004, 'fraction_numerical': 0.0, 'mean_word_length': 3.56151832460733, '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': '2311504', 'n_tokens_mistral': 1001, 'n_tokens_neox': 975, 'n_words': 280}