From cc95a2832d93b3c2199c40a9722d2cd76b53dc43 Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 3 Oct 2024 08:45:42 +0200 Subject: [PATCH] feat: Add basic API --- pyproject.toml | 1 + src/fellchensammlung/api/__init__.py | 0 src/fellchensammlung/api/serializers.py | 10 +++++++ src/fellchensammlung/api/urls.py | 8 ++++++ src/fellchensammlung/api/views.py | 37 +++++++++++++++++++++++++ src/fellchensammlung/urls.py | 14 +++++++++- src/notfellchen/settings.py | 1 + 7 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/fellchensammlung/api/__init__.py create mode 100644 src/fellchensammlung/api/serializers.py create mode 100644 src/fellchensammlung/api/urls.py create mode 100644 src/fellchensammlung/api/views.py diff --git a/pyproject.toml b/pyproject.toml index e366ab3..9290537 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,6 +38,7 @@ dependencies = [ "psycopg2-binary", "django-crispy-forms", "crispy-bootstrap4", + "djangorestframework" ] dynamic = ["version", "readme"] diff --git a/src/fellchensammlung/api/__init__.py b/src/fellchensammlung/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/fellchensammlung/api/serializers.py b/src/fellchensammlung/api/serializers.py new file mode 100644 index 0000000..df8acdd --- /dev/null +++ b/src/fellchensammlung/api/serializers.py @@ -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"] \ No newline at end of file diff --git a/src/fellchensammlung/api/urls.py b/src/fellchensammlung/api/urls.py new file mode 100644 index 0000000..553dd2f --- /dev/null +++ b/src/fellchensammlung/api/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from .views import ( + AdoptionNoticeApiView +) + +urlpatterns = [ + path('adoption_notice', AdoptionNoticeApiView.as_view()), +] diff --git a/src/fellchensammlung/api/views.py b/src/fellchensammlung/api/views.py new file mode 100644 index 0000000..4d3a01c --- /dev/null +++ b/src/fellchensammlung/api/views.py @@ -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) diff --git a/src/fellchensammlung/urls.py b/src/fellchensammlung/urls.py index 1c580da..adf6913 100644 --- a/src/fellchensammlung/urls.py +++ b/src/fellchensammlung/urls.py @@ -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')), + ] diff --git a/src/notfellchen/settings.py b/src/notfellchen/settings.py index 136e261..6130b07 100644 --- a/src/notfellchen/settings.py +++ b/src/notfellchen/settings.py @@ -160,6 +160,7 @@ INSTALLED_APPS = [ 'fontawesomefree', 'crispy_forms', "crispy_bootstrap4", + "rest_framework", ] MIDDLEWARE = [