#-*- coding:utf-8 -*-

from django.contrib.auth.models import Permission, Group
from django.core.management.base import BaseCommand

from accounts.models import GroupSettings
from dictionary.models import Lemma_Status

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

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


def get_permission(codename):
  return Permission.objects.get(codename=codename)

def create_groups():
  do_obrobki = Lemma_Status.objects.get(status=u'do obróbki')
  w_obrobce = Lemma_Status.objects.get(status=u'w obróbce')
  gotowe = Lemma_Status.objects.get(status=u'gotowe')
  tymczasowy = Lemma_Status.objects.get(status=u'tymczasowy')
  sprawdzone = Lemma_Status.objects.get(status=u'sprawdzone')
  w_obrobce_f = Lemma_Status.objects.get(status=u'(F) w obróbce')
  gotowe_f = Lemma_Status.objects.get(status=u'(F) gotowe')
  sprawdzone_f = Lemma_Status.objects.get(status=u'(F) sprawdzone')  
  w_obrobce_s = Lemma_Status.objects.get(status=u'(S) w obróbce')
  gotowe_s = Lemma_Status.objects.get(status=u'(S) gotowe')
  sprawdzone_s = Lemma_Status.objects.get(status=u'(S) sprawdzone')
#  Group.objects.all().delete()
#
## prawa gosci
  guests, xx = Group.objects.get_or_create(name=u'Gość')
  guests.permissions.clear()
  guests.permissions.add(get_permission('view_lemma'))
  guests.permissions.add(get_permission('add_notes'))
  guests.permissions.add(get_permission('view_realization'))
  guests_settings, xx = GroupSettings.objects.get_or_create(group=guests,
                                                            priority=30)
  guests_settings.abort_statuses.clear()
  guests_settings.next_statuses.clear()
  guests_settings.active_statuses.clear()
#  
#
## prawa gosci uprzywilejowanych
  guests_plus, xx = Group.objects.get_or_create(name=u'Gość+')
  guests_plus.permissions.clear()
  guests_plus.permissions.add(get_permission('view_lemma'))
  guests_plus.permissions.add(get_permission('add_notes'))
  guests_plus.permissions.add(get_permission('download_vocabulary'))
  guests_plus.permissions.add(get_permission('view_vocab_stats'))
  guests_plus.permissions.add(get_permission('view_arg_stats'))
  guests_plus.permissions.add(get_permission('view_realization'))
  guests_plus_settings, xx = GroupSettings.objects.get_or_create(group=guests_plus,
                                                                 priority=30)
  guests_plus_settings.abort_statuses.clear()
  guests_plus_settings.next_statuses.clear()
  guests_plus_settings.active_statuses.clear()
#
## prawa frazeologow
  phraseologists, xx = Group.objects.get_or_create(name=u'Frazeolog')
  phraseologists.permissions.clear()
  phraseologists.permissions.add(get_permission('view_lemma'))
  phraseologists.permissions.add(get_permission('view_notes'))
  phraseologists.permissions.add(get_permission('add_notes'))
  phraseologists.permissions.add(get_permission('add_phraseologic_frames'))
  phraseologists.permissions.add(get_permission('change_lemmas'))
  phraseologists.permissions.add(get_permission('view_realization'))
  phraseologists.permissions.add(get_permission('see_stats'))
#  phraseologists.permissions.add(get_permission('modify_lemma_examples'))
  phraseologists.permissions.add(get_permission('modify_all_examples'))
  phraseologists_settings, xx = GroupSettings.objects.get_or_create(group=phraseologists,
                                                                    priority=25)
  phraseologists_settings.abort_statuses.clear()
  phraseologists_settings.next_statuses.clear()
  phraseologists_settings.active_statuses.clear()
  phraseologists_settings.abort_statuses.add(w_obrobce_f)
  phraseologists_settings.next_statuses.add(w_obrobce_f)
  phraseologists_settings.next_statuses.add(gotowe_f)
  phraseologists_settings.active_statuses.add(w_obrobce_f)
