site_tps/users/views.py

221 lines
7.2 KiB
Python
Raw Normal View History

from django.contrib.auth.models import User, Group
2018-03-01 10:25:34 +00:00
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
2018-02-28 20:25:44 +00:00
from django.views.generic import CreateView, UpdateView, DeleteView, DetailView
2018-03-01 23:23:41 +00:00
from django.contrib.auth.views import LoginView, LogoutView, PasswordChangeView, login_required
2018-02-28 17:11:47 +00:00
from django.contrib.auth.hashers import make_password
from django.contrib.messages.views import SuccessMessageMixin
2018-03-01 23:23:41 +00:00
from django.contrib import messages
from django.urls import reverse, reverse_lazy
2018-03-01 23:23:41 +00:00
from django.shortcuts import get_object_or_404, redirect
2018-01-14 12:19:11 +00:00
2018-01-31 10:53:37 +00:00
from .models import UserProfile, SchoolProfile
2018-02-28 20:25:44 +00:00
from content.models import Content
class CreateUser(CreateView):
model = User
fields = [
'first_name',
'last_name',
'email',
'username',
'password',
]
template_name = 'edit.html'
def get_success_url(self):
return reverse(
'users:create-userprofile',
kwargs={'pk': self.object.pk}
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = "Inscription"
context['validate'] = "S'inscrire"
return context
2018-02-28 17:11:47 +00:00
def form_valid(self, form):
r = super().form_valid(form)
self.object.set_password(form.cleaned_data['password'])
self.object.save()
return r
2018-03-01 10:25:34 +00:00
class Profile(LoginRequiredMixin, UpdateView):
2018-02-28 17:11:47 +00:00
model = User
template_name = 'users/profile.html'
fields = [
'username',
'first_name',
'last_name',
'email'
]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = self.object.username
context['validate'] = "Modifier"
return context
def get_success_url(self):
return reverse(
2018-03-01 10:25:34 +00:00
'users:profile',
2018-02-28 17:11:47 +00:00
kwargs={'pk': self.object.pk}
)
class CreateUserProfile(CreateView):
model = UserProfile
fields = ['school']
template_name = 'edit.html'
success_url = reverse_lazy('home')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = "Choix de l'école"
context['validate'] = "Choisir"
return context
def form_valid(self, form):
form.instance.user = get_object_or_404(User, pk=self.kwargs['pk'])
return super(CreateUserProfile, self).form_valid(form)
2018-01-31 10:53:37 +00:00
2018-03-01 10:25:34 +00:00
class CreateSchool(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
permission_required = 'users.add_schoolprofile'
2018-01-31 10:53:37 +00:00
model = Group
2018-01-31 12:01:04 +00:00
fields = ['name']
2018-01-31 10:53:37 +00:00
template_name = 'edit.html'
2018-01-31 12:01:04 +00:00
success_url = reverse_lazy('settings:index')
2018-01-31 10:53:37 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = "Création de l'école"
context['validate'] = "Créer"
return context
def form_valid(self, form):
response = super(CreateSchool, self).form_valid(form)
profile = SchoolProfile()
profile.group = form.instance
profile.save()
return response
2018-01-31 12:01:04 +00:00
2018-03-02 08:33:37 +00:00
class EditSchoolName(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
2018-01-31 12:01:04 +00:00
model = Group
fields = ['name']
template_name = 'edit.html'
2018-03-01 10:25:34 +00:00
queryset = Group.objects.filter(school__isnull=False)
def get_success_url(self):
2018-03-02 08:33:37 +00:00
return reverse('users:edit-school-phone', kwargs={'pk':self.object.school.pk})
2018-01-31 12:01:04 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = "Édition de l'école"
context['validate'] = "Modifier"
return context
2018-02-28 14:06:55 +00:00
2018-03-01 10:25:34 +00:00
def has_permission(self):
return self.request.user.has_perm('users.manage_'+str(self.kwargs['pk']))
def form_valid(self, *args, **kwargs):
r = super().form_valid(*args, **kwargs)
self.object.school.save()
return r
2018-02-28 14:06:55 +00:00
2018-03-02 08:33:37 +00:00
class EditSchoolPhone(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
model = SchoolProfile
fields = ['phone']
template_name = 'edit.html'
def get_success_url(self):
return reverse('users:school', kwargs={'pk':self.object.group.pk})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = "Édition de l'école"
context['validate'] = "Modifier"
return context
def has_permission(self):
return self.request.user.has_perm('users.manage_'+str(self.kwargs['pk']))
2018-03-01 10:25:34 +00:00
class DeleteSchool(LoginRequiredMixin, PermissionRequiredMixin, DeleteView):
2018-02-28 14:06:55 +00:00
model = Group
2018-03-01 10:25:34 +00:00
permission_required = 'users.delete_schoolprofile'
2018-03-02 08:33:37 +00:00
queryset = Group.objects.filter(school__isnull=False)
2018-02-28 14:06:55 +00:00
2018-02-28 17:11:47 +00:00
2018-03-01 10:25:34 +00:00
class School(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
model = Group
2018-02-28 20:25:44 +00:00
template_name = "users/school.html"
2018-03-02 08:33:37 +00:00
queryset = Group.objects.filter(school__isnull=False)
2018-02-28 20:25:44 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data()
2018-03-01 10:25:34 +00:00
context['contents'] = Content.objects.filter(school_owner=self.object.school)
2018-02-28 21:16:53 +00:00
context['school'] = True
2018-03-01 10:25:34 +00:00
context['members'] = User.objects.filter(userprofile__school=self.object.school)
2018-03-02 09:28:31 +00:00
context['manager_right'] = 'users.manage_' + str(self.object.pk)
2018-03-01 23:23:41 +00:00
context['manager_group'],_ = Group.objects.get_or_create(name=str(self.object.pk)+'_admins')
2018-02-28 20:25:44 +00:00
return context
2018-03-01 10:25:34 +00:00
def has_permission(self):
return self.request.user.has_perm('users.view_'+str(self.kwargs['pk']))
2018-02-28 20:25:44 +00:00
2018-02-28 17:11:47 +00:00
class Logout(SuccessMessageMixin, LogoutView):
success_message = "Vous vous êtes bien déconnecté."
class Login(SuccessMessageMixin, LoginView):
template_name = "edit.html"
success_message = "Bienvenue !"
extra_context = {
'title' : "Connexion",
'validate' : "Se connecter",
}
class PasswordChange(SuccessMessageMixin, PasswordChangeView):
template_name = "edit.html"
success_url = reverse_lazy("home")
success_message = "Le mot de passe a été changé."
extra_context = {
'title' : "Changer le mot de passe",
'validate' : "Changer",
}
2018-03-01 23:23:41 +00:00
@login_required
def promote_user(request, school_pk, user_pk):
school = get_object_or_404(Group, pk=school_pk)
user = get_object_or_404(User, pk=user_pk)
if request.user.has_perm('manage_'+str(school.pk)):
admins,_ = Group.objects.get_or_create(name=str(school.pk)+'_admins')
user.groups.add(admins)
user.save()
messages.success(request, user.username + ' a été ajouté aux administrateurs de ' + school.name)
return redirect(reverse('users:school', kwargs={'pk':school.pk}))
messages.error(request, "Vous n'aves pas ce droit.")
return redirect('home')
@login_required
def degrade_user(request, school_pk, user_pk):
school = get_object_or_404(Group, pk=school_pk)
user = get_object_or_404(User, pk=user_pk)
if request.user.has_perm('manage_'+str(school.pk)):
admins,_ = Group.objects.get_or_create(name=str(school.pk)+'_admins')
user.groups.remove(admins)
user.save()
messages.success(request, user.username + ' a été enlevé des administrateurs de ' + school.name)
return redirect(reverse('users:school', kwargs={'pk':school.pk}))
messages.error(request, "Vous n'aves pas ce droit.")
return redirect('home')