New REST services for all entities

This commit is contained in:
Dirk Jahnke 2022-04-14 16:31:46 +02:00
parent 04b3377df7
commit 8cc1e7d8ad
13 changed files with 122 additions and 4 deletions

0
api/__init__.py Normal file
View File

3
api/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
api/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api'

View File

3
api/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

31
api/serializers.py Normal file
View File

@ -0,0 +1,31 @@
from container.models import Container, ContainerType
from asset.models import Asset, GtinProduct
from rest_framework import serializers
class ContainerSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Container
fields = ['url', 'id', 'named_id', 'container_type', 'color', 'description']
class ContainerTypeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ContainerType
fields = ['url', 'id',
'named_id', 'description', 'width', 'height', 'length',
'inner_width', 'inner_height', 'inner_length',
'has_cover', 'contains_container']
class AssetSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Asset
fields = ['url', 'id', 'named_id', 'description', 'quantity', 'description']
class GtinProductSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = GtinProduct
fields = ['url', 'id', 'gtin', 'api_request', 'api_response', 'name', 'brand']

3
api/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

15
api/urls.py Normal file
View File

@ -0,0 +1,15 @@
from django.urls import include, path
from rest_framework import routers
from .views import ContainerViewSet, ContainerTypeViewSet, AssetViewSet, GtinProductViewSet
router = routers.DefaultRouter()
router.register(r'containers', ContainerViewSet)
router.register(r'container_types', ContainerTypeViewSet)
router.register(r'assets', AssetViewSet)
router.register(r'products', GtinProductViewSet)
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

42
api/views.py Normal file
View File

@ -0,0 +1,42 @@
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework import permissions
from api.serializers import ContainerSerializer, ContainerTypeSerializer, AssetSerializer, GtinProductSerializer
from container.models import Container, ContainerType
from asset.models import Asset, GtinProduct
class ContainerViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows containers to be viewed or edited.
"""
queryset = Container.objects.all().order_by('named_id')
serializer_class = ContainerSerializer
permission_classes = [permissions.IsAuthenticated]
class ContainerTypeViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows container-types to be viewed or edited.
"""
queryset = ContainerType.objects.all()
serializer_class = ContainerTypeSerializer
permission_classes = [permissions.IsAuthenticated]
class AssetViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows containers to be viewed or edited.
"""
queryset = Asset.objects.all().order_by('named_id')
serializer_class = AssetSerializer
permission_classes = [permissions.IsAuthenticated]
class GtinProductViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows containers to be viewed or edited.
"""
queryset = GtinProduct.objects.all().order_by('gtin')
serializer_class = GtinProductSerializer
permission_classes = [permissions.IsAuthenticated]

View File

@ -1,8 +1,8 @@
from django.urls import path from django.urls import include, path
from container.views import ContainerListView, ContainerUpdateView, ContainerCreateView, ContainerDetailView, ContainerDeleteView, ContainerPrintLabelView, ContainerImportView from container.views import ContainerListView, ContainerUpdateView, ContainerCreateView, ContainerDetailView, ContainerDeleteView, ContainerPrintLabelView, ContainerImportView
from container.views import ContainerTypeListView, ContainerTypeDetailView, ContainerTypeCreateView, ContainerTypeUpdateView, ContainerTypeDeleteView, ContainerTypeImportView from container.views import ContainerTypeListView, ContainerTypeDetailView, ContainerTypeCreateView, ContainerTypeUpdateView, ContainerTypeDeleteView, ContainerTypeImportView
app_name = 'container' app_name = 'container'
urlpatterns = [ urlpatterns = [
path('', ContainerListView.as_view(), name='list'), path('', ContainerListView.as_view(), name='list'),
@ -18,5 +18,6 @@ urlpatterns = [
path('type/import/', ContainerTypeImportView.as_view(), name='container_type_import'), path('type/import/', ContainerTypeImportView.as_view(), name='container_type_import'),
path('type/edit/<int:pk>/', ContainerTypeUpdateView.as_view(), name='container_type_update'), path('type/edit/<int:pk>/', ContainerTypeUpdateView.as_view(), name='container_type_update'),
path('type/delete/<int:pk>/', ContainerTypeDeleteView.as_view(), name='container_type_delete'), path('type/delete/<int:pk>/', ContainerTypeDeleteView.as_view(), name='container_type_delete'),
] ]

View File

@ -2,9 +2,9 @@ from django.views import generic
from .models import Container, ContainerType from .models import Container, ContainerType
import logging, json, re import logging, json, re
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -304,3 +304,4 @@ class ContainerTypeImportView(LoginRequiredMixin, generic.TemplateView):
finally: finally:
ct.save() ct.save()
return super().get(request) return super().get(request)

View File

@ -36,6 +36,8 @@ INSTALLED_APPS = [
'asset', 'asset',
'container', 'container',
'booker', 'booker',
'api',
'rest_framework',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',
@ -165,4 +167,14 @@ LOGGING = {
'handlers': ['console'], 'handlers': ['console'],
'level': 'DEBUG', 'level': 'DEBUG',
}, },
} }
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}

View File

@ -29,4 +29,5 @@ urlpatterns = [
path('booker/', include('booker.urls')), path('booker/', include('booker.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')), path('accounts/', include('django.contrib.auth.urls')),
path('api/', include('api.urls')),
] ]