|
| 1 | +from rest_framework.status import ( |
| 2 | + HTTP_400_BAD_REQUEST, |
| 3 | + HTTP_201_CREATED, |
| 4 | + HTTP_200_OK, |
| 5 | + HTTP_202_ACCEPTED |
| 6 | +) |
| 7 | +from django.db.models.aggregates import Count |
| 8 | +from typing import Any |
| 9 | +from project.models import Project |
| 10 | +from api.mixins import with_model_assertion |
| 11 | +from .serializers import ArchiveSerializer |
| 12 | + |
| 13 | + |
| 14 | +@with_model_assertion(Project, "id", filter={"visible": True},) |
| 15 | +def _get_archives(project: Project) -> tuple[dict[str, Any], int]: |
| 16 | + query = project.archive_set \ |
| 17 | + .annotate(file_count=Count("file")) \ |
| 18 | + .order_by("create_date") |
| 19 | + return ArchiveSerializer(query, many=True).data, HTTP_200_OK |
| 20 | + |
| 21 | + |
| 22 | +@with_model_assertion(Project, "id", filter={"visible": True}) |
| 23 | +def _make_archive( |
| 24 | + project: Project, |
| 25 | + request_data: dict[str, Any] |
| 26 | +) -> tuple[dict[str, Any], int]: |
| 27 | + ... |
| 28 | +# response: dict[str, Any] |
| 29 | +# status: int |
| 30 | + |
| 31 | +# try: |
| 32 | +# attribute = project.attribute_set.get(id=request_data["attribute_id"]) |
| 33 | + |
| 34 | +# assert not attribute.projectgoal_set.count(), "Goal for Attribute already exists" |
| 35 | + |
| 36 | +# goal_attribute_names = [ |
| 37 | +# f"{name} ({level_name})" |
| 38 | +# for name, level_name in |
| 39 | +# attribute |
| 40 | +# .ancestors(include_self=True) |
| 41 | +# .values_list("name", "level__name") |
| 42 | +# ] |
| 43 | + |
| 44 | +# project.projectgoal_set.create( |
| 45 | +# attribute=attribute, |
| 46 | +# amount=int(request_data["amount"]), |
| 47 | +# name=" > ".join(goal_attribute_names), |
| 48 | +# image_mod=int(request_data.get("image_mod", 1)), |
| 49 | +# video_mod=int(request_data.get("video_mod", 1)), |
| 50 | +# ) |
| 51 | + |
| 52 | +# response, status = {"ok": True}, HTTP_201_CREATED |
| 53 | + |
| 54 | +# except AssertionError as e: |
| 55 | +# if int(request_data.get("update", 0)) == 1: |
| 56 | +# goal = attribute.projectgoal_set.first() |
| 57 | +# goal.amount = int(request_data["amount"]) |
| 58 | +# goal.image_mod = int(request_data.get("image_mod", 1)) |
| 59 | +# goal.video_mod = int(request_data.get("video_mod", 1)) |
| 60 | +# goal.save() |
| 61 | + |
| 62 | +# response, status = {"ok": True}, HTTP_202_ACCEPTED |
| 63 | + |
| 64 | +# else: response, status = {"errors": str(e)}, HTTP_400_BAD_REQUEST |
| 65 | + |
| 66 | +# except Exception as e: response, status = {"errors": str(e)}, HTTP_400_BAD_REQUEST |
| 67 | + |
| 68 | +# return response, status |
| 69 | + |
| 70 | +# @with_model_assertion(ProjectGoal, "id") |
| 71 | +# def _delete_goal(self, goal: ProjectGoal): |
| 72 | +# try: |
| 73 | +# goal.delete() |
| 74 | +# return {"ok": True}, HTTP_202_ACCEPTED |
| 75 | + |
| 76 | +# except Exception as e: return {"errors": str(e)}, HTTP_400_BAD_REQUEST |
0 commit comments