Exemplo n.º 1
0
    def setUp(self):
        ''' define some helper variables here
        '''
        self.portal = self.layer['portal']
        self.request = self.layer['request'].clone()
        alsoProvides(self.request, IPloneintranetAttachmentsLayer)

        # Docconv: will generate previews for this
        self.pdf = api.content.create(
            container=self.portal,
            type='File',
            id='test-file',
            file=NamedBlobFile(data=resource_string(
                'ploneintranet.attachments.tests',
                'plone.pdf').decode('latin1', 'utf8'),
                               filename=u'plone.pdf'),
        )
        # This will be skipped by docconv: no need for preview generation
        self.image = api.content.create(
            container=self.portal,
            type='Image',
            id='test-image',
            image=dummy_image(),
        )
        # We also need a file that contains an image :)
        # This will be also skipped by docconv
        self.fileimage = api.content.create(
            container=self.portal,
            type='File',
            id='test-image-file',
            file=dummy_image(),
        )
        # We finally try with an empty file
        # This will be also skipped by docconv
        self.empty = api.content.create(
            container=self.portal,
            type='File',
            id='test-empty',
        )
Exemplo n.º 2
0
    def test_get_file(self):  # pragma: no cover
        self.portal.invokeFactory('File', id='file1')
        self.portal.file1.title = 'File'
        self.portal.file1.description = u'A file'
        pdf_file = os.path.join(os.path.dirname(__file__), u'file.pdf')
        self.portal.file1.file = NamedBlobFile(data=open(pdf_file, 'r').read(),
                                               contentType='application/pdf',
                                               filename=u'file.pdf')
        intids = getUtility(IIntIds)
        file_id = intids.getId(self.portal.file1)
        self.portal.file1.file = RelationValue(file_id)
        transaction.commit()

        response = self.api_session.get(self.portal.file1.absolute_url())

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.headers.get('Content-Type'), 'application/json',
            'When sending a GET request with Content-Type: application/json ' +
            'the server should respond with sending back application/json.')
        self.assertEqual(response.json()['@id'],
                         self.portal.file1.absolute_url())
Exemplo n.º 3
0
 def movetoPublic(self):
     path = '/'.join(self.context.getPhysicalPath())
     all_brains = api.content.find(portal_type='genweb.organs.file',
                                   path=path)
     results = []
     for brain in all_brains:
         obj = brain.getObject()
         # Show initial values
         if getattr(obj, 'visiblefile', False):
             initial_visible = obj.visiblefile.filename
         else:
             initial_visible = 'Empty'
         if getattr(obj, 'hiddenfile', False):
             initial_hidden = obj.hiddenfile.filename
         else:
             initial_hidden = 'Empty'
         final_visible = 'Not modified'
         final_hidden = 'Not modified'
         if obj.hiddenfile and not obj.visiblefile:
             initial_visible = 'Empty'
             initial_hidden = obj.hiddenfile.filename
             # Move private to public
             obj.visiblefile = NamedBlobFile(
                 data=obj.hiddenfile.data,
                 contentType=obj.hiddenfile.contentType,
                 filename=obj.hiddenfile.filename)
             transaction.commit()
             del obj.hiddenfile
             final_visible = obj.visiblefile.filename
             final_hidden = 'Empty (Deleted)'
         element = {
             'original-visiblefile': initial_visible,
             'original-hiddenfile': initial_hidden,
             'path': obj.absolute_url(),
             'final-visiblefile': final_visible,
             'final-hiddenfile': final_hidden,
         }
         results.append(element)
     return json.dumps(results, indent=2, sort_keys=True)
Exemplo n.º 4
0
    def test_getobjsize_file(self):
        from plone.namedfile.file import NamedBlobFile

        filename = os.path.join(os.path.dirname(__file__), u'image.jpg')
        with open(filename, 'rb') as f:
            file_data = f.read()
        test_file = NamedBlobFile(
            data=file_data,
            filename=filename)

        primary_field_info = IPrimaryFieldInfo(self.file)
        primary_field_info.field.set(self.file, test_file)
        self.file.reindexObject()

        brains = self.catalog.searchResults(dict(
            path='/plone/folder/file',
        ))

        self.assertEqual(
            '5.0 KB',
            brains[0].getObjSize,
        )
Exemplo n.º 5
0
    def _addDashboardPODTemplateExportOrganizations(self):
        """Add the export organizations DashboardPODTemplate in the contacts directory."""
        logger.info(
            "Adding 'Export CSV DashboardPODTemplate' to 'contacts' directory..."
        )
        pod_template_id = 'export-organizations'
        contacts = self.portal.contacts
        if pod_template_id in contacts.objectIds():
            self._already_migrated()
            return

        profile_path = self.ps._getImportContext(
            self.profile_name)._profile_path
        odt_path = profile_path + '/../testing/templates/organizations-export.ods'
        odt_file = open(odt_path, 'rb')
        odt_binary = odt_file.read()
        odt_file.close()
        data = {
            'title':
            'Export CSV',
            'pod_formats': ['ods', 'xls'],
            'dashboard_collections':
            contacts.get('orgs-searches').all_orgs.UID(),
            'odt_file':
            NamedBlobFile(
                data=odt_binary,
                contentType='application/vnd.oasis.opendocument.text',
                # pt.odt_file could be relative (../../other_profile/templates/sample.odt)
                filename=u'export-organizations.ods'),
            'use_objects':
            True,
        }
        podTemplate = api.content.create(id=pod_template_id,
                                         type='DashboardPODTemplate',
                                         container=contacts,
                                         **data)
        podTemplate.reindexObject()
        logger.info('Done.')
    def testCloneNamedFileBlobsOnCloneModifiers(self):
        file_fti = DexterityFTI('BlobFile',
                                model_source="""
            <model xmlns="http://namespaces.plone.org/supermodel/schema">
                <schema>
                    <field name="file" type="plone.namedfile.field.NamedBlobFile">
                        <title>File</title>
                        <required>True</required>
                    </field>
                </schema>
            </model>
        """)
        self.portal.portal_types._setObject('BlobFile', file_fti)

        file1 = createContentInContainer(self.portal, 'BlobFile')
        file1.file = NamedBlobFile('dummy test data', filename=u'test.txt')
        modifier = CloneNamedFileBlobs('modifier', 'Modifier')
        pers_id, pers_load, empty1, empty2 = modifier.getOnCloneModifiers(
            file1)
        self.assertTrue(pers_id(file1.file._blob))
        self.assertTrue(pers_load(file1.file._blob) is None)
        self.assertTrue(empty1 == [])
        self.assertTrue(empty2 == [])
