1
0
Fork 0
mirror of https://github.com/Findus23/RPGnotes.git synced 2024-09-19 15:43:45 +02:00
RPGnotes/common/management/commands/usermanagement.py

33 lines
1.2 KiB
Python
Raw Normal View History

2021-09-24 19:08:06 +02:00
from django.core.management.base import BaseCommand, CommandParser
from campaigns.models import Campaign
from users.models import TenantUser
class Command(BaseCommand):
help = 'adds and removes users to/from campaigns'
def __init__(self, *args, **kwargs):
super(Command, self).__init__(*args, **kwargs)
def add_arguments(self, parser: CommandParser):
parser.add_argument("campaign_slug", type=str)
parser.add_argument("user", type=str)
2021-09-29 20:32:57 +02:00
parser.add_argument("--superuser", action="store_true")
2021-09-24 19:08:06 +02:00
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--add", action="store_true")
group.add_argument("--remove", action="store_true")
def handle(self, *args, **options):
user = TenantUser.objects.get(name=options.get("user"))
campaign = Campaign.objects.get(slug=options.get("campaign_slug"))
if options.get("add"):
2021-09-29 20:32:57 +02:00
campaign.add_user(user, is_superuser=options.get("superuser"))
2021-09-24 19:08:06 +02:00
print(f"added {user} to {campaign}")
else:
campaign.remove_user(user)
print(f"removed {user} from {campaign}")
2021-09-24 19:12:30 +02:00
print("current users:")
for cu in campaign.user_set.all():
print(cu)