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

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))