|
| 1 | +from django.conf import settings |
| 2 | +from django.contrib.auth import get_user_model |
| 3 | +from django.contrib.auth.models import Group, Permission |
| 4 | +from django.contrib.contenttypes.models import ContentType |
| 5 | +from django.core.management.base import BaseCommand |
| 6 | + |
| 7 | +from clubs.models import Club |
| 8 | + |
| 9 | + |
| 10 | +class Command(BaseCommand): |
| 11 | + help = "Give superuser to hard-coded user accounts affiliated with OSA." |
| 12 | + web_execute = True |
| 13 | + |
| 14 | + def handle(self, *args, **kwargs): |
| 15 | + User = get_user_model() |
| 16 | + content_type = ContentType.objects.get_for_model(Club) |
| 17 | + approve_perm = Permission.objects.get( |
| 18 | + codename="approve_club", content_type=content_type |
| 19 | + ) |
| 20 | + pending_perm = Permission.objects.get( |
| 21 | + codename="see_pending_clubs", content_type=content_type |
| 22 | + ) |
| 23 | + if not settings.OSA_KEYS: |
| 24 | + raise ValueError("OSA_KEYS not set in settings") |
| 25 | + if not (approvers := Group.objects.filter(name="Approvers").first()): |
| 26 | + raise ValueError("Approvers group not found") |
| 27 | + for key in settings.OSA_KEYS: |
| 28 | + if not key or not (user := User.objects.get(username=key)): |
| 29 | + continue |
| 30 | + user.is_superuser = True |
| 31 | + user.is_staff = True |
| 32 | + user.user_permissions.add(approve_perm) |
| 33 | + user.user_permissions.add(pending_perm) |
| 34 | + approvers.user_set.add(user) |
| 35 | + user.save() |
| 36 | + approvers.save() |
0 commit comments