Skip to content

Commit

Permalink
Filter all location queries by validity
Browse files Browse the repository at this point in the history
  • Loading branch information
weilu committed Nov 26, 2024
1 parent 6f83281 commit fba2ff2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 10 deletions.
32 changes: 25 additions & 7 deletions individual/gql_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db.models import Subquery, Q
from django.utils.translation import gettext as _

from core import filter_validity
from core.gql.gql_mutations.base_mutation import BaseHistoryModelDeleteMutationMixin, BaseMutation, \
BaseHistoryModelUpdateMutationMixin, BaseHistoryModelCreateMutationMixin
from core.schema import OpenIMISMutation
Expand Down Expand Up @@ -167,7 +168,12 @@ def _validate_mutation(cls, user, **data):
IndividualConfig.gql_individual_delete_perms):
raise PermissionDenied(_("unauthorized"))

locations_id = list(Location.objects.filter(individuals__id__in=data['ids']).values_list('id', flat=True))
locations_id = list(
Location.objects.filter(
individuals__id__in=data['ids'],
*filter_validity()
).values_list('id', flat=True)
)
if len(locations_id)>0 and not LocationManager().is_allowed(
user,
locations_id
Expand Down Expand Up @@ -209,7 +215,12 @@ def _validate_mutation(cls, user, **data):
IndividualConfig.gql_individual_undo_delete_perms):
raise PermissionDenied(_("unauthorized"))

locations_id = list(Location.objects.filter(individuals__id__in=data['ids']).values_list('id', flat=True))
locations_id = list(
Location.objects.filter(
individuals__id__in=data['ids'],
*filter_validity()
).values_list('id', flat=True)
)
if len(locations_id)>0 and not LocationManager().is_allowed(
user,
locations_id
Expand Down Expand Up @@ -318,7 +329,12 @@ def _validate_mutation(cls, user, **data):
IndividualConfig.gql_group_delete_perms):
raise PermissionDenied(_("unauthorized"))

