diff --git a/api/views.py b/api/views.py index e9528622..d117057a 100644 --- a/api/views.py +++ b/api/views.py @@ -29,6 +29,7 @@ the response (JSON or other), the CSRF exempting, ... import datetime from django.conf import settings +from django.db.models import Q from rest_framework.authtoken.views import ObtainAuthToken from rest_framework.authtoken.models import Token from rest_framework.response import Response @@ -421,7 +422,7 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet): class HomeCreationViewSet(viewsets.ReadOnlyModelViewSet): """Exposes infos of `users.models.Users` objects to create homes. """ - queryset = users.User.objects.filter(state = 0) + queryset = users.User.objects.exclude(Q(state=User.STATE_DISABLED) | Q(state=User.STATE_NOT_YET_ACTIVE)) serializer_class = serializers.HomeCreationSerializer class ClubViewSet(viewsets.ReadOnlyModelViewSet): diff --git a/cotisations/models.py b/cotisations/models.py index 06791f89..8734c17c 100644 --- a/cotisations/models.py +++ b/cotisations/models.py @@ -243,10 +243,8 @@ def facture_post_save(**kwargs): """ facture = kwargs['instance'] user = facture.user - if facture.valid and facture.vente_set.filter(Q(type_cotisation='All') | Q(type_cotisation='Adhesion')).exists(): - user.state = 0 - user.save() - user.ldap_sync(base=False, access_refresh=True, mac_refresh=False) + user.set_active() + user.ldap_sync(base=True, access_refresh=True, mac_refresh=False) @receiver(post_delete, sender=Facture) @@ -475,12 +473,8 @@ def vente_post_save(**kwargs): purchase.create_cotis() purchase.cotisation.save() user = purchase.facture.user - user.ldap_sync(base=False, access_refresh=True, mac_refresh=False) - if purchase.facture.valid and (purchase.type_cotisation == 'All' or purchase.type_cotisation == 'Adhesion'): - user = purchase.facture.user - if user.state == 3: - user.state = 0 - user.save() + user.set_active() + user.ldap_sync(base=True, access_refresh=True, mac_refresh=False) # TODO : change vente to purchase diff --git a/users/models.py b/users/models.py index d73d5b98..212754e2 100755 --- a/users/models.py +++ b/users/models.py @@ -333,6 +333,14 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, """ Renvoie si l'user est à l'état actif""" return self.state == self.STATE_ACTIVE or self.state == self.STATE_NOT_YET_ACTIVE + def set_active(self): + """Enable this user if he subscribed successfully one time before + Active l'utilisateur définitivement si il a adhéré au moins une fois""" + if self.state == self.STATE_NOT_YET_ACTIVE: + if self.facture_set.filter(valid=True).filter(Q(vente__type_cotisation='All') | Q(vente__type_cotisation='Adhesion')).exists(): + self.state = self.STATE_ACTIVE + self.save() + @property def is_staff(self): """ Fonction de base django, renvoie si l'user est admin"""