|
1 | 1 | """ |
2 | | -Ensure the article_editors Group exists in the database. |
| 2 | +Ensure the website_content_editors Group exists in the database. |
3 | 3 |
|
4 | | -Reuses the same group name as the old articles app so that existing users |
5 | | -already in the group retain their permissions without any manual intervention. |
| 4 | +If the legacy article_editors Group exists, copy its members into the new group |
| 5 | +so existing editors retain access after the rename. |
6 | 6 | """ |
7 | 7 |
|
8 | | -from django.contrib.auth.models import Group |
9 | 8 | from django.db import migrations |
10 | 9 |
|
11 | | -from website_content.constants import GROUP_STAFF_ARTICLE_EDITORS |
| 10 | +OLD_GROUP_NAME = "article_editors" |
| 11 | +NEW_GROUP_NAME = "website_content_editors" |
12 | 12 |
|
13 | 13 |
|
14 | | -def add_editors_group(apps, schema_editor): # noqa: ARG001 |
15 | | - Group.objects.get_or_create(name=GROUP_STAFF_ARTICLE_EDITORS) |
| 14 | +def add_editors_group(apps, schema_editor): |
| 15 | + Group = apps.get_model("auth", "Group") |
16 | 16 |
|
| 17 | + new_group, _ = Group.objects.get_or_create(name=NEW_GROUP_NAME) |
| 18 | + old_group = Group.objects.filter(name=OLD_GROUP_NAME).first() |
17 | 19 |
|
18 | | -def remove_editors_group(apps, schema_editor): # noqa: ARG001 |
19 | | - Group.objects.filter(name=GROUP_STAFF_ARTICLE_EDITORS).delete() |
| 20 | + if old_group and old_group.pk != new_group.pk: |
| 21 | + new_group.user_set.add(*old_group.user_set.all()) |
| 22 | + |
| 23 | + |
| 24 | +def remove_editors_group(apps, schema_editor): |
| 25 | + Group = apps.get_model("auth", "Group") |
| 26 | + Group.objects.filter(name=NEW_GROUP_NAME).delete() |
20 | 27 |
|
21 | 28 |
|
22 | 29 | class Migration(migrations.Migration): |
|
0 commit comments