From 92c8718976c63d1305ed168843d2e1cc98cbe6d4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Wed, 3 Jun 2020 11:14:58 +0200
Subject: [PATCH] admin: factor "last modification block" code (#43564)

---
 wcs/admin/forms.py      | 20 ++----------------
 wcs/admin/utils.py      | 47 +++++++++++++++++++++++++++++++++++++++++
 wcs/admin/workflows.py  | 21 +++---------------
 wcs/backoffice/cards.py | 20 ++----------------
 4 files changed, 54 insertions(+), 54 deletions(-)
 create mode 100644 wcs/admin/utils.py

diff --git a/wcs/admin/forms.py b/wcs/admin/forms.py
index 277a5b769..4fe424871 100644
--- a/wcs/admin/forms.py
+++ b/wcs/admin/forms.py
@@ -44,6 +44,7 @@ from wcs.roles import Role, logged_users_role, get_user_roles
 from wcs.workflows import Workflow
 from wcs.forms.root import qrcode
 
+from . import utils
 from .fields import FieldDefPage, FieldsDirectory
 from .categories import CategoriesDirectory
 from .data_sources import NamedDataSourcesDirectory
@@ -402,24 +403,7 @@ class FormDefPage(Directory):
         r += htmltext('</span>')
         r += htmltext('</div>')
 
-        if self.formdef.last_modification_time:
-            warning_class = ''
-            if (time.time() - time.mktime(self.formdef.last_modification_time)) < 600:
-                if get_request().user and str(get_request().user.id) != self.formdef.last_modification_user_id:
-                    warning_class = 'recent'
-            r += htmltext('<p class="last-modification %s">') % warning_class
-            r += _('Last Modification:')
-            r += ' '
-            r += misc.localstrftime(self.formdef.last_modification_time)
-            r += ' '
-            if self.formdef.last_modification_user_id:
-                try:
-                    r += _('by %s') % get_publisher().user_class.get(
-                                    self.formdef.last_modification_user_id).display_name
-                except KeyError:
-                    pass
-            r += htmltext('</p>')
-
+        r += utils.last_modification_block(obj=self.formdef)
         r += get_session().display_message()
 
         def add_option_line(link, label, current_value):
diff --git a/wcs/admin/utils.py b/wcs/admin/utils.py
new file mode 100644
index 000000000..709263cb0
--- /dev/null
+++ b/wcs/admin/utils.py
@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+#
+# w.c.s. - web application for online forms
+# Copyright (C) 2005-2010  Entr'ouvert
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, see <http://www.gnu.org/licenses/>.
+
+import time
+
+from quixote import get_publisher, get_request
+from quixote.html import TemplateIO, htmltext
+
+from qommon import _, misc
+
+
+def last_modification_block(obj):
+    r = TemplateIO(html=True)
+    if obj.last_modification_time:
+        warning_class = ''
+        if (time.time() - time.mktime(obj.last_modification_time)) < 600:
+            if get_request().user and str(get_request().user.id) != obj.last_modification_user_id:
+                warning_class = 'recent'
+        r += htmltext('<p class="last-modification %s">') % warning_class
+        r += _('Last Modification:')
+        r += ' '
+        r += misc.localstrftime(obj.last_modification_time)
+        r += ' '
+        if obj.last_modification_user_id:
+            try:
+                r += _('by %s') % get_publisher().user_class.get(
+                                obj.last_modification_user_id).display_name
+            except KeyError:
+                pass
+        r += htmltext('</p>')
+
+    return r.getvalue()
diff --git a/wcs/admin/workflows.py b/wcs/admin/workflows.py
index 2fa7ee9f0..e7fa7d220 100644
--- a/wcs/admin/workflows.py
+++ b/wcs/admin/workflows.py
@@ -43,6 +43,8 @@ from wcs.workflows import *
 from wcs.carddef import CardDef
 from wcs.formdef import FormDef
 from wcs.formdata import Evolution
+
+from . import utils
 from .fields import FieldDefPage, FieldsDirectory
 from .data_sources import NamedDataSourcesDirectory
 from .logged_errors import LoggedErrorsDirectory
@@ -1389,24 +1391,7 @@ class WorkflowPage(Directory):
             r += htmltext('</span>')
         r += htmltext('</div>')
 
-        if self.workflow.last_modification_time:
-            warning_class = ''
-            if (time.time() - time.mktime(self.workflow.last_modification_time)) < 600:
-                if get_request().user and str(get_request().user.id) != self.workflow.last_modification_user_id:
-                    warning_class = 'recent'
-            r += htmltext('<p class="last-modification %s">') % warning_class
-            r += _('Last Modification:')
-            r += ' '
-            r += misc.localstrftime(self.workflow.last_modification_time)
-            r += ' '
-            if self.workflow.last_modification_user_id:
-                try:
-                    r += _('by %s') % get_publisher().user_class.get(
-                                    self.workflow.last_modification_user_id).display_name
-                except KeyError:
-                    pass
-            r += htmltext('</p>')
-
+        r += utils.last_modification_block(obj=self.workflow)
         r += get_session().display_message()
 
         r += htmltext('<div class="splitcontent-left">')
diff --git a/wcs/backoffice/cards.py b/wcs/backoffice/cards.py
index c62e4248e..0e57eebfc 100644
--- a/wcs/backoffice/cards.py
+++ b/wcs/backoffice/cards.py
@@ -30,6 +30,7 @@ from wcs.roles import Role
 from wcs.workflows import Workflow
 from wcs.admin.forms import FormsDirectory, FormDefPage, FormDefUI, html_top
 from wcs.admin.logged_errors import LoggedErrorsDirectory
+from wcs.admin import utils
 
 
 class CardDefUI(FormDefUI):
@@ -70,24 +71,7 @@ class CardDefPage(FormDefPage):
         r += htmltext('</span>')
         r += htmltext('</div>')
 
-        if self.formdef.last_modification_time:
-            warning_class = ''
-            if (time.time() - time.mktime(self.formdef.last_modification_time)) < 600:
-                if get_request().user and str(get_request().user.id) != self.formdef.last_modification_user_id:
-                    warning_class = 'recent'
-            r += htmltext('<p class="last-modification %s">') % warning_class
-            r += _('Last Modification:')
-            r += ' '
-            r += misc.localstrftime(self.formdef.last_modification_time)
-            r += ' '
-            if self.formdef.last_modification_user_id:
-                try:
-                    r += _('by %s') % get_publisher().user_class.get(
-                                    self.formdef.last_modification_user_id).display_name
-                except KeyError:
-                    pass
-            r += htmltext('</p>')
-
+        r += utils.last_modification_block(obj=self.formdef)
         r += get_session().display_message()
 
         def add_option_line(link, label, current_value):
-- 
2.27.0