#
## prawa superfrazeologow
  superphraseologists, xx = Group.objects.get_or_create(name=u'Superfrazeolog')
  superphraseologists.permissions.clear()
  superphraseologists.permissions.add(get_permission('view_lemma'))
  superphraseologists.permissions.add(get_permission('view_notes'))
  superphraseologists.permissions.add(get_permission('add_notes'))
  superphraseologists.permissions.add(get_permission('add_phraseologic_frames'))
  superphraseologists.permissions.add(get_permission('change_lemmas'))
  superphraseologists.permissions.add(get_permission('change_all_lemmas'))
  superphraseologists.permissions.add(get_permission('view_realization'))
  superphraseologists.permissions.add(get_permission('confirm_lemma'))
  superphraseologists.permissions.add(get_permission('see_stats'))
  superphraseologists.permissions.add(get_permission('confirm_example'))
  superphraseologists.permissions.add(get_permission('modify_all_examples'))
#  superphraseologists.permissions.add(get_permission('modify_lemma_examples'))
  superphraseologists_settings, xx = GroupSettings.objects.get_or_create(group=superphraseologists,
                                                                         priority=23)
  superphraseologists_settings.abort_statuses.clear()
  superphraseologists_settings.next_statuses.clear()
  superphraseologists_settings.active_statuses.clear()
  superphraseologists_settings.abort_statuses.add(sprawdzone)
  superphraseologists_settings.abort_statuses.add(w_obrobce_f)
  superphraseologists_settings.next_statuses.add(w_obrobce_f)
  superphraseologists_settings.next_statuses.add(gotowe_f)
  superphraseologists_settings.next_statuses.add(sprawdzone_f)
  superphraseologists_settings.active_statuses.add(w_obrobce_f)
  superphraseologists_settings.active_statuses.add(gotowe_f)
  superphraseologists_settings.active_statuses.add(sprawdzone_f)
#  
## prawa leksykografow
  lexicographers, xx = Group.objects.get_or_create(name=u'Leksykograf')
  lexicographers.permissions.clear()
  lexicographers.permissions.add(get_permission('view_lemma'))
  lexicographers.permissions.add(get_permission('add_notes'))
  lexicographers.permissions.add(get_permission('view_notes'))
  lexicographers.permissions.add(get_permission('change_lemmas'))
  lexicographers.permissions.add(get_permission('see_stats'))
  lexicographers.permissions.add(get_permission('add_syntactic_frames'))
  #lexicographers.permissions.add(get_permission('add_phraseologic_frames'))
  lexicographers.permissions.add(get_permission('own_lemmas'))
  lexicographers.permissions.add(get_permission('modify_lemma_examples'))
  lexicographers.permissions.add(get_permission('change_lemma_version')) 
  lexicographers.permissions.add(get_permission('view_realization'))
  lexicographers_settings, xx = GroupSettings.objects.get_or_create(group=lexicographers,
                                                                    priority=20)
  lexicographers_settings.abort_statuses.clear()
  lexicographers_settings.next_statuses.clear()
  lexicographers_settings.active_statuses.clear()
  lexicographers_settings.abort_statuses.add(do_obrobki)
  lexicographers_settings.abort_statuses.add(w_obrobce)
  lexicographers_settings.abort_statuses.add(gotowe)
  lexicographers_settings.next_statuses.add(w_obrobce)
  lexicographers_settings.next_statuses.add(gotowe)
  lexicographers_settings.active_statuses.add(w_obrobce)
