Ejemplo n.º 1
0
    def _get_collection(self, name):
        cid = self.data.collections
        if cid:
            collection = getUtility(ISearchEventCollection)(cid)
            if collection.get(name):

                checked = self.request.form.get(name, [])
                names = []

                attr = name
                if name == 'paths':
                    attr = 'folders'
                    adapter = IAdapter(self.context)
                    portal_path = adapter.portal_path()
                    for nam in collection.get(name):
                        path = '{}{}'.format(portal_path, nam)
                        title = adapter.get_brain(path=path, depth=0).Title
                        names.append({
                            'checked': nam in checked,
                            'key': path,
                            'title': title,
                        })
                else:

                    for nam in collection.get(name):
                        names.append({'checked': nam in checked, 'key': nam})

                return {'names': names, 'title': getattr(self.data, attr)}
Ejemplo n.º 2
0
    def test__get_brains__empty(self):
        from Products.ATContentTypes.interfaces.folder import IATFolder
        base = IAdapter(self.portal)

        query = {}

        self.assertEqual(len(base.get_brains(**query)), 0)
        self.assertEqual(len(base.get_objects(**query)), 0)
        self.assertEqual(len(base.get_content_listing(**query)), 0)
        self.assertIsNone(base.get_brain(**query))
        self.assertIsNone(base.get_object(**query))

        self.assertEqual(len(base.get_brains(interfaces=IATFolder, **query)), 0)
        self.assertEqual(len(base.get_objects(interfaces=IATFolder, **query)), 0)
        self.assertEqual(len(base.get_content_listing(interfaces=IATFolder, **query)), 0)
        self.assertIsNone(base.get_brain(interfaces=IATFolder, **query))
        self.assertIsNone(base.get_object(interfaces=IATFolder, **query))
Ejemplo n.º 3
0
 def link(self):
     base = IBaseAdapter(self.context)
     path = '{}/{}'.format('/'.join(self.context.getPhysicalPath()),
                           self.records().path)
     query = {'path': {'query': path, 'depth': 0}}
     brain = base.get_brain(**query)
     if brain:
         return {
             'title': brain.Title,
             'url': brain.getURL(),
         }
Ejemplo n.º 4
0
 def link(self):
     base = IBaseAdapter(self.context)
     path = '{}/{}'.format(
         '/'.join(self.context.getPhysicalPath()),
         self.records().path)
     query = {'path': {'query': path, 'depth': 0}}
     brain = base.get_brain(**query)
     if brain:
         return {
             'title': brain.Title,
             'url': brain.getURL(),
         }
Ejemplo n.º 5
0
    def test__folder_and_document(self):
        from Products.ATContentTypes.interfaces.document import IATDocument
        from Products.ATContentTypes.interfaces.folder import IATFolder
        base = IAdapter(self.portal)

        folder1 = self.portal[self.portal.invokeFactory('Folder', 'folder1')]
        folder1.reindexObject()
        doc1 = self.portal[self.portal.invokeFactory('Document', 'doc1')]
        doc1.reindexObject()

        self.assertEqual(len(base.get_brains(IATDocument)), 1)
        self.assertEqual(len(base.get_objects(IATDocument)), 1)
        self.assertEqual(len(base.get_content_listing(IATDocument)), 1)
        self.assertEqual(base.get_brain(IATDocument).id, 'doc1')
        self.assertEqual(base.get_object(IATDocument).id, 'doc1')

        self.assertEqual(len(base.get_brains([IATDocument, IATFolder])), 2)
        self.assertEqual(len(base.get_objects([IATDocument, IATFolder])), 2)
        self.assertEqual(len(base.get_content_listing([IATDocument, IATFolder])), 2)

        self.assertEqual(len(base.get_brains([IATDocument], object_provides=IATFolder.__identifier__)), 2)
        self.assertEqual(len(base.get_objects([IATDocument], object_provides=IATFolder.__identifier__)), 2)
        self.assertEqual(len(base.get_content_listing([IATDocument], object_provides=IATFolder.__identifier__)), 2)
