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

fix asset class initialization #2611

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@


@receiver(post_save, sender=Asset)
def delete_asset_cache(sender, instance, created, **kwargs):

Check notice on line 83 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'sender' value is not used

Check notice on line 83 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'created' value is not used

Check notice on line 83 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'kwargs' value is not used
cache.delete("asset:" + str(instance.external_id))
cache.delete("asset:qr:" + str(instance.qr_code_id))
cache.delete("asset:qr:" + str(instance.id))
Expand All @@ -89,7 +89,7 @@
class AssetLocationFilter(filters.FilterSet):
bed_is_occupied = filters.BooleanFilter(method="filter_bed_is_occupied")

def filter_bed_is_occupied(self, queryset, name, value):

Check notice on line 92 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Method is not declared static

Method `filter_bed_is_occupied` may be 'static'

Check notice on line 92 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'name' value is not used
asset_locations = (
AssetBed.objects.select_related("asset", "bed")
.filter(asset__asset_class=AssetClasses.HL7MONITOR.name)
Expand Down Expand Up @@ -135,7 +135,7 @@
return context

def get_queryset(self):
user = self.request.user

Check notice on line 138 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Duplicated code fragment

Duplicated code
queryset = self.queryset
if user.is_superuser:
pass
Expand Down Expand Up @@ -186,7 +186,7 @@
is_permanent = filters.BooleanFilter(method="filter_is_permanent")
warranty_amc_end_of_validity = filters.DateFromToRangeFilter()

def filter_in_use_by_consultation(self, queryset, _, value):

Check notice on line 189 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Method is not declared static

Method `filter_in_use_by_consultation` may be 'static'
if value not in EMPTY_VALUES:
queryset = queryset.annotate(
is_in_use=Exists(
Expand All @@ -200,7 +200,7 @@
queryset = queryset.filter(is_in_use=value)
return queryset.distinct()

def filter_is_permanent(self, queryset, _, value):

Check notice on line 203 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Method is not declared static

Method `filter_is_permanent` may be 'static'
if value not in EMPTY_VALUES:
if value:
queryset = queryset.filter(
Expand All @@ -226,7 +226,7 @@
permission_classes = ()
authentication_classes = ()

def retrieve(self, request, *args, **kwargs):

Check notice on line 229 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'request' value is not used

Check notice on line 229 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'args' value is not used
key = "asset:" + kwargs["external_id"]
hit = cache.get(key)
if not hit:
Expand All @@ -246,7 +246,7 @@
permission_classes = ()
authentication_classes = ()

def retrieve(self, request, *args, **kwargs):

Check notice on line 249 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'request' value is not used

Check notice on line 249 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'args' value is not used
qr_code_id = kwargs["qr_code_id"]
key = "asset:qr:" + qr_code_id
hit = cache.get(key)
Expand Down Expand Up @@ -349,7 +349,7 @@
responses={200: UserDefaultAssetLocationSerializer()}, tags=["asset"]
)
@action(detail=False, methods=["GET"])
def get_default_user_location(self, request, *args, **kwargs):

Check notice on line 352 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'args' value is not used

Check notice on line 352 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'kwargs' value is not used
obj = get_object_or_404(
UserDefaultAssetLocation.objects.filter(user=request.user)
)
Expand All @@ -363,7 +363,7 @@
tags=["asset"],
)
@action(detail=False, methods=["POST"])
def set_default_user_location(self, request, *args, **kwargs):

Check notice on line 366 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'kwargs' value is not used

Check notice on line 366 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'args' value is not used
if "location" not in request.data:
raise ValidationError({"location": "is required"})
allowed_locations = get_asset_location_queryset(request.user)
Expand All @@ -385,7 +385,7 @@
tags=["asset"],
)
@action(detail=True, methods=["POST"])
def operate_assets(self, request, *args, **kwargs):

Check notice on line 388 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'kwargs' value is not used
"""
This API is used to operate assets. API accepts the asset_id and action as parameters.
"""
Expand All @@ -399,9 +399,9 @@
or asset.current_location.facility.middleware_address
)
asset_class: BaseAssetIntegration = AssetClasses[asset.asset_class].value(
{

Check warning on line 402 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Incorrect type

Expected type 'Enum', got 'dict\[str, str \| Any\]' instead
**asset.meta,
"id": asset.external_id,
"id": str(asset.external_id),
"middleware_hostname": middleware_hostname,
}
)
Expand Down Expand Up @@ -447,7 +447,7 @@
)
],
)
def list(self, request, *args, **kwargs):

Check notice on line 450 in care/facility/api/viewsets/asset.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unused local symbols

