From 87967dbad79ba6bed9e03bf59824958d009ace1d Mon Sep 17 00:00:00 2001 From: Qwann Date: Sat, 18 Feb 2017 01:45:22 +0100 Subject: [PATCH] adding user model --- .gitignore | 1 + evenementiel/settings_dev.py | 1 + evenementiel/urls.py | 16 +--- user/__init__.py | 0 user/admin.py | 3 + user/apps.py | 5 ++ user/forms.py | 36 ++++++++ user/migrations/0001_initial.py | 26 ++++++ user/migrations/0002_auto_20160616_1803.py | 35 ++++++++ user/migrations/0003_auto_20160623_1603.py | 31 +++++++ user/migrations/0004_auto_20160623_1808.py | 22 +++++ user/migrations/__init__.py | 0 user/models.py | 3 + user/templates/user/base_user.html | 7 ++ user/templates/user/change_pass.html | 4 + user/templates/user/email_password_reset.html | 6 ++ user/templates/user/login.html | 28 ++++++ user/templates/user/password_reset.html | 4 + .../templates/user/subject_password_reset.txt | 1 + user/templates/user/user_form.html | 35 ++++++++ user/tests.py | 3 + user/urls.py | 88 +++++++++++++++++++ user/views.py | 15 ++++ 23 files changed, 355 insertions(+), 15 deletions(-) create mode 100644 user/__init__.py create mode 100644 user/admin.py create mode 100644 user/apps.py create mode 100644 user/forms.py create mode 100644 user/migrations/0001_initial.py create mode 100644 user/migrations/0002_auto_20160616_1803.py create mode 100644 user/migrations/0003_auto_20160623_1603.py create mode 100644 user/migrations/0004_auto_20160623_1808.py create mode 100644 user/migrations/__init__.py create mode 100644 user/models.py create mode 100644 user/templates/user/base_user.html create mode 100644 user/templates/user/change_pass.html create mode 100644 user/templates/user/email_password_reset.html create mode 100644 user/templates/user/login.html create mode 100644 user/templates/user/password_reset.html create mode 100644 user/templates/user/subject_password_reset.txt create mode 100644 user/templates/user/user_form.html create mode 100644 user/tests.py create mode 100644 user/urls.py create mode 100644 user/views.py diff --git a/.gitignore b/.gitignore index 886ee5c..c067e72 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__ venv evenementiel/settings.py +.*.swp diff --git a/evenementiel/settings_dev.py b/evenementiel/settings_dev.py index 4c798b5..e6a6909 100644 --- a/evenementiel/settings_dev.py +++ b/evenementiel/settings_dev.py @@ -33,6 +33,7 @@ ALLOWED_HOSTS = [] INSTALLED_APPS = [ 'equipment.apps.EquipmentConfig', 'event.apps.EventConfig', + 'user.apps.UserConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', diff --git a/evenementiel/urls.py b/evenementiel/urls.py index 40462fa..a33af57 100644 --- a/evenementiel/urls.py +++ b/evenementiel/urls.py @@ -1,21 +1,7 @@ -"""evenementiel URL Configuration - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/1.9/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.conf.urls import url, include - 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) -""" from django.conf.urls import url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), + url(r'^user/', include('user.urls')), ] diff --git a/user/__init__.py b/user/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/user/admin.py b/user/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/user/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/user/apps.py b/user/apps.py new file mode 100644 index 0000000..35048d4 --- /dev/null +++ b/user/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class UserConfig(AppConfig): + name = 'user' diff --git a/user/forms.py b/user/forms.py new file mode 100644 index 0000000..bdc86ea --- /dev/null +++ b/user/forms.py @@ -0,0 +1,36 @@ +from django.contrib.auth.models import User +from django.contrib.auth.forms import UserCreationForm +from django import forms +from django.conf import settings + + +class CreateUserForm(UserCreationForm): + key = forms.CharField( + label="Clée de sécurité", + widget=forms.PasswordInput, + help_text="Cette clée est fournie par l'administrat-rice-eur " + "du site. Pour en obtenir une veuillez la-le contacter." + ) + error_m = {'wrong_key': "La clef fournie est eronnée."} + + class Meta: + model = User + fields = ('username', 'first_name', 'last_name', + 'email', 'password1', 'password2',) + + def save(self, commit=True): + user = super(CreateUserForm, self).save(commit=False) + user.email = self.cleaned_data["email"] + user.first_name = self.cleaned_data["first_name"] + user.last_name = self.cleaned_data["last_name"] + if commit: + user.save() + return user + + def clean_key(self): + key = self.cleaned_data.get("key") + if key != settings.CREATE_USER_KEY: + raise forms.ValidationError( + self.error_m['wrong_key'], + code='wrong_key') + return key diff --git a/user/migrations/0001_initial.py b/user/migrations/0001_initial.py new file mode 100644 index 0000000..04a012f --- /dev/null +++ b/user/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.6 on 2016-06-14 22:17 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Profil', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/user/migrations/0002_auto_20160616_1803.py b/user/migrations/0002_auto_20160616_1803.py new file mode 100644 index 0000000..4193a65 --- /dev/null +++ b/user/migrations/0002_auto_20160616_1803.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.6 on 2016-06-16 16:03 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('user', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='profil', + name='modif_pad', + field=models.BooleanField(default=False, verbose_name='Modifier tous les pads'), + ), + migrations.AddField( + model_name='profil', + name='read_kholle', + field=models.BooleanField(default=False, verbose_name='Lecture de khôlles'), + ), + migrations.AddField( + model_name='profil', + name='write_kholle', + field=models.BooleanField(default=False, verbose_name='Écriture de khôlles'), + ), + migrations.AddField( + model_name='profil', + name='write_pad', + field=models.BooleanField(default=False, verbose_name='Écrire des pads'), + ), + ] diff --git a/user/migrations/0003_auto_20160623_1603.py b/user/migrations/0003_auto_20160623_1603.py new file mode 100644 index 0000000..401255c --- /dev/null +++ b/user/migrations/0003_auto_20160623_1603.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.6 on 2016-06-23 14:03 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('user', '0002_auto_20160616_1803'), + ] + + operations = [ + migrations.RemoveField( + model_name='profil', + name='modif_pad', + ), + migrations.RemoveField( + model_name='profil', + name='read_kholle', + ), + migrations.RemoveField( + model_name='profil', + name='write_kholle', + ), + migrations.RemoveField( + model_name='profil', + name='write_pad', + ), + ] diff --git a/user/migrations/0004_auto_20160623_1808.py b/user/migrations/0004_auto_20160623_1808.py new file mode 100644 index 0000000..d2c52e2 --- /dev/null +++ b/user/migrations/0004_auto_20160623_1808.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.6 on 2016-06-23 16:08 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('user', '0003_auto_20160623_1603'), + ] + + operations = [ + migrations.RemoveField( + model_name='profil', + name='user', + ), + migrations.DeleteModel( + name='Profil', + ), + ] diff --git a/user/migrations/__init__.py b/user/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/user/models.py b/user/models.py new file mode 100644 index 0000000..4e54d78 --- /dev/null +++ b/user/models.py @@ -0,0 +1,3 @@ +from django.db import models +from django.contrib.auth.models import User + diff --git a/user/templates/user/base_user.html b/user/templates/user/base_user.html new file mode 100644 index 0000000..f3e200d --- /dev/null +++ b/user/templates/user/base_user.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} + +{% block aside %} + {% block user_aside_before %}{% endblock %} +