Exemplo n.º 7
0
 def fixattrs(self, item, obj):
     for key in item.keys():
         if not key.startswith(self.datafield_prefix):
             continue
         value = base64.b64decode(item[key]['data'])
         fieldname = key[len(self.datafield_prefix):]
         if IBaseObject.providedBy(obj):
             field = obj.getField(fieldname)
             if field is None:
                 continue
             old_value = field.get(obj).data
             if value != old_value:
                 field.set(obj, value)
                 obj.setFilename(item[key]['filename'])
                 obj.setContentType(item[key]['content_type'])
         else:  # dexterity
             filename = item[key]['filename']
             if fieldname == 'file':
                 wrapped_data = NamedBlobFile(data=value, filename=filename)
             elif fieldname == 'image':
                 wrapped_data = NamedBlobImage(data=value,
                                               filename=filename)
             setattr(obj, fieldname, wrapped_data)
Exemplo n.º 8
0
    def printCSV(self, rows, type_info):
        lines = []
        if type_info == 'actividad':
            HEADER_CSV.extend(['community', 'activity', 'comments', 'documents', 'links', 'media'])
        elif type_info == 'chats':
            HEADER_CSV.extend(['chat', 'number of messages'])

        lines.append(HEADER_CSV)
        [lines.append(row) for row in rows['rows']]
        portal = getDestinationFolder(type_info)
        filename = "%s.csv" % time.strftime("%02d-%02m-%Y")

        output = StringIO.StringIO()
        a = csv.writer(output, delimiter=',')
        a.writerows(lines)
        data = output.getvalue()

        tb = transaction.begin()
        file = NamedBlobFile(data=data, filename=u'{}'.format(filename), contentType='application/csv')
        obj = createContentInContainer(portal, 'AppFile', id='{}'.format(filename), title='{}'.format(filename), file=file, checkConstraints=False)
        obj.reindexObject()
        tb.commit()
        self.response.setBody('%s' % tb.status, lock=True)
Exemplo n.º 9
0
    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.testfolder = api.content.create(
            type='Folder',
            title=u"Testfolder",
            container=self.portal)
        ff = open(os.path.join(os.path.dirname(__file__), TEST_FILENAME), 'r')
        self.filedata = ff.read()
        ff.close()
        self.testfile = api.content.create(
            type='File',
            id='test-file',
            title=u"Test File",
            file=NamedBlobFile(data=self.filedata, filename=TEST_FILENAME),
            container=self.testfolder)

        self.testdoc = api.content.create(
            type='Document',
            id='test-doc',
            title=u"Test Doc",
            text=u"This is a test doc with no preview generated (yet)",
            container=self.testfolder)
Exemplo n.º 10
0
    def test_dexterity_file_get(self):
        self.portal.invokeFactory('File', id='file')
        self.portal.file.title = 'My File'
        self.portal.file.description = u'This is a file'
        pdf_file = os.path.join(os.path.dirname(__file__), u'file.pdf')
        fd = open(pdf_file, 'rb')
        self.portal.file.file = NamedBlobFile(data=fd.read(),
                                              contentType='application/pdf',
                                              filename=u'file.pdf')
        fd.close()
        intids = getUtility(IIntIds)
        file_id = intids.getId(self.portal.file)
        self.portal.file.file = RelationValue(file_id)
        import transaction
        transaction.commit()

        response = requests.get(self.portal.file.absolute_url(),
                                headers={'Accept': 'application/json'},
                                auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD))

        self.assertEqual(200, response.status_code)
        self.assertEqual(u'file', response.json().get('id'))
        self.assertEqual(u'GET', response.json().get('method'))
Exemplo n.º 11
0
    def revert_to_version(self, version_id, create_version=True, force=False):
        """Reverts the adapted document to a specific version. We only revert
        the file field, since we do not wan't to version the metadata on the
        document.
        If `create_version` is set to `True`, a new version is created after
        reverting.
        """
        if not force and not self.is_revert_allowed():
            # Avoid plone.protect unnecessarily jumping in
            self.request.response.setHeader('content-type', 'text/plain')
            raise Unauthorized()

        version = self.versioner.retrieve_version(version_id)
        old_obj = version.object
        if old_obj.file:
            # Create a new NamedBlobFile instance instead of using
            # a reference in order to avoid the version being reverted
            # to being overwritten later
            old_file_copy = NamedBlobFile(old_obj.file.data,
                                          filename=old_obj.file.filename,
                                          contentType=old_obj.file.contentType)

            self.context.file = old_file_copy
        else:
            self.context.file = None

        if create_version:
            # let's create a version
            msg = _(u'Reverted file to version ${version_id}.',
                    mapping=dict(version_id=version_id))
            comment = translate(msg, context=self.request)
            self.versioner.create_version(comment)

        # event
        notify(
            ObjectRevertedToVersion(self.context, version_id, create_version))
Exemplo n.º 12
0
    def test_dexterity_item_event(self):
        provideHandler(
            factory=self.mock_event.mock_handler,
            adapts=[
                IItemZippedEvent,
            ],
        )

        note = create(
            Builder("event_note").having(
                blob=NamedBlobFile(data='NoteNoteNote', filename=u'note.txt')))

        # Force the generator to fire, the events live there
        list(
            getMultiAdapter((note, self.request),
                            interface=IZipRepresentation).get_files())

        self.assertEquals(1, len(self.mock_event.event_history))

        self.assertIsInstance(self.mock_event.event_history[0],
                              ItemZippedEvent)

        self.assertEquals('event_note',
                          self.mock_event.event_history[0].object.id)
 def sample_file(self):
     file_value = NamedBlobFile(
         data=TestDocumentDefaults.SAMPLE_FILE,
         contentType='text/plain',
         filename=u'b\xe4rengraben.txt')
     return file_value
