|
4 | 4 | from imio.esign import _ |
5 | 5 | from imio.esign.adapters import ISignable |
6 | 6 | from imio.esign.audit import audit |
| 7 | +from imio.esign.browser.table import FilteredSessionsTable |
7 | 8 | from imio.esign.utils import add_files_to_session |
8 | 9 | from imio.esign.utils import get_session_annotation |
9 | 10 | from imio.esign.utils import get_sessions_for |
@@ -149,6 +150,87 @@ def available(self): |
149 | 150 | return self.context.UID() in annot.get("uids", {}) |
150 | 151 |
|
151 | 152 |
|
| 153 | +class AddToCustomEsignSessionView(BrowserView): |
| 154 | + """Overlay view listing draft e-sign sessions with radio buttons, |
| 155 | + letting the user assign the current file to a chosen session.""" |
| 156 | + |
| 157 | + template = ViewPageTemplateFile("templates/add_to_custom_esign_session.pt") |
| 158 | + |
| 159 | + def __call__(self): |
| 160 | + if self.request.method == "POST" and "form.buttons.submit" in self.request.form: |
| 161 | + return self.handle_submit() |
| 162 | + return self.template() |
| 163 | + |
| 164 | + def render_table(self): |
| 165 | + table = FilteredSessionsTable(self.context, self, self.request) |
| 166 | + table.update() |
| 167 | + return table.render() |
| 168 | + |
| 169 | + def has_draft_sessions(self): |
| 170 | + annot = get_session_annotation() |
| 171 | + return any(s.get("state") == "draft" for s in annot.get("sessions", {}).values()) |
| 172 | + |
| 173 | + def get_current_session(self): |
| 174 | + """Return (session_id, session) if the file is already in a session, else (None, None).""" |
| 175 | + annot = get_session_annotation() |
| 176 | + session_id = annot.get("uids", {}).get(self.context.UID()) |
| 177 | + if session_id is not None: |
| 178 | + session = annot["sessions"].get(session_id) |
| 179 | + if session: |
| 180 | + return session_id, session |
| 181 | + return None, None |
| 182 | + |
| 183 | + def handle_submit(self): |
| 184 | + session_id_str = self.request.form.get("session_id") |
| 185 | + if not session_id_str: |
| 186 | + api.portal.show_message( |
| 187 | + _(u"No session selected!"), request=self.request, type="warning" |
| 188 | + ) |
| 189 | + return self.template() |
| 190 | + try: |
| 191 | + session_id = int(session_id_str) |
| 192 | + except (ValueError, TypeError): |
| 193 | + api.portal.show_message( |
| 194 | + _(u"Invalid session!"), request=self.request, type="error" |
| 195 | + ) |
| 196 | + return self.template() |
| 197 | + annot = get_session_annotation() |
| 198 | + session = annot["sessions"].get(session_id) |
| 199 | + if not session or session.get("state") != "draft": |
| 200 | + api.portal.show_message( |
| 201 | + _(u"Session not found or no longer draft!"), |
| 202 | + request=self.request, |
| 203 | + type="error", |
| 204 | + ) |
| 205 | + return self.template() |
| 206 | + file_uid = self.context.UID() |
| 207 | + old_session_id = annot.get("uids", {}).get(file_uid) |
| 208 | + if old_session_id is not None and old_session_id != session_id: |
| 209 | + remove_files_from_session([file_uid]) |
| 210 | + signers = [ |
| 211 | + (s["userid"], s["email"], s["fullname"], s["position"]) |
| 212 | + for s in session.get("signers", []) |
| 213 | + ] |
| 214 | + add_files_to_session( |
| 215 | + signers=signers, |
| 216 | + files_uids=[file_uid], |
| 217 | + session_id=session_id, |
| 218 | + seal=session.get("seal"), |
| 219 | + title=session.get("title", ""), |
| 220 | + ) |
| 221 | + api.portal.show_message( |
| 222 | + _(u"File added to session!"), request=self.request, type="info" |
| 223 | + ) |
| 224 | + self.request.RESPONSE.redirect(self.context.absolute_url()) |
| 225 | + |
| 226 | + def available(self): |
| 227 | + return True |
| 228 | + |
| 229 | + @property |
| 230 | + def portal_url(self): |
| 231 | + return api.portal.get().absolute_url() |
| 232 | + |
| 233 | + |
152 | 234 | class SessionAnnotationInfoView(BrowserView): |
153 | 235 | """Admin-only view displaying imio.esign session annotations for a specific context item.""" |
154 | 236 |
|
|
0 commit comments