From 37dc2596c34c36725b25f880a2f37c64f99e39cd Mon Sep 17 00:00:00 2001 From: Andrew Tavis McAllister Date: Wed, 25 Sep 2024 01:46:52 +0200 Subject: [PATCH] Update event views to incldue args and kwargs as required by mixins --- .../management/commands/populate_db.py | 6 ++-- backend/entities/views.py | 23 +++++++------ backend/events/views.py | 34 +++++++++---------- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/backend/backend/management/commands/populate_db.py b/backend/backend/management/commands/populate_db.py index 00d373e0..e799d7a9 100644 --- a/backend/backend/management/commands/populate_db.py +++ b/backend/backend/management/commands/populate_db.py @@ -60,7 +60,7 @@ def handle(self, *args: str, **options: Unpack[Options]) -> None: for o in range(num_orgs_per_user): user_org = OrganizationFactory( - name=f"{user_topic.name} Organization (U{u}-O{o})", + name=f"{user_topic.name} Organization (U{u}:O{o})", created_by=user, ) @@ -69,7 +69,7 @@ def handle(self, *args: str, **options: Unpack[Options]) -> None: for g in range(num_groups_per_org): user_org_group = GroupFactory( org_id=user_org, - name=f"{user_topic.name} Group (U{u}-O{o}-G{g})", + name=f"{user_topic.name} Group (U{u}:O{o}:G{g})", created_by=user, ) @@ -79,7 +79,7 @@ def handle(self, *args: str, **options: Unpack[Options]) -> None: for e in range(num_events_per_org): user_org_event = EventFactory( - name=f"{user_topic.name} Event (U{u}-O{o}-E{e})", + name=f"{user_topic.name} Event (U{u}:O{o}:E{e})", type=random.choice(["learn", "action"]), created_by=user, ) diff --git a/backend/entities/views.py b/backend/entities/views.py index 38abc7f0..a4b7f185 100644 --- a/backend/entities/views.py +++ b/backend/entities/views.py @@ -67,15 +67,17 @@ def create(self, request: Request) -> Response: serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save(created_by=request.user) - data = {"message": f"New Group created: {serializer.data}"} + data = {"message": f"New group created: {serializer.data}"} return Response(data, status=status.HTTP_201_CREATED) def retrieve(self, request: Request, *args: str, **kwargs: int) -> Response: - group = self.queryset.get(id=kwargs["pk"]) - serializer = self.get_serializer(group) + if group := self.queryset.get(id=kwargs["pk"]): + serializer = self.get_serializer(group) - return Response(serializer.data, status=status.HTTP_200_OK) + return Response(serializer.data, status=status.HTTP_200_OK) + + return Response({"error": "Group not found"}, status.HTTP_404_NOT_FOUND) def partial_update(self, request: Request, *args: str, **kwargs: int) -> Response: group = self.queryset.filter(id=kwargs["pk"]).first() @@ -126,13 +128,19 @@ class OrganizationViewSet(viewsets.ModelViewSet[Organization]): permission_classes = [IsAuthenticatedOrReadOnly] authentication_classes = [TokenAuthentication] + def list(self, request: Request) -> Response: + serializer = self.get_serializer(self.get_queryset(), many=True) + + return Response(serializer.data, status=status.HTTP_200_OK) + def create(self, request: Request) -> Response: serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) org = serializer.save(created_by=request.user) OrganizationApplication.objects.create(org_id=org) + data = {"message": f"New organization created: {serializer.data}"} - return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(data, status=status.HTTP_201_CREATED) def retrieve(self, request: Request, pk: str | None = None) -> Response: if org := self.queryset.filter(id=pk).first(): @@ -142,11 +150,6 @@ def retrieve(self, request: Request, pk: str | None = None) -> Response: return Response({"error": "Organization not found"}, status.HTTP_404_NOT_FOUND) - def list(self, request: Request) -> Response: - serializer = self.get_serializer(self.get_queryset(), many=True) - - return Response(serializer.data, status=status.HTTP_200_OK) - def update(self, request: Request, pk: str | None = None) -> Response: org = self.queryset.filter(id=pk).first() if org is None: diff --git a/backend/events/views.py b/backend/events/views.py index 2ddf7378..1713a1c2 100644 --- a/backend/events/views.py +++ b/backend/events/views.py @@ -48,29 +48,29 @@ class EventViewSet(viewsets.ModelViewSet[Event]): permission_classes = [IsAuthenticatedOrReadOnly] authentication_classes = [TokenAuthentication] - def create(self, request: Request) -> Response: + def list(self, request: Request, *args: str, **kwargs: int) -> Response: + serializer = self.get_serializer(self.get_queryset(), many=True) + + return Response(serializer.data, status=status.HTTP_200_OK) + + def create(self, request: Request, *args: str, **kwargs: int) -> Response: serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) - event = serializer.save(created_by=request.user) - Event.objects.create(id=event) + serializer.save(created_by=request.user) + data = {"message": f"New event created: {serializer.data}"} - return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(data, status=status.HTTP_201_CREATED) - def retrieve(self, request: Request, pk: str | None = None) -> Response: - if event := self.queryset.filter(id=pk).first(): + def retrieve(self, request: Request, *args: str, **kwargs: int) -> Response: + if event := self.queryset.get(id=kwargs["pk"]): serializer = self.get_serializer(event) return Response(serializer.data, status=status.HTTP_200_OK) return Response({"error": "Event not found"}, status.HTTP_404_NOT_FOUND) - def list(self, request: Request) -> Response: - serializer = self.get_serializer(self.get_queryset(), many=True) - - return Response(serializer.data, status=status.HTTP_200_OK) - - def update(self, request: Request, pk: str | None = None) -> Response: - event = self.queryset.filter(id=pk).first() + def update(self, request: Request, *args: str, **kwargs: int) -> Response: + event = self.queryset.filter(id=kwargs["pk"]).first() if event is None: return Response({"error": "Event not found"}, status.HTTP_404_NOT_FOUND) @@ -86,8 +86,8 @@ def update(self, request: Request, pk: str | None = None) -> Response: return Response(serializer.data, status.HTTP_200_OK) - def partial_update(self, request: Request, pk: str | None = None) -> Response: - event = self.queryset.filter(id=pk).first() + def partial_update(self, request: Request, *args: str, **kwargs: int) -> Response: + event = self.queryset.filter(id=kwargs["pk"]).first() if event is None: return Response({"error": "Event not found"}, status.HTTP_404_NOT_FOUND) @@ -103,8 +103,8 @@ def partial_update(self, request: Request, pk: str | None = None) -> Response: return Response(serializer.data, status.HTTP_200_OK) - def destroy(self, request: Request, pk: str | None = None) -> Response: - event = self.queryset.filter(id=pk).first() + def destroy(self, request: Request, *args: str, **kwargs: int) -> Response: + event = self.queryset.filter(id=kwargs["pk"]).first() if event is None: return Response({"error": "Event not found"}, status.HTTP_404_NOT_FOUND)