Exemplo n.º 14
0
    def test_accepts_normal_files(self):
        file = NamedBlobFile('bla bla', filename=u'test.txt')

        validator = UploadValidator(*self.validator_arguments())

        validator.validate(file)
Exemplo n.º 15
0
 def test_upload_file(self):
     document = create(Builder("document"))
     field = IDocumentSchema['file']
     file = NamedBlobFile('bla bla', filename=u'test.txt')
     field.set(document, file)
     self.assertTrue(field.get(document).data == 'bla bla')
Exemplo n.º 16
0
    def folderupload(self, files, title='', description=''):

        if not self.doc.isDocument():
            return

        self.doc.setfile(self.doc, files[0])

        docfolderId = self.doc.getId()
        dbfolderId = self.doc.getParentDatabase().id

        #TODO come configurare diverse cartelle per applicazione o plomino db (da fare sulle singole app)?
        target = self.context.portal_url.getPortalObject()[ATTACHMENT_FOLDER]

        with adopt_roles('Manager'):
            if not dbfolderId in target.keys():
                target.invokeFactory("Folder", id=dbfolderId, title=dbfolderId)
            target = target[dbfolderId]
            if not docfolderId in target.keys():
                target.invokeFactory("Folder",
                                     id=docfolderId,
                                     title=docfolderId)
            docFolder = target[docfolderId]
            for username, roles in self.doc.get_local_roles():
                docFolder.manage_setLocalRoles(username, roles)

        if not isinstance(files, list):
            files = [files]

        #se non  campo multiplo vuoto la cartella
        #TODO tenere allineati i files nella cartella con i nomi sul campo di plomino
        current_files = self.doc.getItem(self.element)
        cleaned_files = {}
        if self.multiple:
            for fName in current_files:
                if fName in docFolder.keys():
                    cleaned_files[fName] = current_files[fName]
            self.doc.setItem(self.element, cleaned_files)

        #se upload singolo cancello tutti i file presenti collegati al campo
        else:
            try:
                docFolder.manage_delObjects(current_files.keys())
            except Exception as error:
                pass
            self.doc.removeItem(self.element)

        namechooser = INameChooser(docFolder)
        loaded = []
        for item in files:
            if item.filename:
                content_type = item.headers.get('Content-Type')
                filename = safe_unicode(item.filename)
                data = item.read()
                id_name = ''
                title = title and title[0] or filename
                # Get a unique id here
                id_name = namechooser.chooseName(title, docFolder)

                # Portal types allowed : File and Image
                # Since Plone 4.x both types use Blob
                if content_type in IMAGE_MIMETYPES:
                    portal_type = 'Image'
                    wrapped_data = NamedBlobImage(data=data, filename=filename)
                else:
                    portal_type = 'File'
                    wrapped_data = NamedBlobFile(data=data, filename=filename)

                # Create content
                docFolder.invokeFactory(portal_type,
                                        id=id_name,
                                        title=title,
                                        description=description[0])

                newfile = docFolder[id_name]
                # Set data
                if portal_type == 'File':
                    if IATFile.providedBy(newfile):
                        newfile.setFile(data, filename=filename)
                    else:
                        newfile.file = wrapped_data
                elif portal_type == 'Image':
                    if IATImage.providedBy(newfile):
                        newfile.setImage(data, filename=filename)
                    else:
                        newfile.image = wrapped_data

                # Finalize content creation, reindex it
                newfile.reindexObject()
                notify(ObjectModifiedEvent(newfile))
                loaded.append(newfile)
            if loaded:
                return loaded
            return False
Exemplo n.º 17
0
 def with_asset_file(self, filename):
     self.document.file = NamedBlobFile(
         assets.load(filename), filename=unicode(filename))
 def setup_content_data(self):
     path = os.path.dirname(__file__)
     mp3_audio = open(os.path.join(path, 'files', 'file.mp3')).read()
     ogg_audio = open(os.path.join(path, 'files', 'file.ogg')).read()
     self.mp3 = NamedBlobFile(mp3_audio, 'audio/mp3', u'file.mp3')
     self.ogg = NamedBlobFile(ogg_audio, 'audio/ogg', u'file.ogg')
Exemplo n.º 19
0
 def sample_msg(self):
     message_value = NamedBlobFile(data=TestMailDefaults.SAMPLE_MAIL,
                                   contentType='message/rfc822',
                                   filename=u'msg.eml')
     return message_value
