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 all linting issues in care #2483

Merged
merged 40 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
24fb143
Fix linting in docs
vigneshhari Sep 22, 2024
162293c
Fix linting issues
vigneshhari Sep 23, 2024
901b5b3
lint fixes for config and other stuff
sainak Sep 23, 2024
747a650
Merge remote-tracking branch 'origin/develop' into vigneshhari/fix-al…
sainak Sep 23, 2024
f560242
fix security issue
sainak Sep 23, 2024
42a8f84
revert
sainak Sep 23, 2024
4b836c9
more fixes
sainak Sep 23, 2024
a0a3c06
some renames and cleanup
sainak Sep 23, 2024
9328412
fix lint in ulid
sainak Sep 23, 2024
867e292
lint care/utils
sainak Sep 23, 2024
ea1fa04
lint care/contrib
sainak Sep 23, 2024
15a2705
lint care/audit_logs
sainak Sep 23, 2024
63c515b
ignore migrations
sainak Sep 23, 2024
b240bb6
lint care/facility/utils
sainak Sep 24, 2024
e57ddc2
lint care/facility/tests
sainak Sep 24, 2024
6b7a272
lint care/facility/templatetags
sainak Sep 24, 2024
e8b582d
lint care/facility/tasks
sainak Sep 24, 2024
cea928f
lint care/facility/static_data/
sainak Sep 24, 2024
28976e7
lint care/facility/signals/
sainak Sep 24, 2024
e561f58
lint care/facility/management/
sainak Sep 24, 2024
9214baa
lint care/facility/events/
sainak Sep 24, 2024
a7c7927
lint care/facility/models/
sainak Sep 24, 2024
4cb6716
lint care/facility/api/viewsets/
sainak Sep 24, 2024
7cb4021
lint care/facility/api/serializers/
sainak Sep 24, 2024
9a7f490
lint remaining stuff
sainak Sep 24, 2024
f12a50b
fix security issues
sainak Sep 24, 2024
17f44d8
Fix linting issues
vigneshhari Sep 24, 2024
e12c771
Fix linting issues
vigneshhari Sep 24, 2024
cb9e8c0
fix tests
sainak Sep 24, 2024
1547ea3
optimize image compare
sainak Sep 24, 2024
66e560b
trying to fix linter workflow
sainak Sep 24, 2024
0461f2b
use a different linter
sainak Sep 24, 2024
bf86bd9
use older version
sainak Sep 24, 2024
3f49b5b
revert plugs
sainak Sep 24, 2024
bcd92b2
Merge remote-tracking branch 'origin/develop' into vigneshhari/fix-al…
sainak Sep 24, 2024
524c98c
revert linter
sainak Sep 24, 2024
256120e
remove black flake8 and isort
sainak Sep 24, 2024
2bec57d
try pre commit as lint
sainak Sep 24, 2024
dc87186
breaking change
sainak Sep 24, 2024
2e83cb2
revert
sainak Sep 24, 2024
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/abdm/api/serializers/abha_number.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# ModelSerializer

Check failure on line 1 in care/abdm/api/serializers/abha_number.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (INP001)

care/abdm/api/serializers/abha_number.py:1:1: INP001 File `care/abdm/api/serializers/abha_number.py` is part of an implicit namespace package. Add an `__init__.py`.
from rest_framework import serializers

from care.abdm.models import AbhaNumber
from care.facility.api.serializers.patient import PatientDetailSerializer
from care.facility.models import PatientRegistration
from care.utils.serializer.external_id_field import ExternalIdSerializerField
from care.utils.serializers.fields import ExternalIdSerializerField


class AbhaNumberSerializer(serializers.ModelSerializer):
Expand Down
21 changes: 10 additions & 11 deletions care/audit_log/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ruff: noqa: SLF001
import re
from fnmatch import fnmatch
from functools import lru_cache
Expand All @@ -14,7 +15,7 @@ def remove_non_member_fields(d: dict):
def instance_finder(v):
return isinstance(
v,
(list, dict, set),
list | dict | set,
)


