Added equipment group serializers, views, and urls

This commit is contained in:
Keannu Bernasol 2023-12-05 20:07:31 +08:00
parent ed79408116
commit 1bf99e8bd2
5 changed files with 148 additions and 4 deletions

View file

@ -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'))
]

View file

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

View file

@ -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

View file

@ -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/<int:group_id>/logs/',
views.EquipmentGroupLogViewSet.as_view({'get': 'list'})),
# Last changed equipment group
path('equipment_groups/latest',
views.LastUpdatedEquipmentGroupViewSet.as_view()),
]

View file

@ -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]