From fb8e6cafef5383c02d3e82a6e06dfd13f0726691 Mon Sep 17 00:00:00 2001
From: Alexandre Iooss <erdnaxe@crans.org>
Date: Wed, 24 Jul 2019 22:40:31 +0200
Subject: [PATCH] Implement clean methode for Note

---
 note/locale/fr/LC_MESSAGES/django.po | 82 +++++++++++++++++-----------
 note/models/notes.py                 | 21 +++++--
 2 files changed, 67 insertions(+), 36 deletions(-)

diff --git a/note/locale/fr/LC_MESSAGES/django.po b/note/locale/fr/LC_MESSAGES/django.po
index 125efe0c..7a9933f6 100644
--- a/note/locale/fr/LC_MESSAGES/django.po
+++ b/note/locale/fr/LC_MESSAGES/django.po
@@ -3,7 +3,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2019-07-17 13:48+0200\n"
+"POT-Creation-Date: 2019-07-24 22:37+0200\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -17,105 +17,125 @@ msgstr ""
 msgid "source"
 msgstr "source"
 
-#: admin.py:120 admin.py:138 models/transactions.py:25
+#: admin.py:120 admin.py:148 models/transactions.py:25
 #: models/transactions.py:52
 msgid "destination"
 msgstr "destination"
 
-#: apps.py:11 models/notes.py:45
+#: apps.py:15 models/notes.py:47
 msgid "note"
 msgstr "note"
 
-#: models/notes.py:22
+#: models/notes.py:24
 msgid "account balance"
 msgstr "solde du compte"
 
-#: models/notes.py:23
+#: models/notes.py:25
 msgid "in centimes, money credited for this instance"
 msgstr "en centimes, argent crédité pour cette instance"
 
-#: models/notes.py:27
+#: models/notes.py:29
 msgid "active"
 msgstr "actif"
 
-#: models/notes.py:30
+#: models/notes.py:32
 msgid ""
 "Designates whether this note should be treated as active. Unselect this "
 "instead of deleting notes."
 msgstr ""
 "Indique si la note est active. Désactiver cela plutôt que supprimer la note."
 
-#: models/notes.py:35
+#: models/notes.py:37
 msgid "display image"
 msgstr "image affichée"
 
-#: models/notes.py:40 models/transactions.py:55
+#: models/notes.py:42 models/transactions.py:55
 msgid "created at"
 msgstr "créée le"
 
-#: models/notes.py:46
+#: models/notes.py:48
 msgid "notes"
 msgstr "notes"
 
-#: models/notes.py:57
+#: models/notes.py:56
+msgid "Note"
+msgstr "Note"
+
+#: models/notes.py:66 models/notes.py:88
+msgid "This alias is already taken."
+msgstr "Cet alias est déjà pris."
+
+#: models/notes.py:103
 msgid "user"
 msgstr "utilisateur"
 
-#: models/notes.py:61
+#: models/notes.py:107
 msgid "one's note"
 msgstr "note d'un utilisateur"
 
-#: models/notes.py:62
+#: models/notes.py:108
 msgid "users note"
 msgstr "notes des utilisateurs"
 
-#: models/notes.py:65
+#: models/notes.py:114
 #, python-format
 msgid "%(user)s's note"
 msgstr "Note de %(user)s"
 
-#: models/notes.py:76
+#: models/notes.py:125
 msgid "club"
 msgstr "club"
 
-#: models/notes.py:80
+#: models/notes.py:129
 msgid "club note"
 msgstr "note d'un club"
 
-#: models/notes.py:81
+#: models/notes.py:130
 msgid "clubs notes"
 msgstr "notes des clubs"
 
-#: models/notes.py:84
+#: models/notes.py:136
 #, python-format
 msgid "Note for %(club)s club"
 msgstr "Note du club %(club)s"
 
-#: models/notes.py:97 models/transactions.py:32 models/transactions.py:65
+#: models/notes.py:149 models/transactions.py:32 models/transactions.py:66
 msgid "type"
 msgstr "type"
 
-#: models/notes.py:103
+#: models/notes.py:155
 msgid "special note"
 msgstr "note spéciale"
 
-#: models/notes.py:104
+#: models/notes.py:156
 msgid "special notes"
 msgstr "notes spéciales"
 
-#: models/notes.py:115 models/transactions.py:18
+#: models/notes.py:167 models/transactions.py:18
 msgid "name"
 msgstr "nom"
 
-#: models/notes.py:125
+#: models/notes.py:173
+msgid "Invalid alias"
+msgstr "Alias invalide"
+
+#: models/notes.py:189
 msgid "alias"
 msgstr "alias"
 
-#: models/notes.py:126
+#: models/notes.py:190
 msgid "aliases"
 msgstr "alias"
 
-#: models/transactions.py:28 models/transactions.py:62
+#: models/notes.py:218
+msgid "Alias too long."
+msgstr "L'alias est trop long."
+
+#: models/notes.py:221
+msgid "An alias with a similar name already exists."
+msgstr "Un alias avec un nom similaire existe déjà."
+
+#: models/transactions.py:28 models/transactions.py:63
 msgid "amount"
 msgstr "montant"
 
@@ -135,27 +155,27 @@ msgstr "modèles de transaction"
 msgid "quantity"
 msgstr "quantité"
 
-#: models/transactions.py:69
+#: models/transactions.py:70
 msgid "reason"
 msgstr "raison"
 
-#: models/transactions.py:73
+#: models/transactions.py:74
 msgid "valid"
 msgstr "valide"
 
-#: models/transactions.py:77
+#: models/transactions.py:79
 msgid "transaction"
 msgstr "transaction"
 
-#: models/transactions.py:78
+#: models/transactions.py:80
 msgid "transactions"
 msgstr "transactions"
 
-#: models/transactions.py:89
+#: models/transactions.py:116
 msgid "membership transaction"
 msgstr "transaction d'adhésion"
 
-#: models/transactions.py:90
+#: models/transactions.py:117
 msgid "membership transactions"
 msgstr "transactions d'adhésion"
 
diff --git a/note/models/notes.py b/note/models/notes.py
index cc764724..d83514bb 100644
--- a/note/models/notes.py
+++ b/note/models/notes.py
@@ -51,7 +51,7 @@ class Note(PolymorphicModel):
         """
         :return: Pretty name of this note
         """
-        return 'Not implemented'
+        return str(self)
 
     pretty.short_description = _('Note')
 
@@ -63,7 +63,7 @@ class Note(PolymorphicModel):
         if aliases.exists():
             # Alias exists, so check if it is linked to this note
             if aliases.first().note != self:
-                raise ValidationError
+                raise ValidationError(_('This alias is already taken.'))
 
             # Save note
             super().save(*args, **kwargs)
@@ -77,6 +77,20 @@ class Note(PolymorphicModel):
             a.note = self
             a.save(force_insert=True)
 
+    def clean(self, *args, **kwargs):
+        """
+        Verify alias (simulate save)
+        """
+        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.'))
+        else:
+            # Alias does not exist yet, so check if it can exist
+            a = Alias(name=str(self))
+            a.clean()
+
 
 class NoteUser(Note):
     """
@@ -144,9 +158,6 @@ class NoteSpecial(Note):
     def __str__(self):
         return self.special_type
 
-    def pretty(self):
-        return self.special_type
-
 
 class Alias(models.Model):
     """
-- 
GitLab