Ejemplo n.º 6
0
    def test__one_folder(self):
        """Add folder under portal."""
        from Products.ATContentTypes.interfaces.folder import IATFolder
        base = IAdapter(self.portal)

        folder1 = self.portal[self.portal.invokeFactory('Folder', 'folder1')]
        folder1.reindexObject()

        query = {}

        self.assertEqual(len(base.get_brains(**query)), 1)
        self.assertEqual(len(base.get_objects(**query)), 1)
        self.assertEqual(len(base.get_content_listing(**query)), 1)
        self.assertEqual(base.get_brain(**query).id, 'folder1')
        self.assertEqual(base.get_object(**query).id, 'folder1')

        self.assertEqual(len(base.get_brains(interfaces=IATFolder, **query)), 1)
        self.assertEqual(len(base.get_objects(interfaces=IATFolder, **query)), 1)
        self.assertEqual(len(base.get_content_listing(interfaces=IATFolder, **query)), 1)
        self.assertEqual(base.get_brain(interfaces=IATFolder, **query).id, 'folder1')
        self.assertEqual(base.get_object(interfaces=IATFolder, **query).id, 'folder1')

        query = {'path': '/'.join(folder1.getPhysicalPath())}

        self.assertEqual(len(base.get_brains(**query)), 1)
        self.assertEqual(len(base.get_objects(**query)), 1)
        self.assertEqual(len(base.get_content_listing(**query)), 1)
        self.assertEqual(base.get_brain(**query).id, 'folder1')
        self.assertEqual(base.get_object(**query).id, 'folder1')

        self.assertEqual(len(base.get_brains(interfaces=IATFolder, **query)), 1)
        self.assertEqual(len(base.get_objects(interfaces=IATFolder, **query)), 1)
        self.assertEqual(len(base.get_content_listing(interfaces=IATFolder, **query)), 1)
        self.assertEqual(base.get_brain(interfaces=IATFolder, **query).id, 'folder1')
        self.assertEqual(base.get_object(interfaces=IATFolder, **query).id, 'folder1')

        query['depth'] = 0
        self.assertEqual(len(base.get_brains(**query)), 1)
        self.assertEqual(len(base.get_objects(**query)), 1)
        self.assertEqual(len(base.get_content_listing(**query)), 1)
        self.assertEqual(base.get_brain(**query).id, 'folder1')
        self.assertEqual(base.get_object(**query).id, 'folder1')

        self.assertEqual(len(base.get_brains(interfaces=IATFolder, **query)), 1)
        self.assertEqual(len(base.get_objects(interfaces=IATFolder, **query)), 1)
        self.assertEqual(len(base.get_content_listing(interfaces=IATFolder, **query)), 1)
        self.assertEqual(base.get_brain(interfaces=IATFolder, **query).id, 'folder1')
        self.assertEqual(base.get_object(interfaces=IATFolder, **query).id, 'folder1')

        query['depth'] = 1
        self.assertEqual(len(base.get_brains(**query)), 0)
        self.assertEqual(len(base.get_objects(**query)), 0)
        self.assertEqual(len(base.get_content_listing(**query)), 0)
        self.assertIsNone(base.get_brain(**query))
        self.assertIsNone(base.get_object(**query))

        self.assertEqual(len(base.get_brains(interfaces=IATFolder, **query)), 0)
        self.assertEqual(len(base.get_objects(interfaces=IATFolder, **query)), 0)
        self.assertEqual(len(base.get_content_listing(interfaces=IATFolder, **query)), 0)
        self.assertIsNone(base.get_brain(interfaces=IATFolder, **query))
        self.assertIsNone(base.get_object(interfaces=IATFolder, **query))

        query['depth'] = 0
        self.assertEqual(len(base.get_brains(**query)), 1)
        self.assertEqual(len(base.get_objects(**query)), 1)
        self.assertEqual(len(base.get_content_listing(**query)), 1)
        self.assertEqual(base.get_brain(**query).id, 'folder1')
        self.assertEqual(base.get_object(**query).id, 'folder1')

        setRoles(self.portal, TEST_USER_ID, ['Member'])

        from plone.app.testing.helpers import logout
        logout()

        base = IAdapter(self.portal)
        self.assertEqual(len(base.get_brains(**query)), 0)

        query['unrestricted'] = True
        self.assertEqual(len(base.get_brains(**query)), 1)
