Skip to content

Commit

Permalink
Merge pull request #34 from borgia-app/deletion_user
Browse files Browse the repository at this point in the history
Deletion user
  • Loading branch information
Mael Lacour authored May 24, 2018
2 parents 18632d9 + 3d52ede commit 8797e50
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 23 deletions.
5 changes: 4 additions & 1 deletion borgia/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
14 changes: 8 additions & 6 deletions finances/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 !")
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion finances/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
4 changes: 3 additions & 1 deletion modules/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
7 changes: 4 additions & 3 deletions users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions users/templates/users/user_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
{% endif %}
</a></th>
{% endfor %}
<th>Etat</th>
{% if has_perm_retrieve_user %}
<th>Détail</th>
{% endif %}
Expand All @@ -60,7 +59,6 @@
<td>{{ user.campus }}</td>
<td>{{ user.year }}</td>
<td>{{ user.balance }}€</td>
<td>{% if user.is_active %}Activé{% else %}Désactivé{% endif %}</td>
{% if has_perm_retrieve_user %}
<td>
<a href="{% url 'url_user_retrieve' group_name=group_name pk=user.pk %}">Détail</a>
Expand Down
27 changes: 19 additions & 8 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 8797e50

Please sign in to comment.