Expand All @@ -41,10 +42,9 @@ class Search(NamedTuple):

def _make_search(item):
splits = item.split(":")
if len(splits) == 2:
if len(splits) == 2: # noqa: PLR2004
return Search(type=splits[0], value=splits[1])
else:
return Search(type="plain", value=splits[0])
return Search(type="plain", value=splits[0])


def candidate_in_scope(
Expand All @@ -62,7 +62,7 @@ def candidate_in_scope(
search_candidate = candidate
if is_application:
splits = candidate.split(".")
if len(splits) == 2:
if len(splits) == 2: # noqa: PLR2004
app_label, model_name = splits
search_candidate = app_label

Expand Down Expand Up @@ -91,12 +91,11 @@ def exclude_model(model_name):
):
return True

if candidate_in_scope(
model_name, settings.AUDIT_LOG["models"]["exclude"]["models"]
):
return True

return False
return bool(
candidate_in_scope(
model_name, settings.AUDIT_LOG["models"]["exclude"]["models"]
)
)


class MetaDataContainer(dict):
Expand Down
18 changes: 9 additions & 9 deletions care/audit_log/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class RequestInformation(NamedTuple):
response: HttpResponse | None
exception: Exception | None


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -50,7 +51,7 @@ def save(request, response=None, exception=None):
if not dal_request_id:
dal_request_id = (
f"{request.method.lower()}::"
f"{md5(request.path.lower().encode('utf-8')).hexdigest()}::"
f"{md5(request.path.lower().encode('utf-8')).hexdigest()}::" # noqa: S324
f"{uuid.uuid4().hex}"
)
request.dal_request_id = dal_request_id
Expand All @@ -69,8 +70,7 @@ def get_current_user():
environ = RequestInformation(*AuditLogMiddleware.thread.__dal__)
if isinstance(environ.request.user, AnonymousUser):
return None
else:
return environ.request.user
return environ.request.user

@staticmethod
def get_current_request():
Expand All @@ -85,14 +85,14 @@ def __call__(self, request: HttpRequest):
response: HttpResponse = self.get_response(request)
self.save(request, response)

if request.user:
current_user_str = f"{request.user.id}|{request.user}"
else:
current_user_str = None
current_user_str = f"{request.user.id}|{request.user}" if request.user else None

logger.info(
f"{request.method} {request.path} {response.status_code} "
f"User:[{current_user_str}]"
"%s %s %s User:[%s]",
request.method,
request.path,
response.status_code,
current_user_str,
)
return response

Expand Down
25 changes: 17 additions & 8 deletions care/audit_log/receivers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ruff: noqa: SLF001
import json
import logging
from typing import NamedTuple
Expand All @@ -22,6 +23,7 @@

logger = logging.getLogger(__name__)


class Event(NamedTuple):
model: str
actor: AbstractUser
Expand All @@ -42,7 +44,7 @@ def pre_save_signal(sender, instance, **kwargs) -> None:

model_name = get_model_name(instance)
if exclude_model(model_name):
logger.debug(f"{model_name} ignored as per settings")
logger.debug("%s ignored as per settings", model_name)
return

get_or_create_meta(instance)
Expand All @@ -61,8 +63,9 @@ def pre_save_signal(sender, instance, **kwargs) -> None:
changes = {}

if operation not in {Operation.INSERT, Operation.DELETE}:
old, new = remove_non_member_fields(pre.__dict__), remove_non_member_fields(
instance.__dict__
old, new = (
remove_non_member_fields(pre.__dict__),
remove_non_member_fields(instance.__dict__),
)

try:
Expand Down Expand Up @@ -107,7 +110,7 @@ def _post_processor(instance, event: Event | None, operation: Operation):
model_name = get_model_name(instance)

if not event and operation != Operation.DELETE:
logger.debug(f"Event not received for {operation}. Ignoring.")
logger.debug("Event not received for %s. Ignoring.", operation)
return

try:
Expand All @@ -118,11 +121,17 @@ def _post_processor(instance, event: Event | None, operation: Operation):
else:
changes = json.dumps(event.changes if event else {}, cls=LogJsonEncoder)
except Exception:
logger.warning(f"Failed to log {event}", exc_info=True)
logger.warning("Failed to log %s", event, exc_info=True)
return

logger.info(
f"AUDIT_LOG::{request_id}|{actor}|{operation.value}|{model_name}|ID:{instance.pk}|{changes}"
"AUDIT_LOG::%s|%s|%s|%s|ID:%s|%s",
request_id,
actor,
operation.value,
model_name,
instance.pk,
changes,
)


Expand All @@ -137,7 +146,7 @@ def post_save_signal(sender, instance, created, update_fields: frozenset, **kwar

model_name = get_model_name(instance)
if exclude_model(model_name):
logger.debug(f"Ignoring {model_name}.")
logger.debug("Ignoring %s.", model_name)
return

operation = Operation.INSERT if created else Operation.UPDATE
Expand All @@ -158,7 +167,7 @@ def post_delete_signal(sender, instance, **kwargs) -> None:

model_name = get_model_name(instance)
if exclude_model(model_name):
logger.debug(f"Ignoring {model_name}.")
logger.debug("Ignoring %s.", model_name)
return

event = instance._meta.dal.event
Expand Down
22 changes: 3 additions & 19 deletions care/facility/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,14 @@ class DistrictFilter(SimpleListFilter):

def lookups(self, request, model_admin):
district = Facility.objects.values_list("district__name", flat=True)
return list(map(lambda x: (x, x), set(district)))
return [(x, x) for x in set(district)]

def queryset(self, request, queryset):
if self.value() is None:
return queryset
return queryset.filter(district__name=self.value())


# class LocalBodyFilter(SimpleListFilter):
# """Local body filter"""

# title = "Local body"
# parameter_name = "local_body"

# def lookups(self, request, model_admin):
# local_body = Facility.objects.values_list("local_body__name", flat=True)
# return list(map(lambda x: (x, x), set(local_body)))

# def queryset(self, request, queryset):
# if self.value() is None:
# return queryset
# return queryset.filter(local_body__name=self.value())


class StateFilter(SimpleListFilter):
"""State filter"""

Expand All @@ -87,7 +71,7 @@ class StateFilter(SimpleListFilter):

def lookups(self, request, model_admin):
state = Facility.objects.values_list("state__name", flat=True)
return list(map(lambda x: (x, x), set(state)))
return [(x, x) for x in set(state)]

def queryset(self, request, queryset):
if self.value() is None:
Expand Down Expand Up @@ -222,7 +206,7 @@ class FacilityFeatureFlagForm(forms.ModelForm):
)

class Meta:
fields = "__all__"
fields = ("flag", "facility")
model = FacilityFlag

form = FacilityFeatureFlagForm
Expand Down
12 changes: 5 additions & 7 deletions care/facility/api/serializers/ambulance.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class AmbulanceDriverSerializer(serializers.ModelSerializer):
class Meta:
model = AmbulanceDriver
exclude = TIMESTAMP_FIELDS + ("ambulance",)
exclude = (*TIMESTAMP_FIELDS, "ambulance")


class AmbulanceSerializer(serializers.ModelSerializer):
Expand All @@ -36,17 +36,16 @@ class Meta:
def validate(self, obj):
validated = super().validate(obj)
if not validated.get("price_per_km") and not validated.get("has_free_service"):
raise ValidationError(
"The ambulance must provide a price or be marked as free"
)
msg = "The ambulance must provide a price or be marked as free"
raise ValidationError(msg)
return validated

def create(self, validated_data):
with transaction.atomic():
drivers = validated_data.pop("drivers", [])
validated_data.pop("created_by", None)

ambulance = super(AmbulanceSerializer, self).create(validated_data)
ambulance = super().create(validated_data)

for d in drivers:
d["ambulance"] = ambulance
Expand All @@ -55,8 +54,7 @@ def create(self, validated_data):

def update(self, instance, validated_data):
validated_data.pop("drivers", [])
ambulance = super(AmbulanceSerializer, self).update(instance, validated_data)
return ambulance
return super().update(instance, validated_data)


class DeleteDriverSerializer(serializers.Serializer):
Expand Down
39 changes: 15 additions & 24 deletions care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from datetime import datetime

from django.core.cache import cache
from django.db import models, transaction
from django.db.models import F, Value
Expand Down Expand Up @@ -38,9 +36,9 @@
from care.utils.assetintegration.hl7monitor import HL7MonitorAsset
from care.utils.assetintegration.onvif import OnvifAsset
from care.utils.assetintegration.ventilator import VentilatorAsset
from care.utils.models.validators import MiddlewareDomainAddressValidator
from care.utils.queryset.facility import get_facility_queryset
from config.serializers import ChoiceField
from config.validators import MiddlewareDomainAddressValidator
from care.utils.serializers.fields import ChoiceField


class AssetLocationSerializer(ModelSerializer):
Expand Down Expand Up @@ -125,9 +123,7 @@ def update(self, instance, validated_data):
)
edit.save()

updated_instance = super().update(instance, validated_data)

return updated_instance
return super().update(instance, validated_data)


@extend_schema_field(
Expand Down Expand Up @@ -159,10 +155,7 @@ class AssetSerializer(ModelSerializer):
class Meta:
model = Asset
exclude = ("deleted", "external_id", "current_location")
read_only_fields = TIMESTAMP_FIELDS + (
"resolved_middleware",
"latest_status",
)
read_only_fields = (*TIMESTAMP_FIELDS, "resolved_middleware", "latest_status")

def validate_qr_code_id(self, value):
value = value or None # treat empty string as null
Expand Down Expand Up @@ -195,15 +188,14 @@ def validate(self, attrs):
):
del attrs["warranty_amc_end_of_validity"]

elif warranty_amc_end_of_validity < datetime.now().date():
raise ValidationError(
"Warranty/AMC end of validity cannot be in the past"
)
elif warranty_amc_end_of_validity < now().date():
msg = "Warranty/AMC end of validity cannot be in the past"
raise ValidationError(msg)

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

# only allow setting asset class on creation (or updation if asset class is not set)
if (
Expand Down Expand Up @@ -250,9 +242,8 @@ def validate(self, attrs):
.first()
)
if asset_using_ip:
raise ValidationError(
f"IP Address {ip_address} is already in use by {asset_using_ip.name} asset"
)
msg = f"IP Address {ip_address} is already in use by {asset_using_ip.name} asset"
raise ValidationError(msg)

return super().validate(attrs)

Expand Down Expand Up @@ -361,7 +352,7 @@ def to_representation(self, instance: Asset):
data["ip_address"] = instance.meta.get("local_ip_address")
if camera_access_key := instance.meta.get("camera_access_key"):
values = camera_access_key.split(":")
if len(values) == 3:
if len(values) == 3: # noqa: PLR2004
data["username"], data["password"], data["access_key"] = values
return data

Expand Down Expand Up @@ -416,7 +407,7 @@ class Meta:


class AssetActionSerializer(Serializer):
def actionChoices():
def action_choices():
actions = [
OnvifAsset.OnvifActions,
HL7MonitorAsset.HL7MonitorActions,
Expand All @@ -428,7 +419,7 @@ def actionChoices():
return choices

type = ChoiceField(
choices=actionChoices(),
choices=action_choices(),
required=True,
)
data = JSONField(required=False)
Expand Down
Loading
Loading