This commit is contained in:
Alex committed 2024-04-19 13:53:25 +03:00
1 parent 631cecc8cd
commit 2aeb3ef513
8 files changed
+158 -105

No files matched your search

+10 -22
View File
@@ -1,5 +1,6 @@
from itertools import groupby
from django.db.models import Count
from rest_framework.generics import ListAPIView
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
@@ -9,30 +10,17 @@ from userauth.serializers import *
from userauth.models import *
# Create your views here.
class SafetyTypeAPIView(ListAPIView):
def get(self, request):
chapters = SafetyType.objects.annotate(Count("type_file")).filter(type_file__count__gt=0)
return Response(SafetyTypeSerializer(chapters, many=True).data)
class SafetyFileAPIView(APIView):
def get(self, request):
chapters = self.request.query_params.get('chapter_id', 1)
all_file = SafetyFile.objects.filter(chapter_id=chapters).order_by('chapter_id', 'category_id').all()
serializer_for_file = SafetyFileSerializer(instance=all_file, many=True)
data = []
for (chapter_id, chapter_name), row in groupby(serializer_for_file.data, lambda x: (x['chapter_id'], x['chapter_name'])):
chapter = {
'id': chapter_id,
'name': chapter_name,
'categories': []
}
data.append(chapter)
for (category_id, category_name), files in groupby(row, lambda x: (x['category_id'], x['category_name'])):
chapter['categories'].append({
'id': category_id,
'name': category_name,
'files': [file for file in files]
})
return Response(data)
chapters = self.request.query_params.get('chapter', "fire_safety")
safety_group = SafetyType.objects.get(path=chapters)
return Response(SafetyFileSerializer(safety_group).data)
class SafetyResponsebilityAPIView(APIView):