Ejemplo n.º 7
0
    def handleAdd(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return

        # 1. Find or create folder where selected items will be archived.
        folder_id = "c"
        folder_title = "{} Converted".format(self.context.Title())
        folder = self.context.get(folder_id)
        if folder is None:
            folder = self.context[self.context.invokeFactory(
                'Folder', folder_id, title=folder_title)]

        # 2. Find original objects.
        adapter = IAdapter(self.context)
        paths = self.paths.split(
            '\r\n') if '\r\n' in self.paths else self.paths.split('\n')
        objs = self.context.getObjectsFromPathList(paths)
        form = self.request.form

        # Data from form
        title = form.get('form.widgets.IBasic.title')
        description = form.get('form.widgets.IBasic.description')
        fpaivays = None
        if form.get('form.widgets.paivays-year') and form.get(
                'form.widgets.paivays-day'):
            fpaivays = datetime(int(form.get('form.widgets.paivays-year')),
                                int(form.get('form.widgets.paivays-month')),
                                int(form.get('form.widgets.paivays-day')))
        text = form.get('form.widgets.text')

        omits = [
            'form.widgets.IBasic.title', 'form.widgets.IBasic.description',
            'form.widgets.paivays-year', 'form.widgets.paivays-month',
            'form.widgets.paivays-day', 'form.widgets.text',
            'form.widgets.text.mimeType', 'form.buttons.convert',
            'form.widgets.paivays-empty-marker',
            'form.widgets.paivays-calendar',
            'form.widgets.IVersionable.changeNote', 'form.widgets.paths'
        ]

        # file
        file_field = self.file_field()
        fname = 'form.widgets.{}'.format(file_field.getName())
        cfile = form.get(fname)
        omits.append(fname)
        if cfile:
            cfile.seek(0)
            file_data = cfile.read()
            file_name = cfile.filename
            cfile.close()

        # image
        image_field = self.image_field()
        iname = 'form.widgets.{}'.format(image_field.getName())
        cimage = form.get(iname)
        omits.append(iname)
        if cimage:
            cimage.seek(0)
            image_data = cimage.read()
            image_name = cimage.filename
            cimage.close()

        keys = [
            key for key in form.keys()
            if key not in omits and key.startswith('form.widgets.')
            and not key.endswith('empty-marker')
        ]
        data = {}
        for key in keys:
            val = form.get(key)
            if val:
                if isinstance(val, list):
                    val = [va.decode('unicode_escape') for va in val]
                data[key.split('.')[2]] = val

        object_ids = []
        # 3. Select values and create archive.
        for obj in objs:
            data = data.copy()
            data['title'] = safe_unicode(obj.Title()) or title
            data['description'] = safe_unicode(
                obj.Description()) or description
            uuid = obj.UID()
            brain = adapter.get_brain(UID=uuid)
            paivays = fpaivays
            if brain.review_state == 'published':
                paivays = brain.effective
            if paivays is None:
                paivays = brain.created
            if not isinstance(paivays, datetime):
                paivays = paivays.asdatetime().replace(tzinfo=None)
            data['paivays'] = paivays
            if obj.getField('text') is not None:
                text = self._strip_dev(
                    rewrite_links(safe_unicode(obj.getField('text').get(obj)),
                                  link_repl_func)) or text
            if text:
                data['text'] = text

            content = createContentInContainer(folder,
                                               'archive',
                                               checkConstraints=False,
                                               **data)

            # file
            filedata = None
            contentType = ''
            ofile = obj.getField('file', obj)
            if ofile:
                file_obj = ofile.get(obj)
                if file_obj and file_obj.get_size():
                    filedata = file_obj.data
                    filename = file_obj.filename or data['title']
                    contentType = file_obj.getContentType()

            if filedata is None and cfile:
                filedata = file_data
                filename = file_name

            if filedata is not None:
                setattr(
                    content, file_field.getName(),
                    NamedBlobFile(data=filedata,
                                  filename=safe_unicode(filename),
                                  contentType=contentType))

            # image
            imagedata = None
            contentType = ''
            oimage = obj.getField('image', obj)
            if oimage:
                image_obj = oimage.get(obj)
                if image_obj and image_obj.get_size():
                    imagedata = image_obj.data if not isinstance(
                        image_obj.data, Pdata) else image_obj.data.data
                    imagename = safe_unicode(
                        image_obj.filename) or data['title']
                    contentType = image_obj.getContentType()

            if imagedata is None and cimage:
                imagedata = image_data
                imagename = image_name

            if imagedata is not None:
                setattr(
                    content, image_field.getName(),
                    NamedBlobImage(data=imagedata,
                                   filename=safe_unicode(imagename),
                                   contentType=contentType))

            alsoProvides(content, IArchive)
            modified(content)

            object_ids.append(obj.id)
        # Remove the original object
        self.context.manage_delObjects(object_ids)

        message = _(
            u"add_converted_archives_success",
            default=
            u"${number} converted archive(s) are added to folder: ${title}",
            mapping={
                'number': len(objs),
                'title': safe_unicode(folder_title)
            })
        IStatusMessage(self.request).addStatusMessage(message, type='info')

        url = '{}/folder_contents'.format(self.context.absolute_url())
        return self.request.response.redirect(url)