Skip to content

Commit

Permalink
Fix all linting issues in care (#2483)
Browse files Browse the repository at this point in the history
Fix all linting issues in care (#2483)

---------

Co-authored-by: Aakash Singh <mail@singhaakash.dev>
  • Loading branch information
vigneshhari and sainak authored Sep 24, 2024
1 parent 7478b9a commit 663bf30
Show file tree
Hide file tree
Showing 184 changed files with 1,573 additions and 2,019 deletions.
23 changes: 5 additions & 18 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,14 @@ on:
- staging
merge_group:

permissions: { }

jobs:
lint:
name: Lint Code Base
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Ruff check
uses: chartboost/ruff-action@491342200cdd1cf4d5132a30ddc546b3b5bc531b
- uses: actions/checkout@v4
with:
version: 0.6.7
args: "check"
changed-files: "true"

- name: Ruff format
uses: chartboost/ruff-action@491342200cdd1cf4d5132a30ddc546b3b5bc531b
fetch-depth: 0
- uses: actions/setup-python@v3
- uses: pre-commit/action@v3.0.1
with:
version: 0.6.7
args: "format"
changed-files: "true"
extra_args: --color=always --from-ref ${{ github.event.pull_request.base.sha }} --to-ref ${{ github.event.pull_request.head.sha }}
3 changes: 0 additions & 3 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ sentry-sdk = "==2.14.0"
whitenoise = "==6.7.0"

[dev-packages]
black = "==24.8.0"
boto3-stubs = { extras = ["s3", "boto3"], version = "==1.35.25" }
coverage = "==7.6.1"
debugpy = "==1.8.5"
Expand All @@ -53,10 +52,8 @@ django-extensions = "==3.2.3"
django-silk = "==5.2.0"
djangorestframework-stubs = "==3.15.1"
factory-boy = "==3.3.1"
flake8 = "==7.1.1"
freezegun = "==1.5.1"
ipython = "==8.27.0"
isort = "==5.13.2"
mypy = "==1.11.2"
pre-commit = "==3.8.0"
requests-mock = "==1.12.1"
Expand Down
311 changes: 111 additions & 200 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion care/abdm/api/serializers/abha_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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
Loading

0 comments on commit 663bf30

Please sign in to comment.