Exemplo n.º 20
0
    def addPodTemplate(self, container, pt, source):
        '''Adds a POD template from p_pt (a PodTemplateDescriptor instance).'''
        cfg = None
        if container.portal_type == 'MeetingConfig':
            cfg = container
            dest_folder = getattr(container, TOOL_FOLDER_POD_TEMPLATES)
        else:
            # contacts DashboardPODTemplate
            dest_folder = container
        # The template must be retrieved on disk from a profile or use another pod_template
        odt_file = None
        pod_template_to_use = None
        if pt.odt_file:
            # pt.odt_file may be a full path or a relative path
            filePath = pt.odt_file.startswith(
                '/') and pt.odt_file or '%s/templates/%s' % (source,
                                                             pt.odt_file)
            data = self.find_binary(filePath)
            odt_file = NamedBlobFile(
                data=data,
                contentType='application/vnd.oasis.opendocument.text',
                # pt.odt_file could be relative (../../other_profile/templates/sample.odt)
                filename=safe_unicode(pt.odt_file.split('/')[-1]),
            )
        elif pt.pod_template_to_use['cfg_id']:
            pod_template_to_use_cfg = self.tool.get(
                pt.pod_template_to_use['cfg_id'])
            if not pod_template_to_use_cfg:
                logger.warning(
                    'Cfg with id {0} not found when adding Pod template {1}, template was not added'
                    .format(pt.pod_template_to_use['cfg_id'],
                            pt.pod_template_to_use['template_id']))
                return
            pod_template = pod_template_to_use_cfg.podtemplates.get(
                pt.pod_template_to_use['template_id'])
            if not pod_template:
                logger.warning(
                    'Pod template with id {0} not found in cfg with id {1}, template was not added'
                    .format(pt.pod_template_to_use['template_id'],
                            pt.pod_template_to_use['cfg_id']))
                return
            pod_template_to_use = pod_template.UID()
        else:
            raise PloneMeetingError(
                'A PodTemplateDescriptor must have a defined odt_file or pod_template_to_use!'
            )
        data = pt.getData(odt_file=odt_file,
                          pod_template_to_use=pod_template_to_use)

        if data['is_style']:
            podType = 'StyleTemplate'
        else:
            podType = pt.dashboard and 'DashboardPODTemplate' or 'ConfigurablePODTemplate'

            # turn the pod_portal_types from MeetingItem to MeetingItemShortname
            adapted_pod_portal_types = []
            for pod_portal_type in data['pod_portal_types']:
                if pod_portal_type.startswith('Meeting'):
                    pod_portal_type = pod_portal_type + cfg.shortName
                adapted_pod_portal_types.append(pod_portal_type)
            data['pod_portal_types'] = adapted_pod_portal_types

            if podType == 'DashboardPODTemplate':
                # parameter use_objects is excluded by default as only relevant for DashboardPODTemplates
                data['use_objects'] = pt.use_objects
                # manage dashboard_collections from dashboard_collection_ids
                # we have ids and we need UIDs
                res = []
                if cfg:
                    collections_container = cfg.searches
                else:
                    # contacts
                    collections_container = container
                for coll_id in pt.dashboard_collections_ids:
                    for folder in collections_container.objectValues(
                            'ATFolder'):
                        if coll_id in folder.objectIds():
                            collection = getattr(folder, coll_id)
                            break
                    res.append(collection.UID())
                data['dashboard_collections'] = res
            for sub_template in data['merge_templates']:
                sub_template['template'] = dest_folder.get(
                    sub_template['template']).UID()

        # associate style template with pod template if necessary
        if not data['is_style'] and data['style_template']:
            # we have a list of style templates
            styles_uids = []
            for style_template in data['style_template']:
                style_template_obj = dest_folder.get(style_template)
                if style_template_obj.id in data['style_template']:
                    styles_uids.append(style_template_obj.UID())

            data['style_template'] = styles_uids

        podTemplate = api.content.create(type=podType,
                                         container=dest_folder,
                                         **data)
        validate_fields(podTemplate, raise_on_errors=True)
        return podTemplate
Exemplo n.º 21
0
    def getDefaultForFieldType(self, field):

        # default string
        default_value = self.placeholder

        # Field Class
        field_klass = field.__class__.__name__

        # Handle different value_type attributes of the field.
        value_type = getattr(field, 'value_type', None)

        if value_type:
            # If we're a data grid field
            if isinstance(value_type, DictRow):
                kwargs = {}

                s = getattr(value_type, 'schema', None)

                if s:
                    for (_name,
                         _field) in getAllSchemaFieldsAndDescriptions(s):
                        if not isinstance(field, Method):
                            kwargs[_name] = self.getDefaultForFieldType(_field)

                return [
                    kwargs,
                ]
            elif isinstance(value_type, (schema.TextLine, )):
                pass
            elif isinstance(value_type, (schema.Choice, )):
                vocabulary_name = getattr(value_type, 'vocabularyName', None)
                vocabulary = getattr(value_type, 'vocabulary', None)

                if vocabulary_name:
                    vocabulary_factory = getUtility(IVocabularyFactory,
                                                    vocabulary_name)
                    vocabulary = vocabulary_factory(self.context)

                if vocabulary:
                    if isinstance(vocabulary, SimpleVocabulary):
                        try:
                            default_value = vocabulary.by_value.keys()[0]
                        except:
                            pass
                    else:
                        pass

        # Return nothing for methods
        if field_klass in [
                'Method',
        ]:
            return None

        # Define dummy fields

        rich_text = RichTextValue(raw='<p>%s</p>' % self.placeholder,
                                  mimeType=u'text/html',
                                  outputMimeType='text/x-html-safe')

        named_blob_file = NamedBlobFile(filename=u'sample.pdf',
                                        contentType='application/pdf')

        named_blob_file.data = self.placeholder.encode('utf-8')

        named_blob_image = NamedBlobImage(filename=u'sample.png',
                                          contentType='image/png')

        named_blob_image.data = self.placeholder.encode('utf-8')

        # Simple field defaults

        defaults = {
            'Int': 10,
            'Text': "\n".join(3 * [self.placeholder]),
            'List': [
                default_value,
            ],
            'Tuple': (default_value, ),
            'TextLine': default_value,
            'Bool': True,
            'Datetime': datetime.now(),
            'RichText': rich_text,
            'NamedBlobFile': named_blob_file,
            'NamedBlobImage': named_blob_image,
            'Choice': default_value,
        }

        # If a default, return that.  Otherwise, return the placeholder.
        return defaults.get(field_klass, self.placeholder)
Exemplo n.º 22
0
def process(context):
    video = context.file

    if not video or video.filename == aws.FILENAME:
        return

    try:
        opened = openBlob(video._blob)
        bfilepath = opened.name
        opened.close()
    except IOError:
        logger.warn('error opening blob file')
        return

    # by default, assume all non-mp4 videos need to be converted
    # but in reality, all videos need converting, even mp4.
    # md5 is only what makes this possible
    convert_it = video.contentType.split('/')[-1] != 'mp4'
    if md5 is not None:
        old_hash = getattr(context, '_file_hash', None)
        current_hash = md5(bfilepath)
        if old_hash is None or old_hash != current_hash:
            convert_it = True

    if context.image and not convert_it:
        # already an mp4 and already has a screen grab
        return

    tmpdir = mkdtemp()
    tmpfilepath = os.path.join(tmpdir, video.filename)
    copyfile(bfilepath, tmpfilepath)

    if convert_it:
        output_filepath = os.path.join(tmpdir, 'output.mp4')
        try:
            avconv.convert(tmpfilepath, output_filepath)
        except:
            pass
        if os.path.exists(
                output_filepath) and os.path.getsize(output_filepath) > 0:
            if md5 is not None:
                try:
                    context._file_hash = md5(output_filepath)
                except:
                    pass
            context._p_jar.sync()
            fi = open(output_filepath)
            namedblob = NamedBlobFile(fi,
                                      filename=switchFileExt(
                                          video.filename, 'mp4'))
            context.file = namedblob
            fi.close()

    if not context.image:
        # try and grab one from video
        output_filepath = os.path.join(tmpdir, u'screengrab.png')
        try:
            avconv.grab_frame(tmpfilepath, output_filepath)
            if os.path.exists(output_filepath):
                fi = open(output_filepath)
                context.image = NamedBlobImage(fi, filename=u'screengrab.png')
                fi.close()
        except:
            logger.warn('error getting thumbnail from video')
    rmtree(tmpdir)
