-
Notifications
You must be signed in to change notification settings - Fork 3
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
Sonny Bakker
committed
Nov 28, 2024
1 parent
b2409b4
commit 715bcbe
Showing
2 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
src/openklant/components/token/migrations/0002_tokenauth_identifier_alter_tokenauth_token.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,120 @@ | ||
# Generated by Django 4.2.15 on 2024-11-28 13:01 | ||
|
||
import logging | ||
|
||
from typing import Set | ||
from django.db import migrations, models | ||
from django.db.migrations.state import StateApps | ||
from django.db.models.aggregates import Count | ||
|
||
from openklant.components.token.utils import generate_token | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _generate_unique_identifier( | ||
token: "TokenAuth", existing_identifiers: Set[str] | ||
) -> str: | ||
identifier = f"token-{token.pk}" | ||
count = token.pk | ||
|
||
while identifier in existing_identifiers: | ||
identifier = f"token-{count}" | ||
|
||
return identifier | ||
|
||
|
||
def _generate_unique_identifiers(apps: StateApps, schema_editor) -> None: | ||
TokenAuth = apps.get_model("token", "TokenAuth") | ||
|
||
existing_identifiers = set( | ||
TokenAuth.objects.values_list("identifier", flat=True) | ||
) | ||
|
||
for token in TokenAuth.objects.all(): | ||
if token.identifier: | ||
continue | ||
|
||
identifier = _generate_unique_identifier(token, existing_identifiers) | ||
|
||
logger.debug(f"Generated {identifier} for token {token.pk}") | ||
|
||
token.identifier = identifier | ||
token.save(update_fields=("identifier",)) | ||
|
||
existing_identifiers.add(identifier) | ||
|
||
|
||
def _generate_unique_token(token: "TokenAuth", existing_tokens: Set[str]) -> str: | ||
_token = generate_token() | ||
|
||
while _token in existing_tokens: | ||
_token = generate_token() | ||
|
||
return _token | ||
|
||
|
||
def _generate_unique_tokens(apps: StateApps, schema_editor) -> None: | ||
TokenAuth = apps.get_model("token", "TokenAuth") | ||
|
||
duplicate_token_values = ( | ||
TokenAuth.objects.values("token") | ||
.annotate(Count("id")) | ||
.order_by() | ||
.filter(id__count__gt=1) | ||
) | ||
|
||
duplicate_tokens = TokenAuth.objects.filter( | ||
token__in=[item["token"] for item in duplicate_token_values] | ||
) | ||
|
||
existing_tokens = set( | ||
TokenAuth.objects.exclude(id__in=[token.id for token in duplicate_tokens]) | ||
.values_list("token", flat=True) | ||
) | ||
|
||
for token in duplicate_tokens: | ||
_token = _generate_unique_token(token, existing_tokens) | ||
|
||
logger.debug(f"Generated a new token for {token.pk}") | ||
|
||
token.token = _token | ||
token.save(update_fields=("token",)) | ||
|
||
existing_tokens.add(token) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("token", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="tokenauth", | ||
name="identifier", | ||
field=models.CharField(blank=True), | ||
), | ||
|
||
migrations.RunPython( | ||
_generate_unique_identifiers, | ||
reverse_code=migrations.RunPython.noop | ||
), | ||
migrations.RunPython( | ||
_generate_unique_tokens, | ||
reverse_code=migrations.RunPython.noop | ||
), | ||
|
||
migrations.AlterField( | ||
model_name="tokenauth", | ||
name="token", | ||
field=models.CharField(max_length=40, unique=True, verbose_name="token"), | ||
), | ||
|
||
migrations.AlterField( | ||
model_name="tokenauth", | ||
name="identifier", | ||
field=models.SlugField(unique=True), | ||
), | ||
] |
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