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 = [