Exemplo n.º 23
0
def install_demo(context):
    """ """
    if context.readDataFile('collectivedocumentgenerator_demo_marker.txt') is None:
        return

    portal = api.portal.get()

    if not hasattr(portal, 'podtemplates'):
        templates_folder = api.content.create(
            type='Folder',
            title=_(u'POD Templates'),
            id='podtemplates',
            container=portal,
            exclude_from_nav=True
        )
        templates_folder.setTitle('POD Templates')
        templates_folder.reindexObject()

    pod_folder = getattr(portal, 'podtemplates')
    constrain_types = IConstrainTypes(pod_folder)
    constrain_types.setConstrainTypesMode(1)
    constrain_types.setLocallyAllowedTypes(POD_TEMPLATE_TYPES.keys())
    constrain_types.setImmediatelyAddableTypes(POD_TEMPLATE_TYPES.keys())

    # Create some test content
    style_template_id = 'test_style_template'
    if not hasattr(pod_folder, style_template_id):
        api.content.create(
            type='StyleTemplate',
            id=style_template_id,
            title='Styles',
            odt_file=NamedBlobFile(
                data=context.readDataFile('templates/styles.odt'),
                contentType='application/vnd.oasis.opendocument.text',
                filename=u'styles.odt',
            ),
            container=pod_folder,
            exclude_from_nav=True
        )
    style_template = getattr(pod_folder, style_template_id)

    style_template_id = 'test_style_template_2'
    if not hasattr(pod_folder, style_template_id):
        api.content.create(
            type='StyleTemplate',
            id=style_template_id,
            title=_(u'Styles n°2'),
            odt_file=NamedBlobFile(
                data=context.readDataFile('templates/styles_2.odt'),
                contentType='application/vnd.oasis.opendocument.text',
                filename=u'styles_2.odt',
            ),
            container=pod_folder,
            exclude_from_nav=True
        )

    sub_template_id = 'sub_template'
    if not hasattr(pod_folder, sub_template_id):
        api.content.create(
            type='SubTemplate',
            id=sub_template_id,
            title=_(u'Header'),
            odt_file=NamedBlobFile(
                data=context.readDataFile('templates/sub_template.odt'),
                contentType='application/vnd.oasis.opendocument.text',
                filename=u'sub_template.odt',
            ),
            container=pod_folder,
            exclude_from_nav=True,
            style_template=[style_template.UID()],
        )
    sub_template = getattr(pod_folder, sub_template_id)

    if not hasattr(pod_folder, 'loop_template'):
        api.content.create(
            type='MailingLoopTemplate',
            id='loop_template',
            title=_(u'Mailing loop template'),
            odt_file=NamedBlobFile(
                data=context.readDataFile('templates/mailing.odt'),
                contentType='application/vnd.oasis.opendocument.text',
                filename=u'mailing.odt',
            ),
            container=pod_folder,
            exclude_from_nav=True,
            style_template=[style_template.UID()],
        )
    loop_template = getattr(pod_folder, 'loop_template')

    if not hasattr(pod_folder, 'test_template'):
        api.content.create(
            type='PODTemplate',
            id='test_template',
            title=_(u'General template'),
            odt_file=NamedBlobFile(
                data=context.readDataFile('templates/modele_general.odt'),
                contentType='application/vnd.oasis.opendocument.text',
                filename=u'modele_general.odt',
            ),
            container=pod_folder,
            exclude_from_nav=True,
        )

    if not hasattr(pod_folder, 'test_template_multiple'):
        api.content.create(
            type='ConfigurablePODTemplate',
            id='test_template_multiple',
            title=_(u'Multiple format template'),
            odt_file=NamedBlobFile(
                data=context.readDataFile('templates/modele_general.odt'),
                contentType='application/vnd.oasis.opendocument.text',
                filename=u'modele_general.odt',
            ),
            container=pod_folder,
            exclude_from_nav=True,
            pod_formats=['odt', 'pdf', 'doc', 'docx'],
            pod_portal_types=['Document'],
            style_template=[style_template.UID()],
            merge_templates=[
                {
                    'template': sub_template.UID(),
                    'pod_context_name': 'header',
                    'do_rendering': True,
                }
            ],
        )

    if not hasattr(pod_folder, 'test_template_bis'):
        api.content.create(
            type='ConfigurablePODTemplate',
            id='test_template_bis',
            title=_(u'Collection template'),
            odt_file=NamedBlobFile(
                data=context.readDataFile(safe_unicode('templates/modèle_collection.odt')),
                contentType='application/vnd.oasis.opendocument.text',
                filename=u'modèle_collection.odt',
            ),
            container=pod_folder,
            exclude_from_nav=True,
            pod_formats=['odt', 'pdf', ],
            pod_portal_types=['Collection', 'Folder'],
            style_template=[style_template.UID()],
            merge_templates=[
                {
                    'template': sub_template.UID(),
                    'pod_context_name': 'header',
                    'do_rendering': False,
                }
            ],
            context_variables=[
                {
                    'name': 'details',
                    'value': '1',
                }
            ],
        )

    if not hasattr(pod_folder, 'test_ods_template'):
        api.content.create(
            type='ConfigurablePODTemplate',
            id='test_ods_template',
            title=_(u'Spreadsheet template'),
            odt_file=NamedBlobFile(
                data=context.readDataFile('templates/modele_general.ods'),
                contentType='application/vnd.oasis.opendocument.spreadsheet',
                filename=u'modele_general.ods',
            ),
            container=pod_folder,
            exclude_from_nav=True,
            pod_formats=['ods', 'xls', ],
            pod_portal_types=['Document'],
            style_template=[style_template.UID()],
        )

    if not hasattr(pod_folder, 'test_template_possibly_mailed'):
        api.content.create(
            type='ConfigurablePODTemplate',
            id='test_template_possibly_mailed',
            title=_(u'Possibly mailed template'),
            odt_file=NamedBlobFile(
                data=context.readDataFile('templates/possibly_mailed_model.odt'),
                contentType='application/vnd.oasis.opendocument.text',
                filename=u'possibly_mailed_model.odt',
            ),
            container=pod_folder,
            exclude_from_nav=True,
            pod_formats=['odt'],
            pod_portal_types=['Folder'],
            mailing_loop_template=loop_template.UID(),
            context_variables=[
                {
                    'name': 'details',
                    'value': '1',
                }
            ],
        )

    if not hasattr(pod_folder, 'test_template_reusable'):
        api.content.create(
            type='ConfigurablePODTemplate',
            id='test_template_reusable',
            title=_(u'Reusable template'),
            odt_file=NamedBlobFile(
                data=context.readDataFile('templates/modele_general.odt'),
                contentType='application/vnd.oasis.opendocument.text',
                filename=u'modele_general.odt',
            ),
            is_reusable=True,
            container=pod_folder,
            pod_formats=['odt'],
            pod_portal_types=['Folder'],
            exclude_from_nav=True
        )

    reusable_template = getattr(pod_folder, 'test_template_reusable')

    if not hasattr(pod_folder, 'test_template_reuse'):
        api.content.create(
            type='ConfigurablePODTemplate',
            id='test_template_reuse',
            title=_(u'Reuse Test Template'),
            container=pod_folder,
            exclude_from_nav=True,
            pod_formats=['odt', 'pdf', 'doc', 'docx'],
            pod_portal_types=['Document'],
            pod_template_to_use=reusable_template.UID()
        )
