general: add initial placeholder support

This commit is contained in:
Frédéric Péters 2014-12-08 10:20:23 +01:00
parent 144a20d59b
commit 65b198f953
10 changed files with 79 additions and 18 deletions

View File

@ -31,6 +31,7 @@ class Page(models.Model):
class CellBase(models.Model):
page = models.ForeignKey(Page)
placeholder = models.CharField(max_length=20)
order = models.PositiveIntegerField()
objects = InheritanceManager()

View File

@ -8,19 +8,31 @@
{% block content %}
<div id="available-cells">
<h2>Available cells</h2>
<ul>
{% for cell_type in cell_types %}
<li><a href="{% url 'combo-manager-page-add-cell' page_pk=object.id cell_type=cell_type.content_type.id %}">{{ cell_type.name }}</a></li>
<li>{{ cell_type.name }}
{% for placeholder in placeholders %}
<a href="{% url 'combo-manager-page-add-cell' page_pk=object.id cell_type=cell_type.content_type.id ph_key=placeholder.key %}">→ {{ placeholder.key }}</a>
{% endfor %}
</li>
{% endfor %}
</ul>
</div>
<h2>Configured cells</h2>
<div class="cell-list">
{% for cell in cells %}
<div>
<h3>{{ cell }}</h3>
<div>{% cell_form cell %}</div>
<div id="placeholders">
{% for placeholder in placeholders %}
<div data-placeholder-key="{{ placeholder.key }}">
<h2>{{ placeholder.name }}</h2>
<div class="cell-list">
{% for cell in placeholder.cells %}
<div>
<h3>{{ cell }}</h3>
<div>{% cell_form cell %}</div>
</div>
{% endfor %}
</div>
</div>
{% endfor %}
</div>

View File

@ -25,7 +25,7 @@ urlpatterns = patterns('combo.views',
url(r'^pages/add/$', views.page_add, name='combo-manager-page-add'),
url(r'^pages/(?P<pk>\w+)/$', views.page_view,
name='combo-manager-page-view'),
url(r'^pages/(?P<page_pk>\w+)/add-cell/(?P<cell_type>\w+)/$', views.page_add_cell,
url(r'^pages/(?P<page_pk>\w+)/add-cell-to-(?P<ph_key>\w+)/(?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'),

View File

@ -14,6 +14,7 @@
# 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.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.forms import models as model_forms
@ -52,17 +53,27 @@ class PageView(DetailView):
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
context['cell_types'] = CellBase.get_cell_content_types()
context['cells'] = CellBase.objects.filter(page_id=self.object.id
cells = CellBase.objects.filter(page_id=self.object.id
).order_by('order').select_subclasses()
template = 'standard'
placeholders = []
combo_template = settings.COMBO_PUBLIC_TEMPLATES.get(template)
for placeholder_key, placeholder in combo_template['placeholders'].items():
placeholders.append({
'key': placeholder_key,
'name': placeholder['name'],
'cells': [x for x in cells if x.placeholder == placeholder_key],
})
context['placeholders'] = placeholders
return context
page_view = requires_csrf_token(PageView.as_view())
class PageAddCellView(RedirectView):
def get_redirect_url(self, page_pk, cell_type):
def get_redirect_url(self, page_pk, cell_type, ph_key):
cell_class = ContentType.objects.get(id=cell_type).model_class()
cell = cell_class(page_id=page_pk)
cell = cell_class(page_id=page_pk, placeholder=ph_key)
orders = [x.order for x in CellBase.objects.filter(page_id=page_pk)]
if orders:
cell.order = max(orders)+1

View File

@ -1,13 +1,11 @@
<!DOCTYPE html>
{% load combo %}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Combo</title>
</head>
<body>
<h1>{{ title }}</h1>
{% for cell in cells %}
<div>{{ cell.render }}</div>
{% endfor %}
<h1>{{ page.title }}</h1>
{% placeholder "content" %}
</body>
</html>

View File

@ -0,0 +1,3 @@
{% for cell in cells %}
<div>{{ cell.render }}</div>
{% endfor %}

View File

View File

@ -0,0 +1,25 @@
# 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
register = template.Library()
@register.inclusion_tag('combo/placeholder.html', takes_context=True)
def placeholder(context, placeholder_name):
context['cells'] = [x for x in context['page_cells'] if
x.placeholder == placeholder_name]
return context

View File

@ -24,8 +24,8 @@ def page(request):
parts = ['index']
page = get_object_or_404(Page, slug=parts[0])
ctx = {
'title': page.title,
'cells': CellBase.objects.filter(page_id=page.id
'page': page,
'page_cells': CellBase.objects.filter(page_id=page.id
).order_by('order').select_subclasses()
}
return render(request, 'combo/page_template.html', ctx)

View File

@ -93,3 +93,14 @@ USE_TZ = True
STATIC_URL = '/static/'
CKEDITOR_UPLOAD_PATH = os.path.join(BASE_DIR, 'uploads/ckeditor')
COMBO_PUBLIC_TEMPLATES = {
'standard': {
'template': 'page_template.html',
'placeholders': {
'content': {
'name': 'Content',
}
}
}
}