diff --git a/apps/note/models/notes.py b/apps/note/models/notes.py index 46c8af44..53dd18ef 100644 --- a/apps/note/models/notes.py +++ b/apps/note/models/notes.py @@ -95,22 +95,16 @@ class Note(PolymorphicModel): """ Save note with it's alias (called in polymorphic children) """ - aliases = Alias.objects.filter(name=str(self)) - if aliases.exists(): - # Alias exists, so check if it is linked to this note - if aliases.first().note != self: - raise ValidationError(_('This alias is already taken.'), - code="same_alias") + # Check that we can save the alias + self.clean() - # Save note - super().save(*args, **kwargs) - else: - # Alias does not exist yet, so check if it can exist + super().save(*args, **kwargs) + + if not Alias.objects.filter(name=str(self)).exists(): a = Alias(name=str(self)) a.clean() - # Save note and alias - super().save(*args, **kwargs) + # Save alias a.note = self a.save(force_insert=True) @@ -155,9 +149,9 @@ class NoteUser(Note): def save(self, *args, **kwargs): if self.pk and self.balance < 0: old_note = NoteUser.objects.get(pk=self.pk) + super().save(*args, **kwargs) if old_note.balance >= 0: # Passage en négatif - super().save(*args, **kwargs) self.last_negative = timezone.now() self._force_save = True self.save(*args, **kwargs) @@ -196,6 +190,7 @@ class NoteClub(Note): def save(self, *args, **kwargs): if self.pk and self.balance < 0: old_note = NoteClub.objects.get(pk=self.pk) + super().save(*args, **kwargs) if old_note.balance >= 0: # Passage en négatif super().save(*args, **kwargs) @@ -203,7 +198,8 @@ class NoteClub(Note): self._force_save = True self.save(*args, **kwargs) self.send_mail_negative_balance() - super().save(*args, **kwargs) + else: + super().save(*args, **kwargs) def send_mail_negative_balance(self): plain_text = render_to_string("note/mails/negative_balance.txt", dict(note=self)) @@ -308,7 +304,7 @@ class Alias(models.Model): self.normalized_name = normalized_name def save(self, *args, **kwargs): - self.normalized_name = self.normalize(self.name) + self.clean() super().save(*args, **kwargs) def delete(self, using=None, keep_parents=False): diff --git a/apps/note/models/transactions.py b/apps/note/models/transactions.py index f9b9dbb0..d88be5a6 100644 --- a/apps/note/models/transactions.py +++ b/apps/note/models/transactions.py @@ -273,10 +273,15 @@ class RecurrentTransaction(Transaction): on_delete=models.PROTECT, ) - category = models.ForeignKey( - TemplateCategory, - on_delete=models.PROTECT, - ) + def clean(self): + if self.template.destination != self.destination: + raise ValidationError( + _("The destination of this transaction must equal to the destination of the template.")) + return super().clean() + + def save(self, *args, **kwargs): + self.clean() + return super().save(*args, **kwargs) @property def type(self): @@ -324,6 +329,10 @@ class SpecialTransaction(Transaction): raise(ValidationError(_("A special transaction is only possible between a" " Note associated to a payment method and a User or a Club"))) + def save(self, *args, **kwargs): + self.clean() + super().save(*args, **kwargs) + class Meta: verbose_name = _("Special transaction") verbose_name_plural = _("Special transactions") diff --git a/note_kfet/static/js/consos.js b/note_kfet/static/js/consos.js index cdd7e964..fc04b2b2 100644 --- a/note_kfet/static/js/consos.js +++ b/note_kfet/static/js/consos.js @@ -226,7 +226,6 @@ function consume(source, source_alias, dest, quantity, amount, reason, type, cat "source": source.id, "source_alias": source_alias, "destination": dest, - "category": category, "template": template }) .done(function () { @@ -259,7 +258,6 @@ function consume(source, source_alias, dest, quantity, amount, reason, type, cat "source": source, "source_alias": source_alias, "destination": dest, - "category": category, "template": template }).done(function() { reset();