Utilisateurs et écoles + création d'utilisateurs.
This commit is contained in:
parent
5869ec943c
commit
5e594d6b85
8 changed files with 171 additions and 6 deletions
|
@ -41,7 +41,8 @@ INSTALLED_APPS = [
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'settings',
|
'settings',
|
||||||
'content',
|
'content',
|
||||||
'vote'
|
'vote',
|
||||||
|
'users',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
|
|
@ -20,8 +20,9 @@ from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
path('', views.home),
|
path('', views.home, name="home"),
|
||||||
path('content/', include('content.urls')),
|
path('content/', include('content.urls')),
|
||||||
path('settings/', include('settings.urls')),
|
path('settings/', include('settings.urls')),
|
||||||
path('vote/', include('vote.urls')),
|
path('vote/', include('vote.urls')),
|
||||||
|
path('users/', include('users.urls')),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
{% extends 'base.html' %}
|
{% extends 'base.html' %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
{% if title %}
|
||||||
|
<h1>{{title}}</h1>
|
||||||
|
{% endif %}
|
||||||
<form action="" method="post">{% csrf_token %}
|
<form action="" method="post">{% csrf_token %}
|
||||||
{{ form.as_p }}
|
{{ form.as_p }}
|
||||||
|
{% if validate %}
|
||||||
|
<input type="submit" value="{{validate}}" />
|
||||||
|
{% else %}
|
||||||
<input type="submit" value="Mettre à jour" />
|
<input type="submit" value="Mettre à jour" />
|
||||||
|
{% endif %}
|
||||||
</form>
|
</form>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -1,3 +1,43 @@
|
||||||
from django.contrib import admin
|
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)
|
||||||
|
|
34
users/migrations/0001_initial.py
Normal file
34
users/migrations/0001_initial.py
Normal file
|
@ -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)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,3 +1,24 @@
|
||||||
from django.db import models
|
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)
|
||||||
|
|
16
users/urls.py
Normal file
16
users/urls.py
Normal file
|
@ -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/<int:pk>/set_school',
|
||||||
|
CreateUserProfile.as_view(),
|
||||||
|
name='create-userprofile'
|
||||||
|
)
|
||||||
|
]
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue