|
| 1 | +from Acquisition import aq_inContextOf |
| 2 | +from Acquisition import aq_inner |
| 3 | +from opengever.base.json_response import JSONResponse |
| 4 | +from opengever.base.security import elevated_privileges |
| 5 | +from opengever.base.transport import PrivilegedReceiveObject |
| 6 | +from opengever.base.transport import Transporter |
| 7 | +from plone import api |
| 8 | +from plone.protect.interfaces import IDisableCSRFProtection |
| 9 | +from plone.restapi.deserializer import json_body |
| 10 | +from plone.restapi.services import Service |
| 11 | +from z3c.relationfield.relation import RelationValue |
| 12 | +from zExceptions import BadRequest |
| 13 | +from zope.component import getUtility |
| 14 | +from zope.interface import alsoProvides |
| 15 | +from zope.intid.interfaces import IIntIds |
| 16 | +import json |
| 17 | + |
| 18 | + |
| 19 | +class RISReturnExcerptService(Service): |
| 20 | + |
| 21 | + def reply(self): |
| 22 | + alsoProvides(self.request, IDisableCSRFProtection) |
| 23 | + |
| 24 | + if not api.user.has_permission("View", obj=self.context): |
| 25 | + return JSONResponse(self.request).error("Forbidden", status=403).dump() |
| 26 | + |
| 27 | + data = json_body(self.request) or {} |
| 28 | + target_cid = data.get("target_admin_unit_id") |
| 29 | + container_path = data.get("target_dossier_relative_path") |
| 30 | + proposal_relative_path = data.get("proposal_relative_path") |
| 31 | + |
| 32 | + if not target_cid or not container_path: |
| 33 | + return ( |
| 34 | + JSONResponse(self.request) |
| 35 | + .error( |
| 36 | + "Target admin_unit_id and dossier_url are required.", |
| 37 | + status=400, |
| 38 | + ) |
| 39 | + .dump() |
| 40 | + ) |
| 41 | + |
| 42 | + container_path = container_path.lstrip("/") |
| 43 | + |
| 44 | + result = Transporter().transport_to( |
| 45 | + obj=self.context, |
| 46 | + target_cid=target_cid, |
| 47 | + container_path=container_path, |
| 48 | + view="receive-ris-return-excerpt", |
| 49 | + proposal_relative_path=proposal_relative_path, |
| 50 | + finalize=data.get("finalize", True), |
| 51 | + ) |
| 52 | + return result |
| 53 | + |
| 54 | + |
| 55 | +class RISReturnExcerptReceive(PrivilegedReceiveObject): |
| 56 | + """Receiver on the target dossier. Runs with elevated privileges.""" |
| 57 | + |
| 58 | + def __call__(self): |
| 59 | + obj = self.receive() |
| 60 | + portal = self.context.portal_url.getPortalObject() |
| 61 | + portal_path = "/".join(portal.getPhysicalPath()) |
| 62 | + |
| 63 | + intids = getUtility(IIntIds) |
| 64 | + |
| 65 | + data = { |
| 66 | + "path": "/".join(obj.getPhysicalPath())[len(portal_path) + 1:], |
| 67 | + "intid": intids.queryId(obj), |
| 68 | + "url": obj.absolute_url(), |
| 69 | + "current_version_id": obj.get_current_version_id(missing_as_zero=True), |
| 70 | + } |
| 71 | + |
| 72 | + self.request.response.setHeader("Content-type", "application/json") |
| 73 | + return json.dumps(data) |
| 74 | + |
| 75 | + @property |
| 76 | + def container(self): |
| 77 | + return self.context |
| 78 | + |
| 79 | + def _traverse_relpath(self, relpath): |
| 80 | + portal = api.portal.get() |
| 81 | + |
| 82 | + rel = (relpath or "").lstrip("/") |
| 83 | + |
| 84 | + if isinstance(rel, unicode): |
| 85 | + rel = rel.encode("utf-8") |
| 86 | + try: |
| 87 | + return portal.unrestrictedTraverse(rel, default=None) |
| 88 | + except Exception: |
| 89 | + return None |
| 90 | + |
| 91 | + def _is_within(self, container, obj): |
| 92 | + return aq_inContextOf(aq_inner(obj), aq_inner(container)) |
| 93 | + |
| 94 | + def _link_as_excerpt(self, proposal, document): |
| 95 | + |
| 96 | + if not hasattr(proposal, "excerpts"): |
| 97 | + raise BadRequest("Proposal has no 'excerpts' field.") |
| 98 | + |
| 99 | + proposal.excerpts = [RelationValue(getUtility(IIntIds).getId(document))] |
| 100 | + proposal.reindexObject(idxs=["excerpts"]) |
| 101 | + |
| 102 | + def receive(self): |
| 103 | + data = self.request.form or {} |
| 104 | + proposal_path = data.get("proposal_relative_path") |
| 105 | + finalize = data.get("finalize", True) |
| 106 | + |
| 107 | + document = super(RISReturnExcerptReceive, self).receive() |
| 108 | + |
| 109 | + if proposal_path: |
| 110 | + proposal = self._traverse_relpath(proposal_path) |
| 111 | + |
| 112 | + if proposal is None: |
| 113 | + raise BadRequest("Invalid 'proposal_relative_path' (object not found).") |
| 114 | + |
| 115 | + if not self._is_within(self.container, proposal): |
| 116 | + raise BadRequest("The proposal is not located in the target dossier.") |
| 117 | + |
| 118 | + self._link_as_excerpt(proposal, document) |
| 119 | + |
| 120 | + if finalize: |
| 121 | + try: |
| 122 | + with elevated_privileges(): |
| 123 | + api.content.transition( |
| 124 | + obj=document, transition="document-transition-finalize" |
| 125 | + ) |
| 126 | + except Exception: |
| 127 | + pass |
| 128 | + |
| 129 | + return document |
0 commit comments