From 7d8c225e83d45f86b761f4324eea06c14d58ae9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 13 Jul 2017 11:29:45 +0200 Subject: [PATCH 1/3] templates: use a template to render formdata history (#17601) --- MANIFEST.in | 1 + wcs/formdata.py | 40 +++++++++++- wcs/forms/common.py | 107 ++------------------------------ wcs/forms/root.py | 1 + wcs/qommon/template.py | 6 ++ wcs/templates/wcs/formdata_history.html | 43 +++++++++++++ wcs/workflows.py | 6 +- 7 files changed, 100 insertions(+), 104 deletions(-) create mode 100644 wcs/templates/wcs/formdata_history.html diff --git a/MANIFEST.in b/MANIFEST.in index d688a50c..b0f3135c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -8,3 +8,4 @@ recursive-include data/themes/default/ *.html *.css *.png *.gif *.jpg *.js *.ezt recursive-include data/themes/alto/ *.html *.css *.png *.gif *.jpg *.js *.ezt *.xml recursive-include data/vendor/ *.dat recursive-include wcs/qommon/static/ *.css *.png *.gif *.jpg *.js *.eot *.svg *.ttf *.woff +recursive-include wcs/templates *.html diff --git a/wcs/formdata.py b/wcs/formdata.py index b6b0fab0..148b521e 100644 --- a/wcs/formdata.py +++ b/wcs/formdata.py @@ -143,10 +143,18 @@ class Evolution(object): return self._formdata def get_author_name(self): + user_id = self.who if self.who == '_submitter': + user_id = self.formdata.user_id + try: + return get_publisher().user_class.get(user_id).display_name + except KeyError: + return None + + def get_author_qualification(self): + if self.who == '_submitter' and not self.formdata.is_submitter(get_request().user): return _('Original Submitter') - else: - return get_publisher().user_class.get(self.who).display_name + return None def add_part(self, part): if not self.parts: @@ -196,6 +204,34 @@ class Evolution(object): del odict['_formdata'] return odict + @property + def datetime(self): + return datetime.datetime(*self.time[:6]) + + def is_hidden(self): + if self.status: + wf_status = self.formdata.get_status(self.status) + if wf_status and not wf_status.is_visible(self.formdata, get_request().user): + return True + return False + + def get_status(self): + return self.formdata.get_status(status=self.status) + + def get_status_label(self): + return self.formdata.get_status_label(status=self.status) + + def is_status_change(self): + if not self.status: + return False + idx = self.formdata.evolution.index(self) + if idx == 0: + return True + while idx: + if self.formdata.evolution[idx-1].status and self.status != self.formdata.evolution[idx-1].status: + return True + idx -= 1 + return False class FormData(StorableObject): _names = 'XX' diff --git a/wcs/forms/common.py b/wcs/forms/common.py index 6397c721..cebae12c 100644 --- a/wcs/forms/common.py +++ b/wcs/forms/common.py @@ -25,6 +25,8 @@ from wcs.api_utils import get_user_from_api_query_string, is_url_signed from wcs.fields import WidgetField, FileField from wcs.workflows import EditableWorkflowStatusItem +from django.template import RequestContext + from qommon import _ from qommon import template from qommon import get_logger @@ -94,6 +96,8 @@ class FormStatusPage(Directory): _q_extra_exports = [] form_page_class = None + history_templates = ['wcs/formdata_history.html'] + def html_top(self, title = None): template.html_top(title = title, default_org = _('Forms')) @@ -251,108 +255,9 @@ class FormStatusPage(Directory): return if not self.formdef.is_user_allowed_read_status_and_history(get_request().user, self.filled): return - r = TemplateIO(html=True) - r += htmltext('
') - r += htmltext('

%s

') % _('Log') - r += htmltext('') - r += htmltext('
') # .bo-block #evolution-log - return r.getvalue() + context = RequestContext(get_request(), {'formdata': self.filled}) + return template.render(self.history_templates, context) def check_receiver(self): session = get_session() diff --git a/wcs/forms/root.py b/wcs/forms/root.py index ab9dbf03..c0471d26 100644 --- a/wcs/forms/root.py +++ b/wcs/forms/root.py @@ -1577,6 +1577,7 @@ class RootDirectory(AccessControlled, Directory): class PublicFormStatusPage(FormStatusPage): _q_exports_orig = ['', 'download', 'status'] form_page_class = FormPage + history_templates = ['wcs/front/formdata_history.html', 'wcs/formdata_history.html'] def __init__(self, *args, **kwargs): FormStatusPage.__init__(self, *args, **kwargs) diff --git a/wcs/qommon/template.py b/wcs/qommon/template.py index cd02358e..e6eab999 100644 --- a/wcs/qommon/template.py +++ b/wcs/qommon/template.py @@ -19,6 +19,8 @@ import os import glob import xml.etree.ElementTree as ET +from django.template.loader import render_to_string + from quixote import get_session, get_request, get_response, get_publisher from quixote.directory import Directory from quixote.util import StaticDirectory, StaticFile @@ -405,3 +407,7 @@ def decorate(body, response): template.generate(fd, vars) return fd.getvalue() + + +def render(template_name, context): + return htmltext(render_to_string(template_name, context).encode('utf-8')) diff --git a/wcs/templates/wcs/formdata_history.html b/wcs/templates/wcs/formdata_history.html new file mode 100644 index 00000000..ea8e53c4 --- /dev/null +++ b/wcs/templates/wcs/formdata_history.html @@ -0,0 +1,43 @@ +{% load i18n %} +
+

{% trans "Log" %}

+ +
diff --git a/wcs/workflows.py b/wcs/workflows.py index 67fe86f1..dff5ce50 100644 --- a/wcs/workflows.py +++ b/wcs/workflows.py @@ -29,7 +29,7 @@ import uuid from quixote import get_request, redirect from qommon import _ -from qommon.misc import C_, get_as_datetime, file_digest +from qommon.misc import C_, get_as_datetime, file_digest, get_foreground_colour from qommon.storage import StorableObject, atomic_write from qommon.form import * from qommon.humantime import seconds2humanduration @@ -1411,6 +1411,10 @@ class WorkflowStatus(object): waitpoint = item.waitpoint or waitpoint return bool(endpoint or waitpoint) + def get_contrast_color(self): + colour = self.colour or 'ffffff' + return misc.get_foreground_colour(colour) + def __getstate__(self): odict = self.__dict__.copy() if odict.has_key('parent'): -- 2.14.1