-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add slug and locale to orderedcontentset
- Loading branch information
Gerrit Vermeulen
committed
Nov 21, 2024
1 parent
c3b8b9f
commit f0e0920
Showing
19 changed files
with
453 additions
and
93 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
80 changes: 80 additions & 0 deletions
80
home/migrations/0085_orderedcontentset_locale_orderedcontentset_slug.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,80 @@ | ||
# Generated by Django 4.2.11 on 2024-11-19 13:19 | ||
|
||
from django.db import migrations, models | ||
from django.db.models import Count | ||
import django.db.models.deletion | ||
|
||
|
||
def rename_duplicate_slugs(OrderedContentSet): | ||
duplicate_slugs = ( | ||
OrderedContentSet.objects.values("slug") | ||
.annotate(count=Count("slug")) | ||
.order_by("-count") | ||
.filter(count__gt=1) | ||
.values_list("slug", flat=True) | ||
) | ||
for slug in duplicate_slugs: | ||
pages = OrderedContentSet.objects.filter(slug=slug) | ||
while pages.count() > 1: | ||
page = pages.first() | ||
|
||
suffix = 1 | ||
candidate_slug = slug | ||
while OrderedContentSet.objects.filter(slug=candidate_slug).exists(): | ||
suffix += 1 | ||
candidate_slug = f"{slug}-{suffix}" | ||
|
||
page.slug = candidate_slug | ||
page.save(update_fields=["slug"]) | ||
|
||
|
||
def set_locale_from_instance(OrderedContentSet, Site): | ||
for ocs in OrderedContentSet.objects.all(): | ||
if ocs.pages: | ||
# Get the first page's data | ||
first_page_data = ocs.pages[0] | ||
contentpage = first_page_data.value.get("contentpage") | ||
ocs.locale = contentpage.locale | ||
else: | ||
site = Site.objects.get(is_default_site=True) | ||
ocs.locale = site.root_page.locale | ||
ocs.save(update_fields=["locale"]) | ||
|
||
|
||
def run_migration(apps, schema_editor): | ||
OrderedContentSet = apps.get_model("home", "OrderedContentSet") | ||
Site = apps.get_model("wagtailcore", "Site") | ||
rename_duplicate_slugs(OrderedContentSet) | ||
set_locale_from_instance(OrderedContentSet, Site) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("wagtailcore", "0089_log_entry_data_json_null_to_object"), | ||
("home", "0084_alter_contentpage_whatsapp_body"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="orderedcontentset", | ||
name="locale", | ||
field=models.ForeignKey( | ||
default="", | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="wagtailcore.locale", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="orderedcontentset", | ||
name="slug", | ||
field=models.SlugField( | ||
default="", | ||
help_text="A unique identifier for this ordered content set", | ||
max_length=255, | ||
), | ||
), | ||
migrations.RunPython( | ||
code=run_migration, reverse_code=migrations.RunPython.noop | ||
), | ||
] |
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,2 +1,2 @@ | ||
Name,Profile Fields,Page Slugs,Time,Unit,Before Or After,Contact Field | ||
Test Set,"gender:male, relationship:in_a_relationship","first_time_user, first_time_user, first_time_user","2, 3, 4","days, months, years","before, before, after",edd | ||
Name,Profile Fields,Page Slugs,Time,Unit,Before Or After,Contact Field,Slug,Locale | ||
Test Set,"gender:male, relationship:in_a_relationship","first_time_user, first_time_user, first_time_user","2, 3, 4","days, months, years","before, before, after",edd,test_set,English |
Binary file not shown.
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,2 +1,2 @@ | ||
Name,Profile Fields,Page Slugs,Time,Unit,Before Or After,Contact Field | ||
Test Set,"gender:male, relationship:in_a_relationship","first_time_user, first_time_user, first_time_user","2, 4","days, years","before, before, after","edd, name, age" | ||
Name,Profile Fields,Page Slugs,Time,Unit,Before Or After,Contact Field,Slug,Locale | ||
Test Set,"gender:male, relationship:in_a_relationship","first_time_user, first_time_user, first_time_user","2, 4","days, years","before, before, after","edd, name, age",test_set,English |
4 changes: 2 additions & 2 deletions
4
home/tests/import-export-data/ordered_content_multiple_contact_fields.csv
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,2 +1,2 @@ | ||
Name,Profile Fields,Page Slugs,Time,Unit,Before Or After,Contact Field | ||
Test Set,"gender:male, relationship:in_a_relationship","first_time_user, first_time_user, first_time_user","2, 3, 4","days, months, years","before, before, after","edd, name, age" | ||
Name,Profile Fields,Page Slugs,Time,Unit,Before Or After,Contact Field,Slug,Locale | ||
Test Set,"gender:male, relationship:in_a_relationship","first_time_user, first_time_user, first_time_user","2, 3, 4","days, months, years","before, before, after","edd, name, age",test_set,English |
4 changes: 2 additions & 2 deletions
4
home/tests/import-export-data/ordered_content_no_profile_fields.csv
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,2 +1,2 @@ | ||
Name,Profile Fields,Page Slugs,Time,Unit,Before Or After,Contact Field | ||
Test Set,,first_time_user,2,days,before,edd | ||
Name,Profile Fields,Page Slugs,Time,Unit,Before Or After,Contact Field,Slug,Locale | ||
Test Set,,first_time_user,2,days,before,edd,test_set,English |
Oops, something went wrong.