Remove historical tracking for equipment groups to simplify handling

This commit is contained in:
Keannu Bernasol 2023-12-05 20:33:25 +08:00
parent acb92b09b0
commit d915852632
6 changed files with 27 additions and 86 deletions

View file

@ -28,7 +28,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = str(os.getenv('SECRET_KEY')) SECRET_KEY = str(os.getenv('SECRET_KEY'))
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False DEBUG = True
ALLOWED_HOSTS = ['*'] ALLOWED_HOSTS = ['*']
CSRF_TRUSTED_ORIGINS = [ CSRF_TRUSTED_ORIGINS = [

View file

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

View file

@ -12,7 +12,6 @@ class EquipmentGroup(models.Model):
date_added = models.DateTimeField(default=now, editable=False) date_added = models.DateTimeField(default=now, editable=False)
last_updated = models.DateTimeField(auto_now=True, editable=False) last_updated = models.DateTimeField(auto_now=True, editable=False)
equipments = models.ManyToManyField(EquipmentInstance) equipments = models.ManyToManyField(EquipmentInstance)
history = HistoricalRecords()
@property @property
def status(self): def status(self):

View file

@ -1,77 +1,29 @@
from rest_framework import serializers 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.utils import extend_schema_field
from drf_spectacular.types import OpenApiTypes from drf_spectacular.types import OpenApiTypes
# -- Equipment Group Serializers # -- 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): class EquipmentGroupSerializer(serializers.HyperlinkedModelSerializer):
date_added = serializers.DateTimeField( date_added = serializers.DateTimeField(
format="%m-%d-%Y %I:%M%p", read_only=True) format="%m-%d-%Y %I:%M%p", read_only=True)
last_updated = serializers.DateTimeField( last_updated = serializers.DateTimeField(
format="%m-%d-%Y %I:%M%p", read_only=True) 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() name = serializers.CharField()
remarks = serializers.CharField() remarks = serializers.CharField()
class Meta: class Meta:
model = EquipmentGroup model = EquipmentGroup
fields = ('id', 'name', 'remarks', fields = ('__all__')
'last_updated', 'last_updated_by', 'date_added') read_only_fields = ('id', 'last_updated', 'equipments',
read_only_fields = ('id', 'last_updated',
'last_updated_by', 'date_added') 'last_updated_by', 'date_added')
@extend_schema_field(OpenApiTypes.STR) def to_representation(self, instance):
def get_history_user(self, obj): representation = super().to_representation(instance)
return obj.history_user.username if obj.history_user else None representation['equipments'] = [
equipment.equipment.name for equipment in instance.equipments.all()]
@extend_schema_field(OpenApiTypes.STR) return representation
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

@ -10,11 +10,6 @@ router.register(r'equipment_groups', views.EquipmentGroupViewSet)
# Additionally, we include login URLs for the browsable API. # Additionally, we include login URLs for the browsable API.
urlpatterns = [ urlpatterns = [
path('', include(router.urls)), 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 # Last changed equipment group
path('equipment_groups/latest', path('equipment_groups/latest',
views.LastUpdatedEquipmentGroupViewSet.as_view()), views.LastUpdatedEquipmentGroupViewSet.as_view()),

View file

@ -14,27 +14,6 @@ class EquipmentGroupViewSet(viewsets.ModelViewSet):
serializer_class = serializers.EquipmentGroupSerializer serializer_class = serializers.EquipmentGroupSerializer
queryset = EquipmentGroup.objects.all().order_by('id') 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 # Last changed equipment