Skip to content

Commit e44edf8

Browse files
committed
Added get_sessions_for + Merged session info viewlet into a single collapsible
1 parent 9c16952 commit e44edf8

File tree

5 files changed

+48
-63
lines changed

5 files changed

+48
-63
lines changed

src/imio/esign/browser/templates/macros.pt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<tr metal:define-macro="signers" i18n:domain="imio.esign">
4141
<td class="table_widget_label"><label i18n:translate="">Signers</label></td>
4242
<td class="table_widget_value">
43-
<table class="no-border no-style-table listing context_viewlet_signers_table"
43+
<table class="context_viewlet_signers_table no-border no-style-table listing"
4444
tal:define="signers python:session.get('signers', [])">
4545
<thead>
4646
<tr>

src/imio/esign/browser/templates/session_info.pt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,19 @@
44
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
55
lang="en"
66
i18n:domain="imio.esign">
7-
<div class="faceted-session-info"
8-
tal:condition="python: view.available()"
9-
tal:define="session_id python: str(view.session.get('id', ''))">
10-
<div tal:attributes="id string:session-info-${session_id};
11-
onclick string:toggleDetails('collapsible-session-info-${session_id}', toggle_parent_active=true);
12-
class python: view.collapsible_css_default()">
7+
<div class="faceted-session-info" tal:condition="python: view.available()">
8+
<div id="session-info"
9+
onclick="toggleDetails('collapsible-session-info', toggle_parent_active=true);"
10+
tal:attributes="class python: view.collapsible_css_default()">
1311
<span i18n:translate="">Session information (<img i18n:name="icon_url" tal:attributes="src string:${view/portal_url}/++resource++imio.esign/parapheo.svg" /> Parapheo)</span>
1412
</div>
15-
<div tal:attributes="id string:collapsible-session-info-${session_id};
16-
class python: view.collapsible_content_css_default()">
13+
<div id="collapsible-session-info"
14+
tal:attributes="class python: view.collapsible_content_css_default()">
1715
<div class="collapsible-inner-content session-info-container"
18-
tal:define="template python: context.unrestrictedTraverse('@@esign-macros').index;
19-
session python: view.session">
16+
tal:repeat="session python:view.sessions"
17+
tal:define="template python: context.unrestrictedTraverse('@@esign-macros').index">
2018
<div class="session-info-column">
21-
<table class="no-style-table table-view-widgets">
19+
<table tal:attributes="id string:context_viewlet_session_table_${session/id}" class="no-style-table table-view-widgets">
2220
<tbody>
2321
<tal:block tal:repeat="macro_name python:view.get_table_rows(1)">
2422
<metal:render_cell use-macro="python: template.macros[macro_name]" />
@@ -28,16 +26,16 @@
2826
</div>
2927

3028
<div class="session-info-column">
31-
<table class="no-style-table table-view-widgets">
29+
<table tal:attributes="id string:context_viewlet_session_signers_${session/id}" class="no-style-table table-view-widgets">
3230
<tbody>
3331
<tal:block tal:repeat="macro_name python:view.get_table_rows(2)">
3432
<metal:render_cell use-macro="python: template.macros[macro_name]" />
3533
</tal:block>
3634
</tbody>
3735
</table>
3836
</div>
39-
<script>$('table.context_viewlet_signers_table').each(setoddeven);</script>
40-
</div>
37+
</div>
38+
<script>$('table.context_viewlet_signers_table').each(setoddeven);</script>
4139
</div>
4240
</div>
4341

src/imio/esign/browser/views.py

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
from AccessControl import Unauthorized
4+
from copy import deepcopy
45
from datetime import datetime
56
from datetime import timedelta
67
from imio.esign import _
@@ -12,6 +13,7 @@
1213
from imio.esign.config import get_registry_signing_users_email_content
1314
from imio.esign.utils import create_external_session
1415
from imio.esign.utils import get_session_annotation
16+
from imio.esign.utils import get_sessions_for
1517
from imio.esign.utils import get_state_description
1618
from imio.esign.utils import remove_session
1719
from imio.helpers.content import uuidToObject
@@ -206,23 +208,24 @@ def sessions_collection_uid(self):
206208
def render(self):
207209
"""Render the viewlet."""
208210
if self.request.form.get("c1[]", None) == self.sessions_collection_uid:
209-
if self.session:
211+
if self.sessions:
210212
return self.index()
211213
return self.sessions_listing_view(self.context, self.request).render_table()
212214
return ""
213215