locations_id = list(Location.objects.filter(groups__id__in=data['ids']).values_list('id', flat=True))
locations_id = list(
Location.objects.filter(
groups__id__in=data['ids'],
*filter_validity()
).values_list('id', flat=True)
)
if len(locations_id)>0 and not LocationManager().is_allowed(
user,
locations_id
Expand Down Expand Up @@ -442,10 +458,12 @@ def _validate_mutation(cls, user, **data):
if not user.has_perms(
IndividualConfig.gql_group_delete_perms):
raise PermissionDenied(_("unauthorized"))
locations_qs = list(Location.objects.filter(
Q(groups__groupindividuals__id__in=data['ids'])|
Q(individuals__groupindividuals__id__in=data['ids'])
).values_list('id', flat=True))
locations_qs = list(
Location.objects.filter(
Q(groups__groupindividuals__id__in=data['ids'])|
Q(individuals__groupindividuals__id__in=data['ids'])
).filter(*filter_validity()).values_list('id', flat=True)
)
# must first check if locations_qs exists in case none of the groups or individuals has location
if len(locations_qs)>0 and not LocationManager().is_allowed(
user,
Expand Down
3 changes: 2 additions & 1 deletion individual/management/commands/fake_individuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from individual.models import GroupIndividual
from individual.tests.test_helpers import generate_random_string
from location.models import Location
from core import filter_validity
from core.models import User


Expand Down Expand Up @@ -65,7 +66,7 @@ def handle(self, *args, **options):
location_qs = Location.objects
if user:
location_qs = Location.get_queryset(location_qs, user)
permitted_locations = list(location_qs.filter(type='V'))
permitted_locations = list(location_qs.filter(type='V', *filter_validity()))

individuals = []
num_individuals = 100
Expand Down
5 changes: 3 additions & 2 deletions individual/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.db import transaction

from calculation.services import get_calculation_object
from core import filter_validity
from core.custom_filters import CustomFilterWizardStorage
from core.models import User
from core.services import BaseService
Expand Down Expand Up @@ -661,14 +662,14 @@ def _query_location_district_ids(df):
query = Q()
for _, row in unique_tuples.iterrows():
query |= Q(name=row['location_name'], code=row['location_code'])
locations = Location.objects.filter(type="V").filter(query)
locations = Location.objects.filter(type="V", *filter_validity()).filter(query)
return {(loc.name, loc.code): loc.parent.parent.id for loc in locations}

@staticmethod
def _query_duplicate_village_name_code():
return (
Location.objects
.filter(type="V")
.filter(type="V", *filter_validity())
.values('name', 'code')
.annotate(name_count=Count('id'))
.filter(name_count__gt=1)
Expand Down
1 change: 1 addition & 0 deletions individual/workflows/base_individual_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def process_update_individuals_workflow(user_uuid, upload_uuid):
ON loc."LocationName" = f."Json_ext"->>'location_name'
AND loc."LocationCode" = f."Json_ext"->>'location_code'
AND loc."LocationType"='V'
AND loc."ValidityTo" IS NULL
WHERE individual_individual."UUID" = (f."Json_ext" ->> 'ID')::UUID
returning individual_individual."UUID", f."UUID" as "individualdatasource_id")
Expand Down
1 change: 1 addition & 0 deletions individual/workflows/base_individual_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def process_import_individuals_workflow(user_uuid, upload_uuid):
ON loc."LocationName" = ds."Json_ext"->>'location_name'
AND loc."LocationCode" = ds."Json_ext"->>'location_code'
AND loc."LocationType"='V'
AND loc."ValidityTo" IS NULL
WHERE ds.upload_id=current_upload_id
AND ds.individual_id is null
AND ds."isDeleted"=False
Expand Down
2 changes: 2 additions & 0 deletions individual/workflows/individual_update_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def process_update_valid_individuals_workflow(user_uuid, upload_uuid, accepted=N
ON loc."LocationName" = ids."Json_ext"->>'location_name'
AND loc."LocationCode" = ids."Json_ext"->>'location_code'
AND loc."LocationType"='V'
AND loc."ValidityTo" IS NULL
WHERE individual_individual."UUID" = (ids."Json_ext" ->> 'ID')::UUID
AND ids.upload_id = current_upload_id
AND validations ->> 'validation_errors' = '[]'
Expand Down Expand Up @@ -215,6 +216,7 @@ def process_update_valid_individuals_workflow(user_uuid, upload_uuid, accepted=N
ON loc."LocationName" = ids."Json_ext"->>'location_name'
AND loc."LocationCode" = ids."Json_ext"->>'location_code'
AND loc."LocationType"='V'
AND loc."ValidityTo" IS NULL
WHERE individual_individual."UUID" = (ids."Json_ext" ->> 'ID')::UUID
AND ids.upload_id = current_upload_id
AND (ids."UUID" = ANY(accepted))
Expand Down
2 changes: 2 additions & 0 deletions individual/workflows/individual_upload_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def process_import_valid_individuals_workflow(user_uuid, upload_uuid, accepted=N
ON loc."LocationName" = ds."Json_ext"->>'location_name'
AND loc."LocationCode" = ds."Json_ext"->>'location_code'
AND loc."LocationType"='V'
AND loc."ValidityTo" IS NULL
WHERE ds.upload_id = current_upload_id
AND ds.individual_id IS NULL
AND ds."isDeleted" = False
Expand Down Expand Up @@ -195,6 +196,7 @@ def process_import_valid_individuals_workflow(user_uuid, upload_uuid, accepted=N
ON loc."LocationName" = ds."Json_ext"->>'location_name'
AND loc."LocationCode" = ds."Json_ext"->>'location_code'
AND loc."LocationType"='V'
AND loc."ValidityTo" IS NULL
WHERE ds.upload_id = current_upload_id
AND ds.individual_id IS NULL
AND ds."isDeleted" = False
Expand Down

0 comments on commit fba2ff2

Please sign in to comment.