create_users.py 3.59 KB
#-*- coding:utf-8 -*-

from django.contrib.auth.models import Group, User
from django.core.management.base import BaseCommand
from dictionary.models import Vocabulary
from accounts.models import UserSettings


class Command(BaseCommand):
    args = 'none'
    help = 'Creates default users'

    def handle(self, **options):
        create_users()


def create_users():
    observers = Group.objects.get(name=u'Obserwator')
    lexicographers = Group.objects.get(name=u'Leksykograf')
    superlexicographers = Group.objects.get(name=u'Superleksykograf')
    patternmakers = Group.objects.get(name=u'Wzorzysta')
    managers = Group.objects.get(name=u'Wydawca')
    admins = Group.objects.get(name=u'Administrator')

    SGJP = Vocabulary.objects.get(id='SGJP')
    SJPDor = Vocabulary.objects.get(id='SJPDor')
    zmiotki = Vocabulary.objects.get(id='zmiotki')
    try:
        WSJP = Vocabulary.objects.get(id='WSJP')
    except Vocabulary.DoesNotExist:
        WSJP = None
    Morfologik, created = Vocabulary.objects.get_or_create(id='Morfologik')
    PoliMorf, created = Vocabulary.objects.get_or_create(id='PoliMorf')

    User.objects.all().delete()
    sgjp = User.objects.create_user('sgjp', 'sgjp@example.com', 'sgjp')
    nzm = User.objects.create_user('nzm', 'nzm@example.com', 'nzm')
    redsgjp = User.objects.create_user('redsgjp', 'redsgjp@example.com',
        'redsgjp')
    rednzm = User.objects.create_user('rednzm', 'rednzm@example.com', 'rednzm')
    supersgjp = User.objects.create_user('supersgjp', 'supersgjp@example.com',
        'supersgjp')
    supernzm = User.objects.create_user('supernzm', 'supernzm@example.com',
        'supernzm')
    wzornik = User.objects.create_user('wzornik', 'wzornik@example.com',
        'wzornik')
    admin = User.objects.create_user('admin', 'admin@example.com', 'admin')

    users = (sgjp, nzm, redsgjp, rednzm, supersgjp, supernzm, wzornik, admin)
    for user in users:
        UserSettings.objects.create(user=user)

    admin.groups.add(admins)

    sgjp_vocabs = (SGJP, SJPDor, zmiotki)
    if WSJP: sgjp_vocabs += (WSJP,)
    nzm_vocabs = (zmiotki, Morfologik, PoliMorf)

    sgjp.groups.add(managers)
    for vocab in sgjp_vocabs:
        sgjp.managed_vocabularies.add(vocab)
    nzm.groups.add(managers)
    for vocab in nzm_vocabs:
        nzm.managed_vocabularies.add(vocab)

    redsgjp.groups.add(lexicographers)
    for vocab in sgjp_vocabs:
        redsgjp.visible_vocabularies.add(vocab)
        redsgjp.editable_vocabularies.add(vocab)
    redsgjp.visible_vocabularies.add(PoliMorf)

    rednzm.groups.add(lexicographers)
    for vocab in nzm_vocabs:
        rednzm.visible_vocabularies.add(vocab)
        rednzm.editable_vocabularies.add(vocab)
    rednzm.visible_vocabularies.add(SGJP)
    if WSJP:
        rednzm.visible_vocabularies.add(WSJP)
    rednzm.visible_vocabularies.add(SJPDor)

    supersgjp.groups.add(superlexicographers)
    for vocab in sgjp_vocabs:
        supersgjp.visible_vocabularies.add(vocab)
        supersgjp.editable_vocabularies.add(vocab)
    supersgjp.visible_vocabularies.add(PoliMorf)

    supernzm.groups.add(superlexicographers)
    for vocab in nzm_vocabs:
        supernzm.visible_vocabularies.add(vocab)
        supernzm.editable_vocabularies.add(vocab)
    supernzm.visible_vocabularies.add(SGJP)
    if WSJP:
        supernzm.visible_vocabularies.add(WSJP)
    supernzm.visible_vocabularies.add(SJPDor)

    wzornik.groups.add(superlexicographers)
    for vocab in sgjp_vocabs:
        wzornik.visible_vocabularies.add(vocab)
        wzornik.editable_vocabularies.add(vocab)
    wzornik.visible_vocabularies.add(PoliMorf)
    wzornik.groups.add(patternmakers)