Allow remarks and description fields to be specified as blank or not be specified at all

This commit is contained in:
Keannu Bernasol 2023-12-09 09:43:53 +08:00
parent 5d5ff22ee4
commit 1e8af2173f

View file

@ -21,7 +21,8 @@ class EquipmentSerializer(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(
max_length=512, required=False, allow_blank=True)
class Meta: class Meta:
model = Equipment model = Equipment
@ -29,14 +30,15 @@ class EquipmentSerializer(serializers.HyperlinkedModelSerializer):
'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',
'last_updated_by', 'date_added') 'last_updated_by', 'date_added')
extra_kwargs = {"description": {"required": False, "allow_null": True}}
def create(self, instance, validated_data): def create(self, validated_data):
user = self.context['request'].user user = self.context['request'].user
# Do not allow users that are not technicians to create equipments # Do not allow users that are not technicians to create equipments
if not user.is_technician: if not user.is_technician:
raise exceptions.ValidationError( raise exceptions.ValidationError(
"Non-technician users cannot create equipments") "Non-technician users cannot create equipments")
return super().create(instance, validated_data) return super().create(validated_data)
def update(self, instance, validated_data): def update(self, instance, validated_data):
user = self.context['request'].user user = self.context['request'].user
@ -121,7 +123,8 @@ class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer):
category = serializers.CharField( category = serializers.CharField(
source='equipment.category', read_only=True) source='equipment.category', read_only=True)
status = serializers.CharField() status = serializers.CharField()
remarks = serializers.CharField(required=False) remarks = serializers.CharField(
max_length=512, required=False, allow_blank=True)
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(
@ -130,13 +133,13 @@ class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer):
status = serializers.ChoiceField( status = serializers.ChoiceField(
choices=EquipmentInstance.EQUIPMENT_INSTANCE_STATUS_CHOICES) choices=EquipmentInstance.EQUIPMENT_INSTANCE_STATUS_CHOICES)
def create(self, instance, validated_data): def create(self, validated_data):
user = self.context['request'].user user = self.context['request'].user
# Do not allow users that are not technicians to create equipment instances # Do not allow users that are not technicians to create equipment instances
if not user.is_technician: if not user.is_technician:
raise exceptions.ValidationError( raise exceptions.ValidationError(
"Non-technician users cannot create equipments") "Non-technician users cannot create equipments")
return super().create(instance, validated_data) return super().create(validated_data)
def update(self, instance, validated_data): def update(self, instance, validated_data):
user = self.context['request'].user user = self.context['request'].user
@ -186,6 +189,7 @@ class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer):
'last_updated', 'last_updated_by', 'date_added') 'last_updated', 'last_updated_by', 'date_added')
read_only_fields = ('id', 'last_updated', 'equipment_name', 'category', read_only_fields = ('id', 'last_updated', 'equipment_name', 'category',
'last_updated_by', 'date_added', 'equipment_name') 'last_updated_by', 'date_added', 'equipment_name')
extra_kwargs = {"remarks": {"required": False, "allow_null": True}}
@extend_schema_field(OpenApiTypes.STR) @extend_schema_field(OpenApiTypes.STR)
def get_history_user(self, obj): def get_history_user(self, obj):