Skip to content

Commit 3669a1b

Browse files
init archive back app
1 parent 7fc2977 commit 3669a1b

File tree

11 files changed

+177
-2
lines changed

11 files changed

+177
-2
lines changed

backend-app/archive/__init__.py

Whitespace-only changes.

backend-app/archive/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ArchiveConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'archive'

backend-app/archive/models.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from django.db.models import (
2+
Model,
3+
CharField,
4+
ForeignKey,
5+
ManyToManyField,
6+
DO_NOTHING,
7+
DateTimeField,
8+
)
9+
10+
11+
class Archive(Model):
12+
STATUSES: tuple = (
13+
("s", "SUCCESS"),
14+
("f", "FAILURE"),
15+
("p", "PENGING"),
16+
)
17+
18+
id = CharField(max_length=24, primary_key=True, unique=True)
19+
result_id = CharField(max_length=24, unique=True)
20+
status = CharField(max_length=1, choices=STATUSES, default="p")
21+
create_date = DateTimeField(auto_now_add=True)
22+
23+
project = ForeignKey("project.Project", on_delete=DO_NOTHING)
24+
author = ForeignKey("user.CustomUser", on_delete=DO_NOTHING)
25+
file = ManyToManyField("file.File")
26+
27+
class Meta:
28+
db_table = "archive"

backend-app/archive/permissions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from rest_framework.permissions import BasePermission
2+
from rest_framework.views import Request, APIView
3+
4+
5+
class ArchivesPermission(BasePermission):
6+
def has_permission(self, request: Request, view: APIView) -> bool | None:
7+
project_perm = bool(request.user.project_download.filter(id=view.kwargs["p_pk"]))
8+
return request.user.is_superuser or project_perm
9+
10+
11+
class ArchivePermission(BasePermission):
12+
def has_permission(self, request: Request, view: APIView) -> bool | None:
13+
project_perm = bool(request.user.project_download.filter(id=view.kwargs["p_pk"]))
14+
arch_perm = bool(request.user.archive_set.filter(id=view.kwargs["pk"]))
15+
return request.user.is_superuser or (project_perm and arch_perm)

backend-app/archive/serializers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from rest_framework.serializers import ModelSerializer
2+
from typing import Any
3+
from .models import Archive
4+
5+
6+
class ArchiveSerializer(ModelSerializer):
7+
class Meta:
8+
model = Archive
9+
fields = ("id", "result_id", "status",)

backend-app/archive/services.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

backend-app/archive/tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

backend-app/archive/views.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from rest_framework.views import Response, APIView, Request
2+
from rest_framework.permissions import IsAuthenticated
3+
from .permissions import ArchivePermission, ArchivesPermission
4+
from .services import _get_archives, _make_archive
5+
6+
7+
class ArchivesViewSet(APIView):
8+
http_method_names = ("post", "get")
9+
permission_classes = (IsAuthenticated, ArchivesPermission)
10+
11+
def get(self, request: Request, p_pk: int) -> Response:
12+
response, status = _get_archives(p_pk)
13+
return Response(response, status=status)
14+
15+
def post(self, request: Request, p_pk: int) -> Response:
16+
response, status = _make_archive(p_pk, request.data)
17+
return Response(response, status=status)
18+
19+
20+
class ArchiveViewSet(APIView):
21+
http_method_names = ("get", "patch")
22+
permission_classes = (IsAuthenticated, ArchivePermission)
23+
24+
def get(self, request: Request, pk: int) -> Response:
25+
...
26+
# response, status = _get_project(pk, request)
27+
# return Response(response, status=status)
28+
29+
def patch(self, request: Request, pk: int) -> Response:
30+
...
31+
# response, status = _patch_project(pk, request.data)
32+
# return Response(response, status=status)

backend-app/proj_back/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
'api',
5757
'project',
5858
'attribute',
59-
'file'
59+
'file',
60+
"archive"
6061
]
6162

6263
MIDDLEWARE = [

backend-app/project/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from django.urls import path
22
from .views import ProjectsViewSet, ProjectViewSet, GoalViewSet
3+
from archive.views import ArchivesViewSet, ArchiveViewSet
34

45
urlpatterns: tuple = (
56
path("", ProjectsViewSet.as_view()),
67
path("goals/<int:pk>/", GoalViewSet.as_view()),
8+
path("archive/<int:p_pk>/<str:pk>/", ArchiveViewSet.as_view()),
9+
path("archive/<int:p_pk>/", ArchivesViewSet.as_view()),
710
path("<int:pk>/", ProjectViewSet.as_view())
811
)

0 commit comments

Comments
 (0)