214216
@property
215-
def session(self):
216-
session = None
217+
def sessions(self):
217218
session_id = self.request.form.get("esign_session_id[]", None)
218219
if not session_id:
219-
return
220+
return []
221+
session_id = int(session_id)
220222
sessions = get_session_annotation()["sessions"]
221-
session = sessions.get(int(session_id))
223+
session = sessions.get(session_id)
222224
if not session:
223-
return
225+
return []
226+
session = deepcopy(session)
224227
session["id"] = session_id
225-
return session
228+
return [session]
226229

227230
def get_table_rows(self, column):
228231
"""Get the table rows following the column"""
@@ -258,37 +261,16 @@ def available(self):
258261
"""Global availability of the viewlet."""
259262
return True
260263

264+
def render(self):
265+
"""Render the viewlet."""
266+
if self.sessions:
267+
return self.index()
268+
return ""
269+
261270
@property
262271
def sessions(self):
263272
"""Return all sessions that contain files from this context."""
264-
annot = get_session_annotation()
265-
result = []
266-
seen = set()
267-
for f_uid in annot["c_uids"].get(self.context.UID(), []):
268-
session_id = annot["uids"].get(f_uid)
269-
if session_id is not None and session_id not in seen:
270-
seen.add(session_id)
271-
session = dict(annot["sessions"].get(session_id, {}))
272-
session["id"] = session_id
273-
result.append(session)
274-
return result
275-
276-
@property
277-
def session(self):
278-
"""Current session during render loop; also used by base template."""
279-
return getattr(self, "_current_session", None) or {}
280-
281-
def render(self):
282-
"""Render viewlet once per session linked to this context."""
283-
sessions = self.sessions
284-
if not sessions:
285-
return ""
286-
parts = []
287-
for s in sessions:
288-
self._current_session = s
289-
parts.append(self.index())
290-
self._current_session = None
291-
return "\n".join(parts)
273+
return get_sessions_for(self.context.UID())
292274

293275

294276
@implementer(IPublishTraverse)

src/imio/esign/tests/test_browser_views.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -474,16 +474,3 @@ def test_sessions_multiple_sessions(self):
474474
self.assertEqual(len(sessions), 2)
475475
session_ids = {s["id"] for s in sessions}
476476
self.assertEqual(session_ids, {0, 1})
477-
478-
rendered_ids = []
479-
def mock_index():
480-
rendered_ids.append(viewlet._current_session["id"])
481-
return u"<div>session {}</div>".format(viewlet._current_session["id"])
482-
viewlet.index = mock_index
483-
result = viewlet.render()
484-
self.assertEqual(len(rendered_ids), 2)
485-
self.assertIn(0, rendered_ids)
486-
self.assertIn(1, rendered_ids)
487-
self.assertIn(u"<div>session 0</div>", result)
488-
self.assertIn(u"<div>session 1</div>", result)
489-
self.assertIsNone(viewlet._current_session)

src/imio/esign/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
from copy import deepcopy
23
from datetime import datetime
34
from datetime import timedelta
45
from imio.esign import _tr as _
@@ -516,3 +517,20 @@ def get_state_description(state):
516517
'returned': u'The session is finished and signed documents are on the way back to the application.',
517518
'finalized': u'The session is finished and signed documents have been sent back to the application.',
518519
}.get(state, "")
520+
521+
522+
def get_sessions_for(context_uid, readonly=True):
523+
"""Returns a list of all sessions involving the provided context_uid"""
524+
annot = get_session_annotation()
525+
result = []
526+
seen = set()
527+
for f_uid in annot["c_uids"].get(context_uid, []):
528+
session_id = annot["uids"].get(f_uid)
529+
if session_id is not None and session_id not in seen:
530+
seen.add(session_id)
531+
session = annot["sessions"][session_id]
532+
if readonly:
533+
session = deepcopy(session)
534+
session["id"] = session_id
535+
result.append(session)
536+
return result

0 commit comments

Comments
 (0)