Vues d'affichage et d'édition pour les nouveaux modèles

This commit is contained in:
Hugo LEVY-FALK 2018-03-09 11:44:49 +01:00
parent 0303710d26
commit c606570a99
17 changed files with 319 additions and 102 deletions

View file

@ -0,0 +1,23 @@
# Generated by Django 2.0.1 on 2018-03-09 10:16
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('content', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='category',
name='description_short',
field=models.TextField(null=True, verbose_name='Description courte'),
),
migrations.AlterField(
model_name='category',
name='description',
field=models.TextField(default='', verbose_name='Description de la catégorie'),
),
]

View file

@ -13,8 +13,12 @@ class Category(models.Model):
max_length=255,
verbose_name="Nom de la catégorie"
)
description_short = models.TextField(
verbose_name="Description courte",
null=True,
)
description = models.TextField(
verbose_name="Descriton de la catégorie",
verbose_name="Description de la catégorie",
default=""
)
image = models.ImageField(
@ -23,7 +27,7 @@ class Category(models.Model):
)
def get_absolute_url(self):
return reverse('content:category-list', kwargs={'pk': self.pk})
return reverse('content:category', kwargs={'pk': self.pk})
def __str__(self):
return self.name

View file

@ -5,15 +5,13 @@
</video>
<div class="card-body">
<h2 class="display-5">{{content.name}}</h2>
<p class="lead">Contenu proposé par {{content.school_owner.group.name}}</p>
{% if content.manager_right in perms %}
<p class="lead">Catégorie : {{content.category.name}}</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<a class="btn btn-sm btn-outline-primary"i href="{% url "content:content-edit" content.pk %}"><i class="fa fa-edit"></i> Éditer</a>
<a class="btn btn-sm btn-outline-danger" href="{% url "content:content-delete" content.pk %}" ><i class="fa fa-trash"></i> Supprimer</a>
</div>
</div>
{% endif %}
</div>
</div>
</div>

View file

@ -22,15 +22,13 @@ $('html, body').animate({scrollTop: $('#category-content').offset().top}, 800);
<div class="position-relative overflow-hidden p-3 p-md-5 text-center bg-light page-title">
<div class="col-md-5 p-lg-5 mx-auto my-5 title-block">
<h1 class="display-4 font-weight-normal">{{category.name}}</h1>
<p class="lead font-weight-normal">{{category.description}}</p>
<p class="lead font-weight-normal">{{category.description_short}}</p>
<a class="btn btn-outline-secondary smooth-scroll" href="#category-content">Aller voir !</a>
</div>
</div>
<br />
<div class="row text-center" id="category-content">
{% for content in contents %}
{% include "content/content.html" %}
{% endfor %}
<div id="category-content">
{{category.description|safe}}
</div>
<br />
<br />

View file

