views.py
3.11 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
# -*- coding: utf-8 -*-
import gzip
from django.contrib.auth.decorators import permission_required
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from common.decorators import render
from dictionary.models import visible_vocabularies
from export.forms import ExportForm, MagicQualifierForm
from export.lexeme_export import LexemeExport
@permission_required('dictionary.export_lexemes')
@render()
def export(request):
if request.GET:
form = ExportForm(user=request.user, data=request.GET)
if form.is_valid():
magic_ids = set()
magic_qualifiers = []
normal_qualifiers = []
for key in request.GET:
split_key = key.split('-')
if key.startswith('magic') and split_key[0] not in magic_ids:
i, name = split_key
magic_ids.add(i)
magic_form = MagicQualifierForm(prefix=i, data=request.GET)
if magic_form.is_valid():
regex = magic_form.cleaned_data['regex']
if regex:
magic_qualifiers.append(
(
regex,
magic_form.cleaned_data['qualifier'].pk))
else:
normal_qualifiers.append(
magic_form.cleaned_data['qualifier'].pk)
data = {
'vocabs': [v.id for v in form.cleaned_data['vocabs']],
'antivocabs': [v.id for v in
form.cleaned_data['antivocabs']],
'variant': form.cleaned_data['variant'].id,
'dict_id': form.cleaned_data['dict_id'],
'copyright': form.cleaned_data['copyright'],
'commonness': form.cleaned_data['commonness'],
'magic_qualifiers': magic_qualifiers,
'excluding_qualifiers': normal_qualifiers
# 'excluding_qualifiers': [
# q.pk for q
# in form.cleaned_data['excluding_qualifiers']],
# 'refl': form.cleaned_data['refl'],
}
response = HttpResponse(content_type='application/x-gzip')
export_gz = gzip.GzipFile(fileobj=response, mode='w')
LexemeExport(data=data, output_file=export_gz).export_from_cache()
filename = 'eksport.tab.gz'
response['Content-Disposition'] = (
'attachment; filename=%s' % filename)
return response
else:
form = ExportForm(user=request.user)
vocabs = {}
for v in visible_vocabularies(request.user):
vocabs.update(dict((str(q.pk), v.id) for q in v.qualifiers.all()))
js_vars = {
'vocabs': vocabs,
'ajax_new_qualifier_row': reverse('magic_qualifier_row'),
'ajax_save_export_data': reverse('save_export_data'),
'ajax_get_export_data': reverse('get_export_data'),
'ajax_delete_export_data': reverse('delete_export_data'),
}
return {'form': form, 'js_vars': js_vars}