259 lines
9 KiB
Python
259 lines
9 KiB
Python
from django.contrib import messages
|
|
from django.urls import reverse, reverse_lazy
|
|
from django.core.mail import send_mail
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
|
from django.template.loader import render_to_string
|
|
from django.contrib.auth import views as auth_views
|
|
|
|
from settings.forms import SelectUserForm
|
|
from content.models import Category
|
|
|
|
from .models import School
|
|
from . import forms
|
|
|
|
|
|
@staff_member_required
|
|
def create_user(request):
|
|
user_form = forms.CreateUser(request.POST or None)
|
|
if user_form.is_valid():
|
|
u = user_form.save(request)
|
|
messages.success(request, "L'utilisateur {} {} a bien été créé un mail lui a été envoyé pour réinitialiser son mot de passe.".format(
|
|
u.first_name, u.last_name))
|
|
return redirect(reverse('settings:index'))
|
|
return render(request, 'edit.html', {
|
|
'form': user_form,
|
|
'title': "Création d'un utilisateur",
|
|
'validate': "Créer"
|
|
})
|
|
|
|
|
|
@staff_member_required
|
|
def create_school(request):
|
|
school_form = forms.CreateSchool(request.POST or None)
|
|
if school_form.is_valid():
|
|
s = school_form.save()
|
|
messages.success(
|
|
request, "L'école {} a bien été créée.".format(s.name))
|
|
return redirect(reverse('settings:index'))
|
|
return render(request, 'edit.html', {
|
|
'form': school_form,
|
|
'title': "Création d'une école",
|
|
'validate': "Créer"
|
|
})
|
|
|
|
|
|
def school(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'))
|
|
return render(request, 'users/school.html', {'school': school})
|
|
|
|
|
|
def edit_phone(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.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(s.get_absolute_url())
|
|
return render(request, 'edit.html', {
|
|
'form': school_form,
|
|
'title': "Édition du numéro de téléphone",
|
|
'validate': "Modifier"
|
|
})
|
|
|
|
|
|
def edit_logo(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.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(s.get_absolute_url())
|
|
return render(request, 'edit.html', {
|
|
'form': school_form,
|
|
'title': "Édition du logo",
|
|
'validate': "Modifier"
|
|
})
|
|
|
|
|
|
def edit_mail(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'))
|
|
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.school.name))
|
|
return redirect(s.school.get_absolute_url())
|
|
return render(request, 'edit.html', {
|
|
'form': user_form,
|
|
'title': "Édition du numéro du mail",
|
|
'validate': "Modifier"
|
|
})
|
|
|
|
|
|
def edit_jury_1(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.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(s.get_absolute_url())
|
|
return render(request, 'edit.html', {
|
|
'form': school_form,
|
|
'title': "Édition du jury 1",
|
|
'validate': "Modifier"
|
|
})
|
|
|
|
|
|
def edit_jury_2(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.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(s.get_absolute_url())
|
|
return render(request, 'edit.html', {
|
|
'form': school_form,
|
|
'title': "Édition du jury 2",
|
|
'validate': "Modifier"
|
|
})
|
|
|
|
|
|
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"
|
|
})
|
|
|
|
|
|
@staff_member_required
|
|
def edit_admin(request, pk):
|
|
school = get_object_or_404(School, pk=pk)
|
|
user_form = SelectUserForm(request.POST or None)
|
|
user_form.populate()
|
|
if user_form.is_valid():
|
|
user = user_form.get_user()
|
|
school.admin = user
|
|
school.save()
|
|
user.save()
|
|
messages.success(request, user.username +
|
|
' a été nommé admin de ' + school.name)
|
|
return redirect(reverse('settings:index'))
|
|
return render(request, 'edit.html', {
|
|
'form': user_form,
|
|
'title': "Définir l'administrateur de {}".format(school.name),
|
|
'validate': 'Ajouter'
|
|
})
|
|
|
|
|
|
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('home'))
|
|
jury_1_ok = any([
|
|
school.first_name_j1,
|
|
school.last_name_j1,
|
|
school.phone_j1,
|
|
school.mail_j1
|
|
])
|
|
jury_2_ok = any([
|
|
school.first_name_j2,
|
|
school.last_name_j2,
|
|
school.phone_j2,
|
|
school.mail_j2
|
|
])
|
|
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:
|
|
messages.warning(request, 'Pas de jury n°2 défini.')
|
|
if not logo_ok:
|
|
messages.warning(request, 'Pas de logo défini.')
|
|
if not phone_ok:
|
|
messages.error(request, 'Pas de téléphone défini.')
|
|
if not mail_ok:
|
|
messages.error(request, 'Pas de mail défini.')
|
|
|
|
for category in Category.objects.all():
|
|
if not category.content_set.filter(school_owner=school):
|
|
messages.warning(
|
|
request, 'Pas de contenu dans la catégorie {}.'.format(category.name))
|
|
|
|
if phone_ok and mail_ok:
|
|
school.validated = True
|
|
school.save()
|
|
messages.success(request, 'Inscription validée.')
|
|
|
|
return redirect(school.get_absolute_url())
|
|
|
|
|
|
class PasswordChangeDoneView(auth_views.PasswordChangeDoneView):
|
|
template_name = "home.html"
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
r = super().dispatch(*args, **kwargs)
|
|
messages.success(self.request, "Le mot de passe a été changé.")
|
|
return r
|
|
|
|
|
|
class PasswordResetView(auth_views.PasswordResetView):
|
|
template_name = "edit.html"
|
|
success_url = reverse_lazy('home')
|
|
email_template_name = "users/password_reset_mail.html"
|
|
|
|
def form_valid(self, form):
|
|
messages.success(self.request, "Un mail pour le changement de mot de passe a été envoyé.")
|
|
return super().form_valid(form)
|
|
|
|
|
|
class PasswordResetCompleteView(auth_views.PasswordResetCompleteView):
|
|
template_name = "home.html"
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
r = super().dispatch(*args, **kwargs)
|
|
messages.success(self.request, "Votre mot de passe a été réinitialisé.")
|
|
return r
|