Skip to content

Commit 1e6ca9e

Browse files
committed
Add data migration for view.sites and task.sites in multisite
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
1 parent 57a75b0 commit 1e6ca9e

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from django.conf import settings
2+
from django.db import migrations
3+
4+
5+
def set_sites_for_tasks_without_sites(apps, schema_editor):
6+
if not settings.MULTISITE:
7+
return
8+
9+
Task = apps.get_model('tasks', 'Task')
10+
Site = apps.get_model('sites', 'Site')
11+
12+
all_sites = Site.objects.all()
13+
if not all_sites.exists():
14+
return
15+
16+
tasks_without_sites = (
17+
Task.objects
18+
.filter(sites__isnull=True)
19+
.distinct()
20+
)
21+
22+
for task in tasks_without_sites:
23+
task.sites.set(all_sites)
24+
25+
26+
class Migration(migrations.Migration):
27+
28+
dependencies = [
29+
('tasks', '0038_alter_task_uri_path'),
30+
]
31+
32+
operations = [
33+
migrations.RunPython(
34+
set_sites_for_tasks_without_sites,
35+
migrations.RunPython.noop,
36+
),
37+
]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from django.conf import settings
2+
from django.db import migrations
3+
4+
5+
def set_sites_for_views_without_sites(apps, schema_editor):
6+
if not settings.MULTISITE:
7+
return
8+
9+
View = apps.get_model('views', 'View')
10+
Site = apps.get_model('sites', 'Site')
11+
12+
all_sites = Site.objects.all()
13+
if not all_sites.exists():
14+
return
15+
16+
views_without_sites = (
17+
View.objects
18+
.filter(sites__isnull=True)
19+
.distinct()
20+
)
21+
22+
for view in views_without_sites:
23+
view.sites.set(all_sites)
24+
25+
26+
class Migration(migrations.Migration):
27+
28+
dependencies = [
29+
('views', '0031_alter_view_uri_path'),
30+
]
31+
32+
operations = [
33+
migrations.RunPython(
34+
set_sites_for_views_without_sites,
35+
migrations.RunPython.noop,
36+
),
37+
]

0 commit comments

Comments
 (0)