Skip to content

Commit

Permalink
Add more type hints and docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
juho-kettunen-nc committed Oct 22, 2024
1 parent 0964121 commit 5232752
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
10 changes: 9 additions & 1 deletion leasing/report/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ class ReportFormBase(forms.Form):
"""Dynamic form that initializes its fields from `input_fields` parameter"""

def __init__(self, *args, **kwargs):
input_fields = kwargs.pop("input_fields")
"""
args is expected to contain the query parameters, which in turn contains
the report settings when the report was requested.
kwargs is expected to contain a key "input_fields", whose value is a
dictionary containing the names of the query parameters and the form
field objects they reference.
"""
input_fields: dict[str, forms.Field] = kwargs.pop("input_fields")
super().__init__(*args, **kwargs)

for field_name, field in input_fields.items():
Expand Down
4 changes: 2 additions & 2 deletions leasing/report/lease/lease_statistic_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import lru_cache

from django import forms
from django.db.models import Q
from django.db.models import Q, QuerySet
from django.utils import formats
from django.utils.translation import gettext_lazy as _
from enumfields.drf import EnumField
Expand Down Expand Up @@ -372,7 +372,7 @@ class LeaseStatisticReport(AsyncReportBase):
},
}

def get_data(self, input_data):
def get_data(self, input_data) -> QuerySet[Lease]:
qs = Lease.objects.select_related(
"identifier__type",
"identifier__district",
Expand Down
4 changes: 2 additions & 2 deletions leasing/report/lease/lease_statistic_report2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from decimal import ROUND_HALF_UP, Decimal

from django import forms
from django.db.models import Q
from django.db.models import Q, QuerySet
from django.utils import formats
from django.utils.translation import gettext_lazy as _
from enumfields.drf import EnumField
Expand Down Expand Up @@ -397,7 +397,7 @@ class LeaseStatisticReport2(AsyncReportBase):
}
async_task_timeout = 60 * 30 # 30 minutes

def get_data(self, input_data):
def get_data(self, input_data) -> QuerySet[Lease]:
qs = Lease.objects.select_related(
"identifier__type",
"identifier__district",
Expand Down
11 changes: 8 additions & 3 deletions leasing/report/report_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,19 @@ def get_response(self, request):

return Response(serialized_report_data)

def get_serializer_class(self):
def get_data(self, input_data: dict[str, Any]) -> list[dict] | QuerySet:
raise NotImplementedError(
"Please implement this method in the concrete report class"
)

def get_serializer_class(self) -> Type[ReportOutputSerializer]:
return ReportOutputSerializer

def get_filename(self, format):
def get_filename(self, file_format: str):
return "{}_{}.{}".format(
timezone.localtime(timezone.now()).strftime("%Y-%m-%d_%H-%M"),
self.slug,
format,
file_format,
)

def get_output_field_attr(self, field_name, attr_name, default=None):
Expand Down

0 comments on commit 5232752

Please sign in to comment.