Пример #1
0
def add_response_for_files(object, event):
    """If a file/image is added or deleted, add a response."""
    if isinstance(event, ObjectAddedEvent):
        parent = event.newParent
        # do not add response if attachment is migrating to DX
        checkid = object.id + '_MIGRATION_'
        if any(att.id == checkid for att in parent.getFolderContents()):
            return
        if parent.portal_type == "Issue":
            issue = parent
            new_response = Response("")
        else:
            return
    elif isinstance(event, ObjectModifiedEvent):
        if object.aq_parent.portal_type == "Issue":
            issue = object.aq_parent
            new_response = Response("")
        else:
            return
    elif isinstance(event, ObjectRemovedEvent):
        if event.oldParent.portal_type == "Issue":
            issue = event.oldParent
            new_response = Response("Attachment deleted: " + object.title)
        else:
            return

    if new_response:
        new_response.attachment = object
        new_response.mimetype =\
            api.portal.get_registry_record('poi.default_issue_mime_type')
        new_response.type = "file"
        folder = IResponseContainer(issue)
        folder.add(new_response)
Пример #2
0
    def __iter__(self):
        for item in self.previous:
            keys = item.keys()
            pathkey = self.pathkey(*keys)[0]

            if not pathkey or ('poi_responses' not in keys):
                yield item; continue
            path = item[pathkey]

            obj = self.context.unrestrictedTraverse(path.lstrip('/'), None)
            if obj is None:  # path doesn't exist
                yield item; continue

            if isinstance(obj, PoiIssue):
                container = IResponseContainer(obj)
                i = 0
                for response in item['poi_responses']:
                    attachment_filename = response.pop('attachment_filename')
                    attachment_url = response.pop('attachment_url')

                    to_add = True
                    try:
                        r_obj = container[i]
                        to_add = False
                    except IndexError:
                        r_obj = Response(response.get('text',''))

                    for k, v in response.items():
                        setattr(r_obj, k, v)

                    if attachment_filename and attachment_url:
                        # import pdb; pdb.set_trace()
                        attachment = self.setAttachment(attachment_filename,
                            self.orig_plone_url + '/'.join(obj.getPhysicalPath()[2:]) + attachment_url)
                        #self.orig_plone_url + '/' + attachment_url)
                        if attachment:
                            r_obj.attachment = attachment

                    if to_add:
                        container.add(r_obj)
                    i += 1

            yield item
Пример #3
0
def add_response_for_files(object, event):
    """If a file/image is added or deleted, add a response."""
    if isinstance(event, ObjectAddedEvent):
        parent = event.newParent
        # do not add response if attachment is migrating to DX
        checkid = object.id + '_MIGRATION_'
        if any(att.id == checkid for att in parent.getFolderContents()):
            return
        if parent.portal_type == "Issue":
            issue = parent
            new_response = Response("")
        else:
            return
    elif isinstance(event, ObjectModifiedEvent):
        if object.aq_parent.portal_type == "Issue":
            issue = object.aq_parent
            new_response = Response("")
        else:
            return
    elif isinstance(event, ObjectRemovedEvent):
        if event.oldParent.portal_type == "Issue":
            issue = event.oldParent
            new_response = Response("Attachment deleted: " + object.title)
        else:
            return

    if new_response:
        new_response.attachment = object
        new_response.mimetype =\
            api.portal.get_registry_record('poi.default_issue_mime_type')
        new_response.type = "file"
        folder = IResponseContainer(issue)
        folder.add(new_response)
Пример #4
0
 def testRemoveResponses(self):
     # add more responses
     for i in range(2, 12):
         self.rc.add(Response("Response %d." % i))
     self.assertEqual(len(self.rc), 11)
     # Responses can be deleted.
     # They're not really removed, but emptied.
     self.rc.delete(7)
     self.assertEqual(len(self.rc), 11)
     self.assertIs(self.rc[7], None)
     # try removing something that doesn't exist
     # calls self.rc.delete(20)
     self.assertRaises(IndexError, self.rc.delete, 20)
Пример #5
0
def replace_old_with_new_responses(issue):
    if not IIssue.providedBy(issue):
        return
    responses = issue.contentValues(filter={'portal_type': 'PoiResponse'})
    folder = IResponseContainer(issue)
    try:
        request = issue.REQUEST
    except AttributeError:
        # When called via prefs_install_products_form (Plone 3.3) we
        # have no REQUEST object here.  We will use a dummy then.
        request = TestRequest()
    createview = Create(issue, request)
    path = '/'.join(issue.getPhysicalPath())
    logger.debug("Migrating %s responses for issue at %s", len(responses),
                 path)
    if not responses:
        return
    for old_response in responses:
        field = old_response.getField('response')
        text = field.getRaw(old_response)
        new_response = Response(text)
        new_response.mimetype = field.getContentType(old_response)
        new_response.creator = old_response.Creator()
        new_response.date = old_response.CreationDate()
        new_response.type = createview.determine_response_type(new_response)
        changes = old_response.getIssueChanges()
        for change in changes:
            new_response.add_change(**change)
        attachment_field = old_response.getField('attachment')
        attachment = attachment_field.getRaw(old_response)
        if attachment.get_size() > 0:
            new_response.attachment = attachment
        folder.add(new_response)
        issue._delObject(old_response.getId())
    # This seems a good time to reindex the issue for good measure.
    issue.reindexObject()
