2018-03-01 23:23:41 +00:00
|
|
|
from django.contrib import messages
|
2018-03-11 21:02:16 +00:00
|
|
|
from django.urls import reverse, reverse_lazy
|
|
|
|
from django.core.mail import send_mail
|
2018-03-08 23:53:38 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
2018-03-11 21:02:16 +00:00
|
|
|
from django.template.loader import render_to_string
|
|
|
|
from django.contrib.auth import views as auth_views
|
2018-03-08 23:53:38 +00:00
|
|
|
|
2018-03-10 13:48:48 +00:00
|
|
|
from settings.forms import SelectUserForm
|
2018-03-08 23:53:38 +00:00
|
|
|
from content.models import Category
|
2018-01-14 12:19:11 +00:00
|
|
|
|
2018-03-08 22:16:03 +00:00
|
|
|
from .models import School
|
2018-03-08 23:53:38 +00:00
|
|
|
from . import forms
|
|
|
|
|
|
|
|
|
|
|
|
@staff_member_required
|
|
|
|
def create_user(request):
|
|
|
|
user_form = forms.CreateUser(request.POST or None)
|
|
|
|
if user_form.is_valid():
|
2018-03-18 10:36:52 +00:00
|
|
|
u = user_form.save(request)
|
2018-03-11 21:02:16 +00:00
|
|
|
messages.success(request, "L'utilisateur {} {} a bien été créé un mail lui a été envoyé pour réinitialiser son mot de passe.".format(
|
2018-03-08 23:53:38 +00:00
|
|
|
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()
|
2018-03-10 13:48:48 +00:00
|
|
|
messages.success(
|
|
|
|
request, "L'école {} a bien été créée.".format(s.name))
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(reverse('settings:index'))
|
2018-03-08 23:53:38 +00:00
|
|
|
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'))
|
2018-03-10 13:48:48 +00:00
|
|
|
return render(request, 'users/school.html', {'school': school})
|
2018-03-08 23:53:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
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')
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(reverse('home'))
|
2018-03-08 23:53:38 +00:00
|
|
|
school_form = forms.EditPhone(request.POST or None, instance=school)
|
|
|
|
if school_form.is_valid():
|
|
|
|
s = school_form.save()
|
2018-03-10 13:48:48 +00:00
|
|
|
messages.success(
|
|
|
|
request, "L'école {} a bien été modifiée.".format(s.name))
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(s.get_absolute_url())
|
2018-03-08 23:53:38 +00:00
|
|
|
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')
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(reverse('home'))
|
2018-03-08 23:53:38 +00:00
|
|
|
school_form = forms.EditLogo(request.POST or None, instance=school)
|
|
|
|
if school_form.is_valid():
|
|
|
|
s = school_form.save()
|
2018-03-10 13:48:48 +00:00
|
|
|
messages.success(
|
|
|
|
request, "L'école {} a bien été modifiée.".format(s.name))
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(s.get_absolute_url())
|
2018-03-08 23:53:38 +00:00
|
|
|
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')
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(reverse('home'))
|
2018-03-08 23:53:38 +00:00
|
|
|
user_form = forms.EditMail(request.POST or None, instance=school.admin)
|
|
|
|
if user_form.is_valid():
|
|
|
|
s = user_form.save()
|
2018-03-10 13:48:48 +00:00
|
|
|
messages.success(
|
|
|
|
request, "L'école {} a bien été modifiée.".format(s.school.name))
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(s.school.get_absolute_url())
|
2018-03-08 23:53:38 +00:00
|
|
|
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')
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(reverse('home'))
|
2018-03-08 23:53:38 +00:00
|
|
|
school_form = forms.EditJury1(request.POST or None, instance=school)
|
|
|
|
if school_form.is_valid():
|
|
|
|
s = school_form.save()
|
2018-03-10 13:48:48 +00:00
|
|
|
messages.success(
|
|
|
|
request, "L'école {} a bien été modifiée.".format(s.name))
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(s.get_absolute_url())
|
2018-03-08 23:53:38 +00:00
|
|
|
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')
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(reverse('home'))
|
2018-03-08 23:53:38 +00:00
|
|
|
school_form = forms.EditJury2(request.POST or None, instance=school)
|
|
|
|
if school_form.is_valid():
|
|
|
|
s = school_form.save()
|
2018-03-10 13:48:48 +00:00
|
|
|
messages.success(
|
|
|
|
request, "L'école {} a bien été modifiée.".format(s.name))
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(s.get_absolute_url())
|
2018-03-08 23:53:38 +00:00
|
|
|
return render(request, 'edit.html', {
|
|
|
|
'form': school_form,
|
|
|
|
'title': "Édition du jury 2",
|
|
|
|
'validate': "Modifier"
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-03-09 10:44:49 +00:00
|
|
|
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()
|
2018-03-10 13:48:48 +00:00
|
|
|
messages.success(
|
|
|
|
request, "L'école {} a bien été modifiée.".format(s.name))
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(s.get_absolute_url())
|
|
|
|
return render(request, 'edit.html', {
|
|
|
|
'form': school_form,
|
|
|
|
'title': "Édition du nom",
|
|
|
|
'validate': "Modifier"
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-03-10 13:48:48 +00:00
|
|
|
@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'
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-03-08 23:53:38 +00:00
|
|
|
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')
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(reverse('home'))
|
2018-03-08 23:53:38 +00:00
|
|
|
jury_1_ok = any([
|
2018-03-09 10:44:49 +00:00
|
|
|
school.first_name_j1,
|
|
|
|
school.last_name_j1,
|
|
|
|
school.phone_j1,
|
|
|
|
school.mail_j1
|
2018-03-08 23:53:38 +00:00
|
|
|
])
|
|
|
|
jury_2_ok = any([
|
2018-03-09 10:44:49 +00:00
|
|
|
school.first_name_j2,
|
|
|
|
school.last_name_j2,
|
|
|
|
school.phone_j2,
|
|
|
|
school.mail_j2
|
2018-03-08 23:53:38 +00:00
|
|
|
])
|
2018-03-09 10:44:49 +00:00
|
|
|
logo_ok = school.logo
|
|
|
|
phone_ok = school.phone
|
|
|
|
mail_ok = school.admin.email
|
2018-03-08 23:53:38 +00:00
|
|
|
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):
|
2018-03-10 13:48:48 +00:00
|
|
|
messages.warning(
|
|
|
|
request, 'Pas de contenu dans la catégorie {}.'.format(category.name))
|
2018-03-08 23:53:38 +00:00
|
|
|
|
|
|
|
if phone_ok and mail_ok:
|
|
|
|
school.validated = True
|
|
|
|
school.save()
|
|
|
|
messages.success(request, 'Inscription validée.')
|
|
|
|
|
2018-03-09 10:44:49 +00:00
|
|
|
return redirect(school.get_absolute_url())
|
2018-03-11 21:02:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|