diff --git a/search/views.py b/search/views.py index fa9a43e8..510837e3 100644 --- a/search/views.py +++ b/search/views.py @@ -74,20 +74,20 @@ def search_result(search, type, request): recherche = {'users_list': None, 'machines_list' : [], 'facture_list' : None, 'ban_list' : None, 'white_list': None, 'port_list': None, 'switch_list': None} if request.user.has_perms(('cableur',)): - query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search) + query = Q(user__pseudo__icontains = search) | Q(user__adherent__name__icontains = search) | Q(user__surname__icontains = search) else: - query = (Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)) & Q(user = request.user) + query = (Q(user__pseudo__icontains = search) | Q(user__adherent__name__icontains = search) | Q(user__surname__icontains = search)) & Q(user = request.user) for i in aff: if i == '0': - query_user_list = Q(room__name__icontains = search) | Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search) & query1 + query_user_list = Q(room__name__icontains = search) | Q(pseudo__icontains = search) | Q(adherent__name__icontains = search) | Q(surname__icontains = search) & query1 if request.user.has_perms(('cableur',)): recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname').distinct() else : recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by('state', 'surname').distinct() if i == '1': - query_machine_list = Q(machine__user__pseudo__icontains = search) | Q(machine__user__name__icontains = search) | Q(machine__user__surname__icontains = search) | Q(mac_address__icontains = search) | Q(ipv4__ipv4__icontains = search) | Q(domain__name__icontains = search) | Q(domain__related_domain__name__icontains = search) + query_machine_list = Q(machine__user__pseudo__icontains = search) | Q(machine__user__adherent__name__icontains = search) | Q(machine__user__surname__icontains = search) | Q(mac_address__icontains = search) | Q(ipv4__ipv4__icontains = search) | Q(domain__name__icontains = search) | Q(domain__related_domain__name__icontains = search) if request.user.has_perms(('cableur',)): data = Interface.objects.filter(query_machine_list).distinct() else: diff --git a/users/forms.py b/users/forms.py index 9b0b1fb6..f07e3d28 100644 --- a/users/forms.py +++ b/users/forms.py @@ -99,7 +99,7 @@ class UserCreationForm(forms.ModelForm): super(UserCreationForm, self).__init__(*args, prefix=prefix, **kwargs) class Meta: - model = User + model = Adherent fields = ('pseudo', 'surname', 'email') def clean_password2(self): @@ -179,7 +179,7 @@ class UserChangeForm(forms.ModelForm): is_admin = forms.BooleanField(label='is admin', required=False) class Meta: - model = User + model = Adherent fields = ('pseudo', 'password', 'surname', 'email') def __init__(self, *args, **kwargs): @@ -252,13 +252,13 @@ class MassArchiveForm(forms.Form): utilisateurs dont la fin d'accès se situe dans le futur !") -class BaseInfoForm(ModelForm): +class NewUserForm(ModelForm): """Formulaire de base d'edition d'un user. Formulaire de base, utilisé pour l'edition de self par self ou un cableur. On formate les champs avec des label plus jolis""" def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) - super(BaseInfoForm, self).__init__(*args, prefix=prefix, **kwargs) + super(NewUserForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['name'].label = 'Prénom' self.fields['surname'].label = 'Nom' self.fields['school'].label = 'Établissement' @@ -292,13 +292,89 @@ class BaseInfoForm(ModelForm): return telephone +class NewClubForm(ModelForm): + """Formulaire de base d'edition d'un user. Formulaire de base, utilisé + pour l'edition de self par self ou un cableur. On formate les champs + avec des label plus jolis""" + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', self.Meta.model.__name__) + super(NewClubForm, self).__init__(*args, prefix=prefix, **kwargs) + self.fields['surname'].label = 'Nom' + self.fields['school'].label = 'Établissement' + self.fields['comment'].label = 'Commentaire' + self.fields['room'].label = 'Chambre' + self.fields['room'].empty_label = "Pas de chambre" + self.fields['school'].empty_label = "Séléctionner un établissement" + + class Meta: + model = Club + fields = [ + 'surname', + 'pseudo', + 'email', + 'school', + 'comment', + 'room', + 'telephone', + ] + + def clean_telephone(self): + """Verifie que le tel est présent si 'option est validée + dans preferences""" + telephone = self.cleaned_data['telephone'] + preferences, _created = OptionalUser.objects.get_or_create() + if not telephone and preferences.is_tel_mandatory: + raise forms.ValidationError( + "Un numéro de téléphone valide est requis" + ) + return telephone + + + +class BaseInfoForm(ModelForm): + """Formulaire de base d'edition d'un user. Formulaire de base, utilisé + pour l'edition de self par self ou un cableur. On formate les champs + avec des label plus jolis""" + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', self.Meta.model.__name__) + super(BaseInfoForm, self).__init__(*args, prefix=prefix, **kwargs) + self.fields['surname'].label = 'Nom' + self.fields['school'].label = 'Établissement' + self.fields['comment'].label = 'Commentaire' + self.fields['room'].label = 'Chambre' + self.fields['room'].empty_label = "Pas de chambre" + self.fields['school'].empty_label = "Séléctionner un établissement" + + class Meta: + model = User + fields = [ + 'surname', + 'pseudo', + 'email', + 'school', + 'comment', + 'room', + 'telephone', + ] + + def clean_telephone(self): + """Verifie que le tel est présent si 'option est validée + dans preferences""" + telephone = self.cleaned_data['telephone'] + preferences, _created = OptionalUser.objects.get_or_create() + if not telephone and preferences.is_tel_mandatory: + raise forms.ValidationError( + "Un numéro de téléphone valide est requis" + ) + return telephone + + class EditInfoForm(BaseInfoForm): """Edition complète d'un user. Utilisé par admin, permet d'editer normalement la chambre, ou le shell Herite de la base""" class Meta(BaseInfoForm.Meta): fields = [ - 'name', 'surname', 'pseudo', 'email', diff --git a/users/models.py b/users/models.py index 64b8fe03..777352db 100644 --- a/users/models.py +++ b/users/models.py @@ -149,7 +149,6 @@ class UserManager(BaseUserManager): def _create_user( self, pseudo, - name, surname, email, password=None, @@ -163,7 +162,6 @@ class UserManager(BaseUserManager): user = self.model( pseudo=pseudo, - name=name, surname=surname, email=self.normalize_email(email), ) @@ -174,19 +172,19 @@ class UserManager(BaseUserManager): user.make_admin() return user - def create_user(self, pseudo, name, surname, email, password=None): + def create_user(self, pseudo, surname, email, password=None): """ Creates and saves a User with the given pseudo, name, surname, email, and password. """ - return self._create_user(pseudo, name, surname, email, password, False) + return self._create_user(pseudo, surname, email, password, False) - def create_superuser(self, pseudo, name, surname, email, password): + def create_superuser(self, pseudo, surname, email, password): """ Creates and saves a superuser with the given pseudo, name, surname, email, and password. """ - return self._create_user(pseudo, name, surname, email, password, True) + return self._create_user(pseudo, surname, email, password, True) class User(AbstractBaseUser): @@ -246,10 +244,36 @@ class User(AbstractBaseUser): rezo_rez_uid = models.PositiveIntegerField(unique=True, blank=True, null=True) USERNAME_FIELD = 'pseudo' - REQUIRED_FIELDS = ['name', 'surname', 'email'] + REQUIRED_FIELDS = ['surname', 'email'] objects = UserManager() + @cached_property + def name(self): + """Si il s'agit d'un adhérent, on renvoie le prénom""" + if self.is_class_adherent: + return self.adherent.name + else: + return '' + + @cached_property + def class_name(self): + """Renvoie si il s'agit d'un adhérent ou d'un club""" + if hasattr(self, 'adherent'): + return "Adhérent" + elif hasattr(self, 'club'): + return "Club" + else: + raise NotImplementedError("Type inconnu") + + @cached_property + def is_class_club(self): + return hasattr(self, 'club') + + @cached_property + def is_class_adherent(self): + return hasattr(self, 'adherent') + @property def is_active(self): """ Renvoie si l'user est à l'état actif""" @@ -280,11 +304,15 @@ class User(AbstractBaseUser): def get_full_name(self): """ Renvoie le nom complet de l'user formaté nom/prénom""" - return '%s %s' % (self.name, self.surname) + name = self.name + if name: + return '%s %s' % (name, self.surname) + else: + return self.surname def get_short_name(self): """ Renvoie seulement le nom""" - return self.name + return self.surname def has_perms(self, perms, obj=None): """ Renvoie true si l'user dispose de la permission. @@ -570,7 +598,7 @@ class User(AbstractBaseUser): .objects.get_or_create() general_options, _created = GeneralOption.objects.get_or_create() context = Context({ - 'nom': str(self.name) + ' ' + str(self.surname), + 'nom': self.get_full_name(), 'asso_name': assooptions.name, 'asso_email': assooptions.contact, 'welcome_mail_fr': mailmessageoptions.welcome_mail_fr, @@ -599,7 +627,7 @@ class User(AbstractBaseUser): options, _created = AssoOption.objects.get_or_create() general_options, _created = GeneralOption.objects.get_or_create() context = { - 'name': str(req.user.name) + ' ' + str(req.user.surname), + 'name': req.user.get_full_name(), 'asso': options.name, 'asso_mail': options.contact, 'site_name': general_options.site_name, @@ -929,7 +957,7 @@ class Ban(models.Model): template = loader.get_template('users/email_ban_notif') options, _created = AssoOption.objects.get_or_create() context = Context({ - 'name': str(self.user.name) + ' ' + str(self.user.surname), + 'name': self.user.get_full_name(), 'raison': self.raison, 'date_end': self.date_end, 'asso_name': options.name, diff --git a/users/templates/users/aff_clubs.html b/users/templates/users/aff_clubs.html new file mode 100644 index 00000000..71ac1606 --- /dev/null +++ b/users/templates/users/aff_clubs.html @@ -0,0 +1,57 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au rezometz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2017 Gabriel Détraz +Copyright © 2017 Goulven Kermarec +Copyright © 2017 Augustin Lemesle + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% if clubs_list.paginator %} +{% include "pagination.html" with list=clubs_list %} +{% endif %} + +