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

Feature/force cache gen #151

Merged
merged 6 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion location/gql_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django.core.exceptions import ValidationError, PermissionDenied
from django.utils.translation import gettext as _
from graphene import InputObjectType
from django.core.cache import cache

import copy

Expand Down
24 changes: 10 additions & 14 deletions location/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from functools import reduce
import django
from django.core.cache import caches
from django_redis.cache import RedisCache
import uuid
Expand Down Expand Up @@ -72,7 +70,6 @@ def parents(self, location_id, loc_type=None):
)
return self.get_location_from_ids((parents), loc_type) if loc_type else parents


def allowed(self, user_id, loc_types=["R", "D", "W", "V"], strict=True, qs=False):
strict_sql = """
AND (
Expand Down Expand Up @@ -262,6 +259,11 @@ def cache_location_graph(location_id=None):
cache.set("location_types", location_types, timeout=None)


def cache_location_if_not_cached():
if not cache.has_key("location_types"):
cache_location_graph()


def extend_allowed_locations(location_pks, strict=True, loc_types=None):
"""
Get underlying locations for given location PKs.
Expand All @@ -271,11 +273,8 @@ def extend_allowed_locations(location_pks, strict=True, loc_types=None):
logger.error(
f"extend_allowed_locations is expecting a list but received {location_pks}"
)
cache_location_if_not_cached()
graph = cache.get("location_graph")
if not graph:
cache_location_graph()
graph = cache.get("location_graph")

result_pks = set()
to_visit = set(location_pks)
visited = set()
Expand Down Expand Up @@ -591,13 +590,11 @@ def get_user_districts(cls, user):
cachedata = cache.get(f"user_districts_{user.id}")
districts = []
if cachedata is None:
cache_district = cache.get("location_types")
if not cache_district:
cache_location_graph()
cache_district = cache.get("location_types")
cache_location_if_not_cached()
cache_location_type = cache.get("location_types")
cachedata = []
if user.is_superuser:
for loc in cache_district['D']:
for loc in cache_location_type['D']:
cachedata.append([0, loc])
elif not isinstance(user, core_models.InteractiveUser):
if isinstance(user, core_models.TechnicalUser):
Expand All @@ -617,7 +614,7 @@ def get_user_districts(cls, user):
.order_by("location__code")
)
for d in districts:
cachedata.append([d.id, d.location])
cachedata.append([d.id, d.location_id])

cache.set(f"user_districts_{user.id}", cachedata)

Expand Down Expand Up @@ -674,7 +671,6 @@ def location_deleted(sender, instance, **kwargs):
free_cache_for_user()



class OfficerVillage(core_models.VersionedModel):
id = models.AutoField(db_column="OfficerVillageId", primary_key=True)
officer = models.ForeignKey(
Expand Down
9 changes: 3 additions & 6 deletions location/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
from claim.test_helpers import create_test_claim_admin
from django.core.cache import caches

from location.models import LocationManager
from core.services import (
create_or_update_interactive_user,
create_or_update_core_user,
create_or_update_user_districts,
)
from location.models import LocationManager, UserDistrict
from core.utils import filter_validity
from core.models.user import Role

Expand Down Expand Up @@ -123,6 +118,8 @@ def test_allowed_location(self):
)
cached = caches["location"].get(f"user_locations_{self.test_user._u.id}")
self.assertIsNotNone(cached)
districts = UserDistrict.get_user_districts(self.test_user)
self.assertIsNotNone(districts)

def test_cache_invalidation(self):
LocationManager().is_allowed(self.test_user, [])
Expand Down
Loading