Les comptes utilisateurs vous donnent accès à la gestion et d'évènements. Vous pouvez vous connecter par le CAS ENS ou via votre compte si vous n'avez pas d'identifiants CAS. Pour créer un tel compte il est nécessaire d'avoir une clée ; merci de contacter un-e administrat-rice-eur pour en obtenir une.

+ {% block user_aside_after %}{% endblock %} +{% endblock %} diff --git a/user/templates/user/change_pass.html b/user/templates/user/change_pass.html new file mode 100644 index 0000000..ff663aa --- /dev/null +++ b/user/templates/user/change_pass.html @@ -0,0 +1,4 @@ +{% extends "user/user_form.html" %} + +{% block action_name %}{% url 'user:password_change' %}{% endblock %} +{% block user_error %}Une erreur s'est produite, veuillez réessayer.{% endblock %} diff --git a/user/templates/user/email_password_reset.html b/user/templates/user/email_password_reset.html new file mode 100644 index 0000000..d9dbd1a --- /dev/null +++ b/user/templates/user/email_password_reset.html @@ -0,0 +1,6 @@ +Bonjour, + +Quelqu'un a demandé à réinitialiser le mot de passe pour le compte utilisateur de qwann.fr utilisant l'adresse mail : {{ email }}. Pour réinitialiser le mot de passe, veuillez suivre le lien suivant : +{{ protocol}}://qwann.fr{% url 'user:password_reset_confirm' uidb64=uid token=token %} + +Merci de ne pas répondre à ce mail. diff --git a/user/templates/user/login.html b/user/templates/user/login.html new file mode 100644 index 0000000..74b291b --- /dev/null +++ b/user/templates/user/login.html @@ -0,0 +1,28 @@ +{% extends "user/user_form.html" %} + +{% block action_name %}{% url 'user:login' %}{% endblock %} +{% block user_error %}L'identitfiant et le mot de passe ne correspondent pas !{% endblock %} + +{% block extra_form_input %} + +{% endblock %} + +{% block user_aside_after %} +
+

Vous n'avez pas de compte utilisateur et vous souhaiteriez en créer un ?

+
+ +
+
+

Vous avez déjà un compte utilisateur et vous avez oublié votre mot de passe ?

