-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
331 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,79 @@ | ||
from care.facility.registries.feature_flag import FlagRegistry, FlagType | ||
from django.core.cache import cache | ||
from django.core.exceptions import ValidationError | ||
from django.db import models | ||
|
||
from care.utils.models.base import BaseModel | ||
from care.utils.registries.feature_flag import FlagName, FlagRegistry, FlagType | ||
|
||
from django.db import models | ||
FACILITY_FLAG_CACHE_KEY = "facility_flag_cache:{facility_id}:{flag_name}" | ||
FACILITY_ALL_FLAGS_CACHE_KEY = "facility_all_flags_cache:{facility_id}" | ||
FACILITY_FLAG_CACHE_TTL = 60 * 60 * 24 # 1 Day | ||
|
||
|
||
class FacilityFlag(BaseModel): | ||
facility = models.ForeignKey( | ||
"Facility", on_delete=models.CASCADE, null=False, blank=False | ||
"facility.Facility", on_delete=models.CASCADE, null=False, blank=False | ||
) | ||
flag = models.CharField(max_length=1024) | ||
|
||
class Meta: | ||
verbose_name = "Facility Flag" | ||
|
||
def __str__(self): | ||
return f"{self.facility.name} - {self.flag}" | ||
|
||
@classmethod | ||
def validate_flag(cls, flag_name): | ||
def validate_flag(cls, flag_name: FlagName): | ||
FlagRegistry.validate_flag_name(FlagType.FACILITY, flag_name) | ||
|
||
def save( | ||
self, *args, **kwargs | ||
): | ||
def save(self, *args, **kwargs): | ||
self.validate_flag(self.flag) | ||
# TODO : Add Unique Together | ||
|
||
if ( | ||
not self.deleted | ||
and self.__class__.objects.filter( | ||
facility_id=self.facility_id, flag=self.flag | ||
).exists() | ||
): | ||
raise ValidationError("Flag Already Exists") | ||
|
||
cache.delete( | ||
FACILITY_FLAG_CACHE_KEY.format( | ||
facility_id=self.facility_id, flag_name=self.flag | ||
) | ||
) | ||
cache.delete(FACILITY_ALL_FLAGS_CACHE_KEY.format(facility_id=self.facility_id)) | ||
|
||
return super().save(*args, **kwargs) | ||
|
||
@classmethod | ||
def check_facility_has_flag(cls , facility , flag_name): | ||
def check_facility_has_flag(cls, facility_id: int, flag_name: FlagName) -> bool: | ||
cls.validate_flag(flag_name) | ||
# TODO : Add Caching , Invalidate on save actions | ||
return cls.objects.filter(facility=facility, flag=flag_name).exists() | ||
return cache.get_or_set( | ||
FACILITY_FLAG_CACHE_KEY.format( | ||
facility_id=facility_id, flag_name=flag_name | ||
), | ||
default=lambda: cls.objects.filter( | ||
facility_id=facility_id, flag=flag_name | ||
).exists(), | ||
timeout=FACILITY_FLAG_CACHE_TTL, | ||
) | ||
|
||
@classmethod | ||
def get_all_flags(cls, facility_id: int) -> tuple[FlagName]: | ||
return cache.get_or_set( | ||
FACILITY_ALL_FLAGS_CACHE_KEY.format(facility_id=facility_id), | ||
default=lambda: tuple( | ||
cls.objects.filter(facility_id=facility_id).values_list( | ||
"flag", flat=True | ||
) | ||
), | ||
timeout=FACILITY_FLAG_CACHE_TTL, | ||
) | ||
|
||
def __str__(self) -> str: | ||
return f"Facility Flag: {self.facility.name} - {self.flag}" | ||
|
||
class Meta: | ||
verbose_name = "Facility Flag" | ||
constraints = [ | ||
models.UniqueConstraint( | ||
fields=["facility", "flag"], | ||
condition=models.Q(deleted=False), | ||
name="unique_facility_flag", | ||
) | ||
] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
care/users/migrations/0017_userflag_userflag_unique_user_flag.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Generated by Django 4.2.15 on 2024-09-10 14:07 | ||
|
||
import uuid | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("users", "0016_upgrade_user_skills"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="UserFlag", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"external_id", | ||
models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), | ||
), | ||
( | ||
"created_date", | ||
models.DateTimeField(auto_now_add=True, db_index=True, null=True), | ||
), | ||
( | ||
"modified_date", | ||
models.DateTimeField(auto_now=True, db_index=True, null=True), | ||
), | ||
("deleted", models.BooleanField(db_index=True, default=False)), | ||
("flag", models.CharField(max_length=1024)), | ||
( | ||
"user", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
options={ | ||
"verbose_name": "User Flag", | ||
}, | ||
), | ||
migrations.AddConstraint( | ||
model_name="userflag", | ||
constraint=models.UniqueConstraint( | ||
condition=models.Q(("deleted", False)), | ||
fields=("user", "flag"), | ||
name="unique_user_flag", | ||
), | ||
), | ||
] |
Oops, something went wrong.