Exemplo n.º 24
0
def dummy_file():
    from plone.namedfile.file import NamedBlobFile
    path = os.path.join(os.path.dirname(__file__), FILENAME)
    return NamedBlobFile(data=open(path, 'r').read(), filename=FILENAME)
Exemplo n.º 25
0
    def __iter__(self):
        for item in self.previous:
            if item['_type'] == 'tdf.templateuploadcenter.tuprelease' or \
               item['_type'] == 'tdf.extensionuploadcenter.euprelease' or \
               item['_type'] == 'PSCRelease':

                release = self.context.unrestrictedTraverse(
                    str(item['_path']).lstrip('/'), None)
                if not release:
                    yield item

                # Get the files below it from the item's original_path
                catalog_query = {
                    'path': {
                        'query': ''
                    },
                    'portal_type': [
                        'PSCFile',
                    ]
                }
                catalog_query['path'][
                    'query'] = self.remote_root + item['_original_path']
                catalog_query = ' '.join(str(catalog_query).split())
                catalog_query = base64.b64encode(catalog_query)

                self.payload = {'catalog_query': catalog_query}

                # Make request
                resp = requests.get(
                    '{}{}/get_catalog_results'.format(self.remote_url,
                                                      self.catalog_path),
                    params=self.payload,
                    auth=(self.remote_username, self.remote_password))
                file_list = resp.json()

                # Get the files
                index = 0
                for rfile in file_list:
                    resp = requests.get(self.remote_url + rfile,
                                        auth=item['_auth_info'])
                    if resp.ok:
                        data = resp.content
                        filename = rfile.split('/')[-1]

                        thefile = NamedBlobFile(
                            data=data,
                            filename=filename,
                            contentType=mimetypes.guess_type(filename)[0]
                            or '')

                        if index < len(AVAILABLE_FIELDS):
                            setattr(release, AVAILABLE_FIELDS[index], thefile)
                        else:
                            migration_error.error(
                                'The release {} has exceeded the number of allowed files. The file {} has been discarded.'
                                .format(aq_parent(release).id, filename))
                        index = index + 1

                    else:
                        migration_error.error(
                            'There was a problem trying to retrieve the file.')

            yield item
Exemplo n.º 26
0
        def set_field_value(self, uid, field, value, field_type):
            """Set field value with a specific type

            XXX: Only dexterity fields are supported
            """
            pc = getToolByName(self, 'portal_catalog')
            results = pc.unrestrictedSearchResults(UID=uid)
            obj = results[0]._unrestrictedGetObject()
            if field_type == 'float':
                value = float(value)
            if field_type == 'int':
                value = int(value)
            if field_type == 'list':
                value = eval(value)
            if field_type.startswith('datetime'):
                # field_type must begin with 'datetime'
                # followed by optional format 'datetime%Y%m%d%H%M'
                # without format: %Y%m%d%H%M is used
                field_type = field_type[8:]
                fmt = field_type and field_type or '%Y%m%d%H%M'
                value = datetime.strptime(value, fmt)
            if field_type.startswith('date'):
                # field_type must begin with 'date'
                # followed by optional format 'date%Y%m%d'
                # without format: %Y%m%d is used
                field_type = field_type[4:]
                fmt = field_type and field_type or '%Y%m%d'
                value = datetime.strptime(value, fmt).date()
            if field_type == 'reference' and HAS_DEXTERITY_RELATIONS:
                results_referenced = pc.unrestrictedSearchResults(UID=value)
                referenced_obj = results_referenced[0]._unrestrictedGetObject()
                intids = getUtility(IIntIds)
                referenced_obj_intid = intids.getId(referenced_obj)
                value = RelationValue(referenced_obj_intid)
            if field_type == 'references' and HAS_DEXTERITY_RELATIONS:
                values = eval(value)
                intids = getUtility(IIntIds)
                value = []
                for uid in values:
                    results_referenced = pc.unrestrictedSearchResults(UID=uid)
                    referenced_obj = results_referenced[
                        0]._unrestrictedGetObject()
                    referenced_obj_intid = intids.getId(referenced_obj)
                    value.append(RelationValue(referenced_obj_intid))
            if field_type == 'text/html':
                value = RichTextValue(value, 'text/html', 'text/html')
                obj.text = value
            if field_type == 'file':
                pdf_file = os.path.join(os.path.dirname(__file__), 'content',
                                        u'file.pdf')
                value = NamedBlobFile(data=open(pdf_file, 'r').read(),
                                      contentType='application/pdf',
                                      filename=u'file.pdf')
            if field_type == 'image':
                image_file = os.path.join(os.path.dirname(__file__),
                                          u'image.jpg')
                value = NamedBlobImage(data=open(image_file, 'r').read(),
                                       contentType='image/jpg',
                                       filename=u'image.jpg')

            setattr(obj, field, value)
            obj.reindexObject()
            notify(ObjectModifiedEvent(obj))
