init_groups.py 918 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
groups = ["droid", "jedi", "padawan"]
luxcem's avatar
luxcem committed
5
permissions = {
6
    "droid": [],
7
    "jedi": [
8
        "can_change_status", "can_change_priority", "can_vote", "can_edit", "can_edit_users", "can_delete_users", "can_create_user",
9 10
    ],
    "padawan": ["can_vote", "add_article"]
luxcem's avatar
luxcem committed
11 12 13 14
}


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

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

        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))
24 25 26 27

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