feat: Add basic API
This commit is contained in:
parent
73a6abef18
commit
cc95a2832d
@ -38,6 +38,7 @@ dependencies = [
|
|||||||
"psycopg2-binary",
|
"psycopg2-binary",
|
||||||
"django-crispy-forms",
|
"django-crispy-forms",
|
||||||
"crispy-bootstrap4",
|
"crispy-bootstrap4",
|
||||||
|
"djangorestframework"
|
||||||
]
|
]
|
||||||
dynamic = ["version", "readme"]
|
dynamic = ["version", "readme"]
|
||||||
|
|
||||||
|
0
src/fellchensammlung/api/__init__.py
Normal file
0
src/fellchensammlung/api/__init__.py
Normal file
10
src/fellchensammlung/api/serializers.py
Normal file
10
src/fellchensammlung/api/serializers.py
Normal 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"]
|
8
src/fellchensammlung/api/urls.py
Normal file
8
src/fellchensammlung/api/urls.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
from django.urls import path
|
||||||
|
from .views import (
|
||||||
|
AdoptionNoticeApiView
|
||||||
|
)
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('adoption_notice', AdoptionNoticeApiView.as_view()),
|
||||||
|
]
|
37
src/fellchensammlung/api/views.py
Normal file
37
src/fellchensammlung/api/views.py
Normal 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)
|
@ -66,6 +66,18 @@ urlpatterns = [
|
|||||||
###########
|
###########
|
||||||
## ADMIN ##
|
## 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')),
|
||||||
|
|
||||||
|
|
||||||
]
|
]
|
||||||
|
@ -160,6 +160,7 @@ INSTALLED_APPS = [
|
|||||||
'fontawesomefree',
|
'fontawesomefree',
|
||||||
'crispy_forms',
|
'crispy_forms',
|
||||||
"crispy_bootstrap4",
|
"crispy_bootstrap4",
|
||||||
|
"rest_framework",
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
Loading…
Reference in New Issue
Block a user