manager: make it possible to edit cells

This commit is contained in:
Frédéric Péters 2014-12-07 20:53:16 +01:00
parent 6d005bf533
commit 4a00c1aa8b
8 changed files with 93 additions and 6 deletions

View File

@ -0,0 +1,12 @@
<form action="{{ url }}" method="post">
{% csrf_token %}
{% if form %}
{{ form.as_p }}
{% else %}
<p>There are no options for this cell</p>
{% endif %}
<a href="#">Delete</a> | <a href="#">Close</a>
{% if form %}
<button>Save</button>
{% endif %}
</form>

View File

@ -1,6 +1,11 @@
{% extends "gadjo/base.html" %}
{% load staticfiles %}
{% block page-title %}Combo{% endblock %}
{% block site-title %}Combo{% endblock %}
{% block footer %}Combo — Copyright © Entr'ouvert{% endblock %}
{% block extrascripts %}
<script src="{% static "ckeditor/ckeditor/ckeditor.js" %}"></script>
<script type="text/javascript" src="{% static "ckeditor/ckeditor-init.js" %}"></script>
{% endblock %}

View File

@ -1,5 +1,6 @@
{% extends "combo/manager_base.html" %}
{% load i18n %}
{% load cells %}
{% block appbar %}
<h2>Page - {{ object.title }}</h2>
@ -15,11 +16,13 @@
</ul>
<h2>Configured cells</h2>
<ul>
<div class="cell-list">
{% for cell in cells %}
<li>{{ cell }}</li>
<div>
<h3>{{ cell }}</h3>
<div>{% cell_form cell %}</div>
</div>
{% endfor %}
</ul>
</ul>
</div>
{% endblock %}

View File

View File

@ -0,0 +1,35 @@
# 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/>.
from django import template
from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.forms import models as model_forms
register = template.Library()
@register.simple_tag(takes_context=True)
def cell_form(context, cell):
context['url'] = reverse('combo-manager-page-edit-cell', kwargs={
'page_pk': cell.page.id, 'cell_pk': cell.id})
fields = [x.name for x in cell._meta.local_concrete_fields if x.name != 'cellbase_ptr']
if fields:
form_class = model_forms.modelform_factory(cell.__class__, fields=fields)
context['form'] = form_class(instance=cell, prefix='c%s' % cell.id)
else:
context['form'] = None
cell_form_template = template.loader.get_template('combo/cell_form.html')
return cell_form_template.render(context)

View File

@ -27,4 +27,7 @@ urlpatterns = patterns('combo.views',
name='combo-manager-page-view'),
url(r'^pages/(?P<page_pk>\w+)/add-cell/(?P<cell_type>\w+)/$', views.page_add_cell,
name='combo-manager-page-add-cell'),
url(r'^pages/(?P<page_pk>\w+)/cell/(?P<cell_pk>\w+)/$', views.page_edit_cell,
name='combo-manager-page-edit-cell'),
(r'^ckeditor/', include('ckeditor.urls')),
)

View File

@ -16,8 +16,11 @@
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.forms import models as model_forms
from django.http import Http404
from django.views.decorators.csrf import requires_csrf_token
from django.views.generic import (TemplateView, RedirectView, DetailView,
CreateView, ListView)
CreateView, UpdateView, ListView)
from combo.data.models import Page, CellBase
@ -53,7 +56,7 @@ class PageView(DetailView):
).order_by('order').select_subclasses()
return context
page_view = PageView.as_view()
page_view = requires_csrf_token(PageView.as_view())
class PageAddCellView(RedirectView):
@ -69,3 +72,26 @@ class PageAddCellView(RedirectView):
return reverse('combo-manager-page-view', kwargs={'pk': page_pk})
page_add_cell = PageAddCellView.as_view()
class PageEditCellView(UpdateView):
def get_object(self, queryset=None):
page_pk = self.kwargs.get('page_pk')
cell_pk = self.kwargs.get('cell_pk')
try:
return CellBase.objects.get_subclass(id=cell_pk, page_id=page_pk)
except CellBase.DoesNotExist:
raise Http404()
def get_prefix(self):
return 'c%s' % self.kwargs.get('cell_pk')
def get_form_class(self):
cell = self.object
fields = [x.name for x in cell._meta.local_concrete_fields if x.name != 'cellbase_ptr']
return model_forms.modelform_factory(cell.__class__, fields=fields)
def get_success_url(self):
return reverse('combo-manager-page-view', kwargs={'pk': self.kwargs.get('page_pk')})
page_edit_cell = PageEditCellView.as_view()

View File

@ -3,3 +3,6 @@ from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^manage/', include('combo.manager.urls')),
)
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()