-
Notifications
You must be signed in to change notification settings - Fork 118
/
views.py
148 lines (96 loc) · 4.42 KB
/
views.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from django.db.models import Q
from django.core.urlresolvers import reverse
from djangorestframework.views import View
from djangorestframework.permissions import IsAuthenticated
from djangorestframework.views import ListOrCreateModelView, InstanceModelView
from djangorestframework.mixins import PaginatorMixin
from djangorestframework.status import HTTP_400_BAD_REQUEST
from djangorestframework.response import ErrorResponse
from djangorestframework.renderers import JSONRenderer, DocumentingHTMLRenderer
from fiber.models import Page, PageContentItem
from .forms import MovePageForm, MovePageContentItemForm
from fiber.app_settings import API_RENDER_HTML
API_RENDERERS = (JSONRenderer, )
if API_RENDER_HTML:
API_RENDERERS = (JSONRenderer, DocumentingHTMLRenderer)
class ApiRoot(View):
"""
The root view for the rest api.
"""
permissions = (IsAuthenticated, )
renderers = API_RENDERERS
def get(self, request):
return [
{'name': 'pages', 'url': reverse('page-resource-root')},
{'name': 'page content items', 'url': reverse('page-content-item-resource-root')},
{'name': 'content items', 'url': reverse('content-item-resource-root')},
{'name': 'images', 'url': reverse('image-resource-root')},
{'name': 'files', 'url': reverse('file-resource-root')},
]
class ListView(ListOrCreateModelView):
permissions = (IsAuthenticated, )
renderers = API_RENDERERS
class TreeListView(View):
def get(self, request):
return Page.objects.create_jqtree_data()
class PaginatedListView(PaginatorMixin, ListView):
limit = 5
def check_fields(self, order_by):
if order_by not in self.orderable_fields:
raise ErrorResponse(status=HTTP_400_BAD_REQUEST, content="Can not order by the passed value.")
class FileListView(PaginatedListView):
orderable_fields = ('filename', 'updated')
def get_queryset(self, *args, **kwargs):
qs = super(FileListView, self).get_queryset(*args, **kwargs)
search = self.request.GET.get('search', None)
if search:
qs = qs.filter(file__icontains=search)
order_by = self.request.GET.get('order_by', 'updated')
self.check_fields(order_by)
if order_by == 'filename':
order_by = 'file'
sort_order = self.request.GET.get('sortorder', 'asc')
qs = qs.order_by('%s%s' % ('-' if sort_order != 'asc' else '', order_by))
return qs
class ImageListView(PaginatedListView):
orderable_fields = ('filename', 'size', 'updated')
def get_queryset(self, *args, **kwargs):
qs = super(ImageListView, self).get_queryset(*args, **kwargs)
search = self.request.GET.get('search', None)
if search:
# TODO: image_icontains searches in the entire path, it should only search in the filename (use iregex for this?)
qs = qs.filter(Q(image__icontains=search) | Q(title__icontains=search) | Q(width__icontains=search) | Q(height__icontains=search))
order_by = self.request.GET.get('order_by', 'updated')
self.check_fields(order_by)
if order_by == 'filename':
order_by = 'image'
elif order_by == 'size':
order_by = 'width'
sort_order = self.request.GET.get('sortorder', 'asc')
qs = qs.order_by('%s%s' % ('-' if sort_order != 'asc' else '', order_by))
return qs
class InstanceView(InstanceModelView):
permissions = (IsAuthenticated, )
renderers = API_RENDERERS
class MovePageView(View):
permissions = (IsAuthenticated, )
renderers = API_RENDERERS
form = MovePageForm
def get(self, request, pk):
return 'Exposes the `Page.move_page` method'
def put(self, request, pk):
position = self.CONTENT['position']
target = self.CONTENT['target_node_id']
page = Page.objects.get(id=pk)
page.move_page(target, position)
class MovePageContentItemView(View):
permissions = (IsAuthenticated, )
renderers = API_RENDERERS
form = MovePageContentItemForm
def get(self, request, pk):
return 'Exposes the `ContentItem.move` method'
def put(self, request, pk):
page_content_item = PageContentItem.objects.get(id=pk)
before_page_content_item_id = self.CONTENT.get('before_page_content_item_id', None)
block_name = self.CONTENT.get('block_name', None)
page_content_item.move(before_page_content_item_id, block_name)