diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/admin.py b/api/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/api/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/api/apps.py b/api/apps.py new file mode 100644 index 0000000..66656fd --- /dev/null +++ b/api/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'api' diff --git a/api/migrations/__init__.py b/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/models.py b/api/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/api/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/api/serializers.py b/api/serializers.py new file mode 100644 index 0000000..5d94c6a --- /dev/null +++ b/api/serializers.py @@ -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'] + diff --git a/api/tests.py b/api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/api/urls.py b/api/urls.py new file mode 100644 index 0000000..7a0f0fb --- /dev/null +++ b/api/urls.py @@ -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')) + ] \ No newline at end of file diff --git a/api/views.py b/api/views.py new file mode 100644 index 0000000..bf76744 --- /dev/null +++ b/api/views.py @@ -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] diff --git a/container/urls.py b/container/urls.py index e9a7d63..0ce9a6e 100644 --- a/container/urls.py +++ b/container/urls.py @@ -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 ContainerTypeListView, ContainerTypeDetailView, ContainerTypeCreateView, ContainerTypeUpdateView, ContainerTypeDeleteView, ContainerTypeImportView + app_name = 'container' urlpatterns = [ path('', ContainerListView.as_view(), name='list'), @@ -18,5 +18,6 @@ urlpatterns = [ path('type/import/', ContainerTypeImportView.as_view(), name='container_type_import'), path('type/edit//', ContainerTypeUpdateView.as_view(), name='container_type_update'), path('type/delete//', ContainerTypeDeleteView.as_view(), name='container_type_delete'), + ] diff --git a/container/views.py b/container/views.py index 8ce610a..faa8680 100644 --- a/container/views.py +++ b/container/views.py @@ -2,9 +2,9 @@ from django.views import generic from .models import Container, ContainerType import logging, json, re from django.core.exceptions import ObjectDoesNotExist -from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin + logger = logging.getLogger(__name__) @@ -304,3 +304,4 @@ class ContainerTypeImportView(LoginRequiredMixin, generic.TemplateView): finally: ct.save() return super().get(request) + diff --git a/homelog/settings.py b/homelog/settings.py index 1f16ff8..01adf0b 100644 --- a/homelog/settings.py +++ b/homelog/settings.py @@ -36,6 +36,8 @@ INSTALLED_APPS = [ 'asset', 'container', 'booker', + 'api', + 'rest_framework', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', @@ -165,4 +167,14 @@ LOGGING = { 'handlers': ['console'], 'level': 'DEBUG', }, -} \ No newline at end of file +} + +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 +} diff --git a/homelog/urls.py b/homelog/urls.py index 73054d7..b87c4f7 100644 --- a/homelog/urls.py +++ b/homelog/urls.py @@ -29,4 +29,5 @@ urlpatterns = [ path('booker/', include('booker.urls')), path('admin/', admin.site.urls), path('accounts/', include('django.contrib.auth.urls')), + path('api/', include('api.urls')), ]