Пример #6
0
def replace_old_with_new_responses(issue):
    if not IIssue.providedBy(issue):
        return
    responses = issue.contentValues(filter={'portal_type': 'PoiResponse'})
    folder = IResponseContainer(issue)
    try:
        request = issue.REQUEST
    except AttributeError:
        # When called via prefs_install_products_form (Plone 3.3) we
        # have no REQUEST object here.  We will use a dummy then.
        request = TestRequest()
    createview = Create(issue, request)
    path = '/'.join(issue.getPhysicalPath())
    logger.debug("Migrating %s responses for issue at %s",
                 len(responses), path)
    if not responses:
        return
    for old_response in responses:
        field = old_response.getField('response')
        text = field.getRaw(old_response)
        new_response = Response(text)
        new_response.mimetype = field.getContentType(old_response)
        new_response.creator = old_response.Creator()
        new_response.date = old_response.CreationDate()
        new_response.type = createview.determine_response_type(new_response)
        changes = old_response.getIssueChanges()
        for change in changes:
            new_response.add_change(**change)
        attachment_field = old_response.getField('attachment')
        attachment = attachment_field.getRaw(old_response)
        if attachment.get_size() > 0:
            new_response.attachment = attachment
        folder.add(new_response)
        issue._delObject(old_response.getId())
    # This seems a good time to reindex the issue for good measure.
    issue.reindexObject()
Пример #7
0
    def __call__(self):
        form = self.request.form
        context = aq_inner(self.context)
        if not self.memship.checkPermission('Poi: Add Response', context):
            raise Unauthorized

        response_text = form.get('response', u'')
        new_response = Response(response_text)
        new_response.mimetype = self.mimetype
        new_response.type = self.determine_response_type(new_response)

        issue_has_changed = False
        transition = form.get('transition', u'')
        if transition and transition in self.available_transitions:
            wftool = getToolByName(context, 'portal_workflow')
            before = wftool.getInfoFor(context, 'review_state')
            before = wftool.getTitleForStateOnType(before, 'PoiIssue')
            wftool.doActionFor(context, transition)
            after = wftool.getInfoFor(context, 'review_state')
            after = wftool.getTitleForStateOnType(after, 'PoiIssue')
            new_response.add_change('review_state', _(u'Issue state'),
                                    before, after)
            issue_has_changed = True

        options = [
            ('severity', _(u'Severity'), 'available_severities'),
            ('responsibleManager', _(u'Responsible manager'),
             'available_managers'),
            ]
        # Changes that need to be applied to the issue (apart from
        # workflow changes that need to be handled separately).
        changes = {}
        for option, title, vocab in options:
            new = form.get(option, u'')
            if new and new in self.__getattribute__(vocab):
                current = self.__getattribute__(option)
                if current != new:
                    changes[option] = new
                    new_response.add_change(option, title,
                                            current, new)
                    issue_has_changed = True

        #('targetRelease', 'Target release', 'available_releases'),
        new = form.get('targetRelease', u'')
        if new and new in self.available_releases:
            current = self.targetRelease
            if current != new:
                # from value (uid) to key (id)
                new_label = self.available_releases.getValue(new)
                current_label = self.available_releases.getValue(current)
                changes['targetRelease'] = new
                new_response.add_change('targetRelease', _(u'Target release'),
                                        current_label, new_label)
                issue_has_changed = True

        attachment = form.get('attachment')
        if attachment:
            # File(id, title, file)
            data = File(attachment.filename, attachment.filename, attachment)
            new_response.attachment = data
            issue_has_changed = True

        if len(response_text) == 0 and not issue_has_changed:
            status = IStatusMessage(self.request)
            msg = _(u"No response text added and no issue changes made.")
            msg = translate(msg, 'Poi', context=self.request)
            status.addStatusMessage(msg, type='error')
        else:
            # Apply changes to issue
            context.update(**changes)
            # Add response
            self.folder.add(new_response)
        self.request.response.redirect(context.absolute_url())
