views.py
10 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import datetime
import os
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django_datatables_view.base_datatable_view import BaseDatatableView
import settings
from common.decorators import render, ajax
from normalization import normalize
from webapp.forms import EditMeaningForm
from webapp.models import Entry, Expression, Meaning, MeaningStatus, Source
@render()
def preview_page(request):
js_vars = {
'ajax_expressions': reverse('expressions'),
'ajax_meanings_info': reverse('expression_info'),
'ajax_refresh_meanings': reverse('expression_info_by_meaning'),
'ajax_edit_meaning_form': reverse('edit_meaning_form'),
'ajax_meaning_form_submit': reverse('meaning_form_submit')
}
return {'js_vars': js_vars}
@login_required
@render()
def edit_page(request):
js_vars = {
'ajax_expressions': reverse('edit_table'),
'ajax_meanings_info': reverse('meaning_info_by_expression'),
'ajax_refresh_meanings': reverse('meaning_info_by_meaning'),
'ajax_edit_meaning_form': reverse('edit_meaning_form'),
'ajax_meaning_form_submit': reverse('meaning_form_submit')
}
return {'js_vars': js_vars}
def download(request):
request_date = datetime.datetime.now()
file_name = 'periphraser-%s.zip' % request_date.strftime('%Y%m%d')
if request.user.is_authenticated():
file_name = 'periphraser-auth-%s.zip' % request_date.strftime('%Y%m%d')
export_path = os.path.join(settings.EXPORTS_PATH, file_name)
while not os.path.isfile(export_path):
file_name = 'periphraser-%s.zip' % request_date.strftime('%Y%m%d')
if request.user.is_authenticated():
file_name = 'periphraser-auth-%s.zip' % request_date.strftime('%Y%m%d')
export_path = os.path.join(settings.EXPORTS_PATH, file_name)
request_date = request_date - datetime.timedelta(days=1)
response = HttpResponse(file(export_path), content_type='application/x-gzip')
response['Content-Disposition'] = 'attachment; filename=%s' % file_name
return response
@csrf_exempt
@render('expression_info.html')
@ajax(method='get', encode_result=False)
def expression_info(request, id):
user_authenticated = request.user.is_authenticated()
entry = Entry.objects.get(id=id)
meanings = []
for meaning in entry.meanings.exclude(status__hidden=True):
gen_expressions = []
orig_expressions = meaning.valid_expressions(user_authenticated)
if orig_expressions.count() == 0:
continue
if not settings.NKJP_VALIDATION and settings.NORMALIZE:
gen_expressions = normalize.expressions(orig_expressions)
meanings.append({'pk': meaning.pk,
'status': meaning.status,
'wikilink': meaning.wikilink,
'domains': meaning.domains.all(),
'orig_expressions': orig_expressions,
'gen_expressions': gen_expressions})
return {'meanings': meanings}
@csrf_exempt
@render('expression_info.html')
@ajax(method='get', encode_result=False)
def expression_info_by_meaning(request, id):
meaning = Meaning.objects.get(id=id)
sel_expr = meaning.expressions.filter(is_catchword=True)[0]
top_expressions = Expression.objects.filter(is_catchword=True, text=sel_expr.text)
top_meanings = [expr.meaning for expr in top_expressions]
meanings = []
for meaning in top_meanings:
gen_expressions = []
orig_expressions = meaning.expressions.all()
if not settings.NKJP_VALIDATION and settings.NORMALIZE:
gen_expressions = normalize.expressions(orig_expressions)
meanings.append({'pk': meaning.pk,
'status': meaning.status,
'wikilink': meaning.wikilink,
'domains': meaning.domains.all(),
'orig_expressions': orig_expressions,
'gen_expressions': gen_expressions})
return {'meanings': meanings}
@csrf_exempt
@render('expression_info.html')
@ajax(method='get', encode_result=False)
def meaning_info_by_expression(request, id):
sel_expr = Expression.objects.get(id=id)
top_meanings = [sel_expr.meaning]
meanings = []
for meaning in top_meanings:
gen_expressions = []
orig_expressions = meaning.expressions.all()
if not settings.NKJP_VALIDATION and settings.NORMALIZE:
gen_expressions = normalize.expressions(orig_expressions)
meanings.append({'pk': meaning.pk,
'status': meaning.status,
'wikilink': meaning.wikilink,
'domains': meaning.domains.all(),
'orig_expressions': orig_expressions,
'gen_expressions': gen_expressions})
return {'meanings': meanings}
@csrf_exempt
@render('expression_info.html')
@ajax(method='get', encode_result=False)
def meaning_info_by_meaning(request, id):
meaning = Meaning.objects.get(id=id)
top_meanings = [meaning]
meanings = []
for meaning in top_meanings:
gen_expressions = []
orig_expressions = meaning.expressions.all()
if not settings.NKJP_VALIDATION and settings.NORMALIZE:
gen_expressions = normalize.expressions(orig_expressions)
meanings.append({'pk': meaning.pk,
'status': meaning.status,
'wikilink': meaning.wikilink,
'domains': meaning.domains.all(),
'orig_expressions': orig_expressions,
'gen_expressions': gen_expressions})
return {'meanings': meanings}
@csrf_exempt
@render('edit_meaning_form.html')
@ajax(method='get', encode_result=False)
def edit_meaning_form(request, id):
meaning = Meaning.objects.get(id=id)
form = EditMeaningForm(instance=meaning)
return {'form': form, 'id': id}
@csrf_exempt
@ajax(method='post', encode_result=False)
def meaning_form_submit(request):
meaning = Meaning.objects.get(id=request.POST['id'])
status = MeaningStatus.objects.get(pk=request.POST['status'])
comment = request.POST['comment']
meaning.status = status
meaning.comment = comment
meaning.save()
return HttpResponse(status=200)
class ExpressionsJson(BaseDatatableView):
model = Entry
columns = ['name', 'sources', 'domains', 'id']
order_columns = ['name', 'sources', 'domains']
max_display_length = 100
def get_initial_queryset(self):
return Entry.objects.none()
def render_column(self, row, column):
user_authenticated = self.request.user.is_authenticated()
if column == 'name':
return u'{0}'.format(row.name)
elif column == 'sources':
sources = row.sources
if not user_authenticated:
sources = sources.filter(protected=False)
return u'{0}'.format(', '.join(['<a href="%s">%s</a>' % (source.url, source.name)
for source in sources.order_by('name')]))
elif column == 'domains':
return u'{0}'.format('; '.join([domain.name for domain in row.domains.order_by('name')]))
else:
return super(ExpressionsJson, self).render_column(row, column)
def filter_queryset(self, qs):
user_authenticated = self.request.user.is_authenticated()
search = self.request.GET.get(u'search[value]', None)
if search:
qs = Entry.objects.filter(name__startswith=search.upper())
qs = qs.exclude(meanings__status__hidden=True)
if not user_authenticated:
qs = qs.filter(protected=False)
source = self.request.GET.get(u'columns[1][search][value]', None)
if search and source:
if user_authenticated:
qs = qs.filter(sources__name__istartswith=source)
else:
qs = qs.filter(sources__name__istartswith=source, sources__protected=False)
domain = self.request.GET.get(u'columns[2][search][value]', None)
if search and domain:
qs = qs.filter(domains__name__istartswith=domain)
return qs
class EditTableJson(BaseDatatableView):
model = Expression
columns = ['text', 'sources', 'domain', 'status', 'id']
order_columns = ['text', 'sources', 'meaning__domain', 'meaning__status']
max_display_length = 100
def get_initial_queryset(self):
return Expression.objects.filter(is_catchword=True).exclude(meaning__status=None).exclude(meaning__status__key='ok')
def render_column(self, row, column):
if column == 'text':
return u'{0}'.format(row.text)
elif column == 'sources':
sources = Source.objects.filter(links__expression__in=row.meaning.expressions.all()).distinct()
return u'{0}'.format(', '.join('<a href="%s">%s</a>' % (source.url, source.name)
for source in sources.all()))
elif column == 'domain':
domains = row.meaning.domains.all()
return u'{0}'.format('; '.join([domain.name for domain in domains.all()]))
elif column == 'status':
return u'{0}'.format(row.meaning.status.name)
else:
return super(EditTableJson, self).render_column(row, column)
def filter_queryset(self, qs):
search = self.request.GET.get(u'search[value]', None)
if search:
qs = qs.filter(text__startswith=search.upper())
source = self.request.GET.get(u'columns[1][search][value]', None)
if source:
qs = qs.filter(link__source__name__istartswith=source)
domain = self.request.GET.get(u'columns[2][search][value]', None)
if domain:
qs = qs.filter(meaning__domains__name__istartswith=domain)
status = self.request.GET.get(u'columns[3][search][value]', None)
if status:
qs = qs.filter(meaning__status__name=status)
return qs.distinct()