Рефакторинг всего раздела

This commit is contained in:
Alex committed 2024-01-23 11:37:22 +03:00
1 parent 1acd4bbc55
commit 154bcb3825
5 files changed
+79 -36

No files matched your search

+19 -1
View File
@@ -1,3 +1,5 @@
from itertools import groupby
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
@@ -10,5 +12,21 @@ class SafetyFileAPIView(APIView):
all_file = SafetyFile.objects.all()
serializer_for_file = SafetyFileSerializer(instance=all_file, many=True)
data = []
return Response(serializer_for_file.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)