init_groups.py 834 Bytes
Newer Older
luxcem's avatar
luxcem committed
1
from django.core.management.base import BaseCommand
2
from django.contrib.auth.models import User, Group, Permission
luxcem's avatar
luxcem committed
3
4
5

groups = ["jedi", "padawan"]
permissions = {
6
7
8
9
    "jedi": [
        "can_change_status", "can_change_priority", "can_vote", "can_edit"
    ],
    "padawan": ["can_vote", "add_article"]
luxcem's avatar
luxcem committed
10
11
12
13
}


class Command(BaseCommand):
14
    help = "Adds initial groups for the application (jedis and padawans)"
luxcem's avatar
luxcem committed
15
16

    def handle(self, *args, **options):
17
18
19
20
21
22

        for g in groups:
            print("Creating group '{}'".format(g))
            new_group, created = Group.objects.get_or_create(name=g)
            for p in permissions[g]:
                new_group.permissions.add(Permission.objects.get(codename=p))
23
24
25
26

        users = User.objects.all()
        for user in users:
            user.groups.add(Group.objects.get(name="padawan"))