|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
"""Pages admin views""" |
|
3
|
|
|
from pages import settings |
|
4
|
|
|
from pages.models import Page, Content |
|
5
|
|
|
from pages.utils import get_placeholders |
|
6
|
|
|
from pages.phttp import get_language_from_request |
|
7
|
|
|
|
|
8
|
|
|
from django.shortcuts import get_object_or_404, render |
|
9
|
|
|
from django.http import HttpResponse, Http404, HttpResponseRedirect |
|
10
|
|
|
from django.contrib.admin.views.decorators import staff_member_required |
|
11
|
|
|
from django.views.decorators.csrf import csrf_exempt |
|
12
|
|
|
from django.core.urlresolvers import reverse |
|
13
|
|
|
from django import forms |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
@staff_member_required |
|
17
|
|
|
@csrf_exempt |
|
18
|
|
|
def change_status(request, page_id): |
|
19
|
|
|
""" |
|
20
|
|
|
Switch the status of a page. |
|
21
|
|
|
""" |
|
22
|
|
|
perm = request.user.has_perm('pages.change_page') |
|
23
|
|
|
if perm and request.method == 'POST': |
|
24
|
|
|
page = Page.objects.get(pk=page_id) |
|
25
|
|
|
page.status = int(request.POST['status']) |
|
26
|
|
|
page.invalidate() |
|
27
|
|
|
page.save() |
|
28
|
|
|
return HttpResponse(str(page.status)) |
|
29
|
|
|
raise Http404 |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
@staff_member_required |
|
33
|
|
|
def list_pages_ajax(request, invalid_move=False): |
|
34
|
|
|
"""Render pages table for ajax function.""" |
|
35
|
|
|
language = get_language_from_request(request) |
|
36
|
|
|
pages = Page.objects.root() |
|
37
|
|
|
context = { |
|
38
|
|
|
'can_publish': request.user.has_perm('pages.can_publish'), |
|
39
|
|
|
'invalid_move': invalid_move, |
|
40
|
|
|
'language': language, |
|
41
|
|
|
'pages': pages, |
|
42
|
|
|
} |
|
43
|
|
|
return render( |
|
44
|
|
|
request, "admin/pages/page/change_list_table.html", |
|
45
|
|
|
context) |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
@staff_member_required |
|
49
|
|
|
@csrf_exempt |
|
50
|
|
|
def modify_content(request, page_id, content_type, language_id): |
|
51
|
|
|
"""Modify the content of a page.""" |
|
52
|
|
|
page = get_object_or_404(Page, pk=page_id) |
|
53
|
|
|
perm = request.user.has_perm('pages.change_page') |
|
54
|
|
|
if perm and request.method == 'POST': |
|
55
|
|
|
content = request.POST.get('content', False) |
|
56
|
|
|
if not content: |
|
57
|
|
|
raise Http404 |
|
58
|
|
|
page = Page.objects.get(pk=page_id) |
|
59
|
|
|
if settings.PAGE_CONTENT_REVISION: |
|
60
|
|
|
Content.objects.create_content_if_changed(page, language_id, |
|
61
|
|
|
content_type, content) |
|
62
|
|
|
else: |
|
63
|
|
|
Content.objects.set_or_create_content(page, language_id, |
|
64
|
|
|
content_type, content) |
|
65
|
|
|
page.invalidate() |
|
66
|
|
|
# to update last modification date |
|
67
|
|
|
page.save() |
|
68
|
|
|
|
|
69
|
|
|
return HttpResponse('ok') |
|
70
|
|
|
raise Http404 |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
@staff_member_required |
|
74
|
|
|
@csrf_exempt |
|
75
|
|
|
def modify_placeholder(request, page_id, language_id): |
|
76
|
|
|
"""Modify the content of a page.""" |
|
77
|
|
|
page = get_object_or_404(Page, pk=page_id) |
|
78
|
|
|
content_type = request.GET.get('content_type') |
|
79
|
|
|
perm = request.user.has_perm('pages.change_page') |
|
80
|
|
|
if perm and request.method == 'POST': |
|
81
|
|
|
placeholders = get_placeholders(page.get_template()) |
|
82
|
|
|
for placeholder in placeholders: |
|
83
|
|
|
if placeholder.name == content_type: |
|
84
|
|
|
|
|
85
|
|
|
initial = placeholder.get_content(page, language_id, lang_fallback=False) |
|
86
|
|
|
form = forms.Form(request.POST) |
|
87
|
|
|
form.fields[content_type] = placeholder.get_field(page, |
|
88
|
|
|
language_id, initial=initial) |
|
89
|
|
|
if not form.is_valid(): |
|
90
|
|
|
return HttpResponse(form.as_p()) |
|
91
|
|
|
|
|
92
|
|
|
placeholder.save(page, language_id, form.cleaned_data[content_type], True) |
|
93
|
|
|
page.invalidate() |
|
94
|
|
|
# to update last modification date |
|
95
|
|
|
page.save() |
|
96
|
|
|
return HttpResponse('ok') |
|
97
|
|
|
raise Http404("Content type not found in placeholders") |
|
98
|
|
|
|
|
99
|
|
|
raise Http404 |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
@staff_member_required |
|
103
|
|
|
@csrf_exempt |
|
104
|
|
|
def delete_content(request, page_id, language_id): |
|
105
|
|
|
page = get_object_or_404(Page, pk=page_id) |
|
106
|
|
|
perm = request.user.has_perm('pages.delete_page') |
|
107
|
|
|
if not perm: |
|
108
|
|
|
raise Http404 |
|
109
|
|
|
|
|
110
|
|
|
for c in Content.objects.filter(page=page, language=language_id): |
|
111
|
|
|
c.delete() |
|
112
|
|
|
|
|
113
|
|
|
destination = request.POST.get( |
|
114
|
|
|
'next', |
|
115
|
|
|
request.META.get( |
|
116
|
|
|
'HTTP_REFERER', |
|
117
|
|
|
reverse("admin:pages_page_change", args=[page_id]) |
|
118
|
|
|
) |
|
119
|
|
|
) |
|
120
|
|
|
return HttpResponseRedirect(destination) |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
@staff_member_required |
|
124
|
|
|
def traduction(request, page_id, language_id): |
|
125
|
|
|
"""Traduction helper.""" |
|
126
|
|
|
page = Page.objects.get(pk=page_id) |
|
127
|
|
|
lang = language_id |
|
128
|
|
|
placeholders = get_placeholders(page.get_template()) |
|
129
|
|
|
language_error = ( |
|
130
|
|
|
Content.objects.get_content(page, language_id, "title") |
|
131
|
|
|
is None |
|
132
|
|
|
) |
|
133
|
|
|
return render(request, 'pages/traduction_helper.html', { |
|
134
|
|
|
'page': page, |
|
135
|
|
|
'lang': lang, |
|
136
|
|
|
'language_error': language_error, |
|
137
|
|
|
'placeholders': placeholders, |
|
138
|
|
|
}) |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
@staff_member_required |
|
142
|
|
|
def get_content(request, page_id, content_id): |
|
143
|
|
|
"""Get the content for a particular page""" |
|
144
|
|
|
content = Content.objects.get(pk=content_id) |
|
145
|
|
|
return HttpResponse(content.body) |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
@staff_member_required |
|
149
|
|
|
def get_last_content(request, page_id, content_type, language_id): |
|
150
|
|
|
"""Get the latest content for a particular type""" |
|
151
|
|
|
page = Page.objects.get(pk=page_id) |
|
152
|
|
|
page.invalidate() |
|
153
|
|
|
content = Content.objects.get_content(page, language_id, content_type) |
|
154
|
|
|
return HttpResponse(content) |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
@staff_member_required |
|
158
|
|
|
@csrf_exempt |
|
159
|
|
|
def move_page(request, page_id, extra_context=None): |
|
160
|
|
|
"""Move the page to the requested target, at the given |
|
161
|
|
|
position.""" |
|
162
|
|
|
page = Page.objects.get(pk=page_id) |
|
163
|
|
|
|
|
164
|
|
|
target = request.POST.get('target', None) |
|
165
|
|
|
position = request.POST.get('position', None) |
|
166
|
|
|
if target is not None and position is not None: |
|
167
|
|
|
try: |
|
168
|
|
|
target = Page.objects.get(pk=target) |
|
169
|
|
|
except Page.DoesNotExist: |
|
170
|
|
|
pass |
|
171
|
|
|
# TODO: should use the django message system |
|
172
|
|
|
# to display this message |
|
173
|
|
|
# _('Page could not been moved.') |
|
174
|
|
|
else: |
|
175
|
|
|
# move_to invalidates cache on a model level |
|
176
|
|
|
from mptt.exceptions import InvalidMove |
|
177
|
|
|
invalid_move = False |
|
178
|
|
|
try: |
|
179
|
|
|
page.move_to(target, position) |
|
180
|
|
|
except InvalidMove: |
|
181
|
|
|
invalid_move = True |
|
182
|
|
|
return list_pages_ajax(request, invalid_move) |
|
183
|
|
|
return HttpResponseRedirect('../../') |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
@staff_member_required |
|
187
|
|
|
def sub_menu(request, page_id): |
|
188
|
|
|
"""Render the children of the requested page with the sub_menu |
|
189
|
|
|
template.""" |
|
190
|
|
|
page = Page.objects.get(id=page_id) |
|
191
|
|
|
pages = page.children.all() |
|
192
|
|
|
page_languages = settings.PAGE_LANGUAGES |
|
193
|
|
|
return render(request, "admin/pages/page/sub_menu.html", { |
|
194
|
|
|
'can_publish': request.user.has_perm('pages.can_publish'), |
|
195
|
|
|
'page': page, |
|
196
|
|
|
'pages': pages, |
|
197
|
|
|
'page_languages': page_languages, |
|
198
|
|
|
}) |
|
199
|
|
|
|