#
## prawa superleksykografow
  superlexicographers, xx = Group.objects.get_or_create(name=u'Superleksykograf')
  superlexicographers.permissions.clear()
  superlexicographers.permissions.add(get_permission('view_lemma'))
  superlexicographers.permissions.add(get_permission('add_notes'))
  superlexicographers.permissions.add(get_permission('view_notes'))
  superlexicographers.permissions.add(get_permission('change_all_lemmas'))
  superlexicographers.permissions.add(get_permission('change_lemmas'))
  superlexicographers.permissions.add(get_permission('confirm_lemma'))  
  superlexicographers.permissions.add(get_permission('manage_vocabulary'))
  superlexicographers.permissions.add(get_permission('download_vocabulary'))
  superlexicographers.permissions.add(get_permission('view_vocab_stats'))
  superlexicographers.permissions.add(get_permission('see_stats'))
  superlexicographers.permissions.add(get_permission('see_all_stats'))
  superlexicographers.permissions.add(get_permission('add_user'))
  superlexicographers.permissions.add(get_permission('create_realization'))
  superlexicographers.permissions.add(get_permission('confirm_example'))
  superlexicographers.permissions.add(get_permission('manage_tool_configuration'))
  #superlexicographers.permissions.add(get_permission('add_phraseologic_frames'))
  superlexicographers.permissions.add(get_permission('add_syntactic_frames'))
  superlexicographers.permissions.add(get_permission('own_lemmas'))
  superlexicographers.permissions.add(get_permission('modify_lemma_examples'))
  superlexicographers.permissions.add(get_permission('change_lemma_version'))
  superlexicographers.permissions.add(get_permission('view_realization'))
  superlexicographers.permissions.add(get_permission('modify_all_examples'))
  
  superlexicographers_settings, xx = GroupSettings.objects.get_or_create(group=superlexicographers,
                                                                         priority=10)
  superlexicographers_settings.abort_statuses.clear()
  superlexicographers_settings.next_statuses.clear()
  superlexicographers_settings.active_statuses.clear()
  superlexicographers_settings.abort_statuses.add(do_obrobki)
  superlexicographers_settings.abort_statuses.add(w_obrobce)
  superlexicographers_settings.abort_statuses.add(gotowe)
  superlexicographers_settings.abort_statuses.add(sprawdzone)
  #superlexicographers_settings.abort_statuses.add(gotowe_f)
  superlexicographers_settings.next_statuses.add(w_obrobce)
  superlexicographers_settings.next_statuses.add(gotowe)
  superlexicographers_settings.next_statuses.add(tymczasowy)
  superlexicographers_settings.next_statuses.add(sprawdzone)
  #superlexicographers_settings.next_statuses.add(w_obrobce_f)
  #superlexicographers_settings.next_statuses.add(gotowe_f)
  #superlexicographers_settings.next_statuses.add(sprawdzone_f)
  superlexicographers_settings.active_statuses.add(do_obrobki)
  superlexicographers_settings.active_statuses.add(w_obrobce)
  superlexicographers_settings.active_statuses.add(gotowe)
  superlexicographers_settings.active_statuses.add(tymczasowy)
  superlexicographers_settings.active_statuses.add(sprawdzone)
  superlexicographers_settings.active_statuses.add(w_obrobce_f)
  superlexicographers_settings.active_statuses.add(gotowe_f)
  superlexicographers_settings.active_statuses.add(sprawdzone_f)
  
  
####################### semantyka ##################################
## prawa semantykow
  semantics, xx = Group.objects.get_or_create(name=u'Semantyk')
  semantics.permissions.clear()
  semantics.permissions.add(get_permission('view_lemma'))
  semantics.permissions.add(get_permission('add_notes'))
  semantics.permissions.add(get_permission('view_notes'))
  semantics.permissions.add(get_permission('view_realization'))
  semantics.permissions.add(get_permission('see_stats'))
  
  semantics.permissions.add(get_permission('add_examples')) # nowe
  semantics.permissions.add(get_permission('semantically_own_lemmas')) # nowe
  semantics.permissions.add(get_permission('add_semantic_frames')) # nowe
  
  semantics_settings, xx = GroupSettings.objects.get_or_create(group=semantics,
                                                               priority=27)
  semantics_settings.abort_statuses.clear()
  semantics_settings.next_statuses.clear()
  semantics_settings.active_statuses.clear()
  semantics_settings.abort_statuses.add(sprawdzone_f)
  semantics_settings.next_statuses.add(w_obrobce_s)
  semantics_settings.next_statuses.add(gotowe_s)
  semantics_settings.active_statuses.add(w_obrobce_s)
