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 all 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
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 instance_finder(v):
return isinstance(
v,
(list, dict, set),
list | dict | set,
)


Expand All @@ -41,10 +42,9 @@

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])

Check warning on line 47 in care/audit_log/helpers.py

View check run for this annotation

Codecov / codecov/patch

care/audit_log/helpers.py#L47

Added line #L47 was not covered by tests


def candidate_in_scope(
Expand All @@ -62,7 +62,7 @@
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 @@
):
return True

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

return False
return bool(

Check warning on line 94 in care/audit_log/helpers.py

View check run for this annotation

Codecov / codecov/patch

care/audit_log/helpers.py#L94

Added line #L94 was not covered by tests
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 @@
response: HttpResponse | None
exception: Exception | None


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -50,7 +51,7 @@
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 @@
environ = RequestInformation(*AuditLogMiddleware.thread.__dal__)
if isinstance(environ.request.user, AnonymousUser):
return None
else:
return environ.request.user
return environ.request.user

Check warning on line 73 in care/audit_log/middleware.py

View check run for this annotation

Codecov / codecov/patch

care/audit_log/middleware.py#L73

Added line #L73 was not covered by tests

@staticmethod
def get_current_request():
Expand All @@ -85,14 +85,14 @@
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 @@

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)

Check warning on line 47 in care/audit_log/receivers.py

View check run for this annotation

Codecov / codecov/patch

care/audit_log/receivers.py#L47

Added line #L47 was not covered by tests
return

get_or_create_meta(instance)
Expand All @@ -61,8 +63,9 @@
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 = (

Check warning on line 66 in care/audit_log/receivers.py

View check run for this annotation

Codecov / codecov/patch

care/audit_log/receivers.py#L66

Added line #L66 was not covered by tests
remove_non_member_fields(pre.__dict__),
remove_non_member_fields(instance.__dict__),
)

try:
Expand Down Expand Up @@ -107,7 +110,7 @@
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)

Check warning on line 113 in care/audit_log/receivers.py

View check run for this annotation

Codecov / codecov/patch

care/audit_log/receivers.py#L113

Added line #L113 was not covered by tests
return

try:
Expand All @@ -118,11 +121,17 @@
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)

Check warning on line 124 in care/audit_log/receivers.py

View check run for this annotation

Codecov / codecov/patch

care/audit_log/receivers.py#L124

Added line #L124 was not covered by tests
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 @@

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

Check warning on line 149 in care/audit_log/receivers.py

View check run for this annotation

Codecov / codecov/patch

care/audit_log/receivers.py#L149

Added line #L149 was not covered by tests
return

operation = Operation.INSERT if created else Operation.UPDATE
Expand All @@ -158,7 +167,7 @@

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

Check warning on line 170 in care/audit_log/receivers.py

View check run for this annotation

Codecov / codecov/patch

care/audit_log/receivers.py#L170

Added line #L170 was not covered by tests
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 @@

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)]

Check warning on line 58 in care/facility/admin.py

View check run for this annotation

Codecov / codecov/patch

care/facility/admin.py#L58

Added line #L58 was not covered by tests

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 @@

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)]

Check warning on line 74 in care/facility/admin.py

View check run for this annotation

Codecov / codecov/patch

care/facility/admin.py#L74

Added line #L74 was not covered by tests

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

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
Loading