Don't update membership information every time

This commit is contained in:
Yohann D'ANELLO 2020-08-13 18:16:26 +02:00
parent d98e46ffc2
commit 721da093e9
1 changed files with 54 additions and 54 deletions

View File

@ -361,65 +361,65 @@ class Membership(models.Model):
).exists(): ).exists():
raise ValidationError(_('User is already a member of the club')) raise ValidationError(_('User is already a member of the club'))
if self.club.parent_club is not None: if self.club.parent_club is not None:
if not Membership.objects.filter( if not Membership.objects.filter(
user=self.user, user=self.user,
club=self.club.parent_club, club=self.club.parent_club,
date_start__gte=self.club.parent_club.membership_start, date_start__gte=self.club.parent_club.membership_start,
).exists(): ).exists():
if hasattr(self, '_force_renew_parent') and self._force_renew_parent: if hasattr(self, '_force_renew_parent') and self._force_renew_parent:
parent_membership = Membership.objects.filter( parent_membership = Membership.objects.filter(
user=self.user,
club=self.club.parent_club,
).order_by("-date_start")
if parent_membership.exists():
# Renew the previous membership of the parent club
parent_membership = parent_membership.first()
parent_membership._force_renew_parent = True
if hasattr(self, '_soge'):
parent_membership._soge = True
if hasattr(self, '_force_save'):
parent_membership._force_save = True
parent_membership.renew()
else:
# Create a new membership in the parent club
parent_membership = Membership(
user=self.user, user=self.user,
club=self.club.parent_club, club=self.club.parent_club,
date_start=self.date_start, ).order_by("-date_start")
) if parent_membership.exists():
parent_membership._force_renew_parent = True # Renew the previous membership of the parent club
if hasattr(self, '_soge'): parent_membership = parent_membership.first()
parent_membership._soge = True parent_membership._force_renew_parent = True
if hasattr(self, '_force_save'): if hasattr(self, '_soge'):
parent_membership._force_save = True parent_membership._soge = True
parent_membership.save() if hasattr(self, '_force_save'):
parent_membership.refresh_from_db() parent_membership._force_save = True
parent_membership.renew()
if self.club.parent_club.name == "BDE":
parent_membership.roles.set(
Role.objects.filter(Q(name="Adhérent BDE") | Q(name="Membre de club")).all())
elif self.club.parent_club.name == "Kfet":
parent_membership.roles.set(
Role.objects.filter(Q(name="Adhérent Kfet") | Q(name="Membre de club")).all())
else: else:
parent_membership.roles.set(Role.objects.filter(name="Membre de club").all()) # Create a new membership in the parent club
parent_membership.save() parent_membership = Membership(
else: user=self.user,
raise ValidationError(_('User is not a member of the parent club') club=self.club.parent_club,
+ ' ' + self.club.parent_club.name) date_start=self.date_start,
)
parent_membership._force_renew_parent = True
if hasattr(self, '_soge'):
parent_membership._soge = True
if hasattr(self, '_force_save'):
parent_membership._force_save = True
parent_membership.save()
parent_membership.refresh_from_db()
if self.user.profile.paid: if self.club.parent_club.name == "BDE":
self.fee = self.club.membership_fee_paid parent_membership.roles.set(
else: Role.objects.filter(Q(name="Adhérent BDE") | Q(name="Membre de club")).all())
self.fee = self.club.membership_fee_unpaid elif self.club.parent_club.name == "Kfet":
parent_membership.roles.set(
Role.objects.filter(Q(name="Adhérent Kfet") | Q(name="Membre de club")).all())
else:
parent_membership.roles.set(Role.objects.filter(name="Membre de club").all())
parent_membership.save()
else:
raise ValidationError(_('User is not a member of the parent club')
+ ' ' + self.club.parent_club.name)
if self.club.membership_duration is not None: if self.user.profile.paid:
self.date_end = self.date_start + datetime.timedelta(days=self.club.membership_duration) self.fee = self.club.membership_fee_paid
else: else:
self.date_end = self.date_start + datetime.timedelta(days=424242) self.fee = self.club.membership_fee_unpaid
if self.club.membership_end is not None and self.date_end > self.club.membership_end:
self.date_end = self.club.membership_end if self.club.membership_duration is not None:
self.date_end = self.date_start + datetime.timedelta(days=self.club.membership_duration)
else:
self.date_end = self.date_start + datetime.timedelta(days=424242)
if self.club.membership_end is not None and self.date_end > self.club.membership_end:
self.date_end = self.club.membership_end
super().save(*args, **kwargs) super().save(*args, **kwargs)