feat: Add basic API

This commit is contained in:
moanos [he/him] 2024-10-03 08:45:42 +02:00
parent 73a6abef18
commit cc95a2832d
7 changed files with 70 additions and 1 deletions

View File

@ -38,6 +38,7 @@ dependencies = [
"psycopg2-binary",
"django-crispy-forms",
"crispy-bootstrap4",
"djangorestframework"
]
dynamic = ["version", "readme"]

View File

View File

@ -0,0 +1,10 @@
from ..models import AdoptionNotice
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"]

View File

@ -0,0 +1,8 @@
from django.urls import path
from .views import (
AdoptionNoticeApiView
)
urlpatterns = [
path('adoption_notice', AdoptionNoticeApiView.as_view()),
]

View File

@ -0,0 +1,37 @@
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 .serializers import AdoptionNoticeSerializer
class AdoptionNoticeApiView(APIView):
permission_classes = [permissions.IsAuthenticated]
def get(self, request, *args, **kwargs):
serializer_context = {
'request': request,
}
adoption_notices = AdoptionNotice.objects.all()
serializer = AdoptionNoticeSerializer(adoption_notices, many=True, context=serializer_context)
return Response(serializer.data, status=status.HTTP_200_OK)
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)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

View File

@ -66,6 +66,18 @@ urlpatterns = [
###########
## ADMIN ##
###########
path('instance-health-check', views.instance_health_check, name="instance-health-check")
path('instance-health-check', views.instance_health_check, name="instance-health-check"),
#############
## Metrics ##
#############
# ex: /metrics
path('metrics/', views.metrics, name="metrics"),
#########
## API ##
#########
path('api/', include('fellchensammlung.api.urls')),
]

View File

@ -160,6 +160,7 @@ INSTALLED_APPS = [
'fontawesomefree',
'crispy_forms',
"crispy_bootstrap4",
"rest_framework",
]
MIDDLEWARE = [