ajax.py
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from common.decorators import ajax, AjaxError
from dictionary.models import Vocabulary
@ajax(method='post')
def add_vocabulary(request, name):
if not request.user.has_perm('dictionary.manage_vocabulary'):
raise AjaxError('access denied')
if not name:
raise AjaxError(_(u'nazwa słownika nie może być pusta!'))
if Vocabulary.objects.filter(id=name):
raise AjaxError(_(u'słownik o tej nazwie już istnieje!'))
vocab = Vocabulary.objects.create(id=name)
vocab.managers.add(request.user) # add
return {}
# nieużywane
@ajax(method='get')
def vocabulary_permissions(request, vocab_id):
if not request.user.has_perm('dictionary.manage_vocabulary'):
raise AjaxError('access denied')
vocab = Vocabulary.objects.get(id=vocab_id)
return {
'managers': list(vocab.all_managers().values_list('pk', flat=True)),
'viewers': list(vocab.all_viewers().values_list('pk', flat=True)),
'editors': list(vocab.editors.values_list('pk', flat=True)),
}
@ajax(method='post')
def set_vocabulary_permission(request, name, user_id, perm, on):
if not request.user.has_perm('dictionary.manage_vocabulary'):
raise AjaxError('access denied')
vocab = Vocabulary.objects.get(id=name)
user = User.objects.get(pk=user_id)
if perm == 'view':
related_manager = vocab.viewers
elif perm == 'change':
related_manager = vocab.editors
else: # type == 'manage'
related_manager = vocab.managers
if on:
related_manager.add(user) # add
else:
related_manager.remove(user) # add
return {}