|
9 | 9 | from django_filters import rest_framework as filters |
10 | 10 | from drf_yasg import openapi |
11 | 11 | from drf_yasg.utils import swagger_auto_schema |
12 | | -from rest_framework import generics, permissions, status |
| 12 | +from rest_framework import generics, permissions, status, viewsets |
13 | 13 | from rest_framework.exceptions import NotFound |
14 | 14 | from rest_framework.permissions import IsAuthenticated |
15 | 15 | from rest_framework.response import Response |
|
30 | 30 | get_recommended_users, |
31 | 31 | update_partner_program, |
32 | 32 | ) |
33 | | -from projects.models import Achievement, Collaborator, Project, ProjectNews |
| 33 | +from projects.models import Achievement, Collaborator, Project, ProjectGoal, ProjectNews |
34 | 34 | from projects.pagination import ProjectNewsPagination, ProjectsPagination |
35 | 35 | from projects.permissions import ( |
36 | 36 | CanBindProjectToProgram, |
37 | 37 | HasInvolvementInProjectOrReadOnly, |
38 | 38 | IsNewsAuthorIsProjectLeaderOrReadOnly, |
39 | 39 | IsProjectLeader, |
| 40 | + IsProjectLeaderOrReadOnly, |
40 | 41 | IsProjectLeaderOrReadOnlyForNonDrafts, |
41 | 42 | TimingAfterEndsProgramPermission, |
42 | 43 | ) |
|
46 | 47 | ProjectCollaboratorSerializer, |
47 | 48 | ProjectDetailSerializer, |
48 | 49 | ProjectDuplicateRequestSerializer, |
| 50 | + ProjectGoalSerializer, |
49 | 51 | ProjectListSerializer, |
50 | 52 | ProjectNewsDetailSerializer, |
51 | 53 | ProjectNewsListSerializer, |
@@ -711,3 +713,24 @@ def post(self, request): |
711 | 713 | }, |
712 | 714 | status=status.HTTP_201_CREATED, |
713 | 715 | ) |
| 716 | + |
| 717 | + |
| 718 | +class GoalViewSet(viewsets.ModelViewSet): |
| 719 | + queryset = ProjectGoal.objects.select_related("project", "responsible") |
| 720 | + serializer_class = ProjectGoalSerializer |
| 721 | + permission_classes = [IsProjectLeaderOrReadOnly] |
| 722 | + |
| 723 | + def get_queryset(self): |
| 724 | + qs = super().get_queryset() |
| 725 | + project_pk = self.kwargs.get("project_pk") |
| 726 | + return qs.filter(project_id=project_pk) if project_pk is not None else qs |
| 727 | + |
| 728 | + def perform_create(self, serializer): |
| 729 | + project_pk = self.kwargs.get("project_pk") |
| 730 | + if project_pk is None: |
| 731 | + serializer.save() |
| 732 | + else: |
| 733 | + serializer.save(project_id=project_pk) |
| 734 | + |
| 735 | + def perform_update(self, serializer): |
| 736 | + serializer.save(project=self.get_object().project) |
0 commit comments