@ -1,8 +1,8 @@
from django.urls import path
from .views import (
ContentCategoryList,
CreateCategory,
ViewCategory,
DeleteCategory,
EditCategory,
CreateContent,
@ -12,11 +12,6 @@ from .views import (
app_name = 'content'
urlpatterns = [
path(
'category/<int:pk>/',
ContentCategoryList.as_view(),
name='category-list'
),
path(
'category/delete/<int:pk>',
DeleteCategory.as_view(),
@ -28,7 +23,12 @@ urlpatterns = [
name='category-new'
),
path(
'category/edit/<int:pk>',
'category/<int:pk>',
ViewCategory.as_view(),
name='category',
),
path(
'category/<int:pk>/edit',
EditCategory.as_view(),
name='category-edit',
),

View file

@ -8,24 +8,11 @@ from .models import Content, Category
from settings.models import SiteSettings
class ContentCategoryList(generic.ListView):
"""Affiche les contenus d'une catégorie."""
model = Content
context_object_name = "contents"
class ViewCategory(generic.DetailView):
"""Affiche une catégorie."""
model = Category
template_name = "content/content_list.html"
def get_queryset(self):
pk = self.kwargs['pk']
category = get_object_or_404(Category, pk=pk)
return Content.objects.filter(category=category)
def get_context_data(self, **kwargs):
context = super(generic.ListView, self).get_context_data(**kwargs)
pk = self.kwargs['pk']
category = get_object_or_404(Category, pk=pk)
context['category'] = category
return context
class CreateCategory(PermissionRequiredMixin, generic.CreateView):
"""Création de catégorie."""

View file

@ -24,7 +24,7 @@
<td>{{admin.last_name}}</td>
<td>{{admin.username}}</td>
<td>
<a class="btn btn-outline-danger btn-sm" href="{% url 'settings:degrade-user' admin.pk %}">
<a class="btn btn-outline-danger btn-sm" href="{% url 'settings:degrade-user' admin.pk %}">
<i class="fa fa-trash"></i>
Enlever le privilège Administrateur
</a>
@ -76,26 +76,28 @@
<table class="table table-striped">
<tr>
<th>Nom</th>
<th>Nombre de membres</th>
<th>Nombre de contenus</th>
<th>Nombre de catégories</th>
<th>Numéro de téléphone</th>
<th></th>
<th>Inscription</th>
{% for school in schools %}
<tr>
<th><a href="{{school.get_absolute_url}}">{{school.group.name}}</a></th>
<td>{{school.group.user_set.count}}</td>
<th><a href="{{school.get_absolute_url}}">{{school.name}}</a></th>
<td>{{school.content_set.count}}</td>
<td>{{school.number_of_categories}}</td>
<td>{{school.phone}}</td>
<td><a class="btn btn-outline-primary btn-sm" href="{% url "users:edit-school-name" pk=school.group.pk%}">
<i class="fas fa-edit"></i>
Éditer
</a>
<a class="btn btn-outline-danger btn-sm" title="Supprimer" href="">
<i class="fas fa-trash-alt"></i>
Supprimer
</a>
<td>
{% if school.validated %}
<span class="badge badge-success">
<i class="fa fa-check"></i>
Inscription validée
</span>
{% else %}
<span class="badge badge-danger">
<i class="fa fa-exclamation"></i>
Inscription non validée
</span>
{% endif %}
</td>
</tr>
{% endfor %}
@ -142,9 +144,5 @@
{% endif %}
</td>
</tr>
<tr>
<th>Nombre minimal de catégories</th>
<td>{{ site_settings.min_number_of_categories }}</td>
</tr>
</table>
{% endblock %}

View file

@ -1,7 +1,7 @@
from django.views.generic import TemplateView, UpdateView
from django.urls import reverse_lazy, reverse
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
from django.contrib.auth.decorators import permission_required
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.models import Group, User
from django.shortcuts import get_object_or_404, redirect, render
from django.contrib import messages
@ -22,7 +22,7 @@ class SettingsView(LoginRequiredMixin, PermissionRequiredMixin, TemplateView):
context['site_settings'], _ = SiteSettings.objects.get_or_create()
context['schools'] = School.objects.all()
context['settings'] = True
context['administrators'] = Group.objects.get(name='admins').user_set.all()
context['administrators'] = User.objects.filter(is_staff=True)
return context
class EditSiteSettingsView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
@ -42,24 +42,22 @@ class EditSiteSettingsView(LoginRequiredMixin, PermissionRequiredMixin, UpdateVi
return context
@permission_required('auth.change_user')
@staff_member_required
def degrade_user(request, pk):
user = get_object_or_404(User, pk=pk)
admins,_ = Group.objects.get_or_create(name='admins')
user.groups.remove(admins)
user.is_staff = False
user.save()
messages.success(request, user.username + ' a été enlevé des administrateurs du site')
return redirect(reverse('settings:index'))
@permission_required('auth.change_user')
@staff_member_required
def promote_user(request):
user_form = SelectUserForm(request.POST or None)
user_form.populate()
if user_form.is_valid():
user=user_form.get_user()
admins,_ = Group.objects.get_or_create(name='admins')
user.groups.add(admins)
user.is_staff = True
user.save()
messages.success(request, user.username + ' a été ajouté des administrateurs du site')
return redirect(reverse('settings:index'))

View file

@ -17,14 +17,14 @@
{% for c in categories %}
<li class="nav-item
{% if category.pk == c.pk %}active{%endif%}">
<a class="nav-link" href="{% url 'content:category-list' c.pk %}">{{c.name}}
<a class="nav-link" href="{% url 'content:category' c.pk %}">{{c.name}}
</a></li>
{% endfor %}
</ul>
<ul class="navbar-nav ml-auto">
{% if request.user.userprofile.school %}
<li class="nav-item {% if school %}active{% endif %}"><a class="nav-link" href="{% url 'users:school' request.user.userprofile.school.group.pk %}"><i class="fas fa-graduation-cap"></i> Mon école</a></li>
{% if request.user.school %}
<li class="nav-item {% if school %}active{% endif %}"><a class="nav-link" href="{% url 'users:school' request.user.school.pk %}"><i class="fas fa-graduation-cap"></i> Mon école</a></li>
{% endif %}
<li class="nav-item {% if settings %}active{% endif %}"><a class="nav-link" href="{% url 'settings:index' %}"><i class="fas fa-cogs"></i> Administration</a></li>
{% if request.user.is_authenticated %}
@ -33,7 +33,6 @@
{{request.user}}
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{% url 'users:profile' request.user.pk%}"><i class="fa fa-user"></i> Accéder à mon profil</a>
<a class="dropdown-item" href="{% url 'users:logout' %}"><i class="fa fa-sign-out-alt"></i> Se déconnecter</a>
</div>
</li>

View file

@ -0,0 +1,6 @@
from django.contrib import admin
from .models import School
class SchoolAdmin(admin.ModelAdmin):
pass
admin.site.register(School, SchoolAdmin)

View file

@ -6,8 +6,14 @@ from .models import School
class CreateSchool(forms.ModelForm):
class Meta:
model = User
fields = '__all__'
model = School
fields = ['name', 'admin']
class EditName(forms.ModelForm):
class Meta:
model = School
fields = ['name']
class CreateUser(forms.ModelForm):

View file

@ -0,0 +1,18 @@
# Generated by Django 2.0.1 on 2018-03-09 08:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('users', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='school',
name='validated',
field=models.BooleanField(default=False, verbose_name='Inscription validé.'),
),
]

View file

@ -0,0 +1,18 @@
# Generated by Django 2.0.1 on 2018-03-09 09:07
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('users', '0002_school_validated'),
]
operations = [
migrations.AddField(
model_name='school',
name='name',
field=models.CharField(default='', max_length=255, verbose_name="Nom de l'école"),
),
]

View file

@ -13,6 +13,11 @@ def get_upload_to(instance, filename):
class School(models.Model):
"""Ajoute un champ pour distinguer les groupes écoles des autres."""
name = models.CharField(
verbose_name="Nom de l'école",
max_length=255,
default=""
)
admin = models.OneToOneField(
User,
verbose_name="Administrateur de l'école",
@ -83,10 +88,10 @@ class School(models.Model):
def __str__(self):
return self.group.name
return self.name
def get_absolute_url(self):
return reverse("users:school", kwargs={'pk':self.group.pk})
return reverse("users:school", kwargs={'pk':self.pk})
def number_of_categories(self):
return self.content_set.values('category').distinct().count()

View file

@ -2,24 +2,155 @@
{% load bootstrap4 %}
{% block content %}
<h1>{{school.name}}</h1>
{% if manager_right in perms %}
Numéro de téléphone :
{% if school.phone %}{{school.phone}}
{%else%}
Non indiqué{%endif%}<br/>
{%endif%}
<a class="btn btn-primary btn-sm" href="{% url 'users:edit-school-name' school.pk %}">
{% if school.validated %}
<span class="badge badge-success">
<i class="fa fa-check"></i>
Inscription validée
</span>
{% endif %}
<h1>
{{school.name}}
</h1>
<a class="btn btn-primary btn-sm" href="{% url 'users:edit-school-name' school.pk %}">
<i class="fa fa-edit"></i>
Éditer
</a>
<table class="table table-striped">
<tr>
<th>
Numéro de téléphone
</th>
<td>
{% if school.phone %}{{school.phone}}{%else%}
Non indiqué{%endif%}
</td>
<td>
<a class="btn btn-primary btn-sm" href="{% url 'users:edit-school-phone' school.pk %}">
<i class="fa fa-edit"></i>
Éditer
</a>
</td>
</tr>
<tr>
<th>
Email
</th>
<td>
{{school.admin.email}}
</td>
<td>
<a class="btn btn-primary btn-sm" href="{% url 'users:edit-school-mail' school.pk %}">
<i class="fa fa-edit"></i>
Éditer
</a>
</td>
</tr>
<tr>
<th>
Juré n°1
</th>
<td>
<table class="table table-striped">
<tr>
<th>
Nom
</th>
<td>
{{school.last_name_j1}}
</td>
</tr>
<tr>
<th>
Prénom
</th>
<td>
{{school.first_name_j1}}
</td>
</tr>
<tr>
<th>
Email
</th>
<td>
{{school.mail_j1}}
</td>
</tr>
<tr>
<th>
N° de téléphone
</th>
<td>
{{school.phone_j1}}
</td>
</tr>
</table>
</td>
<td>
<a class="btn btn-primary btn-sm" href="{% url 'users:edit-jury-1' school.pk %}">
<i class="fa fa-edit"></i>
Éditer
</a>
</td>
</tr>
<tr>
<th>
Juré n°2
</th>
<td>
<table class="table table-striped">
<tr>
<th>
Nom
</th>
<td>
{{school.last_name_j2}}
</td>
</tr>
<tr>
<th>
Prénom
</th>
<td>
{{school.first_name_j2}}
</td>
</tr>
<tr>
<th>
Email
</th>
<td>
{{school.mail_j2}}
</td>
</tr>
<tr>
<th>
N° de téléphone
</th>
<td>
{{school.phone_j2}}
</td>
</tr>
</table>
</td>
<td>
<a class="btn btn-primary btn-sm" href="{% url 'users:edit-jury-2' school.pk %}">
<i class="fa fa-edit"></i>
Éditer
</a>
</td>
</tr>
</table>
<a class="btn btn-success btn-lg" href="{% url 'users:validate' school.pk %}">
<i class="fa fa-check"></i>
Valider mon inscription
</a>
<h2>Contenus</h2>
{% if manager_right in perms %}
<a class="btn btn-success btn-sm" href="{% url 'content:content-new' %}">
<i class="fa fa-plus"></i>
Ajouter un contenu
</a>
{% endif %}
<br />
<br />
<div class="row">

View file

@ -21,12 +21,17 @@ urlpatterns = [
'school/<int:pk>',
views.school,
name='school',
)
),
path(
'school/<int:pk>/edit_phone',
views.edit_phone,
name='edit-school-phone'
),
path(
'school/<int:pk>/edit_name',
views.edit_name,
name='edit-school-name'
),
path(
'school/<int:pk>/edit_mail',
views.edit_mail,
@ -42,4 +47,9 @@ urlpatterns = [
views.edit_jury_2,
name='edit-jury-2'
),
path(
'school/<int:pk>/validate',
views.validate,
name='validate',
)
]

View file

@ -30,7 +30,7 @@ def create_school(request):
if school_form.is_valid():
s = school_form.save()
messages.success(request, "L'école {} a bien été créée.".format(s.name))
return redirect(redirect('settings:index'))
return redirect(reverse('settings:index'))
return render(request, 'edit.html', {
'form': school_form,
'title': "Création d'une école",
@ -52,12 +52,12 @@ def edit_phone(request, pk):
can = request.user.is_staff or request.user == school.admin
if not can:
messages.error(request, 'Vous ne pouvez pas accéder à cette page')
return redirect(reverse('settings:index'))
return redirect(reverse('home'))
school_form = forms.EditPhone(request.POST or None, instance=school)
if school_form.is_valid():
s = school_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.name))
return redirect(redirect('settings:index'))
return redirect(s.get_absolute_url())
return render(request, 'edit.html', {
'form': school_form,
'title': "Édition du numéro de téléphone",
@ -70,12 +70,12 @@ def edit_logo(request, pk):
can = request.user.is_staff or request.user == school.admin
if not can:
messages.error(request, 'Vous ne pouvez pas accéder à cette page')
return redirect(reverse('settings:index'))
return redirect(reverse('home'))
school_form = forms.EditLogo(request.POST or None, instance=school)
if school_form.is_valid():
s = school_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.name))
return redirect(redirect('settings:index'))
return redirect(s.get_absolute_url())
return render(request, 'edit.html', {
'form': school_form,
'title': "Édition du logo",
@ -88,12 +88,12 @@ def edit_mail(request, pk):
can = request.user.is_staff or request.user == school.admin
if not can:
messages.error(request, 'Vous ne pouvez pas accéder à cette page')
return redirect(reverse('settings:index'))
return redirect(reverse('home'))
user_form = forms.EditMail(request.POST or None, instance=school.admin)
if user_form.is_valid():
s = user_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.name))
return redirect(redirect('settings:index'))
messages.success(request, "L'école {} a bien été modifiée.".format(s.school.name))
return redirect(s.school.get_absolute_url())
return render(request, 'edit.html', {
'form': user_form,
'title': "Édition du numéro du mail",
@ -106,12 +106,12 @@ def edit_jury_1(request, pk):
can = request.user.is_staff or request.user == school.admin
if not can:
messages.error(request, 'Vous ne pouvez pas accéder à cette page')
return redirect(reverse('settings:index'))
return redirect(reverse('home'))
school_form = forms.EditJury1(request.POST or None, instance=school)
if school_form.is_valid():
s = school_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.name))
return redirect(redirect('settings:index'))
return redirect(s.get_absolute_url())
return render(request, 'edit.html', {
'form': school_form,
'title': "Édition du jury 1",
@ -124,12 +124,12 @@ def edit_jury_2(request, pk):
can = request.user.is_staff or request.user == school.admin
if not can:
messages.error(request, 'Vous ne pouvez pas accéder à cette page')
return redirect(reverse('settings:index'))
return redirect(reverse('home'))
school_form = forms.EditJury2(request.POST or None, instance=school)
if school_form.is_valid():
s = school_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.name))
return redirect(redirect('settings:index'))
return redirect(s.get_absolute_url())
return render(request, 'edit.html', {
'form': school_form,
'title': "Édition du jury 2",
@ -137,27 +137,45 @@ def edit_jury_2(request, pk):
})
def edit_name(request, pk):
school = get_object_or_404(School, pk=pk)
can = request.user.is_staff or request.user == school.admin
if not can:
messages.error(request, 'Vous ne pouvez pas accéder à cette page')
return redirect(reverse('home'))
school_form = forms.EditName(request.POST or None, instance=school)
if school_form.is_valid():
s = school_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.name))
return redirect(s.get_absolute_url())
return render(request, 'edit.html', {
'form': school_form,
'title': "Édition du nom",
'validate': "Modifier"
})
def validate(request, pk):
school = get_object_or_404(School, pk=pk)
can = request.user.is_staff or request.user == school.admin
if not can:
messages.error(request, 'Vous ne pouvez pas accéder à cette page')
return redirect(reverse('settings:index'))
return redirect(reverse('home'))
jury_1_ok = any([
school.first_name_j1 is not None,
school.last_name_j1 is not None,
school.phone_j1 is not None,
school.mail_j1 is not None
school.first_name_j1,
school.last_name_j1,
school.phone_j1,
school.mail_j1
])
jury_2_ok = any([
school.first_name_j2 is not None,
school.last_name_j2 is not None,
school.phone_j2 is not None,
school.mail_j2 is not None
school.first_name_j2,
school.last_name_j2,
school.phone_j2,
school.mail_j2
])
logo_ok = school.logo is not None
phone_ok = school.phone is not None
mail_ok = school.admin.email is not None
logo_ok = school.logo
phone_ok = school.phone
mail_ok = school.admin.email
if not jury_1_ok:
messages.warning(request, 'Pas de jury n°1 défini.')
if not jury_2_ok:
@ -178,4 +196,4 @@ def validate(request, pk):
school.save()
messages.success(request, 'Inscription validée.')
return redirect(reverse('settings:index'))
return redirect(school.get_absolute_url())