Exemplo n.º 27
0
    def create_content(self, *args, **kwargs):
        """Create content and return its UID"""
        disableCSRFProtection()
        # XXX: Because kwargs are only supported with robotframework >= 2.8.3,
        # we must parse them here to support robotframework < 2.8.3.
        for arg in [x for x in args if '=' in x]:
            name, value = arg.split('=', 1)
            kwargs[name] = value

        assert 'type' in kwargs, u"Keyword arguments must include 'type'."
        portal_type = kwargs.get('type')
        portal = getSite()
        if 'container' in kwargs:
            pc = getToolByName(portal, 'portal_catalog')
            uid_or_path = kwargs.pop('container')
            uid_results =\
                pc.unrestrictedSearchResults(UID=uid_or_path)
            path_results = \
                pc.unrestrictedSearchResults(
                    path={'query': uid_or_path.rstrip('/'), 'depth': 0})
            container =\
                (uid_results or path_results)[0]._unrestrictedGetObject()
        else:
            container = portal

        # if we create 'file' and 'image' kwargs entries, they should not be
        # used to create the content but be set afterwards
        create_kwargs = {}
        create_kwargs.update(kwargs)

        if HAS_DEXTERITY:
            if portal_type in ('File', ) and 'file' not in kwargs:
                pdf_file = os.path.join(os.path.dirname(__file__), 'content',
                                        u'file.pdf')
                value = NamedBlobFile(data=open(pdf_file, 'r').read(),
                                      contentType='application/pdf',
                                      filename=u'file.pdf')
                kwargs['file'] = value

        if portal_type in ('Image', 'News Item') and 'image' not in kwargs:
            prefill_image_types(portal, kwargs)

        id_ = kwargs.pop('id', None)
        type_ = kwargs.pop('type')

        content = None
        if HAS_DEXTERITY:
            # The title attribute for Dexterity types needs to be unicode
            if 'title' in kwargs and isinstance(kwargs['title'], str):
                kwargs['title'] = kwargs['title'].decode('utf-8')
            from plone.dexterity.interfaces import IDexterityFTI
            from plone.dexterity.utils import createContentInContainer
            try:
                getUtility(IDexterityFTI, name=type_)
                content = createContentInContainer(container, type_,
                                                   **create_kwargs)
                if id_ is not None and content.id != id_:
                    container.manage_renameObject(content.id, id_)
            except ComponentLookupError:
                pass

        if HAS_DEXTERITY and content:
            # For dexterity-types, we need a second pass to fill all fields
            # using their widgets to get e.g. RichText-values created
            # correctly.
            fti = getUtility(IDexterityFTI, name=type_)
            schema = fti.lookupSchema()
            fields = {}
            for name in schema:
                fields[name] = schema[name]
            for schema in getAdditionalSchemata(portal_type=type_):
                for name in schema:
                    fields[name] = schema[name]
            for name, field in fields.items():
                widget = queryMultiAdapter((field, getRequest()), IFieldWidget)
                if widget and name in kwargs:
                    if not IFromUnicode.providedBy(field):
                        value = kwargs[name]
                    elif isinstance(kwargs[name], unicode):
                        value = kwargs[name]
                    else:
                        value = unicode(str(kwargs[name]),
                                        'utf-8',
                                        errors='ignore')
                    converter = IDataConverter(widget)
                    dm = queryMultiAdapter((content, field), IDataManager)
                    if dm:
                        dm.set(converter.toFieldValue(value))

        if content is None:
            if id_ is None:
                normalizer = queryUtility(IURLNormalizer)
                id_ = normalizer.normalize(kwargs['title'])

            # It must be Archetypes based content:
            content = container[container.invokeFactory(type_, id_, **kwargs)]
            content.processForm()

        return IUUID(content)
Exemplo n.º 28
0
def process_local_images(data, obj, base_image_path):
    get_file_type = magic.Magic(mime=True)
    image_fieldnames_added = []

    if data.get("set_dummy_image", False) and isinstance(
            data.get("set_dummy_image"), list):
        new_file = BytesIO()
        generate_image().save(new_file, "png")
        new_file = new_file if type(new_file) == str else new_file.getvalue()
        for image_field in data["set_dummy_image"]:
            setattr(
                obj,
                image_field,
                NamedBlobImage(data=new_file, contentType="image/png"),
            )

        image_fieldnames_added + data["set_dummy_image"]

    elif data.get("set_dummy_image", False) and isinstance(
            data.get("set_dummy_image"), bool):
        # Legacy behavior, set_dummy_image is a boolean
        obj.image = NamedBlobImage(data=generate_image().tobytes(),
                                   contentType="image/png")

        image_fieldnames_added.append("image")

    if data.get("set_dummy_file", False) and isinstance(
            data.get("set_dummy_file"), list):
        new_file = BytesIO()
        generate_image().save(new_file, "png")
        new_file = new_file if type(new_file) == str else new_file.getvalue()
        for image_field in data["set_dummy_file"]:
            setattr(
                obj,
                image_field,
                NamedBlobFile(data=new_file, contentType="image/png"),
            )

    elif data.get("set_dummy_file", False) and isinstance(
            data.get("set_dummy_file"), bool):
        # Legacy behavior, set_dummy_file is a boolean
        obj.file = NamedBlobFile(data=generate_image().tobytes(),
                                 contentType="image/png")

    if data.get("set_local_image", False) and isinstance(
            data.get("set_local_image"), dict):
        for image_data in data["set_local_image"].items():
            new_file = open(os.path.join(base_image_path, image_data[1]), "rb")
            # Get the correct content-type
            content_type = get_file_type.from_buffer(new_file.read())
            new_file.seek(0)

            setattr(
                obj,
                image_data[0],
                NamedBlobImage(
                    data=new_file.read(),
                    filename=image_data[1],
                    contentType=content_type,
                ),
            )

            image_fieldnames_added.append(image_data[0])

    elif data.get("set_local_image", False) and isinstance(
            data.get("set_local_image"), str):
        new_file = open(
            os.path.join(base_image_path, data.get("set_local_image")), "rb")
        # Get the correct content-type
        content_type = get_file_type.from_buffer(new_file.read())
        new_file.seek(0)

        obj.image = NamedBlobImage(
            data=new_file.read(),
            filename=data.get("set_local_image"),
            contentType=content_type,
        )

        image_fieldnames_added.append("image")

    if data.get("set_local_file", False) and isinstance(
            data.get("set_local_file"), dict):
        for image_data in data["set_local_file"].items():
            new_file = open(os.path.join(base_image_path, image_data[1]), "rb")
            # Get the correct content-type
            content_type = get_file_type.from_buffer(new_file.read())
            new_file.seek(0)

            setattr(
                obj,
                image_data[0],
                NamedBlobFile(
                    data=new_file.read(),
                    filename=image_data[1],
                    contentType=content_type,
                ),
            )

    elif data.get("set_local_file", False) and isinstance(
            data.get("set_local_file"), str):
        new_file = open(
            os.path.join(base_image_path, data.get("set_local_file")), "rb")
        # Get the correct content-type
        content_type = get_file_type.from_buffer(new_file.read())
        new_file.seek(0)

        obj.file = NamedBlobFile(
            data=new_file.read(),
            filename=data.get("set_local_file"),
            contentType=content_type,
        )

    return image_fieldnames_added
