-
Notifications
You must be signed in to change notification settings - Fork 4
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 #935 from ae-utbm/groups
Remove `RealGroup` and `MetaGroup`
- Loading branch information
Showing
21 changed files
with
1,032 additions
and
664 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
129 changes: 129 additions & 0 deletions
129
club/migrations/0012_club_board_group_club_members_group.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,129 @@ | ||
# Generated by Django 4.2.16 on 2024-11-20 17:08 | ||
|
||
import django.db.models.deletion | ||
import django.db.models.functions.datetime | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
from django.db.migrations.state import StateApps | ||
from django.db.models import Q | ||
from django.utils.timezone import localdate | ||
|
||
|
||
def migrate_meta_groups(apps: StateApps, schema_editor): | ||
"""Attach the existing meta groups to the clubs. | ||
Until now, the meta groups were not attached to the clubs, | ||
nor to the users. | ||
This creates actual foreign relationships between the clubs | ||
and theirs groups and the users and theirs groups. | ||
Warnings: | ||
When the meta groups associated with the clubs aren't found, | ||
they are created. | ||
Thus the migration shouldn't fail, and all the clubs will | ||
have their groups. | ||
However, there will probably be some groups that have | ||
not been found but exist nonetheless, | ||
so there will be duplicates and dangling groups. | ||
There must be a manual cleanup after this migration. | ||
""" | ||
Group = apps.get_model("core", "Group") | ||
Club = apps.get_model("club", "Club") | ||
|
||
meta_groups = Group.objects.filter(is_meta=True) | ||
clubs = list(Club.objects.all()) | ||
for club in clubs: | ||
club.board_group = meta_groups.get_or_create( | ||
name=club.unix_name + settings.SITH_BOARD_SUFFIX | ||
)[0] | ||
club.members_group = meta_groups.get_or_create( | ||
name=club.unix_name + settings.SITH_MEMBER_SUFFIX | ||
)[0] | ||
club.save() | ||
club.refresh_from_db() | ||
memberships = club.members.filter( | ||
Q(end_date=None) | Q(end_date__gt=localdate()) | ||
).select_related("user") | ||
club.members_group.users.set([m.user for m in memberships]) | ||
club.board_group.users.set( | ||
[ | ||
m.user | ||
for m in memberships.filter(role__gt=settings.SITH_MAXIMUM_FREE_ROLE) | ||
] | ||
) | ||
|
||
|
||
# steps of the migration : | ||
# - Create a nullable field for the board group and the member group | ||
# - Edit those new fields to make them point to currently existing meta groups | ||
# - When this data migration is done, make the fields non-nullable | ||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("core", "0040_alter_user_options_user_user_permissions_and_more"), | ||
("club", "0011_auto_20180426_2013"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="club", | ||
name="edit_groups", | ||
), | ||
migrations.RemoveField( | ||
model_name="club", | ||
name="owner_group", | ||
), | ||
migrations.RemoveField( | ||
model_name="club", | ||
name="view_groups", | ||
), | ||
migrations.AddField( | ||
model_name="club", | ||
name="board_group", | ||
field=models.OneToOneField( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.PROTECT, | ||
related_name="club_board", | ||
to="core.group", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="club", | ||
name="members_group", | ||
field=models.OneToOneField( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.PROTECT, | ||
related_name="club", | ||
to="core.group", | ||
), | ||
), | ||
migrations.RunPython( | ||
migrate_meta_groups, reverse_code=migrations.RunPython.noop, elidable=True | ||
), | ||
migrations.AlterField( | ||
model_name="club", | ||
name="board_group", | ||
field=models.OneToOneField( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="club_board", | ||
to="core.group", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="club", | ||
name="members_group", | ||
field=models.OneToOneField( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="club", | ||
to="core.group", | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="membership", | ||
constraint=models.CheckConstraint( | ||
check=models.Q(("end_date__gte", models.F("start_date"))), | ||
name="end_after_start", | ||
), | ||
), | ||
] |
Oops, something went wrong.