Parameter 'args' value is not used
"""
This API is used by the middleware to retrieve assets and their configurations
for a given facility and middleware hostname.
Expand Down
2 changes: 1 addition & 1 deletion care/facility/tasks/asset_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
asset_class: BaseAssetIntegration = AssetClasses[
asset.asset_class
].value(
{

Check warning on line 66 in care/facility/tasks/asset_monitor.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Incorrect type

Expected type 'Enum', got 'dict\[str, str \| Any\]' instead
**asset.meta,
"id": asset.external_id,
"id": str(asset.external_id),
"middleware_hostname": resolved_middleware,
}
)
Expand All @@ -86,7 +86,7 @@
result = asset_class.api_post(
asset_class.get_url("cameras/status"), data=assets_config
)
except Exception:

Check notice on line 89 in care/facility/tasks/asset_monitor.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unclear exception clauses

Too broad exception clause
result = asset_class.api_get(
asset_class.get_url("cameras/status")
)
Expand Down
14 changes: 14 additions & 0 deletions care/facility/tests/test_asset_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from care.facility.models import Asset, Bed
from care.users.models import User
from care.utils.assetintegration.asset_classes import AssetClasses
from care.utils.assetintegration.base import BaseAssetIntegration
from care.utils.assetintegration.hl7monitor import HL7MonitorAsset
from care.utils.assetintegration.onvif import OnvifAsset
from care.utils.assetintegration.ventilator import VentilatorAsset
Expand All @@ -14,8 +15,8 @@

class AssetViewSetTestCase(TestUtils, APITestCase):
@classmethod
def setUpTestData(cls) -> None:

Check notice on line 18 in care/facility/tests/test_asset_api.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 naming convention violation

Function name should be lowercase
cls.state = cls.create_state()

Check notice on line 19 in care/facility/tests/test_asset_api.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Duplicated code fragment

Duplicated code
cls.district = cls.create_district(cls.state)
cls.local_body = cls.create_local_body(cls.district)
cls.super_user = cls.create_super_user("su", cls.district)
Expand All @@ -33,12 +34,25 @@

def setUp(self) -> None:
super().setUp()
self.asset = self.create_asset(self.asset_location)

Check notice on line 37 in care/facility/tests/test_asset_api.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute asset defined outside __init__

def validate_invalid_meta(self, asset_class, meta):
with self.assertRaises(ValidationError):
asset_class(meta)

def test_asset_class_initialization(self):
asset = self.create_asset(
self.asset_location, asset_class=AssetClasses.ONVIF.name
)
asset_class = AssetClasses[asset.asset_class].value(
{

Check warning on line 48 in care/facility/tests/test_asset_api.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Incorrect type

Expected type 'Enum', got 'dict\[str, str\]' instead
**asset.meta,
"id": str(asset.external_id),
"middleware_hostname": "middleware.local",
}
)
self.assertIsInstance(asset_class, BaseAssetIntegration)

def test_meta_validations_for_onvif_asset(self):
valid_meta = {
"local_ip_address": "192.168.0.1",
Expand Down Expand Up @@ -325,8 +339,8 @@

class AssetConfigValidationTestCase(TestUtils, APITestCase):
@classmethod
def setUpTestData(cls) -> None:

Check notice on line 342 in care/facility/tests/test_asset_api.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 naming convention violation

Function name should be lowercase
cls.state = cls.create_state()

Check notice on line 343 in care/facility/tests/test_asset_api.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Duplicated code fragment

Duplicated code
cls.district = cls.create_district(cls.state)
cls.local_body = cls.create_local_body(cls.district)
cls.super_user = cls.create_super_user("su", cls.district)
Expand All @@ -341,7 +355,7 @@
cls.user = cls.create_user("staff", cls.district, home_facility=cls.facility)

def test_create_asset_with_unique_ip(self):
sample_data = {

Check notice on line 358 in care/facility/tests/test_asset_api.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Duplicated code fragment

Duplicated code
"name": "Test Asset",
"asset_type": 50,
"location": self.asset_location.external_id,
Expand All @@ -358,7 +372,7 @@
asset_class=AssetClasses.HL7MONITOR.name,
meta={"local_ip_address": "192.168.1.14"},
)
sample_data = {

Check notice on line 375 in care/facility/tests/test_asset_api.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Duplicated code fragment

Duplicated code
"name": "Test Asset",
"asset_type": 50,
"location": self.asset_location.external_id,
Expand All @@ -379,7 +393,7 @@
asset_class=AssetClasses.HL7MONITOR.name,
meta={"local_ip_address": "192.168.1.14"},
)
sample_data = {

Check notice on line 396 in care/facility/tests/test_asset_api.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Duplicated code fragment

Duplicated code

Check notice on line 396 in care/facility/tests/test_asset_api.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Duplicated code fragment

Duplicated code
"name": "Test Asset",
"asset_type": 50,
"location": test_location.external_id,
Expand Down
Loading