Skip to content

Commit

Permalink
Merge pull request #52 from onehealthtoolkit/fix_issues
Browse files Browse the repository at this point in the history
OSDAS-291 report/case filter include child authorities.
  • Loading branch information
pphetra authored Dec 4, 2023
2 parents 737afc9 + bce83e0 commit 481d50b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 19 deletions.
35 changes: 30 additions & 5 deletions cases/schema/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import django_filters
from graphene_django import DjangoObjectType
from django.db.models import Q
from accounts.models import Authority

from accounts.schema.types import AuthorityType
from graphene.types.generic import GenericScalar
Expand All @@ -17,6 +18,7 @@
CaseStateTransition,
)
from common.types import AdminValidationProblem
from reports.models.report import IncidentReport
from reports.schema.types import IncidentReportType


Expand Down Expand Up @@ -257,6 +259,33 @@ class Meta:
fields = ["id", "state", "transition"]


class CaseTypeFilter(django_filters.FilterSet):
include_child_authorities = django_filters.BooleanFilter(
method="child_authorities_filter"
)

class Meta:
model = Case
fields = {
"report__created_at": ["lte", "gte"],
"report__relevant_authorities__id": ["in"],
"report__report_type__id": ["in"],
}

def child_authorities_filter(self, queryset, name, value):
relevant_authorities = self.data["report__relevant_authorities__id__in"]
if relevant_authorities and len(relevant_authorities) == 1:
include_child_authorities = self.data["include_child_authorities"]
if include_child_authorities:
authority = Authority.objects.get(pk=relevant_authorities[0])
child_authorities = authority.all_inherits_down()
queryset = queryset.filter(
report__relevant_authorities__in=child_authorities
)

return queryset


class CaseType(DjangoObjectType):
state_definition = graphene.Field(DeepStateDefinitionType)
report = graphene.Field(IncidentReportType)
Expand All @@ -278,11 +307,7 @@ class Meta:
"outbreak_plan_info",
"status_label",
]
filter_fields = {
"report__created_at": ["lte", "gte"],
"report__relevant_authorities__id": ["in"],
"report__report_type__id": ["in"],
}
filterset_class = CaseTypeFilter

@classmethod
def get_queryset(cls, queryset, info):
Expand Down
40 changes: 32 additions & 8 deletions reports/schema/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from graphene.types.generic import GenericScalar
from graphene_django import DjangoObjectType
from django.db.models import Q
from accounts.models import Authority

from accounts.schema.types import UserType, AuthorityType
from common.types import AdminValidationProblem
Expand Down Expand Up @@ -77,6 +78,36 @@ class Meta:
]


## Report type
class IncidentReportTypeFilter(django_filters.FilterSet):
include_child_authorities = django_filters.BooleanFilter(
method="child_authorities_filter"
)

class Meta:
model = IncidentReport
fields = {
"created_at": ["lte", "gte"],
"incident_date": ["lte", "gte"],
"relevant_authorities__name": ["istartswith", "exact"],
"relevant_authorities__id": ["in"],
"report_type__id": ["in"],
"test_flag": ["exact"],
}

def child_authorities_filter(self, queryset, name, value):
relevant_authorities = self.data["relevant_authorities__id__in"]
if relevant_authorities and len(relevant_authorities) == 1:

This comment has been minimized.

Copy link
@pphetra

pphetra Dec 4, 2023

Author Contributor

@wattana ให้ loop relevant_authorities และหา down authorities มา merge รวมกันก้อน query

This comment has been minimized.

Copy link
@pphetra

pphetra Dec 4, 2023

Author Contributor

@wattana query มันตีกัน
สุดท้ายมันจะได้

AND "reports_incidentreport_relevant_authorities"."authority_id" IN (22, 11, 9, 15, 26, 19, 21, 17, 10, 14, 13, 2, 16, 12, 24, 25, 20, 1, 18, 23, 8)

กับ

AND T4."authority_id" IN (1)

อยู่ใน query เดียวกัน ทำให้ search ได้แค่ authority_id = 1 อันเดียว

include_child_authorities = self.data["include_child_authorities"]
if include_child_authorities:
authority = Authority.objects.get(pk=relevant_authorities[0])
child_authorities = authority.all_inherits_down()
queryset = queryset.filter(relevant_authorities__in=child_authorities)
print(queryset.query)

return queryset


class IncidentReportType(DjangoObjectType):
data = GenericScalar()
original_data = GenericScalar()
Expand Down Expand Up @@ -115,14 +146,7 @@ class Meta:
"followups",
"is_followable",
]
filter_fields = {
"created_at": ["lte", "gte"],
"incident_date": ["lte", "gte"],
"relevant_authorities__name": ["istartswith", "exact"],
"relevant_authorities__id": ["in"],
"report_type__id": ["in"],
"test_flag": ["exact"],
}
filterset_class = IncidentReportTypeFilter

def resolve_gps_location(self, info):
return self.gps_location_str
Expand Down
14 changes: 8 additions & 6 deletions summaries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,10 @@ def export_zero_report_xls(request):
)
.values(
"day",
"reported_by__id",
"reported_by__username",
"reported_by__first_name",
"reported_by__last_name",
"reported_by__authorityuser__authority__name",
)
.annotate(total=Count("id"))
)
Expand All @@ -388,22 +389,23 @@ def export_zero_report_xls(request):
# print(rows.query)
dataList = []
for row in rows:
user_id = row["reported_by__id"]
data = next((x for x in dataList if x["user_id"] == user_id), None)
username = row["reported_by__username"]
data = next((x for x in dataList if x["username"] == username), None)
if data is None:
data = {
"user_id": user_id,
"Name": row["reported_by__first_name"]
+ " "
+ row["reported_by__last_name"],
"username": username,
"Authority": row["reported_by__authorityuser__authority__name"],
}
for i in range(to_date.astimezone().day):
data["Day " + str(i + 1)] = ""
dataList.append(data)
data["Day " + str(row["day"].day)] = row["total"]
# print(json.dumps(dataList, indent=4))
for item in dataList:
item.pop("user_id")
# for item in dataList:
# item.pop("user_id")

if len(dataList) == 0:
dataList.append({"Data not found.": ""})
Expand Down

0 comments on commit 481d50b

Please sign in to comment.