-
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 #980 from ae-utbm/ban-groups
Ban groups
- Loading branch information
Showing
22 changed files
with
638 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ | |
from club.models import Club, Membership | ||
from com.calendar import IcsCalendar | ||
from com.models import News, NewsDate, Sith, Weekmail | ||
from core.models import Group, Page, PageRev, SithFile, User | ||
from core.models import BanGroup, Group, Page, PageRev, SithFile, User | ||
from core.utils import resize_image | ||
from counter.models import Counter, Product, ProductType, StudentCard | ||
from election.models import Candidature, Election, ElectionList, Role | ||
|
@@ -94,6 +94,7 @@ def handle(self, *args, **options): | |
Sith.objects.create(weekmail_destinations="[email protected] [email protected]") | ||
Site.objects.create(domain=settings.SITH_URL, name=settings.SITH_NAME) | ||
groups = self._create_groups() | ||
self._create_ban_groups() | ||
|
||
root = User.objects.create_superuser( | ||
id=0, | ||
|
@@ -951,11 +952,6 @@ def _create_groups(self) -> PopulatedGroups: | |
) | ||
) | ||
) | ||
Group.objects.create( | ||
name="Banned from buying alcohol", is_manually_manageable=True | ||
) | ||
Group.objects.create(name="Banned from counters", is_manually_manageable=True) | ||
Group.objects.create(name="Banned to subscribe", is_manually_manageable=True) | ||
sas_admin = Group.objects.create(name="SAS admin", is_manually_manageable=True) | ||
sas_admin.permissions.add( | ||
*list( | ||
|
@@ -995,3 +991,8 @@ def _create_groups(self) -> PopulatedGroups: | |
sas_admin=sas_admin, | ||
pedagogy_admin=pedagogy_admin, | ||
) | ||
|
||
def _create_ban_groups(self): | ||
BanGroup.objects.create(name="Banned from buying alcohol", description="") | ||
BanGroup.objects.create(name="Banned from counters", description="") | ||
BanGroup.objects.create(name="Banned to subscribe", description="") |
164 changes: 164 additions & 0 deletions
164
core/migrations/0043_bangroup_alter_group_description_alter_user_groups_and_more.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,164 @@ | ||
# Generated by Django 4.2.17 on 2024-12-31 13:30 | ||
|
||
import django.contrib.auth.models | ||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
from django.db.migrations.state import StateApps | ||
|
||
|
||
def migrate_ban_groups(apps: StateApps, schema_editor): | ||
Group = apps.get_model("core", "Group") | ||
BanGroup = apps.get_model("core", "BanGroup") | ||
ban_group_ids = [ | ||
settings.SITH_GROUP_BANNED_ALCOHOL_ID, | ||
settings.SITH_GROUP_BANNED_COUNTER_ID, | ||
settings.SITH_GROUP_BANNED_SUBSCRIPTION_ID, | ||
] | ||
# this is a N+1 Queries, but the prod database has a grand total of 3 ban groups | ||
for group in Group.objects.filter(id__in=ban_group_ids): | ||
# auth_group, which both Group and BanGroup inherit, | ||
# is unique by name. | ||
# If we tried give the exact same name to the migrated BanGroup | ||
# before deleting the corresponding Group, | ||
# we would have an IntegrityError. | ||
# So we append a space to the name, in order to create a name | ||
# that will look the same, but that isn't really the same. | ||
ban_group = BanGroup.objects.create( | ||
name=f"{group.name} ", | ||
description=group.description, | ||
) | ||
perms = list(group.permissions.values_list("id", flat=True)) | ||
if perms: | ||
ban_group.permissions.add(*perms) | ||
ban_group.users.add( | ||
*group.users.values_list("id", flat=True), through_defaults={"reason": ""} | ||
) | ||
group.delete() | ||
# now that the original group is no longer there, | ||
# we can remove the appended space | ||
ban_group.name = ban_group.name.strip() | ||
ban_group.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("auth", "0012_alter_user_first_name_max_length"), | ||
("core", "0042_invert_is_manually_manageable_20250104_1742"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="BanGroup", | ||
fields=[ | ||
( | ||
"group_ptr", | ||
models.OneToOneField( | ||
auto_created=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
parent_link=True, | ||
primary_key=True, | ||
serialize=False, | ||
to="auth.group", | ||
), | ||
), | ||
("description", models.TextField(verbose_name="description")), | ||
], | ||
bases=("auth.group",), | ||
managers=[ | ||
("objects", django.contrib.auth.models.GroupManager()), | ||
], | ||
options={ | ||
"verbose_name": "ban group", | ||
"verbose_name_plural": "ban groups", | ||
}, | ||
), | ||
migrations.AlterField( | ||
model_name="group", | ||
name="description", | ||
field=models.TextField(verbose_name="description"), | ||
), | ||
migrations.AlterField( | ||
model_name="user", | ||
name="groups", | ||
field=models.ManyToManyField( | ||
help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.", | ||
related_name="users", | ||
to="core.group", | ||
verbose_name="groups", | ||
), | ||
), | ||
migrations.CreateModel( | ||
name="UserBan", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"created_at", | ||
models.DateTimeField(auto_now_add=True, verbose_name="created at"), | ||
), | ||
( | ||
"expires_at", | ||
models.DateTimeField( | ||
blank=True, | ||
help_text="When the ban should be removed. Currently, there is no automatic removal, so this is purely indicative. Automatic ban removal may be implemented later on.", | ||
null=True, | ||
verbose_name="expires at", | ||
), | ||
), | ||
("reason", models.TextField(verbose_name="reason")), | ||
( | ||
"ban_group", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="user_bans", | ||
to="core.bangroup", | ||
verbose_name="ban type", | ||
), | ||
), | ||
( | ||
"user", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="bans", | ||
to=settings.AUTH_USER_MODEL, | ||
verbose_name="user", | ||
), | ||
), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name="user", | ||
name="ban_groups", | ||
field=models.ManyToManyField( | ||
help_text="The bans this user has received.", | ||
related_name="users", | ||
through="core.UserBan", | ||
to="core.bangroup", | ||
verbose_name="ban groups", | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="userban", | ||
constraint=models.UniqueConstraint( | ||
fields=("ban_group", "user"), name="unique_ban_type_per_user" | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="userban", | ||
constraint=models.CheckConstraint( | ||
check=models.Q(("expires_at__gte", models.F("created_at"))), | ||
name="user_ban_end_after_start", | ||
), | ||
), | ||
migrations.RunPython( | ||
migrate_ban_groups, reverse_code=migrations.RunPython.noop, elidable=True | ||
), | ||
] |
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
Oops, something went wrong.