Pages statiques

This commit is contained in:
Hugo LEVY-FALK 2018-03-11 23:25:59 +01:00
parent c8e0abb073
commit 0f25a87bf6
6 changed files with 136 additions and 5 deletions

View file

@ -65,6 +65,33 @@
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>
<h2>Pages statiques</h2>
<a class="btn btn-success btn-sm" role="button" href="{% url 'settings:staticpage-new' %}">
<i class="fas fa-plus"></i>
Créer une nouvelle page statique
</a>
<br/>
<br/>
<table class="table table-striped">
<tr>
<th>Nom</th>
<th></th>
</tr>
{% for p in static_pages %}
<tr>
<td><a href="{% url 'settings:staticpage' p.pk %}">{{p.name}}</a></td>
<td><a class="btn btn-outline-primary btn-sm" href="{% url 'settings:staticpage-edit' p.pk %}">
<i class="fas fa-edit"></i>
Éditer
</a>
<a class="btn btn-outline-danger btn-sm" title="Supprimer" href="{% url 'settings:staticpage-delete' p.pk %}">
<i class="fas fa-trash-alt"></i>
Supprimer
</a>
</td>
</tr>
{% endfor %}
</table>
<h2>Écoles</h2> <h2>Écoles</h2>
<a class="btn btn-success btn-sm" role="button" href="{% url 'users:new-school' %}"> <a class="btn btn-success btn-sm" role="button" href="{% url 'users:new-school' %}">

View file

@ -0,0 +1,5 @@
{% extends "base.html" %}
{% block content %}
<h1>{{object.name}}</h1>
{{object.text|safe}}
{% endblock %}

View file

@ -1,5 +1,5 @@
from django import template from django import template
from settings.models import SiteSettings from settings.models import SiteSettings, StaticPage
register = template.Library() register = template.Library()
@ -9,7 +9,18 @@ def load_site_settings(parser, token):
return LoadSiteSettingsNode() return LoadSiteSettingsNode()
@register.tag('load_static_pages')
def load_static_pages(parser, token):
return LoadStaticPagesNode()
class LoadSiteSettingsNode(template.Node): class LoadSiteSettingsNode(template.Node):
def render(self, context): def render(self, context):
context['site_settings'] = SiteSettings.get_settings() context['site_settings'] = SiteSettings.get_settings()
return '' return ''
class LoadStaticPagesNode(template.Node):
def render(self, context):
context['static_pages'] = StaticPage.objects.all()
return ''

View file

@ -1,5 +1,5 @@
from django.urls import path from django.urls import path
from .views import SettingsView, EditSiteSettingsView, degrade_user, promote_user from .views import SettingsView, EditSiteSettingsView, degrade_user, promote_user, CreateStaticPageView, StaticPageView, DeleteStaticPageView, EditStaticPageView
app_name = 'settings' app_name = 'settings'
urlpatterns = [ urlpatterns = [
@ -22,6 +22,25 @@ urlpatterns = [
'promote_user', 'promote_user',
promote_user, promote_user,
name='promote-user', name='promote-user',
),
path(
'static_page/new',
CreateStaticPageView.as_view(),
name='staticpage-new'
),
path(
'static_page/<int:pk>',
StaticPageView.as_view(),
name='staticpage'
),
path(
'static_page/<int:pk>/delete',
DeleteStaticPageView.as_view(),
name='staticpage-delete'
),
path(
'static_page/<int:pk>/edit',
EditStaticPageView.as_view(),
name='staticpage-edit'
) )
] ]

View file

@ -1,4 +1,4 @@
from django.views.generic import TemplateView, UpdateView from django.views.generic import TemplateView, UpdateView, CreateView, DetailView, DeleteView
from django.urls import reverse_lazy, reverse from django.urls import reverse_lazy, reverse
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
from django.contrib.admin.views.decorators import staff_member_required from django.contrib.admin.views.decorators import staff_member_required
@ -8,7 +8,7 @@ from django.contrib import messages
from content.models import Category from content.models import Category
from users.models import School from users.models import School
from .models import SiteSettings from .models import SiteSettings, StaticPage
from .forms import SelectUserForm from .forms import SelectUserForm
@ -23,8 +23,10 @@ class SettingsView(LoginRequiredMixin, PermissionRequiredMixin, TemplateView):
context['schools'] = School.objects.all() context['schools'] = School.objects.all()
context['settings'] = True context['settings'] = True
context['administrators'] = User.objects.filter(is_staff=True) context['administrators'] = User.objects.filter(is_staff=True)
context['static_pages'] = StaticPage.objects.all()
return context return context
class EditSiteSettingsView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView): class EditSiteSettingsView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
template_name = "edit.html" template_name = "edit.html"
model = SiteSettings model = SiteSettings
@ -42,6 +44,66 @@ class EditSiteSettingsView(LoginRequiredMixin, PermissionRequiredMixin, UpdateVi
return context return context
class CreateStaticPageView(LoginRequiredMixin, CreateView):
template_name = "edit.html"
model = StaticPage
fields = '__all__'
success_url = reverse_lazy('settings:index')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "Création de page statique"
context["validate"] = "Créer"
return context
@classmethod
def as_view(self, *args, **kwargs):
view = super().as_view(*args, **kwargs)
return staff_member_required(view)
class StaticPageView(DetailView):
template_name = "settings/static_page.html"
model = StaticPage
fields = '__all__'
@classmethod
def as_view(self, *args, **kwargs):
view = super().as_view(*args, **kwargs)
return staff_member_required(view)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['page'] = self.object
return context
class DeleteStaticPageView(DeleteView):
template_name = "confirm_delete.html"
model = StaticPage
success_url = reverse_lazy('settings:index')
@classmethod
def as_view(self, *args, **kwargs):
view = super().as_view(*args, **kwargs)
return staff_member_required(view)
class EditStaticPageView(UpdateView):
template_name = "edit.html"
model = StaticPage
success_url = reverse_lazy('settings:index')
fields = '__all__'
@classmethod
def as_view(self, *args, **kwargs):
view = super().as_view(*args, **kwargs)
return staff_member_required(view)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "Édition de page statique"
context["validate"] = "Éditer"
return context
@staff_member_required @staff_member_required
def degrade_user(request, pk): def degrade_user(request, pk):
user = get_object_or_404(User, pk=pk) user = get_object_or_404(User, pk=pk)

View file

@ -2,6 +2,7 @@
{% load load_settings %} {% load load_settings %}
{% load_categories %} {% load_categories %}
{% load_site_settings %} {% load_site_settings %}
{% load_static_pages %}
<nav class="navbar navbar-expand-lg navbar-light bg-light"> <nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="{% url "home"%}"> <a class="navbar-brand" href="{% url "home"%}">
{% if site_settings.site_logo %} {% if site_settings.site_logo %}
@ -20,6 +21,12 @@
<a class="nav-link" href="{% url 'content:category' c.pk %}">{{c.name}} <a class="nav-link" href="{% url 'content:category' c.pk %}">{{c.name}}
</a></li> </a></li>
{% endfor %} {% endfor %}
{% for p in static_pages %}
<li class="nav-item
{% if page.pk == p.pk %}active{%endif%}">
<a class="nav-link" href="{% url 'settings:staticpage' p.pk %}">{{p.name}}
</a></li>
{% endfor %}
</ul> </ul>
<ul class="navbar-nav ml-auto"> <ul class="navbar-nav ml-auto">