<commit_before>from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render, get_object_or_404
from rest_framework import viewsets, exceptions, status
from rest_framework.response import Response
from laice.utils import spacy_ner

from laice.core.viewsets import ViewMappingMixin
from stories.models import Story, Query
from stories.serializers import (
    StorySerializer,
    StoryAttributeSerializer,
    InputStoryAttributeSerializer,
    QuerySerializer,
    InputQuerySerializer,
    QueryNERSerializer
)
from stories.viewsets import GetStoryMixin


# Render views
def stories_view(request):
    return render(request, 'stories.html')


def inner_story_view(request, story_name):
    inner_story = get_object_or_404(Story, name=story_name)
    return render(request, 'inner-story.html', {'story_name': story_name})


# Rest framework ViewSets
# Story
class StoryViewSet(GetStoryMixin, ViewMappingMixin, viewsets.ModelViewSet):
    queryset = Story.objects.all()
    serializer_class = StorySerializer

    def list(self, request, *args, **kwargs):
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)

    def destroy(self, request, *args, **kwargs):
        story = self.get_object()
        story.delete()
        spacy_ner.clean_story(story)
        return Response(status=status.HTTP_204_NO_CONTENT)


# Story Attributes
class StoryAttributeViewSet(GetStoryMixin, ViewMappingMixin, viewsets.ModelViewSet):
    queryset = Story.objects.all()

    input_serializer_class = InputStoryAttributeSerializer
    output_serializer_class = StoryAttributeSerializer
    serializer_class = output_serializer_class

    second_key = 'attribute_name'

    def create(self, request, *args, **kwargs):
        request_data = request.data.copy()
        request_data['story'] = self.kwargs[self.first_key]
        serializer = self.input_serializer_class(data=request_data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)

    def retrieve(self, request, *args, **kwargs):
        attribute = self.get_attribute()
        serializer = self.output_serializer_class(attribute)<commit_msg>🐞 Fixed project delete bug<commit_after>from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render, get_object_or_404
from rest_framework import viewsets, exceptions, status
from rest_framework.response import Response
from laice.utils import spacy_ner

from laice.core.viewsets import ViewMappingMixin
from stories.models import Story, Query
from stories.serializers import (
    StorySerializer,
    StoryAttributeSerializer,
    InputStoryAttributeSerializer,
    QuerySerializer,
    InputQuerySerializer,
    QueryNERSerializer
)
from stories.viewsets import GetStoryMixin


# Render views
def stories_view(request):
    return render(request, 'stories.html')


def inner_story_view(request, story_name):
    inner_story = get_object_or_404(Story, name=story_name)
    return render(request, 'inner-story.html', {'story_name': story_name})


# Rest framework ViewSets
# Story
class StoryViewSet(GetStoryMixin, ViewMappingMixin, viewsets.ModelViewSet):
    queryset = Story.objects.all()
    serializer_class = StorySerializer

    def list(self, request, *args, **kwargs):
        serializer = self.get_serializer(Story.objects.all(), many=True)
        return Response(serializer.data)

    def destroy(self, request, *args, **kwargs):
        story = self.get_object()
        story.delete()
        spacy_ner.clean_story(story)
        return Response(status=status.HTTP_204_NO_CONTENT)


# Story Attributes
class StoryAttributeViewSet(GetStoryMixin, ViewMappingMixin, viewsets.ModelViewSet):
    queryset = Story.objects.all()

    input_serializer_class = InputStoryAttributeSerializer
    output_serializer_class = StoryAttributeSerializer
    serializer_class = output_serializer_class

    second_key = 'attribute_name'

    def create(self, request, *args, **kwargs):
        request_data = request.data.copy()
        request_data['story'] = self.kwargs[self.first_key]
        serializer = self.input_serializer_class(data=request_data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)

    def retrieve(self, request, *args, **kwargs):
        attribute = self.get_attribute()
        serializer = self.output_serializer_class(attribute)
{'fraction_non_alphanumeric': 0.08349813227862009, 'fraction_numerical': 0.005273566249176005, 'mean_word_length': 4.633663366336633, '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': '3344997', 'n_tokens_mistral': 1331, 'n_tokens_neox': 1278, 'n_words': 277}