ajax.py 1018 Bytes
# -*- coding: utf-8 -*-

from django.contrib.auth.models import User, Group
from accounts.models import manager_groups
from common.decorators import ajax, AjaxError
from dictionary.models import Vocabulary


@ajax(method='post', permission_required='auth.add_user')
def set_group(request, user_id, group_id, set):
    user = User.objects.get(pk=user_id)
    group = Group.objects.get(pk=group_id)
    admin = request.user.has_perm('accounts.create_admin')
    if not admin and group not in manager_groups():
        raise AjaxError('access denied')
    if set:
        user.groups.add(group)
    else:
        user.groups.remove(group)
    return {}


@ajax(method='post', permission_required='dictionary.change_lexeme')
def save_default_owner(request, vocab_id):
    us = request.user.usersettings
    vocab = Vocabulary.objects.get(pk=vocab_id)
    if us.user not in vocab.all_editors():
        raise AjaxError('access denied')
    us.default_owner = Vocabulary.objects.get(pk=vocab_id)
    us.save()
    return {}