diff --git a/search/forms.py b/search/forms.py index 47bac83..05e1899 100644 --- a/search/forms.py +++ b/search/forms.py @@ -5,12 +5,6 @@ from django import forms from django.forms import Form -CHOICES = ( - ('0', 'Actifs'), - ('1', 'Désactivés'), - ('2', 'Archivés'), -) - CHOICES2 = ( ('0', 'Utilisateurs'), ('1', 'Media'), @@ -25,8 +19,6 @@ class SearchForm(Form): class SearchFormPlus(Form): 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, choices=CHOICES2) date_deb = forms.DateField(required=False, label="Date de début", help_text='DD/MM/YYYY', diff --git a/search/views.py b/search/views.py index 1b0e00d..dc08930 100644 --- a/search/views.py +++ b/search/views.py @@ -22,44 +22,39 @@ def form(ctx, template, request): def search_result(search, type, request): date_deb = None date_fin = None - states = [] aff = [] - if (type): + if type: aff = search.cleaned_data['affichage'] - states = search.cleaned_data['filtre'] date_deb = search.cleaned_data['date_deb'] date_fin = search.cleaned_data['date_fin'] date_query = Q() - if aff == []: + if not aff: 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) - if date_fin != None: + if date_fin is not None: date_query = date_query & Q(date_emprunt__lte=date_fin) search = search.cleaned_data['search_field'] 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} 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: - 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) for i in aff: 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 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: - recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by('state', - 'surname') + recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by( + 'surname') if i == '1': recherche['emprunts_list'] = Emprunt.objects.filter(query & date_query).order_by('date_emprunt').reverse() if i == '2': @@ -67,11 +62,11 @@ def search_result(search, type, request): Q(auteur__nom__icontains=search) | Q(titre__icontains=search)) if i == '3': 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)) for r in recherche: - if recherche[r] != None: + if recherche[r] is not None: recherche[r] = recherche[r][:SEARCH_DISPLAY_PAGE] recherche.update({'max_result': SEARCH_DISPLAY_PAGE}) diff --git a/templates/base.html b/templates/base.html index 063e1f5..20062b1 100644 --- a/templates/base.html +++ b/templates/base.html @@ -116,7 +116,7 @@ with this program; if not, write to the Free Software Foundation, Inc., - + diff --git a/users/admin.py b/users/admin.py index b366f9f..8aadbb3 100644 --- a/users/admin.py +++ b/users/admin.py @@ -11,17 +11,6 @@ from .forms import UserChangeForm, UserCreationForm 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): 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. # These override the definitions on the base UserAdmin # 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 = () fieldsets = ( - (None, {'fields': ('pseudo', 'password')}), + (None, {'fields': ('username', 'password')}), ('Personal info', {'fields': ('name', 'surname', 'email')}), ('Permissions', {'fields': ('is_admin',)}), ) @@ -62,11 +51,11 @@ class UserAdmin(VersionAdmin, BaseUserAdmin): add_fieldsets = ( (None, { 'classes': ('wide',), - 'fields': ('pseudo', 'name', 'surname', 'email', 'is_admin', 'password1', 'password2')} + 'fields': ('username', 'name', 'surname', 'email', 'is_admin', 'password1', 'password2')} ), ) - search_fields = ('pseudo',) - ordering = ('pseudo',) + search_fields = ('username',) + ordering = ('username',) filter_horizontal = () diff --git a/users/forms.py b/users/forms.py index c0047ae..9d66406 100644 --- a/users/forms.py +++ b/users/forms.py @@ -28,7 +28,7 @@ class UserCreationForm(forms.ModelForm): class Meta: model = User - fields = ('pseudo', 'name', 'surname', 'email') + fields = ('username', 'name', 'surname', 'email') def clean_password2(self): # Check that the two password entries match @@ -57,7 +57,7 @@ class UserChangeForm(forms.ModelForm): class Meta: model = User - fields = ('pseudo', 'password', 'name', 'surname', 'email') + fields = ('username', 'password', 'name', 'surname', 'email') def __init__(self, *args, **kwargs): super(UserChangeForm, self).__init__(*args, **kwargs) @@ -90,7 +90,7 @@ class BaseInfoForm(ModelForm): model = User fields = [ 'name', - 'pseudo', + 'username', 'surname', 'email', 'telephone', @@ -102,7 +102,7 @@ class InfoForm(BaseInfoForm): class Meta(BaseInfoForm.Meta): fields = [ 'name', - 'pseudo', + 'username', 'surname', 'email', 'telephone', diff --git a/users/migrations/0013_auto_20190802_2125.py b/users/migrations/0013_auto_20190802_2125.py new file mode 100644 index 0000000..365fa8d --- /dev/null +++ b/users/migrations/0013_auto_20190802_2125.py @@ -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', + ), + ] diff --git a/users/migrations/0014_auto_20190802_2126.py b/users/migrations/0014_auto_20190802_2126.py new file mode 100644 index 0000000..9827713 --- /dev/null +++ b/users/migrations/0014_auto_20190802_2126.py @@ -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'), + ), + ] diff --git a/users/models.py b/users/models.py index 68a6ad8..798ce26 100644 --- a/users/models.py +++ b/users/models.py @@ -6,8 +6,9 @@ import datetime import uuid from django.contrib.auth.models import AbstractBaseUser, BaseUserManager +from django.contrib.auth.validators import ASCIIUsernameValidator, UnicodeUsernameValidator 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 med.settings import MAX_EMPRUNT, REQ_EXPIRE_HRS @@ -25,7 +26,7 @@ class UserManager(BaseUserManager): email = self.normalize_email(email) username = self.model.normalize_username(username) user = self.model( - pseudo=username, + username=username, name=name, surname=surname, email=email, @@ -37,22 +38,23 @@ class UserManager(BaseUserManager): user.make_admin() 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. """ - 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. """ - return self._create_user(pseudo, name, surname, email, password, True) + return self._create_user(username, name, surname, email, password, True) class User(AbstractBaseUser): + username_validator = UnicodeUsernameValidator() if six.PY3 else ASCIIUsernameValidator() PRETTY_NAME = "Utilisateurs" name = models.CharField(max_length=255) @@ -61,8 +63,16 @@ class User(AbstractBaseUser): telephone = models.CharField(max_length=15, 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") - pseudo = models.CharField(max_length=32, unique=True, - help_text="Doit contenir uniquement des lettres, chiffres, ou tirets. ") + username = models.CharField( + _('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) is_staff = models.BooleanField( _('staff status'), @@ -79,7 +89,7 @@ class User(AbstractBaseUser): ) registered = models.DateTimeField(auto_now_add=True) - USERNAME_FIELD = 'pseudo' + USERNAME_FIELD = 'username' REQUIRED_FIELDS = ['name', 'surname', 'email'] objects = UserManager() @@ -125,7 +135,8 @@ class User(AbstractBaseUser): @property 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): admin, created = ListRight.objects.get_or_create(listright="admin") @@ -146,7 +157,7 @@ class User(AbstractBaseUser): user_right.delete() def __str__(self): - return self.pseudo + return self.username class Request(models.Model): diff --git a/users/templates/users/aff_users.html b/users/templates/users/aff_users.html index 5a2a3c8..4bbc047 100644 --- a/users/templates/users/aff_users.html +++ b/users/templates/users/aff_users.html @@ -42,7 +42,7 @@ with this program; if not, write to the Free Software Foundation, Inc., - + {% if user.is_adherent %} diff --git a/users/templates/users/profil.html b/users/templates/users/profil.html index c345e4a..cdd85e6 100644 --- a/users/templates/users/profil.html +++ b/users/templates/users/profil.html @@ -65,7 +65,7 @@ with this program; if not, write to the Free Software Foundation, Inc., - + diff --git a/users/views.py b/users/views.py index b8292b7..6d009da 100644 --- a/users/views.py +++ b/users/views.py @@ -81,7 +81,7 @@ def new_user(request): req.save() reset_passwd_mail(req, 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 form({'userform': user}, 'users/user.html', request)
Pseudo{{ request_user.pseudo }}{{ request_user.username }}
Nom
{{ user.name }} {{ user.surname }}{{ user.pseudo }}{{ user.username }} {{ user.email }} {{ user.maxemprunt }}
Pseudo{{ user.pseudo }}{{ user.username }} E-mail {{ user.email }}