-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from maykinmedia/add-service-slug-and-natural-key
Add service slug and natural key
- Loading branch information
Showing
7 changed files
with
103 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Generated by Django 5.1 on 2024-08-12 13:20 | ||
|
||
from django.db import migrations, models | ||
from django.utils.text import slugify | ||
|
||
|
||
def set_service_slug_default_from_api_root(apps, schema_editor): | ||
Service = apps.get_model("zgw_consumers", "Service") | ||
|
||
def generate_unique_slug(original_slug, count=0): | ||
slug = original_slug + ("-" + str(count) if count else "") | ||
if not Service.objects.filter(slug=slug).exists(): | ||
return slug | ||
|
||
return generate_unique_slug(original_slug, count + 1) | ||
|
||
for row in Service.objects.all(): | ||
candidate_slug = slugify(row.label if row.label else row.api_root) | ||
row.slug = generate_unique_slug(candidate_slug) | ||
row.save(update_fields=["slug"]) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("zgw_consumers", "0021_service_api_connection_check_path"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="service", | ||
name="slug", | ||
field=models.SlugField( | ||
unique=False, | ||
blank=True, | ||
db_index=False, | ||
), | ||
preserve_default=False, | ||
), | ||
migrations.RunPython( | ||
set_service_slug_default_from_api_root, | ||
reverse_code=migrations.RunPython.noop, | ||
), | ||
migrations.AlterField( | ||
model_name="service", | ||
name="slug", | ||
field=models.SlugField( | ||
help_text="A unique, human-friendly slug to identify this service. Primarily useful for cross-instance import/export.", | ||
unique=True, | ||
verbose_name="service slug", | ||
), | ||
), | ||
] |
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