GitHub PullRequestReviewCommentEvent at 2014-01-04 14:15:38
Repository: osha.oira
Actor: pysailor
Payload: {"comment":{"url":"https://api.github.com/repos/euphorie/osha.oira/pulls/comments/8651697","id":8651697,"diff_hunk":"@@ -0,0 +1,146 @@\n+import collections\n+from zExceptions import NotFound\n+from Acquisition import aq_inner\n+from five import grok\n+from zope.event import notify\n+from zope.component import getUtility\n+from zope.lifecycleevent import ObjectCopiedEvent\n+from OFS.event import ObjectClonedEvent\n+from z3c.appconfig.interfaces import IAppConfig\n+from plone.i18n.normalizer.interfaces import IIDNormalizer\n+from plone.dexterity.interfaces import IDexterityContainer\n+from plonetheme.nuplone.utils import getPortal\n+from Products.statusmessages.interfaces import IStatusMessage\n+from euphorie.content.interfaces import IQuestionContainer\n+from euphorie.content.module import item_depth\n+from euphorie.content.risk import IRisk\n+from euphorie.content.survey import ISurvey\n+from euphorie.content.behaviour.uniqueid import INameFromUniqueId\n+from euphorie.content.behaviour.uniqueid import get_next_id\n+from ..interfaces import IOSHAContentSkinLayer\n+from .. import _\n+\n+\n+grok.templatedir('templates')\n+\n+\n+def is_allowed(context, item):\n+    try:\n+        context._verifyObjectPaste(item)\n+    except ValueError:\n+        return False\n+    return True\n+\n+\n+def build_survey_tree(context, root):\n+    \"\"\"Build a simple datastructure describing (part of) a survey.\n+\n+    This implementation does a walk over the content itself. It is possible\n+    to also do this based on a catalog query, but since we use light-weight\n+    content items this should be simpler and removes the need to turn a\n+    catalog result back into a tree.\n+    \"\"\"\n+    tree = {'title': root.title,\n+            'children': [],\n+            'url': root.absolute_url(),\n+            }\n+    todo = collections.deque([(root, [], tree['children'])])\n+    normalize = getUtility(IIDNormalizer).normalize\n+    while todo:\n+        (node, index, child_list) = todo.popleft()\n+        for (ix, child) in enumerate(node.values(), 1):\n+            if not (IQuestionContainer.providedBy(child) or IRisk.providedBy(child)):\n+                continue\n+            child_index = index + [str(ix)]\n+            info = {'title': child.title,\n+                    'children': [],\n+                    'number': '.'.join(child_index),\n+                    'path': '/'.join(child.getPhysicalPath()),\n+                    'url': child.absolute_url(),\n+                    'disabled': not is_allowed(context, child),\n+                    'portal_type': normalize(child.portal_type),\n+                    }\n+            child_list.append(info)\n+            todo.append((child, child_index, info['children']))\n+    return tree\n+\n+\n+class Library(grok.View):\n+    grok.context(IQuestionContainer)\n+    grok.layer(IOSHAContentSkinLayer)\n+    grok.require('euphorie.content.AddNewRIEContent')\n+    grok.template('library')\n+\n+    def contents(self):\n+        return build_survey_tree(aq_inner(self.context), self._library)\n+\n+    def _get_library(self):\n+        config = getUtility(IAppConfig).get('euphorie', {})\n+        path = config.get('library', '').lstrip('/')\n+        if not path:\n+            return None\n+        site = getPortal(self.context)\n+        library = site.restrictedTraverse(path)\n+        if not ISurvey.providedBy(library):\n+            return None\n+        return library\n+\n+    def update(self):\n+        self._library = self._get_library()\n+        if self._library is None:\n+            raise NotFound(self, 'library', self.request)\n+        self.depth = item_depth(aq_inner(self.context))\n+        self.at_root = not self.depth\n+        super(Library, self).update()\n+\n+\n+def assign_ids(context, tree):\n+    todo = collections.deque([(None, tree)])\n+    while todo:\n+        (parent, item) = todo.popleft()\n+        if INameFromUniqueId.providedBy(item):\n+            old_id = item.id\n+            new_id = get_next_id(context)\n+            item._setId(new_id)\n+            if parent is not None:\n+                # We need to reset the child in its folder to make sure\n+                # the folder knows of the new id.\n+                position = parent.getObjectPosition(old_id)\n+                del parent[old_id]\n+                parent[new_id] = item\n+                parent.moveObjectToPosition(new_id, position, True)\n+        if IDexterityContainer.providedBy(item):\n+            for gc in item.values():\n+                todo.append((item, gc))\n+\n+\n+class LibraryInsert(grok.View):\n+    grok.name('library-insert')\n+    grok.context(IQuestionContainer)\n+    grok.layer(IOSHAContentSkinLayer)\n+    grok.require('euphorie.content.AddNewRIEContent')\n+\n+    def render(self):\n+        if self.request.method != 'POST':\n+            raise NotFound(self, 'library-insert', self.request)\n+        path = self.request.form.get('path')\n+        if not path:\n+            raise NotFound(self, 'library-insert', self.request)  # XXX Wrong exception type\n+        target = aq_inner(self.context)\n+        app = target.getPhysicalRoot()\n+        source = app.restrictedTraverse(path)\n+        if not is_allowed(target, source):\n+            raise NotFound(self, 'library-insert', self.request)  # XXX Wrong exception type\n+        copy = source._getCopy(target)\n+        assign_ids(target, copy)\n+        notify(ObjectCopiedEvent(copy, source))\n+        target._setObject(copy.id, copy)","path":"src/osha/oira/content/library.py","position":137,"original_position":137,"commit_id":"02cdd72966152ad127cc47be6d99956074367bbb","original_commit_id":"02cdd72966152ad127cc47be6d99956074367bbb","user":{"login":"pysailor","id":642660,"avatar_url":"https://gravatar.com/avatar/126a0dd18a3c75697c848230f740fc72?d=https%3A%2F%2Fidenticons.github.com%2F1f9f87044072dd0cce942feb2161fd03.png&r=x","gravatar_id":"126a0dd18a3c75697c848230f740fc72","url":"https://api.github.com/users/pysailor","html_url":"https://github.com/pysailor","followers_url":"https://api.github.com/users/pysailor/followers","following_url":"https://api.github.com/users/pysailor/following{/other_user}","gists_url":"https://api.github.com/users/pysailor/gists{/gist_id}","starred_url":"https://api.github.com/users/pysailor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pysailor/subscriptions","organizations_url":"https://api.github.com/users/pysailor/orgs","repos_url":"https://api.github.com/users/pysailor/repos","events_url":"https://api.github.com/users/pysailor/events{/privacy}","received_events_url":"https://api.github.com/users/pysailor/received_events","type":"User","site_admin":false},"body":"Same error as above happens here (portal_uidhandler can't be fetched using `copy` as context)\r\n","created_at":"2014-01-04T14:15:38Z","updated_at":"2014-01-04T14:15:38Z","html_url":"https://github.com/euphorie/osha.oira/pull/1#discussion_r8651697","pull_request_url":"https://api.github.com/repos/euphorie/osha.oira/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/euphorie/osha.oira/pulls/comments/8651697"},"html":{"href":"https://github.com/euphorie/osha.oira/pull/1#discussion_r8651697"},"pull_request":{"href":"https://api.github.com/repos/euphorie/osha.oira/pulls/1"}}}}