clubs: move Membership form validation outside of model and fix and add tests

This commit is contained in:
2019-04-24 13:48:32 +02:00
parent d5ad2c5141
commit 80f1f9699c
3 changed files with 65 additions and 25 deletions

View File

@ -326,8 +326,9 @@ class ClubMemberForm(forms.Form):
self.request_user = kwargs.pop("request_user")
super(ClubMemberForm, self).__init__(*args, **kwargs)
# Using a ModelForm forces a save and we don't want that
# Using a ModelForm binds too much the form with the model and we don't want that
# We want the view to process the model creation since they are multiple users
# We also want the form to handle bulk deletion
self.fields.update(
forms.fields_for_model(
Membership,
@ -341,6 +342,7 @@ class ClubMemberForm(forms.Form):
def clean_users(self):
"""
Check that the user is not trying to add an user already in the club
Also check that the user is valid and has a valid subscription
"""
cleaned_data = super(ClubMemberForm, self).clean()
users = []
@ -348,13 +350,17 @@ class ClubMemberForm(forms.Form):
user = User.objects.filter(id=user_id).first()
if not user:
raise forms.ValidationError(
_("One of the selected users doesn't exist", code="invalid")
_("One of the selected users doesn't exist"), code="invalid"
)
if not user.is_subscribed:
raise forms.ValidationError(
_("User must be subscriber to take part to a club"), code="invalid"
)
users.append(user)
if self.club.get_membership_for(user):
raise forms.ValidationError(
_("You can not add the same user twice"), code="invalid"
)
users.append(user)
return users
def clean(self):
@ -364,7 +370,6 @@ class ClubMemberForm(forms.Form):
cleaned_data = super(ClubMemberForm, self).clean()
request_user = self.request_user
membership = self.club.get_membership_for(request_user)
print(request_user.is_root)
if not (
cleaned_data["role"] <= SITH_MAXIMUM_FREE_ROLE
or (membership is not None and membership.role >= cleaned_data["role"])