#
## prawa supersemantykow
  supersemantics, xx = Group.objects.get_or_create(name=u'Supersemantyk')
  supersemantics.permissions.clear()
  
  supersemantics.permissions.add(get_permission('view_lemma'))
  supersemantics.permissions.add(get_permission('add_notes'))
  supersemantics.permissions.add(get_permission('view_notes'))
  supersemantics.permissions.add(get_permission('view_realization'))
  supersemantics.permissions.add(get_permission('see_stats'))
  
  supersemantics.permissions.add(get_permission('add_examples')) # nowe
  supersemantics.permissions.add(get_permission('semantically_own_lemmas')) # nowe
  supersemantics.permissions.add(get_permission('add_semantic_frames')) # nowe
  supersemantics.permissions.add(get_permission('change_all_semantics')) # nowe
  supersemantics.permissions.add(get_permission('confirm_semantics')) # nowe
  
  supersemantics_settings, xx = GroupSettings.objects.get_or_create(group=supersemantics,
                                                                    priority=29)
  supersemantics_settings.abort_statuses.clear()
  supersemantics_settings.next_statuses.clear()
  supersemantics_settings.active_statuses.clear()
  supersemantics_settings.abort_statuses.add(sprawdzone_f)
  supersemantics_settings.next_statuses.add(w_obrobce_s)
  supersemantics_settings.next_statuses.add(gotowe_s)
  supersemantics_settings.next_statuses.add(sprawdzone_s)
  supersemantics_settings.active_statuses.add(w_obrobce_s)
  supersemantics_settings.active_statuses.add(gotowe_s)
  supersemantics_settings.active_statuses.add(sprawdzone_s)

#
## prawa administratorow
  admins, xx = Group.objects.get_or_create(name=u'Administrator')
  admins.permissions.clear()
  admins.permissions.add(get_permission('view_lemma'))
  admins.permissions.add(get_permission('add_notes'))
  admins.permissions.add(get_permission('view_notes'))
  admins.permissions.add(get_permission('change_all_lemmas'))
  admins.permissions.add(get_permission('change_lemmas'))
  admins.permissions.add(get_permission('confirm_lemma'))  
  admins.permissions.add(get_permission('manage_vocabulary'))
  admins.permissions.add(get_permission('download_vocabulary'))
  admins.permissions.add(get_permission('view_vocab_stats'))
  admins.permissions.add(get_permission('see_stats'))
  admins.permissions.add(get_permission('see_all_stats'))
  admins.permissions.add(get_permission('add_user'))
  admins.permissions.add(get_permission('create_realization'))
  admins.permissions.add(get_permission('confirm_example'))
  admins.permissions.add(get_permission('manage_tool_configuration'))
  admins.permissions.add(get_permission('add_phraseologic_frames'))
  admins.permissions.add(get_permission('add_syntactic_frames'))
  admins.permissions.add(get_permission('own_lemmas'))
  admins.permissions.add(get_permission('modify_lemma_examples'))
  admins.permissions.add(get_permission('change_lemma_version'))
  admins.permissions.add(get_permission('view_arg_stats'))
  admins.permissions.add(get_permission('view_realization'))
  admins_settings, xx = GroupSettings.objects.get_or_create(group=admins,
                                                            priority=40)
  admins_settings.abort_statuses.clear()
  admins_settings.next_statuses.clear()
  admins_settings.active_statuses.clear()
  admins_settings.abort_statuses.add(do_obrobki)
  admins_settings.abort_statuses.add(w_obrobce)
  admins_settings.abort_statuses.add(gotowe)
  admins_settings.abort_statuses.add(sprawdzone)
  admins_settings.abort_statuses.add(gotowe_f)
  admins_settings.next_statuses.add(w_obrobce)
  admins_settings.next_statuses.add(gotowe)
  admins_settings.next_statuses.add(tymczasowy)
  admins_settings.next_statuses.add(sprawdzone)
  admins_settings.next_statuses.add(w_obrobce_f)
  admins_settings.next_statuses.add(gotowe_f)
  admins_settings.next_statuses.add(sprawdzone_f)
  admins_settings.active_statuses.add(do_obrobki)
  admins_settings.active_statuses.add(w_obrobce)
  admins_settings.active_statuses.add(gotowe)
  admins_settings.active_statuses.add(tymczasowy)
  admins_settings.active_statuses.add(sprawdzone)
  admins_settings.active_statuses.add(w_obrobce_f)
  admins_settings.active_statuses.add(gotowe_f)
  admins_settings.active_statuses.add(sprawdzone_f)
  
  