From 064a9bf83afc59a331efe0c65763cbe4609af770 Mon Sep 17 00:00:00 2001 From: moanos Date: Sat, 23 Nov 2024 14:50:26 +0100 Subject: [PATCH] feat: Streamline serializer use, check trust level, add log --- src/fellchensammlung/api/serializers.py | 2 +- src/fellchensammlung/api/views.py | 52 +++++++++++++++++-------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/fellchensammlung/api/serializers.py b/src/fellchensammlung/api/serializers.py index df8acdd..c6d0672 100644 --- a/src/fellchensammlung/api/serializers.py +++ b/src/fellchensammlung/api/serializers.py @@ -7,4 +7,4 @@ from rest_framework import serializers class AdoptionNoticeSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = AdoptionNotice - fields = ['created_at', 'last_checked', "searching_since", "name", "description", "further_information", "group_only"] \ No newline at end of file + fields = ['created_at', 'last_checked', "searching_since", "name", "description", "further_information", "group_only"] diff --git a/src/fellchensammlung/api/views.py b/src/fellchensammlung/api/views.py index 4d3a01c..83acdc3 100644 --- a/src/fellchensammlung/api/views.py +++ b/src/fellchensammlung/api/views.py @@ -1,9 +1,12 @@ -from django.contrib.auth.models import User from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from rest_framework import permissions -from ..models import AdoptionNotice +from rest_framework.decorators import api_view, permission_classes +from rest_framework.permissions import IsAuthenticated +from django.db import transaction +from fellchensammlung.models import AdoptionNotice, Animal, Log, TrustLevel +from fellchensammlung.tasks import add_adoption_notice_location from .serializers import AdoptionNoticeSerializer @@ -18,20 +21,35 @@ class AdoptionNoticeApiView(APIView): serializer = AdoptionNoticeSerializer(adoption_notices, many=True, context=serializer_context) return Response(serializer.data, status=status.HTTP_200_OK) + @transaction.atomic def post(self, request, *args, **kwargs): - data = { - 'name': request.data.get('name'), - "searching_since": request.data.get('searching_since'), - "description": request.data.get('description'), - "organization": request.data.get('organization'), - "further_information": request.data.get('further_information'), - "location_string": request.data.get('location_string'), - "group_only": request.data.get('group_only'), - "owner": request.data.get('owner') - } - serializer = AdoptionNoticeSerializer(data=data) - if serializer.is_valid(): - serializer.save() - return Response(serializer.data, status=status.HTTP_201_CREATED) + """ + API view to add an adoption notice.b + """ + serializer = AdoptionNoticeSerializer(data=request.data, context={'request': request}) + if not serializer.is_valid(): + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + adoption_notice = serializer.save(owner=request.user) + + # Add the location + add_adoption_notice_location.delay_on_commit(adoption_notice.pk) + + # Only set active when user has trust level moderator or higher + if request.user.trust_level >= TrustLevel.MODERATOR: + adoption_notice.set_active() + else: + adoption_notice.set_unchecked() + + # Log the action + Log.objects.create( + user=request.user, + action="add_adoption_notice", + text=f"{request.user} added adoption notice {adoption_notice.pk} via API", + ) + + # Return success response with new adoption notice details + return Response( + {"message": "Adoption notice created successfully!", "id": adoption_notice.pk}, + status=status.HTTP_201_CREATED, + )