Пример #8
0
    def __call__(self):
        form = self.request.form
        context = aq_inner(self.context)
        request = context.REQUEST
        authenticator = getMultiAdapter((context, request),
                                        name=u"authenticator")
        # CSRF should be disabled during tests
        if (not IDisableCSRFProtection.providedBy(request) and
           not authenticator.verify()):
            raise Unauthorized
        if not self.memship.checkPermission('Poi: Add Response', context):
            raise Unauthorized

        response_text = form.get('response', u'')
        new_response = Response(response_text)
        new_response.mimetype = self.mimetype
        new_response.type = self.determine_response_type(new_response)

        issue_has_changed = False
        transition = form.get('transition', u'')
        if transition and transition in self.available_transitions:
            wftool = getToolByName(context, 'portal_workflow')
            before = wftool.getInfoFor(context, 'review_state')
            before = wftool.getTitleForStateOnType(before, 'PoiIssue')
            wftool.doActionFor(context, transition)
            after = wftool.getInfoFor(context, 'review_state')
            after = wftool.getTitleForStateOnType(after, 'PoiIssue')
            new_response.add_change('review_state', _(u'Issue state'),
                                    before, after)
            issue_has_changed = True

        options = [
            ('severity', _(u'Severity'), 'available_severities'),
            ('current_assignee', _(u'Assignee'),
             'available_assignees'),
            ('targetRelease', _(u'Target release'), 'available_releases'),
        ]
        for option, title, vocab in options:
            new = form.get(option, u'')
            if new and new in self.__getattribute__(vocab):
                current = self.__getattribute__(option)
                if current == new:
                    continue
                new_response.add_change(option, title, current, new)
                issue_has_changed = True
                if option == 'severity':
                    context.severity = new
                elif option == 'targetRelease':
                    context.target_release = new
                elif option == 'current_assignee':
                    context.assignee = new

        if len(response_text) == 0 and not issue_has_changed:
            status = IStatusMessage(self.request)
            msg = _(u"No response text added and no issue changes made.")
            msg = translate(msg, 'Poi', context=self.request)
            status.addStatusMessage(msg, type='error')
        else:
            # Add response
            self.folder.add(new_response)
        redirect_url = "{0}?_authenticator={1}".format(context.absolute_url(),
                                                       authenticator.token())
        self.request.response.redirect(redirect_url)
Пример #9
0
 def add_response(self, issue, text, mimetype, attachment):
     new_response = Response(text)
     new_response.mimetype = mimetype
     new_response.attachment = attachment
     folder = IResponseContainer(issue)
     folder.add(new_response)
Пример #10
0
    def __call__(self):
        form = self.request.form
        context = aq_inner(self.context)
        request = context.REQUEST
        authenticator = getMultiAdapter((context, request),
                                        name=u"authenticator")
        # CSRF should be disabled during tests
        if (not IDisableCSRFProtection.providedBy(request)
                and not authenticator.verify()):
            raise Unauthorized
        if not self.memship.checkPermission('Poi: Add Response', context):
            raise Unauthorized

        response_text = form.get('response', u'')
        new_response = Response(response_text)
        new_response.mimetype = self.mimetype
        new_response.type = self.determine_response_type(new_response)

        issue_has_changed = False
        transition = form.get('transition', u'')
        if transition and transition in self.available_transitions:
            wftool = getToolByName(context, 'portal_workflow')
            before = wftool.getInfoFor(context, 'review_state')
            before = wftool.getTitleForStateOnType(before, 'PoiIssue')
            wftool.doActionFor(context, transition)
            after = wftool.getInfoFor(context, 'review_state')
            after = wftool.getTitleForStateOnType(after, 'PoiIssue')
            new_response.add_change('review_state', _(u'Issue state'), before,
                                    after)
            issue_has_changed = True

        options = [
            ('severity', _(u'Severity'), 'available_severities'),
            ('current_assignee', _(u'Assignee'), 'available_assignees'),
            ('targetRelease', _(u'Target release'), 'available_releases'),
        ]
        for option, title, vocab in options:
            new = form.get(option, u'')
            if new and new in self.__getattribute__(vocab):
                current = self.__getattribute__(option)
                if current == new:
                    continue
                new_response.add_change(option, title, current, new)
                issue_has_changed = True
                if option == 'severity':
                    context.severity = new
                elif option == 'targetRelease':
                    context.target_release = new
                elif option == 'current_assignee':
                    context.assignee = new

        if len(response_text) == 0 and not issue_has_changed:
            status = IStatusMessage(self.request)
            msg = _(u"No response text added and no issue changes made.")
            msg = translate(msg, 'Poi', context=self.request)
            status.addStatusMessage(msg, type='error')
        else:
            # Add response
            self.folder.add(new_response)
            context.reindexObject()
        redirect_url = "{0}?_authenticator={1}".format(context.absolute_url(),
                                                       authenticator.token())
        self.request.response.redirect(redirect_url)