feat: Streamline serializer use, check trust level, add log
This commit is contained in:
parent
93070a3bcd
commit
064a9bf83a
@ -7,4 +7,4 @@ from rest_framework import serializers
|
|||||||
class AdoptionNoticeSerializer(serializers.HyperlinkedModelSerializer):
|
class AdoptionNoticeSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = AdoptionNotice
|
model = AdoptionNotice
|
||||||
fields = ['created_at', 'last_checked', "searching_since", "name", "description", "further_information", "group_only"]
|
fields = ['created_at', 'last_checked', "searching_since", "name", "description", "further_information", "group_only"]
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
from django.contrib.auth.models import User
|
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework import permissions
|
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
|
from .serializers import AdoptionNoticeSerializer
|
||||||
|
|
||||||
|
|
||||||
@ -18,20 +21,35 @@ class AdoptionNoticeApiView(APIView):
|
|||||||
serializer = AdoptionNoticeSerializer(adoption_notices, many=True, context=serializer_context)
|
serializer = AdoptionNoticeSerializer(adoption_notices, many=True, context=serializer_context)
|
||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
data = {
|
"""
|
||||||
'name': request.data.get('name'),
|
API view to add an adoption notice.b
|
||||||
"searching_since": request.data.get('searching_since'),
|
"""
|
||||||
"description": request.data.get('description'),
|
serializer = AdoptionNoticeSerializer(data=request.data, context={'request': request})
|
||||||
"organization": request.data.get('organization'),
|
if not serializer.is_valid():
|
||||||
"further_information": request.data.get('further_information'),
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
"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)
|
|
||||||
|
|
||||||
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,
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user