<commit_before>from policycompass_services import permissions
from rest_framework import generics
from rest_framework import status
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.response import Response
from rest_framework.views import APIView
from .serializers import *
from rest_framework.reverse import reverse
import json
from .models import Story, Chapter, Content


class Base(APIView):
    def get(self, request):
        result = {
            "Stories": reverse('story-view', request=request),
            "Chapters": reverse('chapter-view', request=request),
            "Contents": reverse('content-view', request=request)
        }
        return Response(result)


class StoryView(generics.ListCreateAPIView):
    model = Story
    serializer_class = StorySerializer
    paginate_by = 10
    paginate_by_param = 'page_size'
    permission_classes = IsAuthenticatedOrReadOnly,

    def pre_save(self, obj):
        obj.creator_path = self.request.user.resource_path

    def get(self, request, *args, **kwargs):
        if len(request._request.GET) is 0 or ('page' in request._request.GET) is True:
            stories = Story.objects.all()
            storyList = []
            for s in range(0, len(stories)):
                storyTitle = stories[s].title
                storyChapters = stories[s].chapters
                chapters = []
                for c in range(0, len(storyChapters)):
                    chapterId = int(str(storyChapters[c]))
                    chapter = Chapter.objects.get(pk=chapterId)
                    chapterContents = chapter.contents
                    contents = []
                    for con in range(0, len(chapterContents)):
                        contentId = int(str(chapterContents[con]))
                        content = Content.objects.get(pk=contentId)
                        contents.append({"type": content.type, "index": content.index, "contentId": content.id})
                    chapters.append({"title": chapter.title, "text": chapter.text, "number": chapter.number, "contents": contents})
                story = {"title": storyTitle, "chapters": chapters, "issued": stories[s].date_created, "modified": stories[s].date_modified,
                         "creator_path": stories[s].creator_path, "id": stories[s].id, "is_draft": stories[s].is_draft}
                storyList.append(story)
            response = {"count": len(storyList), "results": storyList, "next": None}

            return Response(response)

        self.serializer_class = StorySerializer
        id = request._request.GET['id']
        try:
            story = Story.objects.get(pk=id)
            storyChapters = story.chapters
            chapters = []
            for c in range(0, len(storyChapters)):
                chapterId = int(str(storyChapters[c]))
                chapter = Chapter.objects.get(pk=chapterId)
                chapterContents = chapter.contents
                contents = []
                for con in range(0, len(chapterContents)):
                    contentId = int(str(chapterContents[con]))
                    content = Content.objects.get(pk=contentId)
                    contents.append({"type": content.type, "index": content.index, "contentId": content.id})
                chapters.append({"title": chapter.title, "text": chapter.text, "number": chapter.number, "contents": contents})

            storyResult = {"title": story.title, "chapters": chapters, "id": story.id, "creator_path": story.creator_path, "is_draft": story.is_draft}

            return Response(storyResult)
        except:
            errorDict = {"result": 500}
            errorString = str(errorDict).replace("'", '"')
            errorJson = json.loads(errorString)<commit_msg>storymanager: get result includes date_created and date_modified<commit_after>from policycompass_services import permissions
from rest_framework import generics
from rest_framework import status
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.response import Response
from rest_framework.views import APIView
from .serializers import *
from rest_framework.reverse import reverse
import json
from .models import Story, Chapter, Content


class Base(APIView):
    def get(self, request):
        result = {
            "Stories": reverse('story-view', request=request),
            "Chapters": reverse('chapter-view', request=request),
            "Contents": reverse('content-view', request=request)
        }
        return Response(result)


class StoryView(generics.ListCreateAPIView):
    model = Story
    serializer_class = StorySerializer
    paginate_by = 10
    paginate_by_param = 'page_size'
    permission_classes = IsAuthenticatedOrReadOnly,

    def pre_save(self, obj):
        obj.creator_path = self.request.user.resource_path

    def get(self, request, *args, **kwargs):
        if len(request._request.GET) is 0 or ('page' in request._request.GET) is True:
            stories = Story.objects.all()
            storyList = []
            for s in range(0, len(stories)):
                storyTitle = stories[s].title
                storyChapters = stories[s].chapters
                chapters = []
                for c in range(0, len(storyChapters)):
                    chapterId = int(str(storyChapters[c]))
                    chapter = Chapter.objects.get(pk=chapterId)
                    chapterContents = chapter.contents
                    contents = []
                    for con in range(0, len(chapterContents)):
                        contentId = int(str(chapterContents[con]))
                        content = Content.objects.get(pk=contentId)
                        contents.append({"type": content.type, "index": content.index, "contentId": content.id})
                    chapters.append({"title": chapter.title, "text": chapter.text, "number": chapter.number, "contents": contents})
                story = {"title": storyTitle, "chapters": chapters, "issued": stories[s].date_created, "modified": stories[s].date_modified,
                         "creator_path": stories[s].creator_path, "id": stories[s].id, "is_draft": stories[s].is_draft}
                storyList.append(story)
            response = {"count": len(storyList), "results": storyList, "next": None}

            return Response(response)

        self.serializer_class = StorySerializer
        id = request._request.GET['id']
        try:
            story = Story.objects.get(pk=id)
            storyChapters = story.chapters
            chapters = []
            for c in range(0, len(storyChapters)):
                chapterId = int(str(storyChapters[c]))
                chapter = Chapter.objects.get(pk=chapterId)
                chapterContents = chapter.contents
                contents = []
                for con in range(0, len(chapterContents)):
                    contentId = int(str(chapterContents[con]))
                    content = Content.objects.get(pk=contentId)
                    contents.append({"type": content.type, "index": content.index, "contentId": content.id})
                chapters.append({"title": chapter.title, "text": chapter.text, "number": chapter.number, "contents": contents})

            storyResult = {"title": story.title, "chapters": chapters, "id": story.id, "creator_path": story.creator_path, "is_draft": story.is_draft, "issued": story.date_created, "modified": story.date_modified}

            return Response(storyResult)
        except:
            errorDict = {"result": 500}
            errorString = str(errorDict).replace("'", '"')
            errorJson = json.loads(errorString)
{'fraction_non_alphanumeric': 0.11373110995310057, 'fraction_numerical': 0.0028660760812923396, 'mean_word_length': 2.398406374501992, 'pattern_counts': {'":': 68, '<': 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': '2192547', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1957, 'n_words': 463}