ajax_filters.py
1.4 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
# -*- coding: utf-8 -*-
from common.decorators import ajax, AjaxError
from common.util import json_decode
from dictionary.models import SavedFilter
@ajax(method='post')
def save_filter(request, name, serialized_filter, grid_id, super=False,
force=False):
existing_filter = SavedFilter.objects.filter(
user=request.user, type=grid_id, name=name)
if force or not existing_filter:
if existing_filter:
filter = existing_filter.get()
else:
filter = SavedFilter(name=name, user=request.user, type=grid_id)
filter.serialized_filter = serialized_filter
filter.super = super
try:
filter.save()
except ValueError as e:
raise AjaxError(e.message)
else:
return {'exists': True}
return {}
@ajax(method='get')
def get_filters(request, grid_id):
filters = [
{
'id': 'filter%s' % filter.pk,
'name': filter.name,
'params': json_decode(filter.serialized_filter),
}
for filter in SavedFilter.objects.filter(
user=request.user, type=grid_id)
]
return {'filters': filters}
@ajax(method='post')
def delete_filter(request, id):
filter_id = id[len('filter'):]
filter = SavedFilter.objects.get(pk=filter_id)
if filter.user != request.user:
raise AjaxError('access denied')
filter.delete()
return {}