ajax_export.py
1.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
#-*- coding:utf-8 -*-
from common.decorators import render, ajax, AjaxError
from dictionary.forms import MagicQualifierForm
from dictionary.models import SavedExportData
@render()
@ajax(method='get', encode_result=False)
def magic_qualifier_row(request):
return {'form': MagicQualifierForm(prefix='magic_NUM')}
@ajax(method='post')
def save_export_data(request, name, serialized_data, force=False):
existing_data = SavedExportData.objects.filter(name=name)
if force or not existing_data:
if existing_data:
data = existing_data[0]
else:
data = SavedExportData()
data.name = name
data.serialized_data = serialized_data
try:
data.save()
except ValueError as e:
raise AjaxError(e.data)
else:
return {'exists': True}
return {}
@ajax(method='get')
def get_export_data(request):
data_list = [{
'id': 'export%s' % data.pk,
'name': data.name,
'json': data.serialized_data,
}
for data in SavedExportData.objects.all()]
return {'data_list': data_list}
@ajax(method='post')
def delete_export_data(request, data_id):
export_data_id = data_id[len('export'):]
data = SavedExportData.objects.get(pk=export_data_id)
data.delete()
return {}