diff --git a/borgia/forms.py b/borgia/forms.py index 1f3a7289..c56704ee 100644 --- a/borgia/forms.py +++ b/borgia/forms.py @@ -37,9 +37,12 @@ def clean(self): # User credential check try: - User.objects.get(username=cleaned_data['username']) + u = User.objects.get(username=cleaned_data['username']) except ObjectDoesNotExist: raise forms.ValidationError("L'utilisateur n'existe pas") + if not u.is_active: + raise forms.ValidationError("L'utilisateur a été desactivé") + user = authenticate( username=cleaned_data['username'], password=cleaned_data['password'] diff --git a/finances/forms.py b/finances/forms.py index ee8d39ea..f027bd90 100644 --- a/finances/forms.py +++ b/finances/forms.py @@ -56,6 +56,8 @@ def clean_recipient(self): except ObjectDoesNotExist: raise forms.ValidationError("L'utilisateur n'existe pas !") + if not recipient.is_active: + raise forms.ValidationError("L'utilisateur a été desactivé !") if self.sender == recipient: # Send to self : Impossible raise forms.ValidationError("Vous ne pouvez pas transferez à vous même !") @@ -344,7 +346,10 @@ def clean_user(self): try: user = User.objects.get(username=username) except ObjectDoesNotExist: - raise forms.ValidationError("L'utilisateur n'existe pas !") + raise forms.ValidationError("L'utilisateur n'existe pas !") + + if not user.is_active : + raise forms.ValidationError("L'utilisateur a été desactivé !") return user @@ -353,6 +358,7 @@ class SharedEventDownloadXlsxForm(forms.Form): state = forms.ChoiceField(label='Selection', choices=(('year', 'Listes de promotions'), ('registrants', 'Préinscrit'), ('participants', 'Participants'))) + years = forms.MultipleChoiceField(label='Année(s) à inclure', required=False) def __init__(self, **kwargs): super(SharedEventDownloadXlsxForm, self).__init__(**kwargs) @@ -361,11 +367,7 @@ def __init__(self, **kwargs): YEAR_CHOICES.append( (year, year) ) - self.fields['years'] = forms.MultipleChoiceField( - label='Année à inclure ', - choices=YEAR_CHOICES, - required=False - ) + self.fields['years'].choices = YEAR_CHOICES class SharedEventUploadXlsxForm(forms.Form): diff --git a/finances/views.py b/finances/views.py index 0a076180..2b630695 100644 --- a/finances/views.py +++ b/finances/views.py @@ -1742,7 +1742,7 @@ def form_valid(self, form): if form.cleaned_data['years']: list_year_result = form.cleaned_data['years'] # Contains the years selected - users = User.objects.filter(year__in=list_year_result).exclude(groups=Group.objects.get(pk=1)).order_by('username') + users = User.objects.filter(year__in=list_year_result, is_active=True).exclude(groups=Group.objects.get(pk=1)).order_by('username') for u in users: ws.append([u.username, '', '',u.last_name + ' ' + u.first_name, u.surname]) diff --git a/modules/forms.py b/modules/forms.py index 2081c9da..ce6bbc67 100644 --- a/modules/forms.py +++ b/modules/forms.py @@ -47,9 +47,11 @@ def clean(self): self.client = User.objects.get( username= self.cleaned_data['client']) except ObjectDoesNotExist: - raise forms.ValidationError('Utilisateur inconnu') + raise forms.ValidationError("L'utilisateur n'existe pas") except KeyError: raise forms.ValidationError('Utilisateur non sélectionné') + if not self.client.is_active: + raise forms.ValidationError("L'utilisateur a été desactivé") total_price = 0 for field in self.cleaned_data: if field != 'client': diff --git a/users/forms.py b/users/forms.py index 461f28a3..48affa7f 100644 --- a/users/forms.py +++ b/users/forms.py @@ -141,9 +141,10 @@ class UserSearchForm(forms.Form): 'autofocus': 'true', 'placeholder': "Nom / Prénom / Surnom"})) year = forms.ChoiceField(label='Année', required=False) - state = forms.ChoiceField(label='Etat', choices=(('all', 'Tous les utilisateurs'), - ('True', 'Uniquement les activés'), - ('False', 'Uniquement les desactivés')), + state = forms.ChoiceField(label='Etat', choices=(('all', 'Tous les actifs'), + ('negative_balance', 'Uniquement ceux à solde négative'), + ('threshold', 'Uniquement ceux en-dessous du seuil de commande'), + ('unactive', 'Uniquement ceux désactivés')), required=False) def __init__(self, **kwargs): diff --git a/users/models.py b/users/models.py index af11f727..7f6cab71 100644 --- a/users/models.py +++ b/users/models.py @@ -310,7 +310,7 @@ def list_year(): :returns: list of integer years used by users, sorted the decreasing dates. """ list_year = [] - for u in User.objects.all().exclude(groups=6).exclude(pk=1): # Parmis tout les users moins les gadz d'honn'ss et l'admin + for u in User.objects.filter(is_active=True).exclude(groups=6).exclude(pk=1): # Parmis tout les users moins les gadz d'honn'ss et l'admin if u.year not in list_year: if u.year is not None: # year is not mandatory list_year.append(u.year) diff --git a/users/templates/users/user_list.html b/users/templates/users/user_list.html index 93959381..96186a84 100644 --- a/users/templates/users/user_list.html +++ b/users/templates/users/user_list.html @@ -44,7 +44,6 @@ {% endif %} {% endfor %} - Etat {% if has_perm_retrieve_user %} Détail {% endif %} @@ -60,7 +59,6 @@ {{ user.campus }} {{ user.year }} {{ user.balance }}€ - {% if user.is_active %}Activé{% else %}Désactivé{% endif %} {% if has_perm_retrieve_user %} Détail diff --git a/users/views.py b/users/views.py index cb31c8d7..823b87c0 100644 --- a/users/views.py +++ b/users/views.py @@ -7,6 +7,7 @@ from django.views.generic import FormView, View from django.db.models import Q from django.http import HttpResponseBadRequest +from settings_data.utils import settings_safe_get from users.forms import * from users.models import ExtendedPermission @@ -53,7 +54,7 @@ def get_form_kwargs(self): """ Add possible members and permissions to kwargs of the form. - Possible members are all members, except specials members. + Possible members are all members, except specials members and unactive users. Possible permissions are all permissions. :note:: For the special case of a shop management, two groups exist: group of chiefs and group of associates. If the group of associates is @@ -75,7 +76,7 @@ def get_form_kwargs(self): pk__in=human_unused_permissions() ) - kwargs['possible_members'] = User.objects.all().exclude( + kwargs['possible_members'] = User.objects.filter(is_active=True).exclude( groups=Group.objects.get(name='specials')) return kwargs @@ -307,6 +308,9 @@ def post(self, request, *args, **kwargs): user = User.objects.get(pk=kwargs['pk']) if user.is_active is True: user.is_active = False + if Group.objects.get(pk=5) in user.groups.all(): # si c'est un gadz. Special members can't be added to other groups + user.groups.clear() + user.groups.add(Group.objects.get(pk=5)) else: user.is_active = True user.save() @@ -391,8 +395,15 @@ def form_query(self, query): year=self.year) if self.state and self.state != 'all': - query = query.filter( - is_active=self.state) + if self.state == 'negative_balance': + query = query.filter(balance__lt=0.0, is_active=True) + elif self.state == 'threshold': + threshold = settings_safe_get('BALANCE_THRESHOLD_PURCHASE').get_value() + query = query.filter(balance__lt=threshold, is_active=True) + elif self.state == 'unactive': + query = query.filter(is_active=False) + else: + query = query.filter(is_active=True) return query @@ -424,16 +435,16 @@ def username_from_username_part(request): # Fam'ss en entier # where_search = User.objects.filter(family=key).exclude(groups=1).order_by('-year') - where_search = User.objects.exclude(groups=1).filter( family__regex = regex ).order_by('-year') + where_search = User.objects.exclude(groups=1).filter( family__regex = regex, is_active=True ).order_by('-year') if len(key) > 2: if key.isalpha(): # Nom de famille, début ou entier à partir de 3 caractères - where_search = where_search | User.objects.filter(last_name__istartswith=key) + where_search = where_search | User.objects.filter(last_name__istartswith=key, is_active=True) # Prénom, début ou entier à partir de 3 caractères - where_search = where_search | User.objects.filter(first_name__istartswith=key) + where_search = where_search | User.objects.filter(first_name__istartswith=key, is_active=True) # Buque, début ou entier à partir de 3 caractères - where_search = where_search | User.objects.filter(surname__istartswith=key) + where_search = where_search | User.objects.filter(surname__istartswith=key, is_active=True) # Suppression des doublons where_search = where_search.distinct()