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

Solves #611 #643

Merged
merged 1 commit into from
Oct 31, 2021
Merged
Changes from all commits
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
34 changes: 34 additions & 0 deletions care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ class PatientViewSet(
)
filterset_class = PatientFilterSet

date_range_fields = [
"created_date",
"modified_date",
"date_declared_positive",
"date_of_result",
"last_vaccinated_date",
"last_consultation_admission_date",
"last_consultation_discharge_date",
"last_consultation_symptoms_onset_date",
]
CSV_EXPORT_LIMIT = 7

def get_queryset(self):
# filter_query = self.request.query_params.get("disease_status")
queryset = super().get_queryset()
Expand Down Expand Up @@ -222,6 +234,28 @@ def list(self, request, *args, **kwargs):

"""
if settings.CSV_REQUEST_PARAMETER in request.GET:
# Start Date Validation
temp = filters.DjangoFilterBackend().get_filterset(self.request, self.queryset, self)
temp.is_valid()
within_limits = False
for field in self.date_range_fields:
slice_obj = temp.form.cleaned_data.get(field)
if slice_obj:
days_difference = (
temp.form.cleaned_data.get("created_date").stop
- temp.form.cleaned_data.get("created_date").start
).days
if days_difference <= self.CSV_EXPORT_LIMIT:
within_limits = True
else:
raise ValidationError(
{field: f"Cannot export more than {self.CSV_EXPORT_LIMIT} days at a time"}
)
if not within_limits:
raise ValidationError(
{"date": f"Atleast one date field must be filtered to be within {self.CSV_EXPORT_LIMIT} days"}
)
# End Date Limiting Validation
queryset = self.filter_queryset(self.get_queryset()).values(*PatientRegistration.CSV_MAPPING.keys())
return render_to_csv_response(
queryset,
Expand Down