views.py
6.46 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
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
from common.decorators import render, ajax
from webapp.forms import EditMeaningForm
from webapp.models import Domain, Expression, Meaning, MeaningStatus, Source
@login_required
@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}
@csrf_exempt
@render('expression_info.html')
@ajax(method='get', encode_result=False)
def expression_info(request, id):
sel_expr = Expression.objects.get(id=id)
top_expressions = Expression.objects.filter(is_catchword=True, text=sel_expr.text)
top_meanings = [expr.meaning for expr in top_expressions]
return {'meanings': top_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]
return {'meanings': top_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)
return {'meanings': [sel_expr.meaning]}
@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)
return {'meanings': [meaning]}
@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 = Expression
columns = ['text', 'sources', 'domain', 'id']
order_columns = ['text', 'sources', 'meaning__domain']
max_display_length = 100
def get_initial_queryset(self):
return Expression.objects.none()
def render_column(self, row, column):
if column == 'text':
return u'{0}'.format(row.text)
elif column == 'sources':
sources = Source.objects.filter(links__expression__text=row.text).distinct()
return u'{0}'.format(', '.join('<a href="%s">%s</a>' % (source.url, source.name)
for source in sources.all()))
elif column == 'domain':
domains = Domain.objects.filter(meanings__expressions__text=row.text).distinct()
return u'{0}'.format('; '.join([domain.name for domain in domains.all()]))
else:
return super(ExpressionsJson, self).render_column(row, column)
def filter_queryset(self, qs):
search = self.request.GET.get(u'search[value]', None)
if search:
qs = Expression.objects.filter(main_expression=None, is_catchword=True,
text__startswith=search.upper())
source = self.request.GET.get(u'columns[1][search][value]', None)
if search and source:
qs = qs.filter(link__source__name__istartswith=source)
domain = self.request.GET.get(u'columns[2][search][value]', None)
if search and domain:
qs = qs.filter(meaning__domains__name__istartswith=domain)
return qs.distinct('text')
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(main_expression=None, 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()