Exemplo n.º 29
0
    def create_object(self, folder, type_, info):
        filename = info['name']
        name = filename.decode("utf8")
        chooser = INameChooser(folder)
        chooser_name = name.lower().replace('aq_', '')
        newid = chooser.chooseName(chooser_name, folder.aq_parent)
        # strip metadata from file
        if (type_ in ('Image', 'File', 'Video', 'Audio') and
                exiftool is not None and 'tmp_file' in info):
            is_pdf = ('application/pdf' in guess_type(info['tmp_file']))
            if is_pdf and gs_pdf is not None:
                try:
                    gs_pdf(info['tmp_file'])
                except Exception:
                    logger.warn('Could not strip additional metadata with gs {}'.format(info['tmp_file']))  # noqa

            try:
                exiftool(info['tmp_file'])
            except Exception:
                logger.warn('Could not strip metadata from file: %s' % info['tmp_file'])

            if is_pdf and qpdf is not None:
                try:
                    qpdf(info['tmp_file'])
                except Exception:
                    logger.warn('Could not strip additional metadata with qpdf {}'.format(info['tmp_file']))  # noqa

        fi = open(info['tmp_file'], 'r')
        try:
            # Try to determine which kind of NamedBlob we need
            # This will suffice for standard p.a.contenttypes File/Image
            # and any other custom type that would have 'File' or 'Image' in
            # its type name
            filename = ploneutils.safe_unicode(filename)
            create_opts = dict(
                type=type_,
                id=newid,
                container=folder)
            if 'Image' in type_:
                image = NamedBlobImage(data=fi, filename=filename)
                try:
                    image.focal_point = [
                        float(self.request.form.get('focalX')),
                        float(self.request.form.get('focalY'))
                    ]
                except Exception:
                    pass
                create_opts['image'] = image
            else:
                create_opts['file'] = NamedBlobFile(data=fi, filename=filename)

            for field in get_upload_fields():
                if not field.get('name'):
                    continue
                name = field['name']
                if not self.request.form.get(name):
                    continue
                if name in ('tags', 'subject'):
                    # tags needs to be converted
                    create_opts['subject'] = self.request.form.get(name).split(';')
                else:
                    create_opts[name] = self.request.form.get(name, '')
            return api.content.create(**create_opts)
        finally:
            fi.close()
Exemplo n.º 30
0
def create_im_mails(tc,
                    start=1,
                    end=100,
                    senders=[],
                    transitions=[],
                    by_days=200):
    """Create a number of im"""
    print('Creating {} incoming mails'.format(end - start + 1))
    import imio.dms.mail as imiodmsmail
    filespath = "%s/batchimport/toprocess/incoming-mail" % imiodmsmail.__path__[
        0]
    files = [
        unicode(name) for name in os.listdir(filespath)
        if os.path.splitext(name)[1][1:] in ('pdf', 'doc', 'jpg')
    ]
    files_cycle = cycle(files)

    intids = getUtility(IIntIds)
    isenders = [intids.getId(ct) for ct in senders]
    senders_cycle = cycle(isenders)

    services = get_registry_organizations()
    selected_orgs = [
        org for i, org in enumerate(services) if i in (0, 1, 2, 4, 5, 6)
    ]
    orgas_cycle = cycle(selected_orgs)

    ifld = tc.layer['portal']['incoming-mail']
    setattr(ifld, 'folder_period', u'day')
    with api.env.adopt_user(username='******'):
        days = 0
        for i in range(start, end + 1):
            if i % by_days == 0:
                days += 1
            mid = 'im{}'.format(i)
            if mid not in ifld:
                scan_date = datetime.datetime.now() - datetime.timedelta(
                    days=days)
                params = {
                    'title': 'Courrier %d' % i,
                    'mail_type': 'courrier',
                    'internal_reference_no': 'E{:04d}'.format(i),
                    'reception_date': scan_date,
                    # 'sender': [RelationValue(senders_cycle.next())],
                    'treating_groups': orgas_cycle.next(),
                    'recipient_groups':
                    [services[3]],  # Direction générale, communication
                    'description':
                    'Ceci est la description du courrier %d' % i,
                }
                t_st = datetime.datetime.now()
                mail = sub_create(ifld, 'dmsincomingmail', scan_date, mid,
                                  **params)
                if i == start or i % 1000 == 0:
                    print("Creation time at {}: '{}'".format(
                        i,
                        datetime.datetime.now() - t_st))
                filename = files_cycle.next()
                with open("%s/%s" % (filespath, filename), 'rb') as fo:
                    file_object = NamedBlobFile(fo.read(), filename=filename)
                    createContentInContainer(mail,
                                             'dmsmainfile',
                                             title='',
                                             file=file_object,
                                             scan_id='0509999{:08d}'.format(i),
                                             scan_date=scan_date)
                do_transitions(mail, transitions)