views.py
2.58 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
# Create your views here.
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
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.models import Domain, Expression, Source
@login_required
@render()
def main_page(request):
js_vars = {
'ajax_expressions': reverse('expressions'),
'ajax_expression_info': reverse('expression_info')
}
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}
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.filter(main_expression=None, is_catchword=True).distinct('text')
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 = qs.filter(text__istartswith=search)
expr = self.request.GET.get(u'columns[0][search][value]', None)
if expr:
qs = qs.filter(text__istartswith=expr)
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).distinct()
return qs