Skip to content

Commit

Permalink
Manage update
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Oct 10, 2024
1 parent 24336bd commit 3f9d008
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
23 changes: 20 additions & 3 deletions api/tests/test_resource_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
from rest_framework import status
from rest_framework.test import APITestCase

from data.factories import CanteenFactory, UserFactory, WasteActionFactory
from data.factories import (
CanteenFactory,
ResourceActionFactory,
UserFactory,
WasteActionFactory,
)
from data.models import ResourceAction


class TestReviews(APITestCase):
class TestResourceActionsApi(APITestCase):
@classmethod
def setUpTestData(cls):
cls.waste_action = WasteActionFactory()
cls.user = UserFactory()
cls.user_canteen = UserFactory()
cls.canteen = CanteenFactory()
cls.canteen.managers.add(cls.user_canteen)
cls.url = reverse("resource_action_create", kwargs={"resource_pk": cls.waste_action.id})
cls.url = reverse("resource_action_create_or_update", kwargs={"resource_pk": cls.waste_action.id})

def test_create_resource_action(self):
# user is not authenticated
Expand All @@ -39,3 +44,15 @@ def test_create_resource_action(self):
self.assertEqual(ResourceAction.objects.first().resource, self.waste_action)
self.assertEqual(ResourceAction.objects.first().canteen, self.canteen)
self.assertEqual(ResourceAction.objects.first().is_done, True)

def test_update_resource_action(self):
# create an existing ResourceAction
ResourceActionFactory(resource=self.waste_action, canteen=self.canteen, is_done=True)
# user is authenticated and belongs to the canteen
self.client.force_login(user=self.user_canteen)
response = self.client.post(self.url, data={"canteen_id": self.canteen.id, "is_done": False})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(ResourceAction.objects.count(), 1)
self.assertEqual(ResourceAction.objects.first().resource, self.waste_action)
self.assertEqual(ResourceAction.objects.first().canteen, self.canteen)
self.assertEqual(ResourceAction.objects.first().is_done, False)
4 changes: 3 additions & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@
path("territoryCanteens/", TerritoryCanteensListView.as_view(), name="territory_canteens"),
path("wasteActions/", WasteActionsView.as_view(), name="waste_actions_list"),
path("wasteActions/<int:pk>", WasteActionView.as_view(), name="waste_action_detail"),
path("wasteActions/<int:resource_pk>/actions", ResourceActionView.as_view(), name="resource_action_create"),
path(
"wasteActions/<int:resource_pk>/actions", ResourceActionView.as_view(), name="resource_action_create_or_update"
),
}

urlpatterns = format_suffix_patterns(urlpatterns)
27 changes: 20 additions & 7 deletions api/views/resourceaction.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.core.exceptions import ValidationError
from rest_framework import permissions
from rest_framework import permissions, status
from rest_framework.exceptions import PermissionDenied
from rest_framework.generics import CreateAPIView, get_object_or_404
from rest_framework.response import Response

from api.permissions import IsCanteenManager
from api.serializers import ResourceActionSerializer
Expand All @@ -14,13 +15,25 @@ class ResourceActionView(CreateAPIView):
queryset = ResourceAction.objects.all()
serializer_class = ResourceActionSerializer

def perform_create(self, serializer):
resource = get_object_or_404(WasteAction, pk=self.request.parser_context.get("kwargs").get("resource_pk"))
canteen_id = self.request.data.get("canteen_id")
def create(self, request, *args, **kwargs):
# get resource
self.resource = get_object_or_404(WasteAction, pk=kwargs.get("resource_pk"))
# get canteen and check permissions
canteen_id = request.data.get("canteen_id")
try:
canteen = Canteen.objects.get(pk=canteen_id)
self.canteen = Canteen.objects.get(pk=canteen_id)
except Canteen.DoesNotExist:
raise ValidationError({"canteen_id": "La cantine spécifiée n'existe pas"})
if not IsCanteenManager().has_object_permission(self.request, self, canteen):
if not IsCanteenManager().has_object_permission(request, self, self.canteen):
raise PermissionDenied()
serializer.save(resource=resource, canteen=canteen)
# update or create resource action
try:
resource_action = ResourceAction.objects.get(resource=self.resource, canteen=self.canteen)
serializer = self.get_serializer(resource_action)
serializer.update(resource_action, request.data)
return Response(serializer.data, status=status.HTTP_200_OK)
except ResourceAction.DoesNotExist:
return super().create(request, *args, **kwargs)

def perform_create(self, serializer):
serializer.save(resource=self.resource)

0 comments on commit 3f9d008

Please sign in to comment.