From 9216d0b3b23afa8bd8276d8bbb1424751c909d29 Mon Sep 17 00:00:00 2001
From: Erwan Rouchet <rouchet@teklia.com>
Date: Thu, 3 May 2018 16:23:38 +0200
Subject: [PATCH] Add display_name property

---
 src/documents/models.py | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/src/documents/models.py b/src/documents/models.py
index 5b97f9ebab..292624fe58 100644
--- a/src/documents/models.py
+++ b/src/documents/models.py
@@ -109,6 +109,29 @@ class Page(Document):
     direction = EnumField(PageDirection, max_length=10, null=True, blank=True)
     objects = DocumentManager()
 
+    @property
+    def display_name(self):
+        """
+        Generate a pretty display name for a page.
+        If the folio parsing had failed, will return the raw folio.
+        """
+        if self.page_type is None and self.nb is None and self.direction is None:
+            if self.folio.strip() == "":
+                return "Unknown folio"
+            return self.folio
+
+        text = self.page_type.name if self.page_type is not None else 'Page'
+
+        if self.nb is not None:
+            text += ' no. {0:d}'.format(self.nb)
+        else:
+            text += ', unnumbered'
+
+        if self.direction is not None:
+            text += ', {}'.format(self.direction.value)
+
+        return text.capitalize()
+
     def save(self, *args, **kwargs):
         # TODO: move this in Document through introspection
         self.type = DocumentType.Page
-- 
GitLab