site_tps/users/views.py
2018-03-01 11:25:34 +01:00

169 lines
5.1 KiB
Python

from django.contrib.auth.models import User, Group
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
from django.views.generic import CreateView, UpdateView, DeleteView, DetailView
from django.contrib.auth.views import LoginView, LogoutView, PasswordChangeView
from django.contrib.auth.hashers import make_password
from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse, reverse_lazy
from django.shortcuts import get_object_or_404
from .models import UserProfile, SchoolProfile
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
def form_valid(self, form):
r = super().form_valid(form)
self.object.set_password(form.cleaned_data['password'])
self.object.save()
return r
class Profile(LoginRequiredMixin, UpdateView):
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(
'users:profile',
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)
class CreateSchool(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
permission_required = 'users.add_schoolprofile'
model = Group
fields = ['name']
template_name = 'edit.html'
success_url = reverse_lazy('settings:index')
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
class EditSchool(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
model = Group
fields = ['name']
template_name = 'edit.html'
queryset = Group.objects.filter(school__isnull=False)
def get_success_url(self):
return reverse('users:school', kwargs={'pk':self.object.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']))
def form_valid(self, *args, **kwargs):
r = super().form_valid(*args, **kwargs)
self.object.school.save()
return r
class DeleteSchool(LoginRequiredMixin, PermissionRequiredMixin, DeleteView):
model = Group
permission_required = 'users.delete_schoolprofile'
class School(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
model = Group
template_name = "users/school.html"
def get_context_data(self, **kwargs):
context = super().get_context_data()
context['contents'] = Content.objects.filter(school_owner=self.object.school)
context['school'] = True
context['members'] = User.objects.filter(userprofile__school=self.object.school)
return context
def has_permission(self):
return self.request.user.has_perm('users.view_'+str(self.kwargs['pk']))
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",
}