kopia lustrzana https://github.com/rtts/django-simplecms
I can't believe it actually works!
rodzic
c0464a5ab6
commit
25770feb57
|
@ -249,13 +249,10 @@ section {
|
|||
}
|
||||
}
|
||||
|
||||
section.contactsection {
|
||||
section.contact {
|
||||
div#message {
|
||||
display: none;
|
||||
}
|
||||
textarea, input {
|
||||
font-family: inherit;
|
||||
}
|
||||
}
|
||||
|
||||
/* Form elements */
|
||||
|
|
|
@ -176,12 +176,9 @@ section {
|
|||
text-align: center;
|
||||
padding: 1em 0; }
|
||||
|
||||
section.contactsection div#message {
|
||||
section.contact div#message {
|
||||
display: none; }
|
||||
|
||||
section.contactsection textarea, section.contactsection input {
|
||||
font-family: inherit; }
|
||||
|
||||
/* Form elements */
|
||||
form.cms section {
|
||||
margin-top: 3em; }
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -11,7 +11,7 @@
|
|||
|
||||
{% if user.is_staff %}
|
||||
<div class="edit page">
|
||||
<a href="{% if page.slug %}{% url 'cms:editpage' page.slug %}{% else %}{% url 'cms:editpage' %}{% endif %}"><img src="{% static 'cms/edit.svg' %}"></a>
|
||||
<a href="{% if page.slug %}{% url 'cms:updatepage' page.slug %}{% else %}{% url 'cms:updatepage' %}{% endif %}"><img src="{% static 'cms/edit.svg' %}"></a>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
from django.urls import path
|
||||
from .views import PageView, EditPage, CreatePage
|
||||
from .views import PageView, CreatePage, UpdatePage
|
||||
|
||||
app_name = 'cms'
|
||||
|
||||
urlpatterns = [
|
||||
path('edit/', EditPage.as_view(), name='editpage'),
|
||||
path('<slug:slug>/edit/', EditPage.as_view(), name='editpage'),
|
||||
path('createpage/', CreatePage.as_view(), name='createpage'),
|
||||
path('new/', CreatePage.as_view(), name='createpage'),
|
||||
path('edit/', UpdatePage.as_view(), name='updatepage'),
|
||||
path('<slug:slug>/edit/', UpdatePage.as_view(), name='updatepage'),
|
||||
path('', PageView.as_view(), name='page'),
|
||||
path('<slug:slug>/', PageView.as_view(), name='page'),
|
||||
]
|
||||
|
|
76
cms/views.py
76
cms/views.py
|
@ -1,16 +1,13 @@
|
|||
import json
|
||||
import swapper
|
||||
|
||||
from django.views import generic
|
||||
from django.http import Http404
|
||||
from django.views.generic import base, detail, edit
|
||||
from django.http import Http404, HttpResponseRedirect
|
||||
from django.shortcuts import redirect
|
||||
from django.views.generic.edit import FormMixin
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
from django.contrib.admin.utils import NestedObjects
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib.auth.mixins import UserPassesTestMixin
|
||||
from django.http import HttpResponseRedirect, HttpResponseBadRequest
|
||||
#from django.http import HttpResponseRedirect, HttpResponseBadRequest
|
||||
|
||||
from .decorators import register_view
|
||||
from .forms import PageForm, SectionForm, SectionFormSet
|
||||
|
@ -32,7 +29,7 @@ class SectionView:
|
|||
'''Override this to customize a section's context'''
|
||||
return kwargs
|
||||
|
||||
class SectionFormView(FormMixin, SectionView):
|
||||
class SectionFormView(edit.FormMixin, SectionView):
|
||||
'''Generic section with associated form'''
|
||||
|
||||
def post(self, request):
|
||||
|
@ -44,7 +41,7 @@ class SectionFormView(FormMixin, SectionView):
|
|||
return form
|
||||
|
||||
class SectionFormSetView(SectionView):
|
||||
'''Generic section with associated form'''
|
||||
'''Generic section with associated formset'''
|
||||
|
||||
formset_class = None
|
||||
|
||||
|
@ -65,7 +62,7 @@ class SectionFormSetView(SectionView):
|
|||
kwargs['formset'] = self.get_formset()
|
||||
return super().get_context_data(**kwargs)
|
||||
|
||||
class PageView(generic.DetailView):
|
||||
class PageView(detail.DetailView):
|
||||
'''View of a page with heterogeneous (polymorphic) sections'''
|
||||
model = Page
|
||||
template_name = 'cms/page.html'
|
||||
|
@ -88,7 +85,7 @@ class PageView(generic.DetailView):
|
|||
page = self.object = self.get_object()
|
||||
except Http404:
|
||||
if self.request.user.has_perm('cms_page_create'):
|
||||
return redirect('cms:editpage', self.kwargs['slug'])
|
||||
return redirect('cms:updatepage', self.kwargs['slug'])
|
||||
else:
|
||||
raise
|
||||
context = self.get_context_data(**kwargs)
|
||||
|
@ -134,34 +131,26 @@ class PageView(generic.DetailView):
|
|||
})
|
||||
return context
|
||||
|
||||
class EditPage(UserPassesTestMixin, generic.UpdateView):
|
||||
class EditPage(UserPassesTestMixin, edit.ModelFormMixin, base.TemplateResponseMixin, base.View):
|
||||
model = Page
|
||||
form_class = PageForm
|
||||
template_name = 'cms/edit.html'
|
||||
|
||||
def test_func(self):
|
||||
return self.request.user.has_perm('cms_page_change')
|
||||
|
||||
def setup(self, *args, slug='', **kwargs):
|
||||
'''Supply a default argument for slug'''
|
||||
super().setup(*args, slug=slug, **kwargs)
|
||||
|
||||
def test_func(self):
|
||||
return self.request.user.has_perm('cms_page_change')
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super().get_form_kwargs()
|
||||
kwargs.update({'label_suffix': ''})
|
||||
kwargs.update({
|
||||
'label_suffix': '',
|
||||
'initial': {'slug': self.kwargs['slug']},
|
||||
})
|
||||
return kwargs
|
||||
|
||||
def form_valid(self, form):
|
||||
object = form.save()
|
||||
return redirect(object.get_absolute_url())
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
try:
|
||||
page = self.object = self.get_object()
|
||||
except Http404:
|
||||
return CreatePage.as_view()(request, slug=self.kwargs['slug'])
|
||||
return super().get(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
if 'formset' not in context:
|
||||
|
@ -179,23 +168,30 @@ class EditPage(UserPassesTestMixin, generic.UpdateView):
|
|||
})
|
||||
return context
|
||||
|
||||
def get_object(self):
|
||||
try:
|
||||
return super().get_object()
|
||||
except:
|
||||
return None
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
self.object = self.get_object()
|
||||
return self.render_to_response(self.get_context_data())
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
self.object = self.get_object()
|
||||
form = self.get_form()
|
||||
formset = SectionFormSet(request.POST, request.FILES, instance=self.object)
|
||||
if form.is_valid() and formset.is_valid():
|
||||
formset.save()
|
||||
return self.form_valid(form)
|
||||
else:
|
||||
return self.form_invalid(form, formset)
|
||||
|
||||
def form_invalid(self, form, formset):
|
||||
if form.is_valid():
|
||||
page = form.save()
|
||||
formset = SectionFormSet(request.POST, request.FILES, instance=page)
|
||||
if formset.is_valid():
|
||||
formset.save()
|
||||
return HttpResponseRedirect(page.get_absolute_url())
|
||||
return self.render_to_response(self.get_context_data(form=form, formset=formset))
|
||||
|
||||
class CreatePage(generic.CreateView):
|
||||
model = Page
|
||||
form_class = PageForm
|
||||
template_name = 'cms/edit.html'
|
||||
class CreatePage(EditPage):
|
||||
def get_object(self):
|
||||
pass
|
||||
|
||||
def get_form_kwargs(self, *args, **kwargs):
|
||||
return {'initial': {'slug': self.kwargs['slug']}}
|
||||
class UpdatePage(EditPage):
|
||||
pass
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
<section class="{{section.type}} color{{section.color}}">
|
||||
<section class="button">
|
||||
<div class="wrapper">
|
||||
{% if section.button_text %}
|
||||
<div class="button">
|
||||
<a class="button" href="{{section.button_link}}">{{section.button_text}}</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="button">
|
||||
<a class="button" href="{{section.href}}">{{section.title}}</a>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
{% load i18n %}
|
||||
<section class="{{section.type}} color{{section.color}}">
|
||||
<section class="contact">
|
||||
<div class="wrapper">
|
||||
<div class="title">
|
||||
<h1>{{section.title}}</h1>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{% load thumbnail %}
|
||||
|
||||
<section class="{{section.type}} color{{section.color}}">
|
||||
<section class="image">
|
||||
<div class="wrapper">
|
||||
{% if section.image %}
|
||||
<div class="image">
|
||||
|
|
|
@ -1,19 +1,15 @@
|
|||
{% load markdown %}
|
||||
|
||||
<section class="{{section.type}} color{{section.color}}">
|
||||
<section class="text">
|
||||
<div class="wrapper">
|
||||
{% if section.title %}
|
||||
<div class="title">
|
||||
<h1>
|
||||
{{section.title}}
|
||||
</h1>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="title">
|
||||
<h1>
|
||||
{{section.title}}
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
{% if section.content %}
|
||||
<div class="content">
|
||||
{{section.content|markdown}}
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="content">
|
||||
{{section.content|markdown}}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{% load embed_video_tags %}
|
||||
|
||||
<section class="{{section.type}} color{{section.color}}">
|
||||
<section class="video">
|
||||
<div class="wrapper">
|
||||
{% if section.video %}
|
||||
<div class="video">
|
||||
|
|
Ładowanie…
Reference in New Issue