Skip to content

Commit 6f22268

Browse files
committed
refactor: rename TokenFamilyBlacklist model to BlacklistedTokenFamily and add abstract workaround
- Renamed TokenFamilyBlacklist to BlacklistedTokenFamily to align with the existing naming convention used in the token_blacklist app (BlacklistedToken model). - Updated all references to TokenFamilyBlacklist in the codebase to reflect the new model name. - Added 'abstract' attribute to the token_family app models to match the pattern used in the token_blacklist models.
1 parent c9bc33f commit 6f22268

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

rest_framework_simplejwt/token_family/models.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ class TokenFamily(models.Model):
2121
class Meta:
2222
verbose_name = _("Token Family")
2323
verbose_name_plural = _("Token Families")
24+
# Work around for a bug in Django:
25+
# https://code.djangoproject.com/ticket/19422
26+
#
27+
# Also see corresponding ticket:
28+
# https://github.com/encode/django-rest-framework/issues/705
29+
#
30+
# NOTE: Although this issue did not manifest in the Django shell (calling save()
31+
# raised an error as expected), it did occur when running the tests.
32+
abstract = (
33+
"rest_framework_simplejwt.token_family" not in settings.INSTALLED_APPS
34+
)
2435

2536
def __str__(self) -> str:
2637
return _("Token Family for %(user)s (%(family_id)s)") % {
@@ -29,7 +40,7 @@ def __str__(self) -> str:
2940
}
3041

3142

32-
class TokenFamilyBlacklist(models.Model):
43+
class BlacklistedTokenFamily(models.Model):
3344
id = models.BigAutoField(primary_key=True, serialize=False)
3445
family = models.OneToOneField(TokenFamily, on_delete=models.CASCADE, related_name="blacklisted")
3546

@@ -38,6 +49,17 @@ class TokenFamilyBlacklist(models.Model):
3849
class Meta:
3950
verbose_name = _("Token Family Blacklist")
4051
verbose_name_plural = _("Blacklisted Token Families")
52+
# Work around for a bug in Django:
53+
# https://code.djangoproject.com/ticket/19422
54+
#
55+
# Also see corresponding ticket:
56+
# https://github.com/encode/django-rest-framework/issues/705
57+
#
58+
# NOTE: Although this issue did not manifest in the Django shell (calling save()
59+
# raised an error as expected), it did occur when running the tests.
60+
abstract = (
61+
"rest_framework_simplejwt.token_family" not in settings.INSTALLED_APPS
62+
)
4163

4264
def __str__(self) -> str:
4365
return _("Blacklisted Token Family (%(family_id)s) for %(user)s") % {

rest_framework_simplejwt/tokens.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from .models import TokenUser
1919
from .settings import api_settings
2020
from .token_blacklist.models import BlacklistedToken, OutstandingToken
21-
from .token_family.models import TokenFamily, TokenFamilyBlacklist
21+
from .token_family.models import TokenFamily, BlacklistedTokenFamily
2222
from .utils import (
2323
aware_utcnow,
2424
datetime_from_epoch,
@@ -378,7 +378,7 @@ def verify(self, *args, **kwargs) -> None:
378378

379379
super().verify(*args, **kwargs) # type: ignore
380380

381-
def blacklist_family(self) -> TokenFamilyBlacklist:
381+
def blacklist_family(self) -> BlacklistedTokenFamily:
382382
"""
383383
Blacklists the token family.
384384
"""
@@ -397,7 +397,7 @@ def blacklist_family(self) -> TokenFamilyBlacklist:
397397
)
398398

399399
# Blacklist the entire family
400-
return TokenFamilyBlacklist.objects.get_or_create(family=family)[0]
400+
return BlacklistedTokenFamily.objects.get_or_create(family=family)[0]
401401

402402
def get_family_id(self) -> Optional[str]:
403403
return self.payload.get(api_settings.TOKEN_FAMILY_CLAIM, None)
@@ -443,7 +443,7 @@ def check_family_blacklist(token: T) -> None:
443443
logger.warning(f"Token of user:{user_id} does not have a family_id. Skipping family blacklist check.")
444444
return
445445

446-
if TokenFamilyBlacklist.objects.filter(family__family_id=family_id).exists():
446+
if BlacklistedTokenFamily.objects.filter(family__family_id=family_id).exists():
447447
raise TokenError(_("Token family is blacklisted"))
448448

449449
@staticmethod

0 commit comments

Comments
 (0)