From 5e594d6b85de6f5b7420258643d48f75e1e5bec8 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Wed, 31 Jan 2018 10:15:51 +0100 Subject: [PATCH] =?UTF-8?q?Utilisateurs=20et=20=C3=A9coles=20+=20cr=C3=A9a?= =?UTF-8?q?tion=20d'utilisateurs.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site_tps/settings.py | 3 +- site_tps/urls.py | 3 +- templates/edit.html | 7 +++++ users/admin.py | 42 ++++++++++++++++++++++++++- users/migrations/0001_initial.py | 34 ++++++++++++++++++++++ users/models.py | 23 ++++++++++++++- users/urls.py | 16 +++++++++++ users/views.py | 49 ++++++++++++++++++++++++++++++-- 8 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 users/migrations/0001_initial.py create mode 100644 users/urls.py diff --git a/site_tps/settings.py b/site_tps/settings.py index 0905a1b..aa6dbfe 100644 --- a/site_tps/settings.py +++ b/site_tps/settings.py @@ -41,7 +41,8 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'settings', 'content', - 'vote' + 'vote', + 'users', ] MIDDLEWARE = [ diff --git a/site_tps/urls.py b/site_tps/urls.py index e603025..2c80d35 100644 --- a/site_tps/urls.py +++ b/site_tps/urls.py @@ -20,8 +20,9 @@ from . import views urlpatterns = [ path('admin/', admin.site.urls), - path('', views.home), + path('', views.home, name="home"), path('content/', include('content.urls')), path('settings/', include('settings.urls')), path('vote/', include('vote.urls')), + path('users/', include('users.urls')), ] diff --git a/templates/edit.html b/templates/edit.html index 9e38274..feb69a4 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -1,7 +1,14 @@ {% extends 'base.html' %} {% block content %} +{% if title %} +

{{title}}

+{% endif %}
{% csrf_token %} {{ form.as_p }} + {% if validate %} + + {% else %} + {% endif %}
{% endblock %} diff --git a/users/admin.py b/users/admin.py index 8c38f3f..419505c 100644 --- a/users/admin.py +++ b/users/admin.py @@ -1,3 +1,43 @@ from django.contrib import admin +from django.contrib.auth.admin import UserAdmin as BaseUserAdmin +from django.contrib.auth.admin import GroupAdmin as BaseGroupAdmin +from django.contrib.auth.models import User, Group -# Register your models here. +from .models import UserProfile, SchoolProfile + +# Define an inline admin descriptor for Employee model +# which acts a bit like a singleton + + +class UserInline(admin.StackedInline): + model = UserProfile + can_delete = False + verbose_name_plural = 'user profiles' + +# Define a new User admin + + +class UserAdmin(BaseUserAdmin): + inlines = (UserInline, ) + +# Define an inline admin descriptor for Employee model +# which acts a bit like a singleton + + +class SchoolInline(admin.StackedInline): + model = SchoolProfile + can_delete = False + verbose_name_plural = 'schools' + +# Define a new User admin + + +class GroupAdmin(BaseGroupAdmin): + inlines = (SchoolInline, ) + + +# Re-register UserAdmin +admin.site.unregister(User) +admin.site.register(User, UserAdmin) +admin.site.unregister(Group) +admin.site.register(Group, GroupAdmin) diff --git a/users/migrations/0001_initial.py b/users/migrations/0001_initial.py new file mode 100644 index 0000000..93fe642 --- /dev/null +++ b/users/migrations/0001_initial.py @@ -0,0 +1,34 @@ +# Generated by Django 2.0.1 on 2018-01-31 09:12 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0009_alter_user_last_name_max_length'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='SchoolProfile', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('is_school', models.BooleanField()), + ('group', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='auth.Group')), + ], + ), + migrations.CreateModel( + name='UserProfile', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('school', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='users.SchoolProfile')), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/users/models.py b/users/models.py index 71a8362..0436886 100644 --- a/users/models.py +++ b/users/models.py @@ -1,3 +1,24 @@ from django.db import models +from django.contrib.auth.models import User, Group +from django.db.models.signals import post_save +from django.dispatch import receiver -# Create your models here. + +class SchoolProfile(models.Model): + """Ajoute un champ pour distinguer les groupes écoles des autres.""" + is_school = models.BooleanField() + group = models.OneToOneField(Group, on_delete=models.CASCADE) + + def __str__(self): + return self.group.name + + +class UserProfile(models.Model): + """Profil d'un utilisateur""" + school = models.ForeignKey(SchoolProfile, on_delete=models.SET_NULL, null=True, blank=True) + user = models.OneToOneField(User, on_delete=models.CASCADE) + + +@receiver(post_save, sender=UserProfile) +def update_groups(sender, instance, **kwargs): + instance.user.groups.add(instance.school.group) diff --git a/users/urls.py b/users/urls.py new file mode 100644 index 0000000..51cb8cd --- /dev/null +++ b/users/urls.py @@ -0,0 +1,16 @@ +from django.urls import path +from .views import CreateUser, CreateUserProfile + +app_name = 'users' +urlpatterns = [ + path( + 'user/new', + CreateUser.as_view(), + name='new-user' + ), + path( + 'user//set_school', + CreateUserProfile.as_view(), + name='create-userprofile' + ) +] diff --git a/users/views.py b/users/views.py index 91ea44a..abc4750 100644 --- a/users/views.py +++ b/users/views.py @@ -1,3 +1,48 @@ -from django.shortcuts import render +from django.contrib.auth.models import User, Group +from django.views.generic import CreateView +from django.urls import reverse, reverse_lazy +from django.shortcuts import get_object_or_404 -# Create your views here. +from .models import UserProfile + + +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 + + +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)