<commit_before>from django.http import Http404
from django.http import HttpResponse
from rest_framework import parsers
from rest_framework import permissions
from rest_framework import viewsets
from rest_framework.response import Response

from content.models import Content
from content.serializers import ContentSerializer
from section.services import get_children, Section
from version.models import Version


class ContentPermission(permissions.IsAuthenticated):
    def has_object_permission(self, request, view, obj):
        permitted_by_super = super().has_object_permission(request, view, obj)

        if request.method not in ('POST',):
            return permitted_by_super

        print(obj.version.created_by)
        print(request.user)

        return permitted_by_super and obj.version.created_by == request.user


def _convert_to_int(*args):
    return (int(arg) if arg is not None else None for arg in args)


class IsCreatorOrReadOnly(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        if request.method in permissions.SAFE_METHODS:
            return True

        return obj.version.created_by == request.user


class PlainTextParser(parsers.BaseParser):
    media_type = 'text/plain'

    def parse(self, stream, media_type=None, parser_context=None):
        return stream.read()


class ContentViewSet(viewsets.GenericViewSet):
    serializer_class = ContentSerializer
    permission_classes = (permissions.IsAuthenticated, IsCreatorOrReadOnly, )
    parser_classes = (PlainTextParser,)

    @staticmethod
    def _get_content_or_404(section_pk, version_pk):
        version_pk = version_pk if version_pk is not None else Version.objects.order_by('id')[0].id
        try:
            return Content.objects.get(section__id=section_pk, version__id=version_pk)
        except (Section.DoesNotExist, Version.DoesNotExist, Content.DoesNotExist):
            raise Http404

    @staticmethod
    def _get_content_or_create(section_pk, version_pk):
        try:
            section = Section.objects.get(id=section_pk)
            version = Version.objects.get(id=version_pk)
            return Content.objects.get(section=section, version=version)
        except (Section.DoesNotExist, Version.DoesNotExist):<commit_msg>fix: auth permission<commit_after>from django.http import Http404
from django.http import HttpResponse
from rest_framework import parsers
from rest_framework import permissions
from rest_framework import viewsets
from rest_framework.response import Response

from content.models import Content
from content.serializers import ContentSerializer
from section.services import get_children, Section
from version.models import Version


def _convert_to_int(*args):
    return (int(arg) if arg is not None else None for arg in args)


class IsCreatorOrReadOnly(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        if request.method in permissions.SAFE_METHODS:
            return True

        return obj.version.created_by == request.user


class PlainTextParser(parsers.BaseParser):
    media_type = 'text/plain'

    def parse(self, stream, media_type=None, parser_context=None):
        return stream.read()


class ContentViewSet(viewsets.GenericViewSet):
    serializer_class = ContentSerializer
    permission_classes = (permissions.IsAuthenticated, IsCreatorOrReadOnly,)
    parser_classes = (PlainTextParser,)

    @staticmethod
    def _get_content_or_404(section_pk, version_pk):
        version_pk = version_pk if version_pk is not None else Version.objects.order_by('id')[0].id
        try:
            return Content.objects.get(section__id=section_pk, version__id=version_pk)
        except (Section.DoesNotExist, Version.DoesNotExist, Content.DoesNotExist):
            raise Http404

    @staticmethod
    def _get_content_or_create(section_pk, version_pk):
        try:
            section = Section.objects.get(id=section_pk)
            version = Version.objects.get(id=version_pk)
            return Content.objects.get(section=section, version=version)
        except (Section.DoesNotExist, Version.DoesNotExist):
{'fraction_non_alphanumeric': 0.0731824747231584, 'fraction_numerical': 0.004814636494944632, 'mean_word_length': 4.012062726176116, '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': '6463019', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1134, 'n_words': 286}