From 1bf99e8bd24bb409f69e5e7c76d85599b8a78717 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 5 Dec 2023 20:07:31 +0800 Subject: [PATCH] Added equipment group serializers, views, and urls --- equipment_tracker/api/urls.py | 3 +- equipment_tracker/config/settings.py | 2 +- .../equipment_groups/serializers.py | 77 +++++++++++++++++++ equipment_tracker/equipment_groups/urls.py | 21 +++++ equipment_tracker/equipment_groups/views.py | 49 +++++++++++- 5 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 equipment_tracker/equipment_groups/serializers.py create mode 100644 equipment_tracker/equipment_groups/urls.py diff --git a/equipment_tracker/api/urls.py b/equipment_tracker/api/urls.py index 03edd51..c8177c4 100644 --- a/equipment_tracker/api/urls.py +++ b/equipment_tracker/api/urls.py @@ -2,5 +2,6 @@ from django.urls import path, include urlpatterns = [ path('accounts/', include('accounts.urls')), - path('equipments/', include('equipments.urls')) + path('equipments/', include('equipments.urls')), + path('equipment_groups/', include('equipment_groups.urls')) ] diff --git a/equipment_tracker/config/settings.py b/equipment_tracker/config/settings.py index d7c8953..ee53d4f 100644 --- a/equipment_tracker/config/settings.py +++ b/equipment_tracker/config/settings.py @@ -28,7 +28,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = str(os.getenv('SECRET_KEY')) # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = False +DEBUG = True ALLOWED_HOSTS = ['*'] CSRF_TRUSTED_ORIGINS = [ diff --git a/equipment_tracker/equipment_groups/serializers.py b/equipment_tracker/equipment_groups/serializers.py new file mode 100644 index 0000000..7472358 --- /dev/null +++ b/equipment_tracker/equipment_groups/serializers.py @@ -0,0 +1,77 @@ +from rest_framework import serializers +from .models import EquipmentGroup +from drf_spectacular.utils import extend_schema_field +from drf_spectacular.types import OpenApiTypes + +# -- Equipment Group Serializers + + +class EquipmentGroupHistoricalRecordField(serializers.ListField): + child = serializers.DictField() + + def to_representation(self, data): + return super().to_representation(data.values('name', 'remarks', 'history_date', 'history_user').order_by('-history_date')) + + +class EquipmentGroupSerializer(serializers.HyperlinkedModelSerializer): + date_added = serializers.DateTimeField( + format="%m-%d-%Y %I:%M%p", read_only=True) + last_updated = serializers.DateTimeField( + format="%m-%d-%Y %I:%M%p", read_only=True) + last_updated_by = serializers.SerializerMethodField() + name = serializers.CharField() + remarks = serializers.CharField() + + class Meta: + model = EquipmentGroup + fields = ('id', 'name', 'remarks', + 'last_updated', 'last_updated_by', 'date_added') + read_only_fields = ('id', 'last_updated', + 'last_updated_by', 'date_added') + + @extend_schema_field(OpenApiTypes.STR) + def get_history_user(self, obj): + return obj.history_user.username if obj.history_user else None + + @extend_schema_field(OpenApiTypes.STR) + def get_last_updated_by(self, obj): + return obj.history.first().history_user.username if obj.history.first().history_user else None + + +class EquipmentGroupLogsSerializer(serializers.HyperlinkedModelSerializer): + history_date = serializers.DateTimeField( + format="%m-%d-%Y %I:%M%p", read_only=True) + history_user = serializers.SerializerMethodField() + + class Meta: + model = EquipmentGroup.history.model + fields = ('history_id', 'id', 'name', 'remarks', + 'history_date', 'history_user') + read_only_fields = ('history_id', 'id', 'name', 'remarks', + 'history_date', 'history_user') + + @extend_schema_field(OpenApiTypes.STR) + def get_history_user(self, obj): + return obj.history_user.username if obj.history_user else None + + +class EquipmentGroupLogSerializer(serializers.HyperlinkedModelSerializer): + date_added = serializers.DateTimeField( + format="%m-%d-%Y %I:%M%p", read_only=True) + last_updated = serializers.DateTimeField( + format="%m-%d-%Y %I:%M%p", read_only=True) + last_updated_by = serializers.SerializerMethodField() + name = serializers.CharField() + remarks = serializers.CharField(required=False) + history = EquipmentGroupHistoricalRecordField() + + class Meta: + model = EquipmentGroup + fields = ('id', 'name', 'remarks', + 'last_updated', 'date_added', 'last_updated_by', 'history') + read_only_fields = ('id', 'last_updated', + 'date_added', 'last_updated_by', 'history') + + @extend_schema_field(OpenApiTypes.STR) + def get_last_updated_by(self, obj): + return obj.history.first().history_user.username if obj.history.first().history_user else None diff --git a/equipment_tracker/equipment_groups/urls.py b/equipment_tracker/equipment_groups/urls.py new file mode 100644 index 0000000..3c49562 --- /dev/null +++ b/equipment_tracker/equipment_groups/urls.py @@ -0,0 +1,21 @@ +from django.urls import include, path +from rest_framework import routers +from . import views + +router = routers.DefaultRouter() +# For viewing all equipments +router.register(r'equipment_groups', views.EquipmentGroupViewSet) + +# Wire up our API using automatic URL routing. +# Additionally, we include login URLs for the browsable API. +urlpatterns = [ + path('', include(router.urls)), + # Logs for all equipments group + path('equipment_groups/logs', views.EquipmentGroupsLogsViewSet.as_view()), + # Logs for each equipment group + path('equipment_groups//logs/', + views.EquipmentGroupLogViewSet.as_view({'get': 'list'})), + # Last changed equipment group + path('equipment_groups/latest', + views.LastUpdatedEquipmentGroupViewSet.as_view()), +] diff --git a/equipment_tracker/equipment_groups/views.py b/equipment_tracker/equipment_groups/views.py index 91ea44a..bcdbfbe 100644 --- a/equipment_tracker/equipment_groups/views.py +++ b/equipment_tracker/equipment_groups/views.py @@ -1,3 +1,48 @@ -from django.shortcuts import render +from rest_framework.permissions import IsAuthenticated +from rest_framework import viewsets, generics +from .models import EquipmentGroup +from . import serializers +from config.settings import DEBUG +from accounts.permissions import IsTechnician -# Create your views here. +# -- Equipment Viewsets + + +class EquipmentGroupViewSet(viewsets.ModelViewSet): + if (not DEBUG): + permission_classes = [IsAuthenticated, IsTechnician] + serializer_class = serializers.EquipmentGroupSerializer + queryset = EquipmentGroup.objects.all().order_by('id') + +# For viewing all logs for all equipments + + +class EquipmentGroupsLogsViewSet(generics.ListAPIView): + if (not DEBUG): + permission_classes = [IsAuthenticated, IsTechnician] + serializer_class = serializers.EquipmentGroupLogsSerializer + queryset = EquipmentGroup.history.all().order_by('-history_date') + +# For viewing logs per individual equipment + + +class EquipmentGroupLogViewSet(viewsets.ReadOnlyModelViewSet): + if (not DEBUG): + permission_classes = [IsAuthenticated, IsTechnician] + serializer_class = serializers.EquipmentGroupLogSerializer + + def get_queryset(self): + group_id = self.kwargs['group_id'] + return EquipmentGroup.objects.filter(id=group_id) + +# Last changed equipment + + +class LastUpdatedEquipmentGroupViewSet(generics.ListAPIView): + if (not DEBUG): + permission_classes = [IsAuthenticated, IsTechnician] + serializer_class = serializers.EquipmentGroupSerializer + queryset = EquipmentGroup.objects.all().order_by('-date_added') + + def get_queryset(self): + return super().get_queryset()[:1]