views.py
6.32 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- coding: utf-8 -*-
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from common.decorators import render
from common.util import make_form
from management.forms import AddClassificationForm, AddClassificationValueForm, \
AddExclusionClassForm, ExclusionClassForm, AddQualifierForm, ChangeClassForm, \
VocabularyForm, TemplateChoiceForm
from dictionary.models import Vocabulary, managed_vocabularies, \
ClassificationValue, Classification, Qualifier, QualifierExclusionClass
@login_required
@render()
def management_menu(request):
return {}
@permission_required('dictionary.manage_vocabulary')
@render()
def manager_view(request):
if request.user.has_perm('dictionary.view_all_lexemes'):
vocabs = Vocabulary.objects.all()
else:
vocabs = Vocabulary.objects.filter(managers=request.user)
to_return = {
'users': User.objects.order_by('username'),
'vocabularies_info': [
(
v, request.user in v.all_managers(),
v.all_viewers(), v.all_editors(), v.all_managers()
)
for v in vocabs.order_by('id')
],
'js_vars': {
'ajax_add_vocabulary': reverse('add_vocabulary'),
# 'ajax_get_permissions': reverse('vocabulary_permissions'),
'ajax_set_permission': reverse('set_vocabulary_permission'),
},
}
return to_return
@permission_required('dictionary.manage_vocabulary')
@render()
def manage_classifications(request):
to_return = {}
add_form = make_form(request, AddClassificationForm)
if add_form.is_valid():
add_form.save()
to_return['add_form'] = add_form
if request.POST.get('det') == 'value_form':
add_form = AddClassificationValueForm(data=request.POST)
if add_form.is_valid():
add_form.save()
if request.POST.get('det') == 'vocabulary_form':
form = VocabularyForm(
data=request.POST, queryset=managed_vocabularies(request.user))
if form.is_valid():
c = form.cleaned_data['classification']
v = form.cleaned_data['vocabulary']
c.vocabularies.add(v) # add
if request.POST.get('det') == 'value_remove':
pk = int(request.POST['id'])
try:
cv = ClassificationValue.objects.get(pk=pk)
cv.deleted = True
cv.save()
except ClassificationValue.DoesNotExist:
pass
if request.POST.get('det') == 'value_rename':
pk = int(request.POST['id'])
label = request.POST['new_name']
try:
v = ClassificationValue.objects.get(pk=pk)
v.label = label
v.save()
except ClassificationValue.DoesNotExist:
pass
if request.POST:
return HttpResponseRedirect(reverse('manage_classifications'))
classification_forms = []
for c in Classification.objects.all():
value_form = AddClassificationValueForm(classification=c)
vocabs = managed_vocabularies(request.user)
using = c.vocabularies.all() & vocabs
other = vocabs.exclude(pk__in=using.values_list('pk', flat=True))
vocab_form = VocabularyForm(classification=c, queryset=other)
classification_forms.append((c, value_form, vocab_form, using))
to_return['classification_forms'] = classification_forms
return to_return
@permission_required('dictionary.manage_vocabulary')
@render()
def manage_qualifiers(request):
det = request.POST.get('det')
if det == 'add_exclusion_class':
form = AddExclusionClassForm(data=request.POST)
if form.is_valid():
form.save()
if det == 'remove_exclusion_class':
form = ExclusionClassForm(data=request.POST)
if form.is_valid():
ec = form.cleaned_data['ec']
ec.delete()
if det == 'add_qualifier':
form = AddQualifierForm(data=request.POST)
if form.is_valid():
form.save()
prefix = q = None
for name, _value in request.POST.iteritems():
if name.startswith('cec'):
prefix = name.split('-')[0]
q = Qualifier.objects.get(pk=int(prefix[3:]))
break
if prefix:
form = ChangeClassForm(data=request.POST, instance=q, prefix=prefix)
if form.is_valid():
form.save()
if det == 'remove_qualifier':
pk = int(request.POST['id'])
try:
q = Qualifier.objects.get(pk=pk)
q.deleted = True
q.save()
except Qualifier.DoesNotExist:
pass
if request.POST:
return HttpResponseRedirect(reverse('manage_qualifiers'))
to_return = {}
form_list = []
for vocabulary in managed_vocabularies(request.user):
add_exclusion_class_form = AddExclusionClassForm(vocabulary=vocabulary)
empty_ec = QualifierExclusionClass.objects.filter(
vocabulary=vocabulary).extra(where=[
'not exists (select * from kwalifikatory '
'where klasa = klasy_wykluczania.id)'])
if empty_ec:
remove_exclusion_class_form = ExclusionClassForm(queryset=empty_ec)
else:
remove_exclusion_class_form = None
add_qualifier_form = AddQualifierForm(vocabulary=vocabulary)
qualifiers = Qualifier.objects.filter(vocabulary=vocabulary)
qualifier_forms = []
for q in qualifiers:
change_class_form = ChangeClassForm(
instance=q, prefix='cec%s' % q.pk)
qualifier_forms.append((q, change_class_form))
form_list.append((
vocabulary.id, add_exclusion_class_form,
remove_exclusion_class_form,
add_qualifier_form, qualifier_forms
))
to_return['form_list'] = form_list
return to_return
@permission_required('dictionary.manage_vocabulary')
@render('table_view.html')
def table_view(request):
js_vars = {
'ajax_load_template': reverse('table_edit_form'),
'ajax_template_row': reverse('new_template_row'),
'ajax_preview_form': reverse('template_preview_form'),
'ajax_load_preview': reverse('template_preview'),
'ajax_save_template': reverse('save_template'),
}
return {'form': TemplateChoiceForm(), 'js_vars': js_vars}