views.py
9.2 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# -*- coding: utf-8 -*-
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.models import User, AnonymousUser
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse
from common.decorators import render
from common.util import json_decode
from dictionary.ajax_lexeme_slickgrid import LexemeQuery
from dictionary.reports import report_functions
from dictionary.models import PartOfSpeech, Lexeme, Classification, Qualifier, \
get_exclusion_classes, CrossReferenceType, LexemeAttribute, Gender, \
BorrowingSource, POSName, Vocabulary
from patterns.models import PatternType
from dictionary.util import check_query_params
from dictionary.wsjp import make_data
def main_page(request):
return HttpResponseRedirect(reverse('reader_view'))
@login_required
def main(request):
if request.user.has_perm('dictionary.view_lexeme'):
return HttpResponseRedirect(reverse('lexeme_view'))
if request.user.has_perm('patterns.view_pattern'):
return HttpResponseRedirect(reverse('pattern_view'))
if request.user.has_perm('dictionary.manage_vocabulary') or \
request.user.has_perm('auth.add_user'):
return HttpResponseRedirect(reverse('management_menu'))
if request.user.has_perm('dictionary.export_lexemes'):
return HttpResponseRedirect(reverse('export'))
return HttpResponseRedirect(reverse('auth_login')) # niezbyt prawdopodobne
def common_lexeme_js_vars(request, visible, reader=False):
qualifier_options = []
lexeme_qualifier_options = []
form_qualifier_options = []
for vocab in visible:
qualifiers = vocab.qualifiers.all()
if qualifiers:
qualifier_options.append((vocab.id, []))
lexeme_qualifier_options.append((vocab.id, []))
form_qualifier_options.append((vocab.id, []))
for q in vocab.qualifiers.all():
qualifier_options[-1][1].append((q.pk, q.label))
if q.type != Qualifier.TYPE_FORM:
lexeme_qualifier_options[-1][1].append((q.pk, q.label))
if q.type != Qualifier.TYPE_SCOPE:
form_qualifier_options[-1][1].append((q.pk, q.label))
classifications = Classification.objects.filter(
vocabularies__in=visible).distinct()
cv_options = [
(classification.multilingual_name.translation(),
classification.make_choices())
for classification in classifications]
cr_type_options = []
for cr_type in CrossReferenceType.objects.order_by('symbol'):
cr_type_options.append((
cr_type.pk,
'%s %s->%s' % (cr_type.symbol, cr_type.from_pos, cr_type.to_pos)
))
extra_attrs = []
for la in LexemeAttribute.objects.all():
attr_data = {
'id': la.id,
'name': la.multilingual_name.translation(),
'gender': la.name_gender,
'closed': la.closed,
}
if attr_data['closed']:
attr_data['options'] = [(v.id, v.value) for v in la.values.all()]
extra_attrs.append(attr_data)
js_vars = {
'ajax_get_page': reverse('get_lexemes'),
'ajax_inflection_tables': reverse('inflection_tables'),
'ajax_search_index': reverse('search_index'),
'ajax_search_by_form': reverse('search_by_form'),
'ajax_row_index': reverse('row_index'),
'ajax_export_list': reverse('export_list'),
'ajax_get_entry': reverse('get_entry'),
'lexeme_qualifier_options': lexeme_qualifier_options,
'form_qualifier_options': form_qualifier_options,
'qualifier_options': qualifier_options,
'cv_options': cv_options,
'cr_type_options': cr_type_options,
'extra_attributes': extra_attrs,
'visible_vocabularies': [(v.id, v.id) for v in visible],
'parts_of_speech': [
(pos.symbol, pos.symbol) for pos in PartOfSpeech.objects.all()],
'abbr_poses': [
(pos_name.id, pos_name.extended)
for pos_name in POSName.objects.order_by('extended')],
'status_options': [
(key, unicode(value)) for key, value in Lexeme.STATUS_CHOICES],
'gender_options': [(g.id, g.symbol) for g in Gender.objects.all()],
'pattern_type_options': PatternType.options(),
'commonness': Classification.objects.get(name=u'pospolitość').pk,
'initial_columns': LexemeQuery.default_columns,
}
session_prefix = 'reader_' if reader else ''
session_variables = ('sort_rules', 'columns')
js_vars.update(dict(
(var, request.session[session_prefix + var])
for var in session_variables if
(session_prefix + var) in request.session))
get_filter = request.GET.get('filter')
if get_filter:
try:
js_vars['filter'] = json_decode(get_filter)
except ValueError:
pass
return js_vars
@permission_required('dictionary.view_lexeme')
@render('lexeme_view.html')
def lexeme_view(request):
exclusion_classes = get_exclusion_classes()
visible = Vocabulary.visible_vocabularies(request.user)
editable_vocabs = Vocabulary.editable_vocabularies(request.user)
vocabs = {}
for v in editable_vocabs:
vocabs.update(dict((str(q.pk), v.id) for q in v.qualifiers.all()))
default_owner = request.user.usersettings.get_default_owner()
user_options = list(
User.objects.order_by('username').values_list('id', 'username'))
js_vars = common_lexeme_js_vars(request, visible)
js_vars.update({
'ajax_history_table': reverse('history_table'),
'ajax_odm_forms': reverse('odm_forms'),
'ajax_edit_form': reverse('lexeme_edit_form'),
'ajax_update_lexeme': reverse('update_lexeme'),
'ajax_save_filter': reverse('save_filter'),
'ajax_get_filters': reverse('get_filters'),
'ajax_delete_filter': reverse('delete_filter'),
'ajax_table_preview': reverse('table_preview'),
'ajax_new_inflection_row': reverse('new_inflection_edit_row'),
'ajax_new_cr_row': reverse('new_cross_reference_row'),
'ajax_delete_lexeme': reverse('delete_lexeme'),
'ajax_prompter_list': reverse('prompter_list'),
'ajax_check_pos': reverse('check_pos'),
'ajax_check_pattern': reverse('check_pattern'),
'ajax_check_classifications': reverse('check_classifications'),
'ajax_save_default_owner': reverse('save_default_owner'),
'ajax_create_lexeme': reverse('create_lexeme'),
'ajax_classification_forms': reverse('classification_forms'),
'ajax_extra_attributes': reverse('extra_attributes'),
'ajax_check_attributes': reverse('check_attributes'),
'ajax_homonym_count': reverse('homonym_count'),
'ajax_cr_homonyms': reverse('cr_homonyms'),
'ajax_new_action_row': reverse('new_action_row'),
'ajax_dynamic_fields': reverse('dynamic_action_fields'),
'ajax_execute_actions': reverse('execute_group_actions'),
'ajax_create_derivatives': reverse('create_derivatives'),
'ajax_update_back_references': reverse('update_back_references'),
'ajax_clone_lexeme': reverse('clone_lexeme'),
'vocabs': vocabs,
'bs_options': [
(bs.id, bs.label) for bs in BorrowingSource.objects.all()],
'user_options': [(0, u'(brak)')] + user_options,
'exclusion_classes': exclusion_classes,
'inflection_types': dict(
PartOfSpeech.objects.values_list('symbol', 'inflection_type')),
'auto_search': request.user.usersettings.incremental_search,
'default_owner': default_owner.id if default_owner else '',
})
return {'js_vars': js_vars, 'editable_vocabularies': editable_vocabs}
@render('reader_view.html')
def reader_view(request):
visible = Vocabulary.visible_vocabularies(AnonymousUser())
narrowed_filter_extra_attr_ids = [
LexemeAttribute.objects.get(name=u'zwrotność').id,
LexemeAttribute.objects.get(name=u'aspekt').id,
]
js_vars = common_lexeme_js_vars(request, visible, reader=True)
js_vars.update({
'vocabs': {},
'user_options': [],
'auto_search': True,
'narrowed_attr_ids': narrowed_filter_extra_attr_ids,
})
return {'js_vars': js_vars}
@login_required
@render()
def tasks(request):
return {}
@login_required
@render()
def reports(request):
report_lists = {}
for report_fun in report_functions:
report_lists.update(report_fun())
return {
'report_lists': report_lists
}
@permission_required('dictionary.view_lexeme')
def export_list(request):
query_params = json_decode(request.GET['query_params'])
check_query_params(request, query_params)
columns = json_decode(request.GET['columns'])
filename = request.GET.get('filename') or 'lista.txt'
query = LexemeQuery(query_params)
response = HttpResponse(content_type='text/plain')
query.export_list(columns, response)
response['Content-Disposition'] = ('attachment; filename=%s' % filename)
return response
def wsjp_table(request, entry):
data = make_data(entry)
result = '\n'.join(
'\t'.join(['%s'] * len(data_row)) % data_row for data_row in data)
return HttpResponse(result, content_type="text/plain; charset=utf-8")