Gestion des utilisateurs.

This commit is contained in:
Klafyvel 2018-02-28 18:11:47 +01:00
parent 714aa2a69c
commit 331c8bf1d4
6 changed files with 106 additions and 3 deletions

View file

@ -127,3 +127,6 @@ STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"

View file

@ -13,6 +13,14 @@
<body>
{% include 'nav_bar.html' %}
<div class="container">
{% for message in messages %}
<div class="alert alert-{{message.tags}} alert-dismissible fade show" role="alert">
{{message}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% endfor %}
{% block content %}{% endblock %}
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

View file

@ -23,14 +23,18 @@
{{request.user}}
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#"><i class="fa fa-user"></i> Accéder à mon profil</a>
<a class="dropdown-item" href="#"><i class="fa fa-sign-out-alt"></i> Se déconnecter</a>
<a class="dropdown-item" href="{% url 'users:profile' request.user.pk%}"><i class="fa fa-user"></i> Accéder à mon profil</a>
<a class="dropdown-item" href="{% url 'users:logout' %}"><i class="fa fa-sign-out-alt"></i> Se déconnecter</a>
</div>
</li>
{% else %}
<li class="nav-item {% if active == 4 %}active{% endif %}">
<a class="nav-link" href="">Connexion<span class="sr-only">(current)</span></a>
<a class="nav-link" href="{% url 'users:login' %}">Connexion<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'users:new-user' %}">Inscription<span class="sr-only">(current)</span></a>
</li>
{% endif %}
</ul>
</div>

View file

@ -0,0 +1,11 @@
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block content %}
<h1>{{form.instance.username}}</h1><a class="btn btn-outline-primary btn-sm" href="{% url 'users:change-password' %}">Changer de mot de passe</a>
<form action="" method="post">
{% csrf_token %}
{% bootstrap_form form %}
<button type="submit" class="btn btn-primary">Mettre à jour</button>
</form>
{% endblock %}

View file

@ -5,6 +5,10 @@ from .views import (
CreateSchool,
EditSchool,
DeleteSchool,
Login,
Logout,
PasswordChange,
Profile,
)
app_name = 'users'
@ -14,11 +18,31 @@ urlpatterns = [
CreateUser.as_view(),
name='new-user'
),
path(
'login',
Login.as_view(),
name='login'
),
path(
'logout',
Logout.as_view(),
name='logout',
),
path(
'change_password',
PasswordChange.as_view(),
name='change-password'
),
path(
'user/<int:pk>/set_school',
CreateUserProfile.as_view(),
name='create-userprofile'
),
path(
'user/<int:pk>',
Profile.as_view(),
name='profile',
),
path(
'school/new',
CreateSchool.as_view(),

View file

@ -1,5 +1,8 @@
from django.contrib.auth.models import User, Group
from django.views.generic import CreateView, UpdateView, DeleteView
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
@ -29,6 +32,34 @@ class CreateUser(CreateView):
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(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:reset_password',
kwargs={'pk': self.object.pk}
)
class CreateUserProfile(CreateView):
model = UserProfile
@ -84,3 +115,25 @@ class EditSchool(UpdateView):
class DeleteSchool(DeleteView):
model = Group
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",
}