[views] Add incontext and outofcontext Answer table views, to be used in the
authorVladimir Popescu <vladimir.popescu@logilab.fr>
Mon, 20 Jan 2014 14:34:11 +0000
changeset 646 ee961cbb4c07
parent 645 f4014875a284
child 647 de3c857db0e6
[views] Add incontext and outofcontext Answer table views, to be used in the Question and QuestionnaireRun primary views, respectively (related to #3323338)
views/tableviews.py
views/uicfg.py
--- a/views/tableviews.py	Tue Dec 17 11:13:36 2013 +0100
+++ b/views/tableviews.py	Mon Jan 20 14:34:11 2014 +0000
@@ -17,6 +17,7 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
 """cubicweb-brainomics views/forms/actions/components for web ui"""
+from logilab.mtconverter import xml_escape
 from cubicweb.selectors import is_instance
 from cubicweb.web.views.tableview import (TableLayout, EntityTableView,
                                           RelationColRenderer, EntityTableColRenderer)
@@ -143,3 +144,35 @@
                'reduced_dosis']
     column_renderers = {'taken_in_therapy': RelationColRenderer(role='subject', vid='incontext'),
                         'drug': RelationColRenderer(role='subject')}
+
+
+###############################################################################
+### ANSWERS ###################################################################
+###############################################################################
+class AnswerInContextTableView(EntityTableView):
+    __select__ = EntityTableView.__select__ & is_instance('Answer')
+    __regid__ = 'answer-incontext-table-view'
+    columns = ['answer', 'value', 'datetime']
+
+    def render_text(w, entity):
+        w(u'%s' % entity.question[0].text)
+
+    def render_answer(w, entity):
+        w(u'<a href="%s">%s</a>' % (xml_escape(entity.absolute_url()),
+                                    xml_escape(entity.dc_title())))
+
+    column_renderers = {'answer': EntityTableColRenderer(renderfunc=render_answer)}
+
+
+class AnswerOutOfContextTableView(AnswerInContextTableView):
+    __regid__ = 'answer-outofcontext-table-view'
+    columns = ['question', 'text', 'value', 'datetime']
+
+    def render_question(w, entity):
+        entity = entity.question[0]
+        w(u'<a href="%s">%s</a>' % (xml_escape(entity.absolute_url()),
+                                    xml_escape(entity.dc_title())))
+
+
+    column_renderers = {'question': EntityTableColRenderer(renderfunc=render_question),
+                        'text': EntityTableColRenderer(renderfunc=AnswerInContextTableView.render_text)}
--- a/views/uicfg.py	Tue Dec 17 11:13:36 2013 +0100
+++ b/views/uicfg.py	Mon Jan 20 14:34:11 2014 +0000
@@ -240,7 +240,8 @@
 _pvdc.tag_subject_of(('QuestionnaireRun', 'external_resources', '*'), {'vid': 'list'})
 # Answers
 _pvs.tag_object_of(('*', 'questionnaire_run', 'QuestionnaireRun'), 'relations')
-_pvdc.tag_object_of(('*', 'questionnaire_run', 'QuestionnaireRun'), {'vid': 'table'})
+_pvdc.tag_object_of(('*', 'questionnaire_run', 'QuestionnaireRun'),
+                    {'vid': 'answer-outofcontext-table-view'})
 
 
 ###############################################################################
@@ -269,7 +270,7 @@
 _pvdc.tag_subject_of(('Question', 'questionnaire', '*'), {'vid': 'incontext'})
 # Answers
 _pvs.tag_object_of(('*', 'question', 'Question'), 'relations')
-_pvdc.tag_object_of(('*', 'question', 'Question'), {'vid': 'table'})
+_pvdc.tag_object_of(('*', 'question', 'Question'), {'vid': 'answer-incontext-table-view'})
 
 
 ###############################################################################