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

CSV export for Assets #1592

Merged
merged 1 commit into from
Sep 11, 2023
Merged
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
6 changes: 4 additions & 2 deletions care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
AssetService,
AssetServiceEdit,
AssetTransaction,
AssetTypeChoices,
StatusChoices,
UserDefaultAssetLocation,
)
from care.users.api.serializers.user import UserBaseMinimumSerializer
Expand Down Expand Up @@ -113,8 +115,8 @@ def update(self, instance, validated_data):

class AssetSerializer(ModelSerializer):
id = UUIDField(source="external_id", read_only=True)
status = ChoiceField(choices=Asset.StatusChoices, read_only=True)
asset_type = ChoiceField(choices=Asset.AssetTypeChoices)
status = ChoiceField(choices=StatusChoices, read_only=True)
asset_type = ChoiceField(choices=AssetTypeChoices)
location_object = AssetLocationSerializer(source="current_location", read_only=True)
location = UUIDField(write_only=True, required=True)
last_service = AssetServiceSerializer(read_only=True)
Expand Down
18 changes: 16 additions & 2 deletions care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from django.conf import settings
from django.core.cache import cache
from django.db.models import Exists, OuterRef, Q
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django_filters import rest_framework as filters
from django_filters.constants import EMPTY_VALUES
from djqscsv import render_to_csv_response
from drf_spectacular.utils import extend_schema, inline_serializer
from dry_rest_permissions.generics import DRYPermissions
from rest_framework import exceptions
Expand Down Expand Up @@ -43,6 +45,7 @@
ConsultationBedAsset,
UserDefaultAssetLocation,
)
from care.facility.models.asset import AssetTypeChoices, StatusChoices
from care.users.models import User
from care.utils.assetintegration.asset_classes import AssetClasses
from care.utils.assetintegration.base import BaseAssetIntegration
Expand All @@ -51,8 +54,8 @@
from care.utils.queryset.asset_location import get_asset_location_queryset
from care.utils.queryset.facility import get_facility_queryset

inverse_asset_type = inverse_choices(Asset.AssetTypeChoices)
inverse_asset_status = inverse_choices(Asset.StatusChoices)
inverse_asset_type = inverse_choices(AssetTypeChoices)
inverse_asset_status = inverse_choices(StatusChoices)


class AssetLocationViewSet(
Expand Down Expand Up @@ -220,6 +223,17 @@ def get_queryset(self):
)
return queryset

def list(self, request, *args, **kwargs):
if settings.CSV_REQUEST_PARAMETER in request.GET:
mapping = Asset.CSV_MAPPING.copy()
queryset = self.filter_queryset(self.get_queryset()).values(*mapping.keys())
pretty_mapping = Asset.CSV_MAKE_PRETTY.copy()
return render_to_csv_response(
queryset, field_header_map=mapping, field_serializer_map=pretty_mapping
)

return super(AssetViewSet, self).list(request, *args, **kwargs)

def destroy(self, request, *args, **kwargs):
user = self.request.user
if user.user_type >= User.TYPE_VALUE_MAP["DistrictAdmin"]:
Expand Down
57 changes: 47 additions & 10 deletions care/facility/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db import models
from django.db.models import JSONField, Q

from care.facility.models import reverse_choices
from care.facility.models.facility import Facility
from care.facility.models.json_schema.asset import ASSET_META
from care.facility.models.mixins.permissions.asset import AssetsPermissionMixin
Expand Down Expand Up @@ -46,21 +47,28 @@ class RoomType(enum.Enum):
)


class Asset(BaseModel):
class AssetType(enum.Enum):
INTERNAL = 50
EXTERNAL = 100
class AssetType(enum.Enum):
INTERNAL = 50
EXTERNAL = 100


AssetTypeChoices = [(e.value, e.name) for e in AssetType]

AssetClassChoices = [(e.name, e.value._name) for e in AssetClasses]

AssetTypeChoices = [(e.value, e.name) for e in AssetType]

AssetClassChoices = [(e.name, e.value._name) for e in AssetClasses]
class Status(enum.Enum):
ACTIVE = 50
TRANSFER_IN_PROGRESS = 100

class Status(enum.Enum):
ACTIVE = 50
TRANSFER_IN_PROGRESS = 100

StatusChoices = [(e.value, e.name) for e in Status]
StatusChoices = [(e.value, e.name) for e in Status]

REVERSE_ASSET_TYPE = reverse_choices(AssetTypeChoices)
REVERSE_STATUS = reverse_choices(StatusChoices)


class Asset(BaseModel):
name = models.CharField(max_length=1024, blank=False, null=False)
description = models.TextField(default="", null=True, blank=True)
asset_type = models.IntegerField(
Expand Down Expand Up @@ -100,6 +108,35 @@ class Status(enum.Enum):
related_name="last_service",
)

CSV_MAPPING = {
"name": "Name",
"description": "Description",
"asset_type": "Type",
"asset_class": "Class",
"status": "Working Status",
"current_location": "Current Location",
"is_working": "Is Working",
"not_working_reason": "Not Working Reason",
"serial_number": "Serial Number",
"warranty_details": "Warranty Details",
"vendor_name": "Vendor Name",
"support_name": "Support Name",
"support_phone": "Support Phone Number",
"support_email": "Support Email",
"qr_code_id": "QR Code ID",
"manufacturer": "Manufacturer",
"warranty_amc_end_of_validity": "Warrenty End Date",
"last_service__serviced_on": "Last Service Date",
"last_service__note": "Notes",
"meta__local_ip_address": "Config - IP Address",
"meta__camera_access_key": "Config: Camera Access Key",
}

CSV_MAKE_PRETTY = {
"asset_type": (lambda x: REVERSE_ASSET_TYPE[x]),
"status": (lambda x: REVERSE_STATUS[x]),
}

class Meta:
constraints = [
models.UniqueConstraint(
Expand Down
Loading