<commit_before>from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework import viewsets
from .models import Vault, Recent, Tag
from .serializers import VaultSerializer, TagSerializer, RecentSerializer


class VaultViewSet(viewsets.ModelViewSet):
    authentication_classes = (SessionAuthentication, BasicAuthentication, JSONWebTokenAuthentication)
    permission_classes = (IsAuthenticated,)
    queryset = Vault.objects.all()
    serializer_class = VaultSerializer


class TagViewSet(viewsets.ModelViewSet):
    authentication_classes = (SessionAuthentication, BasicAuthentication, JSONWebTokenAuthentication)
    permission_classes = (IsAuthenticated,)
    queryset = Tag.objects.all()
    serializer_class = TagSerializer<commit_msg>finished all views<commit_after>from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework import viewsets
from .models import Vault, Recent, Tag
from .serializers import VaultSerializer, TagSerializer, RecentSerializer
from rest_framework.decorators import detail_route
from rest_framework.response import Response
from django.utils import timezone
from django.core import serializers


class VaultViewSet(viewsets.ModelViewSet):
    authentication_classes = (SessionAuthentication, BasicAuthentication, JSONWebTokenAuthentication)
    permission_classes = (IsAuthenticated,)
    queryset = Vault.objects.all()
    serializer_class = VaultSerializer

    def retrieve(self, request, pk=None, *args, **kwargs):
        """
        Save recently accessed item to recent table
        """
        instance = self.get_object()
        if instance:
            obj, created = Recent.objects.get_or_create(vault=instance)
            if not created:
                count = Recent.objects.count()
                if count == 20:  # table can contain 20 max items, otherwise delete the 1st item brutally :D
                    obj_del = Recent.objects.first()
                    obj_del.delete()
                obj.accessed_at = timezone.now()
                obj.save()
        serializer = self.get_serializer(instance)
        return Response(serializer.data)


class TagViewSet(viewsets.ModelViewSet):
    authentication_classes = (SessionAuthentication, BasicAuthentication, JSONWebTokenAuthentication)
    permission_classes = (IsAuthenticated,)
    queryset = Tag.objects.all()
    serializer_class = TagSerializer

    @detail_route(methods=['get'])
    def tagged(self, request, *args, **kwargs):
        """
        Get all Vault item with specific tag
        """
        instance = self.get_object()
        if instance:
            hiren = Vault.objects.filter(tag=instance.id)
            data = serializers.serialize("json", hiren)
            return Response(data)


class RecentViewSet(viewsets.ModelViewSet):
    authentication_classes = (SessionAuthentication, BasicAuthentication, JSONWebTokenAuthentication)
    permission_classes = (IsAuthenticated,)
    queryset = Recent.objects.order_by('-id')
    serializer_class = RecentSerializer
{'fraction_non_alphanumeric': 0.07065868263473053, 'fraction_numerical': 0.0014970059880239522, 'mean_word_length': 4.077507598784194, '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': '6110880', 'n_tokens_mistral': 837, 'n_tokens_neox': 817, 'n_words': 210}