site_tps/users/views.py
2018-03-09 00:53:38 +01:00

181 lines
6.6 KiB
Python

from django.contrib import messages
from django.urls import reverse
from django.shortcuts import get_object_or_404, redirect, render
from django.contrib.admin.views.decorators import staff_member_required
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()
messages.success(request, "L'utilisateur {} {} a bien été créé.".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(redirect('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('settings:index'))
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 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('settings:index'))
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 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('settings:index'))
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'))
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('settings:index'))
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 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('settings:index'))
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 render(request, 'edit.html', {
'form': school_form,
'title': "Édition du jury 2",
'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'))
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
])
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
])
logo_ok = school.logo is not None
phone_ok = school.phone is not None
mail_ok = school.admin.email is not None
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(reverse('settings:index'))