Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list detail serializer facility asset #1425

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,89 @@ def validate_qr_code_id(self, value):
return value

def validate(self, attrs):
user = self.context["request"].user
if "location" in attrs:
location = get_object_or_404(
AssetLocation.objects.filter(external_id=attrs["location"])
)

facilities = get_facility_queryset(user)
if not facilities.filter(id=location.facility.id).exists():
raise PermissionError()
del attrs["location"]
attrs["current_location"] = location

# validate that warraty date is not in the past
if "warranty_amc_end_of_validity" in attrs:
if (
attrs["warranty_amc_end_of_validity"]
and attrs["warranty_amc_end_of_validity"] < datetime.now().date()
):
raise ValidationError(
"Warranty/AMC end of validity cannot be in the past"
)

# validate that last serviced date is not in the future
if "last_serviced_on" in attrs and attrs["last_serviced_on"]:
if attrs["last_serviced_on"] > datetime.now().date():
raise ValidationError("Last serviced on cannot be in the future")

return super().validate(attrs)

def update(self, instance, validated_data):
user = self.context["request"].user
with transaction.atomic():
if (
"current_location" in validated_data
and instance.current_location != validated_data["current_location"]
):
if (
instance.current_location.facility.id
!= validated_data["current_location"].facility.id
):
raise ValidationError(
{"location": "Interfacility transfer is not allowed here"}
)
AssetTransaction(
from_location=instance.current_location,
to_location=validated_data["current_location"],
asset=instance,
performed_by=user,
).save()
updated_instance = super().update(instance, validated_data)
cache.delete(f"asset:{instance.external_id}")
return updated_instance


class AssetListSerializer(ModelSerializer):
id = UUIDField(source="external_id", read_only=True)
location_object = AssetLocationSerializer(source="current_location", read_only=True)

class Meta:
model = Asset
fields = ["id", "name", "asset_class", "is_working", "location_object"]
read_only_fields = TIMESTAMP_FIELDS


class AssetDetailSerializer(AssetListSerializer):
status = ChoiceField(choices=Asset.StatusChoices, read_only=True)
asset_type = ChoiceField(choices=Asset.AssetTypeChoices)
location = UUIDField(write_only=True, required=True)

class Meta:
model = Asset
exclude = ("deleted", "external_id", "current_location")
read_only_fields = TIMESTAMP_FIELDS

def validate_qr_code_id(self, value):
value = value or None # treat empty string as null
UniqueValidator(
queryset=Asset.objects.filter(qr_code_id__isnull=False),
message="QR code already assigned",
)(value, self.fields.get("qr_code_id"))
return value

def validate(self, attrs):
user = self.context["request"].user
if "location" in attrs:
location = get_object_or_404(
Expand Down
10 changes: 9 additions & 1 deletion care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from rest_framework.viewsets import GenericViewSet

from care.facility.api.serializers.asset import (
AssetDetailSerializer,
AssetListSerializer,
AssetLocationSerializer,
AssetSerializer,
AssetTransactionSerializer,
Expand Down Expand Up @@ -145,13 +147,19 @@ class AssetViewSet(
.select_related("current_location", "current_location__facility")
.order_by("-created_date")
)
serializer_class = AssetSerializer
serializer_class = AssetDetailSerializer
lookup_field = "external_id"
filter_backends = (filters.DjangoFilterBackend, drf_filters.SearchFilter)
search_fields = ["name", "serial_number", "qr_code_id"]
permission_classes = [IsAuthenticated]
filterset_class = AssetFilter

def get_serializer_class(self):
if self.action == "list":
return AssetListSerializer
else:
return self.serializer_class

def get_queryset(self):
user = self.request.user
queryset = self.queryset
Expand Down