+
+ +
+ +{% endblock %} diff --git a/user/templates/user/password_reset.html b/user/templates/user/password_reset.html new file mode 100644 index 0000000..799ab8e --- /dev/null +++ b/user/templates/user/password_reset.html @@ -0,0 +1,4 @@ +{% extends "user/user_form.html" %} + +{% block action_name %}{% url 'user:password_reset' %}{% endblock %} +{% block user_error %}L'identitfiant et le mot de passe ne correspondent pas !{% endblock %} diff --git a/user/templates/user/subject_password_reset.txt b/user/templates/user/subject_password_reset.txt new file mode 100644 index 0000000..68bd26c --- /dev/null +++ b/user/templates/user/subject_password_reset.txt @@ -0,0 +1 @@ +[Qwann.fr] Réinitialisation du mot de passe diff --git a/user/templates/user/user_form.html b/user/templates/user/user_form.html new file mode 100644 index 0000000..b344281 --- /dev/null +++ b/user/templates/user/user_form.html @@ -0,0 +1,35 @@ +{% extends "user/base_user.html" %} +{% load bootstrap %} + +{% block section_title %}{{ sec_title }}{% endblock %} +{% block content %} + {% if form.errors %} +

{% block user_error %}{% endblock %}

+ {% endif %} +
+ {% csrf_token %} +
+ {% for field in form %} + {{ field | bootstrap }} + {% endfor %} +
+
+ + {% block extra_form_input %}{% endblock %} +
+
+ +{% endblock %} + diff --git a/user/tests.py b/user/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/user/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/user/urls.py b/user/urls.py new file mode 100644 index 0000000..2d6ef44 --- /dev/null +++ b/user/urls.py @@ -0,0 +1,88 @@ +from django.conf.urls import url, include +from django.contrib.auth import views as auth_views +from django.core.urlresolvers import reverse_lazy +from event.views import Index #TODO : mettre le vrai home +from user.views import CreateUser + +app_name = 'user' +urlpatterns = [ + # CREATE USER + url('^create/$', CreateUser.as_view(), name='create_user'), + # LOGIN + url('^login/$', + auth_views.login, + { 'template_name': 'user/login.html', + 'extra_context': { + 'sec_title' : 'Connexion', + 'button' : 'Se connecter', + }, + }, + name='login', + ), + # LOGOUT + url('^logout/$', + auth_views.logout, + #TODO : mettre le vrai home + { 'next_page': reverse_lazy('event:index'), + }, + name='logout',), + # PASSWORD_CHANGE + url('^password_change/$', + auth_views.password_change, + { 'template_name': 'user/change_pass.html', + #TODO : mettre le vrai home + 'post_change_redirect': reverse_lazy('event:index'), + 'extra_context': { + 'sec_title' : 'Changement de mot de passe', + 'button' : 'Modifier', + }, + }, + name='password_change'), + # url('^password_change/done/$', name='password_change_done'), + # RESET PASSWORD + url('^password_reset/$', + auth_views.password_reset, + { 'template_name' : 'user/password_reset.html', + 'email_template_name': 'email_password_reset.html', + 'subject_template_name': 'subject_password_reset.txt', + 'post_reset_redirect': reverse_lazy('user:password_reset_done'), + 'extra_context': { + 'sec_title' : 'Demande de nouveau mot de passe', + 'button' : 'Envoyer' + }, + }, + name='password_reset'), + # PASS RESET DONE + url('^password_reset/done/$', + #TODO : mettre le vrai home + Index.as_view(), + name='password_reset_done'), + # PASS RESET CONFIRM + url('^reset/(?P[0-9A-Za-z_\-]+)/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', + auth_views.password_reset_confirm, + { + 'template_name': 'user/user_form.html', + 'post_reset_redirect' : reverse_lazy('user:password_reset_complete'), + 'extra_context': { + 'sec_title' : 'Changer de mot de passe', + 'button' : 'Changer' + }, + }, + name='password_reset_confirm'), + # PASS RESET COMPLETE + url('^reset/done/$', + #TODO : mettre le vrai home + Index.as_view(), + name='password_reset_complete'), + ] + +# Inclu les vues suivantes : + +# ^login/$ [name='login'] +# ^logout/$ [name='logout'] +# ^password_change/$ [name='password_change'] +# ^password_change/done/$ [name='password_change_done'] +# ^password_reset/$ [name='password_reset'] +# ^password_reset/done/$ [name='password_reset_done'] +# ^reset/(?P[0-9A-Za-z_\-]+)/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$ [name='password_reset_confirm'] +# ^reset/done/$ [name='password_reset_complete'] diff --git a/user/views.py b/user/views.py new file mode 100644 index 0000000..86160db --- /dev/null +++ b/user/views.py @@ -0,0 +1,15 @@ +from user.forms import CreateUserForm +from django.views.generic.edit import CreateView +from django.contrib.messages.views import SuccessMessageMixin +from django.core.urlresolvers import reverse_lazy + +class CreateUser(SuccessMessageMixin, CreateView): + template_name = 'user/user_form.html' + form_class = CreateUserForm + success_url = reverse_lazy('erkan:index') + success_message = "Votre compte utilisateur a été correctement créé !" + def get_context_data(self, **kwargs): + ctx = super(CreateUser, self).get_context_data(**kwargs) + ctx['button'] = 'Créer' + ctx['sec_title'] = "Création d'utilisateur" + return ctx