ajax_filters.py
1.27 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
# -*- coding: utf-8 -*-
from common.decorators import ajax, AjaxError
from dictionary.models import SavedFilter
@ajax(method='post')
def save_filter(request, name, serialized_filter, super=False, force=False):
existing_filter = SavedFilter.objects.filter(user=request.user, name=name)
if force or not existing_filter:
if existing_filter:
filter = existing_filter[0]
else:
filter = SavedFilter()
filter.name = name
filter.user = request.user
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):
filters = [{
'id': 'filter%s' % filter.pk,
'name': filter.name,
'json': filter.serialized_filter,
}
for filter in SavedFilter.objects.filter(user=request.user)]
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 {}