|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | + collective.iconifiedcategory overrided views |
| 4 | +""" |
| 5 | +from collective.iconifiedcategory.browser.actionview import ApprovedChangeView as BaseApprovedChangeView |
| 6 | +from collective.iconifiedcategory.browser.tabview import ApprovedColumn as BaseApprovedColumn |
| 7 | +from imio.dms.mail.utils import add_file_to_approval |
| 8 | +from imio.dms.mail.utils import approve_file |
| 9 | +from imio.dms.mail.utils import can_approve |
| 10 | +from imio.dms.mail.utils import get_approval_annot |
| 11 | +from imio.dms.mail.utils import is_file_approved |
| 12 | +from imio.dms.mail.utils import logger # noqa F401 |
| 13 | +from imio.dms.mail.utils import remove_file_from_approval |
| 14 | +from plone import api |
| 15 | +from zope.i18n import translate |
| 16 | + |
| 17 | + |
| 18 | +""" |
| 19 | +{ |
| 20 | + 'approval': 1, |
| 21 | + 'files': {'4115fb4c265647ca82d85285504973b8': {1: {'status': 'p'}, 2: {'status': 'w'}}}, |
| 22 | + 'numbers': { |
| 23 | + 1: {'status': 'p', 'signer': ('dirg', '[email protected]', u'Maxime DG', u'Directeur G\xe9n\xe9ral'), |
| 24 | + 'users': ['dirg']}, |
| 25 | + 2: {'status': 'w', 'signer': ('bourgmestre', '[email protected]', u'Paul BM', u'Bourgmestre'), |
| 26 | + 'users': ['bourgmestre', 'chef']}}, |
| 27 | + 'session_id': None, |
| 28 | + 'users': {'bourgmestre': {'status': 'w', 'editor': False, 'name': u'Monsieur Paul BM', 'order': 2}, |
| 29 | + 'chef': {'status': 'w', 'editor': False, 'name': u'Monsieur Michel Chef', 'order': 2}, |
| 30 | + 'dirg': {'status': 'w', 'editor': True, 'name': u'Monsieur Maxime DG', 'order': 1}}, |
| 31 | +} |
| 32 | +""" # noqa |
| 33 | + |
| 34 | + |
| 35 | +class ApprovedColumn(BaseApprovedColumn): |
| 36 | + |
| 37 | + the_object = True |
| 38 | + |
| 39 | + def __init__(self, context, request, table): |
| 40 | + super(ApprovedColumn, self).__init__(context, request, table) |
| 41 | + # self.context is the mail here |
| 42 | + self.a_a = get_approval_annot(self.context) |
| 43 | + self.msg = u"" |
| 44 | + |
| 45 | + def alt(self, content): |
| 46 | + return translate( |
| 47 | + self.msg, |
| 48 | + context=self.table.request, |
| 49 | + domain='collective.iconifiedcategory', |
| 50 | + ) |
| 51 | + |
| 52 | + def css_class(self, content): |
| 53 | + av = self.get_action_view(content) |
| 54 | + editable = self.is_editable(content) and " editable" or "" |
| 55 | + # when deactivated, anyone see a grey icon |
| 56 | + if av.p_state not in ("to_approve", "to_be_signed", "signed", "sent"): |
| 57 | + if self.is_deactivated(content): |
| 58 | + if editable: |
| 59 | + self.msg = u"Deactivated for approval (click to activate)" |
| 60 | + return " deactivated editable" |
| 61 | + else: |
| 62 | + self.msg = u"Deactivated for approval" |
| 63 | + return " deactivated" |
| 64 | + elif editable: |
| 65 | + self.msg = u"Activated for approval (click to deactivate)" |
| 66 | + return editable |
| 67 | + else: |
| 68 | + self.msg = u"Activated for approval" |
| 69 | + return "" |
| 70 | + # when to_approve, the red icon (no class) is shown if : |
| 71 | + # * no approval at all |
| 72 | + # * but the current approver see a green icon when he just approved |
| 73 | + elif av.p_state == "to_approve": |
| 74 | + if can_approve(self.a_a, av.userid, av.uid): |
| 75 | + if self.a_a["files"][content.UID][self.a_a["approval"]]["status"] == "a": |
| 76 | + self.msg = u"Already approved (click to change)" |
| 77 | + return " active{}".format(editable) |
| 78 | + self.msg = u"Waiting for your approval (click to approve)" |
| 79 | + return editable |
| 80 | + else: |
| 81 | + self.msg = u"Waiting for approval but you can't approve (now)" |
| 82 | + # after a first approval, we show a partially or totally approved icon even for a previously or future approver |
| 83 | + if content["approved"]: # all approved |
| 84 | + self.msg = u"Totally approved" |
| 85 | + return " totally-approved" |
| 86 | + elif is_file_approved(self.a_a, content.UID, totally=False): |
| 87 | + self.msg = u"Partially approved. Still waiting for other approval(s)" |
| 88 | + return " partially-approved" |
| 89 | + return "" |
| 90 | + |
| 91 | + def get_url(self, content): |
| 92 | + av = self.get_action_view(content) |
| 93 | + # after to_approve state, no one can click on the icon |
| 94 | + if av.p_state in ("to_be_signed", "signed", "sent"): |
| 95 | + return "#" |
| 96 | + # when to_approve, only an approver can click on the icon |
| 97 | + if av.p_state == "to_approve" and not can_approve(self.a_a, av.userid, av.uid): |
| 98 | + return "#" |
| 99 | + return '{url}/@@{action}'.format( |
| 100 | + url=content.getURL(), |
| 101 | + action=self.get_action_view_name(content), |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +class ApprovedChangeView(BaseApprovedChangeView): |
| 106 | + permission = "View" |
| 107 | + |
| 108 | + def __init__(self, context, request): |
| 109 | + super(ApprovedChangeView, self).__init__(context, request) |
| 110 | + self.parent = self.context.__parent__ |
| 111 | + self.p_state = api.content.get_state(self.parent) |
| 112 | + self.a_a = get_approval_annot(self.parent) |
| 113 | + self.reload = False |
| 114 | + self.user_id = None |
| 115 | + self.uid = self.context.UID() |
| 116 | + self.msg = u"" |
| 117 | + |
| 118 | + @property |
| 119 | + def userid(self): |
| 120 | + if self.user_id is None: |
| 121 | + self.user_id = api.user.get_current().getId() |
| 122 | + return self.user_id |
| 123 | + |
| 124 | + def _get_next_values(self, old_values): |
| 125 | + """ """ |
| 126 | + values = {} |
| 127 | + status = 0 |
| 128 | + # logger.info("Before annot change: %s", self.a_a) |
| 129 | + # logger.info("Before values change: %s", old_values) |
| 130 | + if self.p_state == "to_approve": |
| 131 | + # in to_approve state, only an approver can approve or not |
| 132 | + if can_approve(self.a_a, self.userid, self.uid): |
| 133 | + if self.a_a["files"][self.uid][self.a_a["approval"]]["status"] == "a": |
| 134 | + status = 0 |
| 135 | + self.msg = u"Already approved (click to change)" |
| 136 | + # TODO TO BE HANDLED |
| 137 | + else: |
| 138 | + self.msg = u"Waiting for your approval (click to approve)" |
| 139 | + # the status is changed (if totally approved) in sub method |
| 140 | + # must we pass self to update self.msg: no need for now because we reload in all cases !! |
| 141 | + ret, self.reload = approve_file(self.a_a, self.parent, self.context, self.userid, values=values, |
| 142 | + transition="propose_to_be_signed") |
| 143 | + status = int(ret) |
| 144 | + elif self.p_state not in ("to_be_signed", "signed", "sent"): |
| 145 | + # before to_approve state, we can only enable or disable to_approve |
| 146 | + if old_values['to_approve'] is False: |
| 147 | + values['to_approve'] = True |
| 148 | + values['approved'] = False |
| 149 | + status = 0 |
| 150 | + add_file_to_approval(self.a_a, self.uid) |
| 151 | + self.msg = u"Activated for approval (click to deactivate)" |
| 152 | + else: |
| 153 | + values['to_approve'] = False |
| 154 | + values['approved'] = False |
| 155 | + status = -1 |
| 156 | + remove_file_from_approval(self.a_a, self.uid) |
| 157 | + self.msg = u"Deactivated for approval (click to activate)" |
| 158 | + self.reload = False |
| 159 | + else: |
| 160 | + # cannot be in after to_approve state because get_url column method ? |
| 161 | + logger.warn("IN else of approved change view ???") |
| 162 | + # logger.info("After annot change: %s, ", self.a_a) |
| 163 | + # logger.info("After values change: %s, %s", status, values) |
| 164 | + return status, values |
| 165 | + |
| 166 | + def _may_set_values(self, values, ): |
| 167 | + if self.p_state in ("to_be_signed", "signed", "sent"): |
| 168 | + return False |
| 169 | + return super(ApprovedChangeView, self)._may_set_values(values) |
| 170 | + |
| 171 | + def set_values(self, values): |
| 172 | + old_values = self.get_current_values() |
| 173 | + status, values = self._get_next_values(old_values) |
| 174 | + super(BaseApprovedChangeView, self).set_values(values) |
| 175 | + return status, self.msg |
| 176 | + |
| 177 | + def __call__(self): |
| 178 | + # TODO ajouter un comparatif de date afin de voir si on agit bien sur qlq chose à jour... |
| 179 | + json_resp = super(ApprovedChangeView, self).__call__() |
| 180 | + if self.reload and json_resp.rstrip().endswith("}"): |
| 181 | + # logger.info("RELOAD TRUE") |
| 182 | + json_resp = json_resp.rstrip()[:-1] + ',"reload": true}' |
| 183 | + return json_resp |
0 commit comments