From b707199bc0f2bbc75d84c53ad0854c994d4fc129 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Sat, 2 Dec 2023 22:01:27 +0800 Subject: [PATCH] Make remarks and description optional and nullable and add category field to equipment instance serializers --- ...02_alter_equipment_description_and_more.py | 33 +++++++++++++++++++ equipment_tracker/equipments/models.py | 4 +-- equipment_tracker/equipments/serializers.py | 25 +++++++++----- 3 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 equipment_tracker/equipments/migrations/0002_alter_equipment_description_and_more.py diff --git a/equipment_tracker/equipments/migrations/0002_alter_equipment_description_and_more.py b/equipment_tracker/equipments/migrations/0002_alter_equipment_description_and_more.py new file mode 100644 index 0000000..dfb2bc7 --- /dev/null +++ b/equipment_tracker/equipments/migrations/0002_alter_equipment_description_and_more.py @@ -0,0 +1,33 @@ +# Generated by Django 4.2.7 on 2023-12-02 13:15 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('equipments', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='equipment', + name='description', + field=models.TextField(max_length=512, null=True), + ), + migrations.AlterField( + model_name='equipmentinstance', + name='remarks', + field=models.TextField(max_length=512, null=True), + ), + migrations.AlterField( + model_name='historicalequipment', + name='description', + field=models.TextField(max_length=512, null=True), + ), + migrations.AlterField( + model_name='historicalequipmentinstance', + name='remarks', + field=models.TextField(max_length=512, null=True), + ), + ] diff --git a/equipment_tracker/equipments/models.py b/equipment_tracker/equipments/models.py index a198c2a..b7476cf 100644 --- a/equipment_tracker/equipments/models.py +++ b/equipment_tracker/equipments/models.py @@ -18,7 +18,7 @@ class Equipment(models.Model): ) name = models.CharField(max_length=40) - description = models.TextField(max_length=512) + description = models.TextField(max_length=512, null=True) date_added = models.DateTimeField(default=now, editable=False) last_updated = models.DateTimeField(auto_now=True, editable=False) category = models.CharField( @@ -40,7 +40,7 @@ class EquipmentInstance(models.Model): equipment = models.ForeignKey(Equipment, on_delete=models.CASCADE) status = models.CharField( max_length=20, choices=STATUS_CHOICES, default='PENDING') - remarks = models.TextField(max_length=512) + remarks = models.TextField(max_length=512, null=True) date_added = models.DateTimeField(default=now, editable=False) last_updated = models.DateTimeField(auto_now=True, editable=False) history = HistoricalRecords() diff --git a/equipment_tracker/equipments/serializers.py b/equipment_tracker/equipments/serializers.py index 10fd223..6e52763 100644 --- a/equipment_tracker/equipments/serializers.py +++ b/equipment_tracker/equipments/serializers.py @@ -64,7 +64,7 @@ class EquipmentLogSerializer(serializers.HyperlinkedModelSerializer): format="%m-%d-%Y %I:%M%p", read_only=True) last_updated_by = serializers.SerializerMethodField() name = serializers.CharField() - description = serializers.CharField() + description = serializers.CharField(required=False) history = EquipmentHistoricalRecordField() class Meta: @@ -86,7 +86,8 @@ class EquipmentInstanceHistoricalRecordField(serializers.ListField): def to_representation(self, data): data = data.annotate(equipment_name=F('equipment__name')) - return super().to_representation(data.values('equipment', 'equipment_name', 'status', 'remarks', 'history_date', 'history_user').order_by('-history_date')) + data = data.annotate(category=F('equipment__category')) + return super().to_representation(data.values('equipment', 'equipment_name', 'category', 'status', 'remarks', 'history_date', 'history_user').order_by('-history_date')) class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer): @@ -94,8 +95,10 @@ class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer): slug_field='id', queryset=Equipment.objects.all()) equipment_name = serializers.CharField( source='equipment.name', read_only=True) + category = serializers.CharField( + source='equipment.category', read_only=True) status = serializers.CharField() - remarks = serializers.CharField() + remarks = serializers.CharField(required=False) date_added = serializers.DateTimeField( format="%m-%d-%Y %I:%M%p", read_only=True) last_updated = serializers.DateTimeField( @@ -111,9 +114,9 @@ class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = EquipmentInstance - fields = ('id', 'equipment', 'equipment_name', 'status', 'remarks', + fields = ('id', 'equipment', 'equipment_name', 'category', 'status', 'remarks', 'last_updated', 'last_updated_by', 'date_added') - read_only_fields = ('id', 'last_updated', + read_only_fields = ('id', 'last_updated', 'equipment_name', 'category', 'last_updated_by', 'date_added', 'equipment_name') @extend_schema_field(OpenApiTypes.STR) @@ -133,12 +136,14 @@ class EquipmentInstanceLogsSerializer(serializers.HyperlinkedModelSerializer): slug_field='id', queryset=Equipment.objects.all()) equipment_name = serializers.CharField( source='equipment.name', read_only=True) + category = serializers.CharField( + source='equipment.category', read_only=True) class Meta: model = EquipmentInstance.history.model - fields = ('history_id', 'id', 'equipment', 'equipment_name', 'status', 'remarks', + fields = ('history_id', 'id', 'equipment', 'equipment_name', 'category', 'status', 'remarks', 'history_date', 'history_user') - read_only_fields = ('history_id', 'id', 'equipment', 'status', 'remarks', + read_only_fields = ('history_id', 'id', 'equipment', 'equipment_name', 'category', 'status', 'remarks', 'history_date', 'history_user', 'equipment_name') @extend_schema_field(OpenApiTypes.STR) @@ -159,6 +164,8 @@ class EquipmentInstanceLogSerializer(serializers.HyperlinkedModelSerializer): format="%m-%d-%Y %I:%M%p", read_only=True) last_updated_by = serializers.SerializerMethodField() history = EquipmentInstanceHistoricalRecordField() + category = serializers.CharField( + source='equipment.category', read_only=True) # Forbid user from changing equipment field once the instance is already created def update(self, instance, validated_data): @@ -168,9 +175,9 @@ class EquipmentInstanceLogSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = EquipmentInstance - fields = ('id', 'equipment', 'equipment_name', 'status', 'remarks', + fields = ('id', 'equipment', 'equipment_name', 'category', 'status', 'remarks', 'last_updated', 'date_added', 'last_updated_by', 'history') - read_only_fields = ('id', 'last_updated', + read_only_fields = ('id', 'last_updated', 'equipment_name', 'category', 'date_added', 'last_updated_by', 'history', 'equipment_name') @extend_schema_field(OpenApiTypes.STR)