Rename pseudo to username

This commit is contained in:
Alexandre Iooss 2019-08-02 21:35:30 +02:00
parent afdb08b926
commit e82053d16f
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
11 changed files with 91 additions and 63 deletions

View File

@ -5,12 +5,6 @@
from django import forms from django import forms
from django.forms import Form from django.forms import Form
CHOICES = (
('0', 'Actifs'),
('1', 'Désactivés'),
('2', 'Archivés'),
)
CHOICES2 = ( CHOICES2 = (
('0', 'Utilisateurs'), ('0', 'Utilisateurs'),
('1', 'Media'), ('1', 'Media'),
@ -25,8 +19,6 @@ class SearchForm(Form):
class SearchFormPlus(Form): class SearchFormPlus(Form):
search_field = forms.CharField(label='Search', max_length=100, required=False) search_field = forms.CharField(label='Search', max_length=100, required=False)
filtre = forms.MultipleChoiceField(label="Filtre utilisateurs", required=False, widget=forms.CheckboxSelectMultiple,
choices=CHOICES)
affichage = forms.MultipleChoiceField(label="Filtre affichage", required=False, widget=forms.CheckboxSelectMultiple, affichage = forms.MultipleChoiceField(label="Filtre affichage", required=False, widget=forms.CheckboxSelectMultiple,
choices=CHOICES2) choices=CHOICES2)
date_deb = forms.DateField(required=False, label="Date de début", help_text='DD/MM/YYYY', date_deb = forms.DateField(required=False, label="Date de début", help_text='DD/MM/YYYY',

View File

@ -22,43 +22,38 @@ def form(ctx, template, request):
def search_result(search, type, request): def search_result(search, type, request):
date_deb = None date_deb = None
date_fin = None date_fin = None
states = []
aff = [] aff = []
if (type): if type:
aff = search.cleaned_data['affichage'] aff = search.cleaned_data['affichage']
states = search.cleaned_data['filtre']
date_deb = search.cleaned_data['date_deb'] date_deb = search.cleaned_data['date_deb']
date_fin = search.cleaned_data['date_fin'] date_fin = search.cleaned_data['date_fin']
date_query = Q() date_query = Q()
if aff == []: if not aff:
aff = ['0', '1', '2', '3'] aff = ['0', '1', '2', '3']
if date_deb != None: if date_deb is not None:
date_query = date_query & Q(date_emprunt__gte=date_deb) date_query = date_query & Q(date_emprunt__gte=date_deb)
if date_fin != None: if date_fin is not None:
date_query = date_query & Q(date_emprunt__lte=date_fin) date_query = date_query & Q(date_emprunt__lte=date_fin)
search = search.cleaned_data['search_field'] search = search.cleaned_data['search_field']
query1 = Q() query1 = Q()
for s in states:
query1 = query1 | Q(state=s)
connexion = []
recherche = {'users_list': None, 'emprunts_list': None, 'medias_list': None, 'jeux_list': None} recherche = {'users_list': None, 'emprunts_list': None, 'medias_list': None, 'jeux_list': None}
if request.user.has_perms(('perm',)): if request.user.has_perms(('perm',)):
query = Q(user__pseudo__icontains=search) | Q(user__name__icontains=search) | Q(user__surname__icontains=search) query = Q(user__username__icontains=search) | Q(user__name__icontains=search) | Q(
user__surname__icontains=search)
else: else:
query = (Q(user__pseudo__icontains=search) | Q(user__name__icontains=search) | Q( query = (Q(user__username__icontains=search) | Q(user__name__icontains=search) | Q(
user__surname__icontains=search)) & Q(user=request.user) user__surname__icontains=search)) & Q(user=request.user)
for i in aff: for i in aff:
if i == '0': if i == '0':
query_user_list = Q(pseudo__icontains=search) | Q(name__icontains=search) | Q( query_user_list = Q(username__icontains=search) | Q(name__icontains=search) | Q(
surname__icontains=search) & query1 surname__icontains=search) & query1
if request.user.has_perms(('perm',)): if request.user.has_perms(('perm',)):
recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname') recherche['users_list'] = User.objects.filter(query_user_list).order_by('surname')
else: else:
recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by('state', recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by(
'surname') 'surname')
if i == '1': if i == '1':
recherche['emprunts_list'] = Emprunt.objects.filter(query & date_query).order_by('date_emprunt').reverse() recherche['emprunts_list'] = Emprunt.objects.filter(query & date_query).order_by('date_emprunt').reverse()
@ -67,11 +62,11 @@ def search_result(search, type, request):
Q(auteur__nom__icontains=search) | Q(titre__icontains=search)) Q(auteur__nom__icontains=search) | Q(titre__icontains=search))
if i == '3': if i == '3':
recherche['jeux_list'] = Jeu.objects.filter( recherche['jeux_list'] = Jeu.objects.filter(
Q(nom__icontains=search) | Q(proprietaire__pseudo__icontains=search) | Q( Q(nom__icontains=search) | Q(proprietaire__username__icontains=search) | Q(
proprietaire__name__icontains=search) | Q(proprietaire__surname__icontains=search)) proprietaire__name__icontains=search) | Q(proprietaire__surname__icontains=search))
for r in recherche: for r in recherche:
if recherche[r] != None: if recherche[r] is not None:
recherche[r] = recherche[r][:SEARCH_DISPLAY_PAGE] recherche[r] = recherche[r][:SEARCH_DISPLAY_PAGE]
recherche.update({'max_result': SEARCH_DISPLAY_PAGE}) recherche.update({'max_result': SEARCH_DISPLAY_PAGE})

View File

@ -116,7 +116,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<table class="table"> <table class="table">
<tr> <tr>
<th scope="row">Pseudo</th> <th scope="row">Pseudo</th>
<td class="text-right">{{ request_user.pseudo }}</td> <td class="text-right">{{ request_user.username }}</td>
</tr> </tr>
<tr> <tr>
<th scope="row">Nom</th> <th scope="row">Nom</th>

View File

@ -11,17 +11,6 @@ from .forms import UserChangeForm, UserCreationForm
from .models import User, Right, Adhesion, ListRight, Clef, Request from .models import User, Right, Adhesion, ListRight, Clef, Request
class UserAdmin(admin.ModelAdmin):
list_display = (
'name',
'surname',
'pseudo',
'email',
'is_active'
)
search_fields = ('name', 'surname', 'pseudo')
class RequestAdmin(admin.ModelAdmin): class RequestAdmin(admin.ModelAdmin):
list_display = ('user', 'type', 'created_at', 'expires_at') list_display = ('user', 'type', 'created_at', 'expires_at')
@ -50,10 +39,10 @@ class UserAdmin(VersionAdmin, BaseUserAdmin):
# The fields to be used in displaying the User model. # The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin # These override the definitions on the base UserAdmin
# that reference specific fields on auth.User. # that reference specific fields on auth.User.
list_display = ('pseudo', 'name', 'surname', 'email', 'is_admin') list_display = ('username', 'name', 'surname', 'email', 'is_admin')
list_filter = () list_filter = ()
fieldsets = ( fieldsets = (
(None, {'fields': ('pseudo', 'password')}), (None, {'fields': ('username', 'password')}),
('Personal info', {'fields': ('name', 'surname', 'email')}), ('Personal info', {'fields': ('name', 'surname', 'email')}),
('Permissions', {'fields': ('is_admin',)}), ('Permissions', {'fields': ('is_admin',)}),
) )
@ -62,11 +51,11 @@ class UserAdmin(VersionAdmin, BaseUserAdmin):
add_fieldsets = ( add_fieldsets = (
(None, { (None, {
'classes': ('wide',), 'classes': ('wide',),
'fields': ('pseudo', 'name', 'surname', 'email', 'is_admin', 'password1', 'password2')} 'fields': ('username', 'name', 'surname', 'email', 'is_admin', 'password1', 'password2')}
), ),
) )
search_fields = ('pseudo',) search_fields = ('username',)
ordering = ('pseudo',) ordering = ('username',)
filter_horizontal = () filter_horizontal = ()

View File

@ -28,7 +28,7 @@ class UserCreationForm(forms.ModelForm):
class Meta: class Meta:
model = User model = User
fields = ('pseudo', 'name', 'surname', 'email') fields = ('username', 'name', 'surname', 'email')
def clean_password2(self): def clean_password2(self):
# Check that the two password entries match # Check that the two password entries match
@ -57,7 +57,7 @@ class UserChangeForm(forms.ModelForm):
class Meta: class Meta:
model = User model = User
fields = ('pseudo', 'password', 'name', 'surname', 'email') fields = ('username', 'password', 'name', 'surname', 'email')
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(UserChangeForm, self).__init__(*args, **kwargs) super(UserChangeForm, self).__init__(*args, **kwargs)
@ -90,7 +90,7 @@ class BaseInfoForm(ModelForm):
model = User model = User
fields = [ fields = [
'name', 'name',
'pseudo', 'username',
'surname', 'surname',
'email', 'email',
'telephone', 'telephone',
@ -102,7 +102,7 @@ class InfoForm(BaseInfoForm):
class Meta(BaseInfoForm.Meta): class Meta(BaseInfoForm.Meta):
fields = [ fields = [
'name', 'name',
'pseudo', 'username',
'surname', 'surname',
'email', 'email',
'telephone', 'telephone',

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.22 on 2019-08-02 19:25
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('users', '0012_auto_20190802_2112'),
]
operations = [
migrations.RenameField(
model_name='user',
old_name='pseudo',
new_name='username',
),
]

View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.22 on 2019-08-02 19:26
from __future__ import unicode_literals
import django.contrib.auth.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('users', '0013_auto_20190802_2125'),
]
operations = [
migrations.AlterField(
model_name='user',
name='username',
field=models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username'),
),
]

View File

@ -6,8 +6,9 @@ import datetime
import uuid import uuid
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.contrib.auth.validators import ASCIIUsernameValidator, UnicodeUsernameValidator
from django.db import models from django.db import models
from django.utils import timezone from django.utils import six, timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from med.settings import MAX_EMPRUNT, REQ_EXPIRE_HRS from med.settings import MAX_EMPRUNT, REQ_EXPIRE_HRS
@ -25,7 +26,7 @@ class UserManager(BaseUserManager):
email = self.normalize_email(email) email = self.normalize_email(email)
username = self.model.normalize_username(username) username = self.model.normalize_username(username)
user = self.model( user = self.model(
pseudo=username, username=username,
name=name, name=name,
surname=surname, surname=surname,
email=email, email=email,
@ -37,22 +38,23 @@ class UserManager(BaseUserManager):
user.make_admin() user.make_admin()
return user return user
def create_user(self, pseudo, name, surname, email, password=None): def create_user(self, username, name, surname, email, password=None):
""" """
Creates and saves a User with the given pseudo, name, surname, email, Creates and saves a User with the given username, name, surname, email,
and password. and password.
""" """
return self._create_user(pseudo, name, surname, email, password, False) return self._create_user(username, name, surname, email, password, False)
def create_superuser(self, pseudo, name, surname, email, password): def create_superuser(self, username, name, surname, email, password):
""" """
Creates and saves a superuser with the given pseudo, name, surname, Creates and saves a superuser with the given username, name, surname,
email, and password. email, and password.
""" """
return self._create_user(pseudo, name, surname, email, password, True) return self._create_user(username, name, surname, email, password, True)
class User(AbstractBaseUser): class User(AbstractBaseUser):
username_validator = UnicodeUsernameValidator() if six.PY3 else ASCIIUsernameValidator()
PRETTY_NAME = "Utilisateurs" PRETTY_NAME = "Utilisateurs"
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
@ -61,8 +63,16 @@ class User(AbstractBaseUser):
telephone = models.CharField(max_length=15, null=True, blank=True) telephone = models.CharField(max_length=15, null=True, blank=True)
adresse = models.CharField(max_length=255, null=True, blank=True) adresse = models.CharField(max_length=255, null=True, blank=True)
maxemprunt = models.IntegerField(default=MAX_EMPRUNT, help_text="Maximum d'emprunts autorisés") maxemprunt = models.IntegerField(default=MAX_EMPRUNT, help_text="Maximum d'emprunts autorisés")
pseudo = models.CharField(max_length=32, unique=True, username = models.CharField(
help_text="Doit contenir uniquement des lettres, chiffres, ou tirets. ") _('username'),
max_length=150,
unique=True,
help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
validators=[username_validator],
error_messages={
'unique': _("A user with that username already exists."),
},
)
comment = models.CharField(help_text="Commentaire, promo", max_length=255, blank=True) comment = models.CharField(help_text="Commentaire, promo", max_length=255, blank=True)
is_staff = models.BooleanField( is_staff = models.BooleanField(
_('staff status'), _('staff status'),
@ -79,7 +89,7 @@ class User(AbstractBaseUser):
) )
registered = models.DateTimeField(auto_now_add=True) registered = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'pseudo' USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['name', 'surname', 'email'] REQUIRED_FIELDS = ['name', 'surname', 'email']
objects = UserManager() objects = UserManager()
@ -125,7 +135,8 @@ class User(AbstractBaseUser):
@property @property
def is_adherent(self): def is_adherent(self):
return self in Adhesion.objects.all().order_by('annee_debut').reverse().first().adherent.all() last_adh_year = Adhesion.objects.all().order_by('annee_debut').reverse().first()
return last_adh_year and self in last_adh_year.adherent.all()
def get_admin_right(self): def get_admin_right(self):
admin, created = ListRight.objects.get_or_create(listright="admin") admin, created = ListRight.objects.get_or_create(listright="admin")
@ -146,7 +157,7 @@ class User(AbstractBaseUser):
user_right.delete() user_right.delete()
def __str__(self): def __str__(self):
return self.pseudo return self.username
class Request(models.Model): class Request(models.Model):

View File

@ -42,7 +42,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<tr> <tr>
<td>{{ user.name }}</td> <td>{{ user.name }}</td>
<td>{{ user.surname }}</td> <td>{{ user.surname }}</td>
<td>{{ user.pseudo }}</td> <td>{{ user.username }}</td>
<td>{{ user.email }}</td> <td>{{ user.email }}</td>
<td>{{ user.maxemprunt }}</td> <td>{{ user.maxemprunt }}</td>
{% if user.is_adherent %} {% if user.is_adherent %}

View File

@ -65,7 +65,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
</tr> </tr>
<tr> <tr>
<th>Pseudo</th> <th>Pseudo</th>
<td>{{ user.pseudo }}</td> <td>{{ user.username }}</td>
<th>E-mail</th> <th>E-mail</th>
<td>{{ user.email }}</td> <td>{{ user.email }}</td>
</tr> </tr>

View File

@ -81,7 +81,7 @@ def new_user(request):
req.save() req.save()
reset_passwd_mail(req, request) reset_passwd_mail(req, request)
messages.success(request, messages.success(request,
"L'utilisateur %s a été crée, un mail pour l'initialisation du mot de passe a été envoyé" % user.pseudo) "L'utilisateur %s a été crée, un mail pour l'initialisation du mot de passe a été envoyé" % user.username)
return redirect("/users/profil/" + str(user.id)) return redirect("/users/profil/" + str(user.id))
return form({'userform': user}, 'users/user.html', request) return form({'userform': user}, 'users/user.html', request)