Empêche la création d'asso orphelines #119

Fusionnée
fpoulain a fusionné 1 révision(s) à partir de fpoulain/benevalibre:asso_orphelines vers develop 2020-01-08 12:20:38 +01:00
2 fichiers modifiés avec 67 ajouts et 9 suppressions

Voir le fichier

@ -374,6 +374,7 @@ class Role(HTMLDocString, AbstractAssociated, AbstractRole):
def clean(self):
# FIXME: impossible de changer le rôle new_comers dans ces conditions
errors = []
if self.association_id:
if (
self.new_comers
@ -383,11 +384,13 @@ class Role(HTMLDocString, AbstractAssociated, AbstractRole):
association_id=self.association_id,
).exists()
):
raise ValidationError(
"Un autre rôle possède déjà l'attribut « nouvel arrivant "
"». Il ne peut n'y en avoir qu'un."
errors.append(
ValidationError(
"Un autre rôle possède déjà l'attribut « nouvel "
"arrivant ». Il ne peut n'y en avoir qu'un."
)
)
elif (
if (
not self.new_comers
and not Role.objects.filter(
~models.Q(id=self.id),
@ -395,11 +398,33 @@ class Role(HTMLDocString, AbstractAssociated, AbstractRole):
association_id=self.association_id,
).exists()
):
raise ValidationError(
"Aucun autre rôle ne possède l'attribut « nouvel arrivant "
"». Vous devez en disposer d'un pour permettre "
"l'inscription d'un⋅e bénévole."
errors.append(
ValidationError(
"Aucun autre rôle ne possède l'attribut « nouvel "
"arrivant ». Vous devez en disposer d'un pour "
"permettre l'inscription d'un⋅e bénévole."
)
)
if (
self.id
and not self.manage_association
and not Engagement.objects.filter(
~models.Q(role_id=self.id),
association_id=self.association_id,
role__manage_association=True,
).exists()
):
errors.append(
ValidationError(
"Modifier ainsi ce rôle rendrait impossible la "
"gestion de l'association. Avant ça, vous devez "
"prévoir qu'au moins un rôle lié à au moins une "
"personne conserve la capacité de gérer "
"l'association.",
)
)
if errors:
raise ValidationError(errors)
class EngagementQuerySet(models.QuerySet):

Voir le fichier

@ -935,7 +935,7 @@ class TestRole(ManagerOnlyViewMixin):
)
assert len(table_row) == 3
def test_self_downgrade_go_back_to_home(self, client, user, manager):
def test_self_downgrade_dont_create_orphans(self, client, user, manager):
client.force_login(user)
assert self.association.can_manage_engagements(user)
@ -950,6 +950,39 @@ class TestRole(ManagerOnlyViewMixin):
follow=True,
)
assert self.association.can_manage_engagements(user)
assert response.status_code == 200
assert response.resolver_match.view_name == 'association:role:update'
assert count_text_in_content(
response, "rendrait impossible la gestion"
)
def test_self_downgrade_go_back_to_home(
self, client, user, foreign, manager
):
# we need to define the next leader before leaving
r = self.association.role_set.get_benevole()
r.manage_association = True
r.save()
self.association.engagement_set.create(user=foreign, role=r)
assert self.association.can_manage_engagements(user)
assert self.association.can_manage_engagements(foreign)
client.force_login(user)
data = {'name': 'underground'}
response = client.post(
reverse(
'association:role:update',
args=[self.association.id, manager.role.id],
),
data,
follow=True,
)
assert not self.association.can_manage_engagements(user)
assert response.status_code == 200