2014-12-07 16:03:47 +01:00
|
|
|
# combo - content management system
|
|
|
|
# Copyright (C) 2014 Entr'ouvert
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Affero General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-12-07 15:35:07 +01:00
|
|
|
|
2018-08-08 17:21:15 +02:00
|
|
|
import hashlib
|
2014-12-30 15:43:52 +01:00
|
|
|
import json
|
2015-10-31 16:47:35 +01:00
|
|
|
import os
|
2014-12-30 15:43:52 +01:00
|
|
|
|
2014-12-08 10:20:23 +01:00
|
|
|
from django.conf import settings
|
2018-04-02 12:07:34 +02:00
|
|
|
from django.contrib import messages
|
2015-04-18 21:04:23 +02:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
|
2015-02-18 15:48:44 +01:00
|
|
|
from django.core.urlresolvers import reverse, reverse_lazy
|
2017-05-15 14:10:06 +02:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect, Http404
|
2014-12-16 15:17:15 +01:00
|
|
|
from django.shortcuts import redirect
|
2015-01-12 13:37:34 +01:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-08-18 12:18:16 +02:00
|
|
|
from django.utils.encoding import force_text, force_bytes
|
2018-04-02 12:07:34 +02:00
|
|
|
from django.utils.formats import date_format
|
|
|
|
from django.utils.timezone import localtime
|
2014-12-07 20:53:16 +01:00
|
|
|
from django.views.decorators.csrf import requires_csrf_token
|
2018-06-12 08:35:35 +02:00
|
|
|
from django.views.generic import (RedirectView, DetailView,
|
2015-02-18 15:48:44 +01:00
|
|
|
CreateView, UpdateView, ListView, DeleteView, FormView)
|
2014-12-07 16:03:47 +01:00
|
|
|
|
2018-04-01 15:23:01 +02:00
|
|
|
from combo.data.models import Page, CellBase, ParentContentCell, PageSnapshot
|
2016-02-07 00:25:34 +01:00
|
|
|
from combo.data.library import get_cell_class
|
2018-03-04 15:51:56 +01:00
|
|
|
from combo.data.utils import export_site, import_site, MissingGroups
|
2015-03-11 15:49:54 +01:00
|
|
|
from combo import plugins
|
2014-12-07 16:03:47 +01:00
|
|
|
|
2016-05-16 20:28:48 +02:00
|
|
|
from .forms import (PageEditTitleForm, PageVisibilityForm, SiteImportForm,
|
2016-05-19 16:30:52 +02:00
|
|
|
PageEditRedirectionForm, PageSelectTemplateForm, PageEditSlugForm,
|
2017-12-25 18:27:34 +01:00
|
|
|
PageEditPictureForm, PageEditExcludeFromNavigationForm,
|
2018-10-28 13:00:43 +01:00
|
|
|
PageEditDescriptionForm, CellVisibilityForm)
|
2014-12-08 10:37:26 +01:00
|
|
|
|
2014-12-07 16:03:47 +01:00
|
|
|
|
2015-01-12 13:24:38 +01:00
|
|
|
class HomepageView(ListView):
|
2014-12-07 16:03:47 +01:00
|
|
|
model = Page
|
2015-01-12 13:24:38 +01:00
|
|
|
template_name = 'combo/manager_home.html'
|
2014-12-07 16:03:47 +01:00
|
|
|
|
2014-12-13 16:43:29 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
2014-12-16 15:17:15 +01:00
|
|
|
self.object_list = Page.get_as_reordered_flat_hierarchy(self.object_list)
|
2015-01-12 13:24:38 +01:00
|
|
|
context = super(HomepageView, self).get_context_data(**kwargs)
|
2015-03-11 15:49:54 +01:00
|
|
|
context['extra_actions'] = plugins.get_extra_manager_actions()
|
2014-12-13 16:43:29 +01:00
|
|
|
return context
|
|
|
|
|
2015-01-12 13:24:38 +01:00
|
|
|
homepage = HomepageView.as_view()
|
2014-12-07 16:03:47 +01:00
|
|
|
|
|
|
|
|
2015-02-18 14:15:31 +01:00
|
|
|
class SiteExportView(ListView):
|
|
|
|
model = Page
|
|
|
|
|
|
|
|
def render_to_response(self, context, **response_kwargs):
|
|
|
|
response = HttpResponse(content_type='application/json')
|
2017-04-02 14:29:10 +02:00
|
|
|
json.dump(export_site(), response, indent=2)
|
2015-02-18 14:15:31 +01:00
|
|
|
return response
|
|
|
|
|
|
|
|
site_export = SiteExportView.as_view()
|
|
|
|
|
|
|
|
|
2015-02-18 15:48:44 +01:00
|
|
|
class SiteImportView(FormView):
|
|
|
|
form_class = SiteImportForm
|
|
|
|
template_name = 'combo/site_import.html'
|
|
|
|
success_url = reverse_lazy('combo-manager-homepage')
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2018-03-17 17:59:31 +01:00
|
|
|
try:
|
2019-08-18 14:23:02 +02:00
|
|
|
json_site = json.loads(force_text(self.request.FILES['site_json'].read()))
|
2018-03-17 17:59:31 +01:00
|
|
|
except ValueError:
|
|
|
|
form.add_error('site_json', _('File is not in the expected JSON format.'))
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2018-03-04 15:51:56 +01:00
|
|
|
try:
|
|
|
|
import_site(json_site)
|
|
|
|
except MissingGroups as e:
|
|
|
|
form.add_error('site_json', unicode(e))
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2015-02-18 15:48:44 +01:00
|
|
|
return super(SiteImportView, self).form_valid(form)
|
|
|
|
|
|
|
|
site_import = SiteImportView.as_view()
|
|
|
|
|
|
|
|
|
2014-12-07 16:03:47 +01:00
|
|
|
class PageAddView(CreateView):
|
|
|
|
model = Page
|
|
|
|
template_name = 'combo/page_add.html'
|
2016-05-16 20:28:48 +02:00
|
|
|
form_class = PageEditTitleForm
|
2014-12-08 10:37:26 +01:00
|
|
|
|
2015-01-12 13:37:34 +01:00
|
|
|
def get_initial(self):
|
|
|
|
initial = super(PageAddView, self).get_initial()
|
2016-05-16 20:28:48 +02:00
|
|
|
if Page.objects.count() == 0: # first page
|
2015-01-12 13:37:34 +01:00
|
|
|
initial['title'] = _('Home')
|
|
|
|
return initial
|
|
|
|
|
2016-05-28 14:34:32 +02:00
|
|
|
def form_valid(self, form):
|
|
|
|
response = super(PageAddView, self).form_valid(form)
|
|
|
|
if self.object.slug != 'index' or self.object.parent_id:
|
2017-11-09 12:50:37 +01:00
|
|
|
for placeholder in self.object.get_placeholders(request=self.request):
|
2017-10-13 10:34:02 +02:00
|
|
|
if placeholder.acquired is True:
|
|
|
|
ParentContentCell(page=self.object,
|
|
|
|
placeholder=placeholder.key, order=0).save()
|
2016-05-28 14:34:32 +02:00
|
|
|
|
|
|
|
return response
|
|
|
|
|
2014-12-08 10:37:26 +01:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('combo-manager-page-view', kwargs={'pk': self.object.id})
|
2014-12-07 16:03:47 +01:00
|
|
|
|
|
|
|
page_add = PageAddView.as_view()
|
|
|
|
|
|
|
|
|
2014-12-08 10:37:26 +01:00
|
|
|
class PageEditView(UpdateView):
|
|
|
|
model = Page
|
|
|
|
template_name = 'combo/page_add.html'
|
2018-04-01 15:23:01 +02:00
|
|
|
comment = None
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
result = super(PageEditView, self).form_valid(form)
|
|
|
|
PageSnapshot.take(self.get_object(), request=self.request, comment=self.comment)
|
|
|
|
return result
|
2014-12-08 10:37:26 +01:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('combo-manager-page-view', kwargs={'pk': self.object.id})
|
|
|
|
|
2016-05-16 12:15:46 +02:00
|
|
|
|
|
|
|
class PageSelectTemplateView(PageEditView):
|
|
|
|
form_class = PageSelectTemplateForm
|
|
|
|
|
2018-04-01 15:23:01 +02:00
|
|
|
@property
|
|
|
|
def comment(self):
|
|
|
|
return _('switched template to %s') % settings.COMBO_PUBLIC_TEMPLATES[self.object.template_name]['name']
|
|
|
|
|
2016-05-28 14:34:32 +02:00
|
|
|
def form_valid(self, form):
|
|
|
|
old_template_name = self.get_object().template_name
|
|
|
|
new_template_name = self.object.template_name
|
|
|
|
response = super(PageSelectTemplateView, self).form_valid(form)
|
|
|
|
if old_template_name != new_template_name:
|
2017-11-09 12:50:37 +01:00
|
|
|
old_placeholder_keys = [x.key for x in self.get_object().get_placeholders(request=self.request)]
|
2016-05-28 14:34:32 +02:00
|
|
|
page_cells = CellBase.get_cells(page_id=self.object.id)
|
2017-11-09 12:50:37 +01:00
|
|
|
for placeholder in self.object.get_placeholders(request=self.request, template_name=new_template_name):
|
2017-10-13 10:34:02 +02:00
|
|
|
if not placeholder.acquired:
|
2016-05-28 14:34:32 +02:00
|
|
|
continue
|
2017-10-13 10:34:02 +02:00
|
|
|
if placeholder.key in old_placeholder_keys:
|
2016-05-28 14:34:32 +02:00
|
|
|
continue
|
2017-10-13 10:34:02 +02:00
|
|
|
if [x for x in page_cells if x.placeholder == placeholder.key]:
|
2016-05-28 14:34:32 +02:00
|
|
|
# skip placeholders that already have cells
|
|
|
|
continue
|
2017-10-13 10:34:02 +02:00
|
|
|
ParentContentCell(page=self.object, placeholder=placeholder.key, order=0).save()
|
2016-05-28 14:34:32 +02:00
|
|
|
return response
|
|
|
|
|
2016-05-16 12:15:46 +02:00
|
|
|
page_select_template = PageSelectTemplateView.as_view()
|
|
|
|
|
|
|
|
|
|
|
|
class PageEditRedirectionView(PageEditView):
|
|
|
|
form_class = PageEditRedirectionForm
|
2018-04-01 15:23:01 +02:00
|
|
|
comment = _('changed redirection')
|
2016-05-16 12:15:46 +02:00
|
|
|
|
|
|
|
page_edit_redirection = PageEditRedirectionView.as_view()
|
|
|
|
|
|
|
|
|
2016-05-19 16:30:52 +02:00
|
|
|
class PageEditExcludeFromNavigationView(PageEditView):
|
|
|
|
form_class = PageEditExcludeFromNavigationForm
|
2018-04-01 15:23:01 +02:00
|
|
|
comment = _('changed navigation exclusion')
|
2016-05-19 16:30:52 +02:00
|
|
|
|
|
|
|
page_edit_exclude_from_navigation = PageEditExcludeFromNavigationView.as_view()
|
|
|
|
|
|
|
|
|
2016-05-16 20:28:48 +02:00
|
|
|
class PageEditSlugView(PageEditView):
|
|
|
|
form_class = PageEditSlugForm
|
2018-04-01 15:23:01 +02:00
|
|
|
comment = _('changed slug')
|
2016-05-16 20:28:48 +02:00
|
|
|
|
|
|
|
page_edit_slug = PageEditSlugView.as_view()
|
|
|
|
|
|
|
|
|
2018-04-19 10:08:51 +02:00
|
|
|
class PageEditDescriptionView(PageEditView):
|
|
|
|
form_class = PageEditDescriptionForm
|
2018-04-01 15:23:01 +02:00
|
|
|
comment = _('changed description')
|
2018-04-19 10:08:51 +02:00
|
|
|
|
|
|
|
page_edit_description = PageEditDescriptionView.as_view()
|
|
|
|
|
|
|
|
|
2016-05-16 20:28:48 +02:00
|
|
|
class PageEditTitleView(PageEditView):
|
|
|
|
form_class = PageEditTitleForm
|
2018-04-01 15:23:01 +02:00
|
|
|
comment = _('changed title')
|
2016-05-16 20:28:48 +02:00
|
|
|
|
|
|
|
page_edit_title = PageEditTitleView.as_view()
|
|
|
|
|
|
|
|
|
2015-01-07 10:48:06 +01:00
|
|
|
class PageVisibilityView(PageEditView):
|
|
|
|
form_class = PageVisibilityForm
|
2018-04-01 15:23:01 +02:00
|
|
|
comment = _('changed visibility')
|
2015-01-07 10:48:06 +01:00
|
|
|
|
|
|
|
page_visibility = PageVisibilityView.as_view()
|
|
|
|
|
2014-12-08 10:37:26 +01:00
|
|
|
|
2017-05-15 14:10:06 +02:00
|
|
|
class PageEditPictureView(PageEditView):
|
|
|
|
form_class = PageEditPictureForm
|
2018-04-01 15:23:01 +02:00
|
|
|
comment = _('changed picture')
|
2017-05-15 14:10:06 +02:00
|
|
|
|
|
|
|
page_edit_picture = PageEditPictureView.as_view()
|
|
|
|
|
|
|
|
|
|
|
|
class PageRemovePictureView(DetailView):
|
|
|
|
model = Page
|
|
|
|
|
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
page = self.get_object()
|
|
|
|
page.picture = None
|
|
|
|
page.save()
|
2018-04-01 15:23:01 +02:00
|
|
|
PageSnapshot.take(page, request=self.request, comment=_('removed picture'))
|
2017-05-15 14:10:06 +02:00
|
|
|
return HttpResponseRedirect(reverse('combo-manager-page-view', kwargs={'pk': page.id}))
|
|
|
|
|
|
|
|
page_remove_picture = PageRemovePictureView.as_view()
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-07 16:03:47 +01:00
|
|
|
class PageView(DetailView):
|
|
|
|
model = Page
|
|
|
|
template_name = 'combo/page_view.html'
|
|
|
|
|
2014-12-07 17:36:21 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
2015-01-17 14:15:11 +01:00
|
|
|
context = super(PageView, self).get_context_data(**kwargs)
|
2016-05-16 12:31:04 +02:00
|
|
|
cell_type_groups = {}
|
|
|
|
for cell_type in CellBase.get_all_cell_types():
|
|
|
|
if not cell_type['group'] in cell_type_groups:
|
|
|
|
cell_type_groups[cell_type['group']] = []
|
|
|
|
cell_type_groups[cell_type['group']].append(cell_type)
|
|
|
|
for cell_group in cell_type_groups.values():
|
|
|
|
cell_group.sort(key=lambda x: (x.get('order'), x.get('name')))
|
|
|
|
if 'data' in cell_type_groups.keys():
|
|
|
|
cell_type_groups[''] = cell_type_groups.get('data')
|
|
|
|
del cell_type_groups['data']
|
2018-07-25 14:28:05 +02:00
|
|
|
context['cell_type_groups'] = list(cell_type_groups.items())
|
2016-05-16 12:31:04 +02:00
|
|
|
context['cell_type_groups'].sort(key=lambda x: x[0])
|
|
|
|
|
2014-12-24 07:34:52 +01:00
|
|
|
cells = CellBase.get_cells(page_id=self.object.id)
|
2014-12-08 10:37:26 +01:00
|
|
|
template = self.object.template_name
|
2014-12-08 10:20:23 +01:00
|
|
|
placeholders = []
|
|
|
|
combo_template = settings.COMBO_PUBLIC_TEMPLATES.get(template)
|
2014-12-30 13:10:48 +01:00
|
|
|
|
2017-11-09 12:50:37 +01:00
|
|
|
for placeholder in self.object.get_placeholders(request=self.request, traverse_cells=True):
|
2014-12-30 13:10:48 +01:00
|
|
|
placeholder_dict = {
|
2017-10-13 10:34:02 +02:00
|
|
|
'key': placeholder.key,
|
2017-11-02 17:04:51 +01:00
|
|
|
'name': placeholder.get_name(),
|
2014-12-30 13:10:48 +01:00
|
|
|
}
|
2017-10-13 10:34:02 +02:00
|
|
|
placeholder_dict['cells'] = [x for x in cells if x.placeholder == placeholder.key]
|
2016-05-28 14:34:32 +02:00
|
|
|
placeholders.append(placeholder_dict)
|
|
|
|
|
2014-12-08 10:20:23 +01:00
|
|
|
context['placeholders'] = placeholders
|
2014-12-07 17:36:21 +01:00
|
|
|
return context
|
|
|
|
|
2014-12-07 20:53:16 +01:00
|
|
|
page_view = requires_csrf_token(PageView.as_view())
|
2014-12-07 17:36:21 +01:00
|
|
|
|
|
|
|
|
2015-01-12 13:28:11 +01:00
|
|
|
class PageDeleteView(DeleteView):
|
|
|
|
model = Page
|
2016-07-27 12:06:08 +02:00
|
|
|
template_name = 'combo/delete_page.html'
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
deleted_page = self.get_object()
|
|
|
|
|
|
|
|
if request.POST.get('choice') == 'delete-one':
|
|
|
|
new_parent = deleted_page.parent_id
|
|
|
|
Page.objects.filter(parent=deleted_page).update(parent=new_parent)
|
|
|
|
|
2018-04-01 15:23:01 +02:00
|
|
|
PageSnapshot.take(deleted_page, request=self.request, deletion=True)
|
2016-07-27 12:06:08 +02:00
|
|
|
return self.delete(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
subpages = Page.objects.filter(parent=self.get_object().id)
|
|
|
|
context = super(PageDeleteView, self).get_context_data()
|
|
|
|
context['display_choice'] = True if subpages else False
|
|
|
|
return context
|
2015-01-12 13:28:11 +01:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('combo-manager-homepage')
|
|
|
|
|
|
|
|
page_delete = PageDeleteView.as_view()
|
|
|
|
|
|
|
|
|
2014-12-30 15:43:52 +01:00
|
|
|
class PageExportView(DetailView):
|
|
|
|
model = Page
|
|
|
|
|
|
|
|
def render_to_response(self, context, **response_kwargs):
|
|
|
|
response = HttpResponse(content_type='application/json')
|
2017-06-06 13:34:00 +02:00
|
|
|
json.dump({'pages': [self.object.get_serialized_page()]}, response, indent=2)
|
2014-12-30 15:43:52 +01:00
|
|
|
return response
|
|
|
|
|
|
|
|
page_export = PageExportView.as_view()
|
|
|
|
|
2018-04-01 15:23:01 +02:00
|
|
|
|
|
|
|
class PageHistoryView(ListView):
|
|
|
|
model = PageSnapshot
|
|
|
|
template_name = 'combo/page_history.html'
|
|
|
|
paginate_by = 20
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
self.page = Page.objects.get(id=self.kwargs['pk'])
|
|
|
|
return self.page.pagesnapshot_set.all()
|
|
|
|
|
|
|
|
page_history = PageHistoryView.as_view()
|
|
|
|
|
|
|
|
|
2018-04-02 12:07:34 +02:00
|
|
|
class SnapshotRestoreView(DetailView):
|
|
|
|
http_method_names = ['get', 'post']
|
|
|
|
model = PageSnapshot
|
|
|
|
template_name = 'combo/snapshot_restore.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(SnapshotRestoreView, self).get_context_data(**kwargs)
|
|
|
|
context['page'] = Page.objects.get(id=self.kwargs['page_pk'])
|
|
|
|
return context
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
snapshot = self.get_object()
|
|
|
|
datetime_str = date_format(localtime(snapshot.timestamp), format='DATETIME_FORMAT')
|
|
|
|
page = self.get_object().restore()
|
|
|
|
PageSnapshot.take(page, request=self.request,
|
|
|
|
comment=_('restored snapshot from %s') % datetime_str)
|
|
|
|
messages.info(self.request,
|
|
|
|
_('Snapshot from %s has been restored.') % datetime_str)
|
|
|
|
return HttpResponseRedirect(reverse('combo-manager-page-view', kwargs={'pk': self.kwargs['page_pk']}))
|
|
|
|
|
|
|
|
snapshot_restore = SnapshotRestoreView.as_view()
|
|
|
|
|
|
|
|
|
2014-12-07 17:36:21 +01:00
|
|
|
class PageAddCellView(RedirectView):
|
2015-03-27 10:15:01 +01:00
|
|
|
permanent = False
|
|
|
|
|
2014-12-13 11:25:53 +01:00
|
|
|
def get_redirect_url(self, page_pk, cell_type, variant, ph_key):
|
2016-02-07 00:25:34 +01:00
|
|
|
cell_class = get_cell_class(cell_type)
|
2014-12-08 10:20:23 +01:00
|
|
|
cell = cell_class(page_id=page_pk, placeholder=ph_key)
|
2014-12-13 11:25:53 +01:00
|
|
|
cell.set_variant(variant)
|
2014-12-24 07:34:52 +01:00
|
|
|
orders = [x.order for x in CellBase.get_cells(page_id=page_pk)]
|
2014-12-07 17:36:21 +01:00
|
|
|
if orders:
|
|
|
|
cell.order = max(orders)+1
|
|
|
|
else:
|
|
|
|
cell.order = 1
|
|
|
|
cell.save()
|
2018-04-01 15:23:01 +02:00
|
|
|
PageSnapshot.take(cell.page, request=self.request, comment=_('added cell "%s"') % cell)
|
2019-05-18 14:28:51 +02:00
|
|
|
return reverse('combo-manager-page-view', kwargs={'pk': page_pk}) + '#cell-' + cell.get_reference()
|
2014-12-07 17:36:21 +01:00
|
|
|
|
|
|
|
page_add_cell = PageAddCellView.as_view()
|
2014-12-07 20:53:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PageEditCellView(UpdateView):
|
2019-03-28 17:53:02 +01:00
|
|
|
def get_template_names(self):
|
|
|
|
return [self.template_name or self.object.manager_form_template]
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(PageEditCellView, self).get_context_data(**kwargs)
|
|
|
|
context['cell'] = self.get_object()
|
|
|
|
return context
|
2015-12-05 21:53:57 +01:00
|
|
|
|
2014-12-07 20:53:16 +01:00
|
|
|
def get_object(self, queryset=None):
|
|
|
|
page_pk = self.kwargs.get('page_pk')
|
2014-12-24 07:34:52 +01:00
|
|
|
cell_reference = self.kwargs.get('cell_reference')
|
2014-12-07 20:53:16 +01:00
|
|
|
try:
|
2014-12-24 07:34:52 +01:00
|
|
|
return CellBase.get_cell(cell_reference, page_id=page_pk)
|
|
|
|
except ObjectDoesNotExist:
|
2014-12-07 20:53:16 +01:00
|
|
|
raise Http404()
|
|
|
|
|
|
|
|
def get_prefix(self):
|
2014-12-24 07:34:52 +01:00
|
|
|
return 'c%s' % self.kwargs.get('cell_reference')
|
2014-12-07 20:53:16 +01:00
|
|
|
|
|
|
|
def get_form_class(self):
|
2014-12-24 07:34:52 +01:00
|
|
|
return self.object.get_default_form_class()
|
2014-12-07 20:53:16 +01:00
|
|
|
|
|
|
|
def get_success_url(self):
|
2019-05-18 14:28:51 +02:00
|
|
|
return reverse('combo-manager-page-view', kwargs={'pk': self.kwargs.get('page_pk')}
|
|
|
|
) + '#cell-' + self.object.get_reference()
|
2014-12-07 20:53:16 +01:00
|
|
|
|
2015-12-05 21:53:57 +01:00
|
|
|
def form_valid(self, form):
|
|
|
|
if self.request.is_ajax():
|
|
|
|
self.object = form.save()
|
2018-04-01 15:23:01 +02:00
|
|
|
response = self.form_invalid(form) # avoid redirection
|
|
|
|
else:
|
|
|
|
response = super(PageEditCellView, self).form_valid(form)
|
|
|
|
PageSnapshot.take(self.object.page, request=self.request, comment=_('changed cell "%s"') % self.object)
|
|
|
|
return response
|
2015-12-05 21:53:57 +01:00
|
|
|
|
2014-12-07 20:53:16 +01:00
|
|
|
page_edit_cell = PageEditCellView.as_view()
|
2014-12-08 15:32:30 +01:00
|
|
|
|
|
|
|
|
2014-12-08 16:55:13 +01:00
|
|
|
class PageDeleteCellView(DeleteView):
|
|
|
|
template_name = 'combo/generic_confirm_delete.html'
|
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
page_pk = self.kwargs.get('page_pk')
|
2014-12-24 07:34:52 +01:00
|
|
|
cell_reference = self.kwargs.get('cell_reference')
|
2014-12-08 16:55:13 +01:00
|
|
|
try:
|
2014-12-24 07:34:52 +01:00
|
|
|
return CellBase.get_cell(cell_reference, page_id=page_pk)
|
|
|
|
except ObjectDoesNotExist:
|
2014-12-08 16:55:13 +01:00
|
|
|
raise Http404()
|
|
|
|
|
2018-04-01 15:23:01 +02:00
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
cell = self.get_object()
|
|
|
|
comment = _('removed cell "%s"') % cell
|
|
|
|
page = cell.page
|
|
|
|
response = super(PageDeleteCellView, self).delete(request, *args, **kwargs)
|
|
|
|
PageSnapshot.take(page, request=self.request, comment=comment)
|
|
|
|
return response
|
|
|
|
|
2014-12-08 16:55:13 +01:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('combo-manager-page-view', kwargs={'pk': self.kwargs.get('page_pk')})
|
|
|
|
|
|
|
|
page_delete_cell = PageDeleteCellView.as_view()
|
|
|
|
|
|
|
|
|
2015-01-13 10:41:46 +01:00
|
|
|
class PageCellVisibilityView(PageEditCellView):
|
|
|
|
template_name = 'combo/cell_visibility.html'
|
|
|
|
|
|
|
|
def get_form_class(self):
|
2018-10-28 13:00:43 +01:00
|
|
|
return CellVisibilityForm
|
2015-01-13 10:41:46 +01:00
|
|
|
|
|
|
|
page_cell_visibility = PageCellVisibilityView.as_view()
|
|
|
|
|
|
|
|
|
2015-02-17 15:00:09 +01:00
|
|
|
class PageCellOptionsView(PageEditCellView):
|
|
|
|
template_name = 'combo/cell_options.html'
|
|
|
|
|
|
|
|
def get_form_class(self):
|
|
|
|
return self.object.get_options_form_class()
|
|
|
|
|
|
|
|
page_cell_options = PageCellOptionsView.as_view()
|
|
|
|
|
|
|
|
|
2014-12-08 15:32:30 +01:00
|
|
|
def cell_order(request, page_pk):
|
2018-04-01 15:23:01 +02:00
|
|
|
has_changes = False
|
2014-12-24 07:34:52 +01:00
|
|
|
for cell in CellBase.get_cells(page_id=page_pk):
|
2014-12-08 15:32:30 +01:00
|
|
|
old_order = cell.order
|
|
|
|
old_placeholder = cell.placeholder
|
2014-12-24 07:34:52 +01:00
|
|
|
key_suffix = cell.get_reference()
|
2015-05-21 07:57:44 +02:00
|
|
|
try:
|
|
|
|
new_order = int(request.GET.get('pos_' + key_suffix))
|
|
|
|
except TypeError:
|
|
|
|
# the cell is not present in the query string, most probably
|
|
|
|
# because it's in a different placeholder
|
|
|
|
continue
|
2014-12-24 07:34:52 +01:00
|
|
|
new_placeholder = request.GET.get('ph_' + key_suffix)
|
2014-12-08 15:32:30 +01:00
|
|
|
if new_order != old_order or new_placeholder != old_placeholder:
|
|
|
|
cell.order = new_order
|
|
|
|
cell.placeholder = new_placeholder
|
2018-04-01 15:23:01 +02:00
|
|
|
has_changes = True
|
2014-12-08 15:32:30 +01:00
|
|
|
cell.save()
|
2018-04-01 15:23:01 +02:00
|
|
|
if has_changes:
|
|
|
|
page = Page.objects.get(id=page_pk)
|
|
|
|
PageSnapshot.take(page, request=request, comment=_('reordered cells'))
|
2017-06-29 20:21:56 +02:00
|
|
|
return HttpResponse(status=204)
|
2014-12-16 15:17:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def page_order(request):
|
|
|
|
new_order = [int(x) for x in request.GET['new-order'].split(',')]
|
2015-01-17 14:13:58 +01:00
|
|
|
moved_page = Page.objects.get(id=request.GET['moved-page-id'])
|
2018-08-08 17:21:15 +02:00
|
|
|
current_parent_id = moved_page.parent_id
|
2014-12-16 15:17:15 +01:00
|
|
|
if request.GET['moved-page-new-parent']:
|
2015-01-17 14:13:58 +01:00
|
|
|
moved_page.parent_id = request.GET['moved-page-new-parent']
|
2014-12-16 15:17:15 +01:00
|
|
|
else:
|
|
|
|
moved_page.parent_id = None
|
|
|
|
moved_page.save()
|
2018-08-08 17:21:15 +02:00
|
|
|
slug_conflict = False
|
2014-12-16 15:17:15 +01:00
|
|
|
for page in Page.objects.filter(parent_id=moved_page.parent_id):
|
|
|
|
page.order = new_order.index(page.id)+1
|
|
|
|
page.save()
|
2018-08-08 17:21:15 +02:00
|
|
|
if moved_page.id != page.id and moved_page.slug == page.slug:
|
|
|
|
slug_conflict = True
|
|
|
|
if slug_conflict:
|
|
|
|
# slug conflict after a page got moved, reload and rename
|
|
|
|
moved_page = Page.objects.get(id=request.GET['moved-page-id'])
|
2019-08-18 12:18:16 +02:00
|
|
|
moved_page.slug = moved_page.slug + '-' + hashlib.md5(force_bytes(moved_page.id)).hexdigest()[:4]
|
2018-08-08 17:21:15 +02:00
|
|
|
moved_page.save()
|
2015-01-12 13:24:38 +01:00
|
|
|
return redirect(reverse('combo-manager-homepage'))
|
2015-03-15 16:35:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def page_get_additional_label(request, page_pk, cell_reference):
|
|
|
|
cell = CellBase.get_cell(cell_reference, page_id=page_pk)
|
|
|
|
response = HttpResponse(content_type='application/json')
|
2015-12-05 17:15:07 +01:00
|
|
|
json.dump({'label': force_text(cell.get_additional_label()) or ''}, response)
|
2015-03-15 16:35:05 +01:00
|
|
|
return response
|
2015-04-18 21:04:23 +02:00
|
|
|
|
|
|
|
|
2015-05-06 10:31:07 +02:00
|
|
|
def menu_json(request):
|
2017-02-03 14:52:17 +01:00
|
|
|
if settings.TEMPLATE_VARS.get('site_title'):
|
2017-02-24 12:08:54 +01:00
|
|
|
label = _('Editing "%(site_title)s"') % settings.TEMPLATE_VARS
|
2015-05-06 10:31:07 +02:00
|
|
|
else:
|
|
|
|
label = _('Content Management')
|
2018-11-17 15:52:47 +01:00
|
|
|
|
|
|
|
slug = 'portal'
|
|
|
|
if getattr(settings, 'KNOWN_SERVICES') and 'combo' in settings.KNOWN_SERVICES:
|
|
|
|
# switch to custom slug if the site is the portal agent.
|
|
|
|
for service_slug, service in settings.KNOWN_SERVICES['combo'].items():
|
|
|
|
if service.get('is-portal-agent') and not service.get('secret'):
|
|
|
|
slug = 'portal-agent'
|
|
|
|
break
|
|
|
|
|
2015-05-06 10:31:07 +02:00
|
|
|
json_str = json.dumps([{'label': force_text(label),
|
2018-11-17 15:52:47 +01:00
|
|
|
'slug': slug,
|
2015-05-06 10:31:07 +02:00
|
|
|
'url': request.build_absolute_uri(reverse('combo-manager-homepage'))
|
|
|
|
}])
|
2017-01-19 16:59:27 +01:00
|
|
|
content_type = 'application/json'
|
2015-05-06 10:31:07 +02:00
|
|
|
for variable in ('jsonpCallback', 'callback'):
|
|
|
|
if variable in request.GET:
|
|
|
|
json_str = '%s(%s);' % (request.GET[variable], json_str)
|
2017-01-19 16:59:27 +01:00
|
|
|
content_type = 'application/javascript'
|
2015-05-06 10:31:07 +02:00
|
|
|
break
|
2017-01-19 16:59:27 +01:00
|
|
|
response = HttpResponse(content_type=content_type)
|
2015-05-06 10:31:07 +02:00
|
|
|
response.write(json_str)
|
|
|
|
return response
|