def handle(self, *args, **options):
        ignore_lang = options.get('ignore_lang', False)

        if not ignore_lang:
            # activate the language
            activate(django_settings.LANGUAGE_CODE)

        group_name = lambda name: '%s_%s' % (get_package_name(), name)

        content_type = ContentType.objects.get_for_model(Ticket)
        sm_group, smg_created = Group.objects.get_or_create(name=group_name('supermanager'))
        m_group, mg_created = Group.objects.get_or_create(name=group_name('manager'))

        sm_perm_list = []

        for codename, desc in SUPPORT_PERMISSIONS:
            permission, is_created = Permission.objects.get_or_create(
                codename=codename,
                content_type=content_type,
                defaults=dict(name=desc)
            )
            if not is_created:
                permission.name = desc
                permission.save()
            m_group.permissions.add(permission)
            sm_group.permissions.add(permission)
            sm_perm_list.append(codename)

        def set_other_permissions(group, types):
            for permission in Permission.objects \
                .filter(content_type__in=types.values()) \
                .exclude(codename__in=sm_perm_list):
                group.permissions.add(permission)

        set_other_permissions(sm_group, ContentType.objects.get_for_models(Ticket, Message, TicketType, Tag, StatusLog))
        set_other_permissions(m_group, ContentType.objects.get_for_models(Ticket, Message))

        if not ignore_lang:
            # deactivate the language
            deactivate()
Ejemplo n.º 2
0
def perm_name(name):
    return '%s.%s' % (get_package_name(), name)