From d915852632cf9d0f55be433e6ac047419e9fa12e Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 5 Dec 2023 20:33:25 +0800 Subject: [PATCH] Remove historical tracking for equipment groups to simplify handling --- equipment_tracker/config/settings.py | 2 +- .../0002_delete_historicalequipmentgroup.py | 16 +++++ equipment_tracker/equipment_groups/models.py | 1 - .../equipment_groups/serializers.py | 68 +++---------------- equipment_tracker/equipment_groups/urls.py | 5 -- equipment_tracker/equipment_groups/views.py | 21 ------ 6 files changed, 27 insertions(+), 86 deletions(-) create mode 100644 equipment_tracker/equipment_groups/migrations/0002_delete_historicalequipmentgroup.py 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/migrations/0002_delete_historicalequipmentgroup.py b/equipment_tracker/equipment_groups/migrations/0002_delete_historicalequipmentgroup.py new file mode 100644 index 0000000..9ca2035 --- /dev/null +++ b/equipment_tracker/equipment_groups/migrations/0002_delete_historicalequipmentgroup.py @@ -0,0 +1,16 @@ +# Generated by Django 4.2.7 on 2023-12-05 12:31 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('equipment_groups', '0001_initial'), + ] + + operations = [ + migrations.DeleteModel( + name='HistoricalEquipmentGroup', + ), + ] diff --git a/equipment_tracker/equipment_groups/models.py b/equipment_tracker/equipment_groups/models.py index 87cde52..5c99fb3 100644 --- a/equipment_tracker/equipment_groups/models.py +++ b/equipment_tracker/equipment_groups/models.py @@ -12,7 +12,6 @@ class EquipmentGroup(models.Model): date_added = models.DateTimeField(default=now, editable=False) last_updated = models.DateTimeField(auto_now=True, editable=False) equipments = models.ManyToManyField(EquipmentInstance) - history = HistoricalRecords() @property def status(self): diff --git a/equipment_tracker/equipment_groups/serializers.py b/equipment_tracker/equipment_groups/serializers.py index 7472358..671b43c 100644 --- a/equipment_tracker/equipment_groups/serializers.py +++ b/equipment_tracker/equipment_groups/serializers.py @@ -1,77 +1,29 @@ from rest_framework import serializers -from .models import EquipmentGroup +from .models import EquipmentGroup, EquipmentInstance 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() + equipments = serializers.SlugRelatedField( + many=True, slug_field='id', queryset=EquipmentInstance.objects.all()) 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', + fields = ('__all__') + read_only_fields = ('id', 'last_updated', 'equipments', '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 + def to_representation(self, instance): + representation = super().to_representation(instance) + representation['equipments'] = [ + equipment.equipment.name for equipment in instance.equipments.all()] + return representation diff --git a/equipment_tracker/equipment_groups/urls.py b/equipment_tracker/equipment_groups/urls.py index 3c49562..d99a0c3 100644 --- a/equipment_tracker/equipment_groups/urls.py +++ b/equipment_tracker/equipment_groups/urls.py @@ -10,11 +10,6 @@ router.register(r'equipment_groups', views.EquipmentGroupViewSet) # 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 bcdbfbe..af61f71 100644 --- a/equipment_tracker/equipment_groups/views.py +++ b/equipment_tracker/equipment_groups/views.py @@ -14,27 +14,6 @@ class EquipmentGroupViewSet(viewsets.ModelViewSet): 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