Make remarks and description optional and nullable and add category field to equipment instance serializers

This commit is contained in:
Keannu Bernasol 2023-12-02 22:01:27 +08:00
parent 4a6bd1ef35
commit b707199bc0
3 changed files with 51 additions and 11 deletions

View file

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

View file

@ -18,7 +18,7 @@ class Equipment(models.Model):
) )
name = models.CharField(max_length=40) 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) 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)
category = models.CharField( category = models.CharField(
@ -40,7 +40,7 @@ class EquipmentInstance(models.Model):
equipment = models.ForeignKey(Equipment, on_delete=models.CASCADE) equipment = models.ForeignKey(Equipment, on_delete=models.CASCADE)
status = models.CharField( status = models.CharField(
max_length=20, choices=STATUS_CHOICES, default='PENDING') 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) 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)
history = HistoricalRecords() history = HistoricalRecords()

View file

@ -64,7 +64,7 @@ class EquipmentLogSerializer(serializers.HyperlinkedModelSerializer):
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() last_updated_by = serializers.SerializerMethodField()
name = serializers.CharField() name = serializers.CharField()
description = serializers.CharField() description = serializers.CharField(required=False)
history = EquipmentHistoricalRecordField() history = EquipmentHistoricalRecordField()
class Meta: class Meta:
@ -86,7 +86,8 @@ class EquipmentInstanceHistoricalRecordField(serializers.ListField):
def to_representation(self, data): def to_representation(self, data):
data = data.annotate(equipment_name=F('equipment__name')) 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): class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer):
@ -94,8 +95,10 @@ class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer):
slug_field='id', queryset=Equipment.objects.all()) slug_field='id', queryset=Equipment.objects.all())
equipment_name = serializers.CharField( equipment_name = serializers.CharField(
source='equipment.name', read_only=True) source='equipment.name', read_only=True)
category = serializers.CharField(
source='equipment.category', read_only=True)
status = serializers.CharField() status = serializers.CharField()
remarks = serializers.CharField() remarks = serializers.CharField(required=False)
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(
@ -111,9 +114,9 @@ class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer):
class Meta: class Meta:
model = EquipmentInstance model = EquipmentInstance
fields = ('id', 'equipment', 'equipment_name', 'status', 'remarks', fields = ('id', 'equipment', 'equipment_name', 'category', 'status', 'remarks',
'last_updated', 'last_updated_by', 'date_added') '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') 'last_updated_by', 'date_added', 'equipment_name')
@extend_schema_field(OpenApiTypes.STR) @extend_schema_field(OpenApiTypes.STR)
@ -133,12 +136,14 @@ class EquipmentInstanceLogsSerializer(serializers.HyperlinkedModelSerializer):
slug_field='id', queryset=Equipment.objects.all()) slug_field='id', queryset=Equipment.objects.all())
equipment_name = serializers.CharField( equipment_name = serializers.CharField(
source='equipment.name', read_only=True) source='equipment.name', read_only=True)
category = serializers.CharField(
source='equipment.category', read_only=True)
class Meta: class Meta:
model = EquipmentInstance.history.model 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') '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') 'history_date', 'history_user', 'equipment_name')
@extend_schema_field(OpenApiTypes.STR) @extend_schema_field(OpenApiTypes.STR)
@ -159,6 +164,8 @@ class EquipmentInstanceLogSerializer(serializers.HyperlinkedModelSerializer):
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() last_updated_by = serializers.SerializerMethodField()
history = EquipmentInstanceHistoricalRecordField() history = EquipmentInstanceHistoricalRecordField()
category = serializers.CharField(
source='equipment.category', read_only=True)
# Forbid user from changing equipment field once the instance is already created # Forbid user from changing equipment field once the instance is already created
def update(self, instance, validated_data): def update(self, instance, validated_data):
@ -168,9 +175,9 @@ class EquipmentInstanceLogSerializer(serializers.HyperlinkedModelSerializer):
class Meta: class Meta:
model = EquipmentInstance 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') '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') 'date_added', 'last_updated_by', 'history', 'equipment_name')
@extend_schema_field(OpenApiTypes.STR) @extend_schema_field(OpenApiTypes.STR)