def manage_upload(self, file=None, REQUEST=None) : """ Zope calls this when the content of the enclosed file changes. The 'cells' attribute is updated, but already defined cells are not erased, they are saved in the 'all_cells' attribute so if the pdf file is reverted, you do not loose the cells definitions. """ if not file or not hasattr(file, "read") : raise ValueError ("The pdf form file should not be empty") file.seek(0) # file is always valid here values = PDFTk().dumpDataFields(file) self.cells = {} for v in values : if v["FieldType"] not in ("Button", "Choice")\ or not int(v["FieldFlags"]) & 65536: k = v["FieldName"] if not self.all_cells.has_key(k) : self.cells[k] = "" else : self.cells[k] = self.all_cells[k] self.all_cells.update(self.cells) file.seek(0) File.manage_upload(self, file, REQUEST) if REQUEST: message = "Saved changes." return self.manage_main(self, REQUEST, manage_tabs_message=message)
def __setstate__(self, dict): mimetype = dict.get('mimetype', None) if IMimetype.isImplementedBy(mimetype): dict['mimetype'] = str(mimetype) dict['binary'] = not not mimetype.binary assert(dict.has_key('mimetype'), 'no mimetype in setstate dict') File.__setstate__(self, dict)
def __init__ (self, id, title='', pdf_file=''): # holds all the cell informations, even those not related to this form self.all_cells = PersistentMapping() # holds the cells related to this pdf form self.cells = PersistentMapping() # File constructor will set the file content File.__init__(self, id, title, pdf_file)
def __init__(self, id): """ Initialise a new instance of XWFFile. """ self.id = id self.initProperties() File.__init__(self, id, id, '', '', '') self.update_data('', '', 0)
def manage_beforeDelete( self, item, container ): """ For cleaning up as we are removed. """ if IXWFFileReader.isImplementedBy( self.storage_plugin ): self.storage_plugin.set_physicalPath( None ) XWFCatalogAware.manage_beforeDelete( self, item, container ) File.manage_beforeDelete( self, item, container )
def manage_afterClone( self, item ): """ For configuring the object post copy. """ File.manage_afterClone( self, item ) if IXWFFileReader.isImplementedBy( self.storage_plugin ): self.storage_plugin.set_physicalPath( self.getPhysicalPath() ) XWFCatalogAware.manage_afterClone( self, item )
def manage_afterAdd( self, item, container ): """ For cleaning up as we are removed. """ File.manage_afterAdd( self, item, container ) if IXWFFileReader.isImplementedBy( self.storage_plugin ): self.storage_plugin.set_physicalPath( self.getPhysicalPath() ) XWFCatalogAware.manage_afterAdd( self, item, container )
def testBlobbableOFSFile(self): obj = File('foo', 'Foo', getFile('plone.pdf'), 'application/pdf') obj.filename = 'foo.pdf' blobbable = IBlobbable(obj) target = Blob() blobbable.feed(target) self.assertEqual(target.open('r').read(), getFile('plone.pdf').read()) self.assertEqual(blobbable.filename(), 'foo.pdf') self.assertEqual(blobbable.mimetype(), 'application/pdf')
def _copyFile(f_from, f_to, id, keep_new=False): obj_id = id if hasattr(f_to,id): if keep_new: obj_id = 'old_' + id else: _renameObj (f_to, id, 'new_' + id) oldobj = f_from._getOb(id) newobj = File(obj_id,oldobj.title,'',oldobj.content_type) newobj.size = oldobj.size newobj.data = oldobj.data[:] f_to._setObject(obj_id, newobj)
def __init__( self, id, storage_plugin ): """ Initialise a new instance of XWFPluggableFile. """ self.storage_plugin = storage_plugin() # we do a quick, fake, init here, and do the actual file # later File.__init__( self, id, id, '' ) security = getSecurityManager() self.manage_addProperty( 'dc_creator', security.getUser().getId(), 'ustring' ) self.manage_addProperty( 'original_filename', '', 'ustring' )
def writeFile(self, path, data): basepath = '/'.join(path.split('/')[:-1]) if basepath: self.makeDirectory(basepath) filename = path.split('/')[-1] if isinstance(filename, unicode): filename = filename.encode('utf-8') f = File(filename, filename, data) if f.getContentType() == 'text/html': # otherwise HTTPResponse.setBody assumes latin1 and mangles things f.content_type = 'text/html; charset=utf-8' container = self.context.unrestrictedTraverse(basepath) if filename in container: container._delOb(filename) container._setOb(filename, f)
def test_OFSFileLastModified_File(self): from OFS.Image import File dummy = File('dummy', 'Dummy', 'data') self.assertEquals(None, ILastModified(dummy)()) timestamp = 987654321.0 # time stamp (in UTC) ts = TimeStamp(*time.gmtime(timestamp)[:6]) # corresponding TimeStamp # equivalent in local time, which is what the last-modified adapter # should return mod = datetime.datetime.fromtimestamp(timestamp, tzlocal()) dummy._p_jar = FauxDataManager() dummy._p_serial = repr(ts) self.assertEquals(mod, ILastModified(dummy)())
def viewOriginal(self, REQUEST=None, RESPONSE=None, *args, **kwargs) : """ publish original pdf """ pdf = File.index_html(self, REQUEST, RESPONSE, *args, **kwargs) RESPONSE.setHeader('Content-Type', 'application/pdf') RESPONSE.setHeader('Content-Disposition', 'inline;filename="%s.pdf"' % (self.title_or_id())) return pdf
def manage_upload( self, file='', REQUEST=None ): """ This overrided the manage_upload provided by the File class to add a hook for setting the original filename. """ LOG('XWFPluggableFile1.1',INFO,str(dir(file))) File.manage_upload( self, file ) filename = getattr( file, 'filename', '' ) filename = convertTextToAscii( removePathsFromFilenames( filename ) ) self.filename = filename self.manage_changeProperties(original_filename=filename)\ if REQUEST: message="Saved changes." return self.manage_main( self, REQUEST, manage_tabs_message=message )
def index_html( self, REQUEST, RESPONSE ): """ Extends the file index_html to set the download filename. """ filename = self.get_filename() REQUEST.RESPONSE.setHeader( 'Content-Disposition', 'inline; filename="%s"' % filename ) return File.index_html( self, REQUEST, RESPONSE )
def index_html(self, REQUEST, RESPONSE): """ Override for File.index_html """ # this is to deal with an acquisition issue, where # the context gets lost when .data is called bfd = self.get_baseFilesDir() if self._base_files_dir != bfd: self._base_files_dir = bfd return File.index_html(self, REQUEST, RESPONSE)
def writeFile(self, path, data): basepath = '/'.join(path.split('/')[:-1]) if basepath: self.makeDirectory(basepath) filename = path.split('/')[-1] if isinstance(filename, unicode): filename = filename.encode('utf-8') f = File(filename, filename, data) ct = f.getContentType() if ct.startswith('text/') or ct == 'application/javascript': # otherwise HTTPResponse.setBody assumes latin1 and mangles things f.content_type = ct + '; charset=utf-8' container = self.context.unrestrictedTraverse(basepath) if filename in container: container._delOb(filename) event = PloneResourceModifiedEvent else: event = PloneResourceCreatedEvent container._setOb(filename, f) obj = container._getOb(filename) notify(event(obj))
def loadFile(name,dir='skins/zwiki'): """ Load and return a File from the filesystem, or None. """ filepath = os.path.join(abszwikipath(dir),name) if os.path.exists(filepath): f = None try: try: f = open(filepath,'rb') data = f.read() mtime = os.path.getmtime(filepath) file = File(name,'',data) # bug workaround: bobobase_modification_time will otherwise be current time file.bobobase_modification_time = lambda:mtime return file except IOError: return None finally: if f: f.close() else: return None
def test_uploadFile(self): file = File('foo', 'Foo', getFile('plone.pdf'), 'application/pdf') file.filename = 'foo.pdf' myUpload = makeFileUpload(file, 'test.gif') myUpload.method = 'GET' view = VNCCollabUtilView(self.portal, myUpload) self.assertRaises(Exception, lambda: view.uploadFile(myUpload)) myUpload = makeFileUpload(file, 'test.gif') myUpload.method = 'POST' myUpload.form = {} headers, output, request = makeResponse(myUpload) view = VNCCollabUtilView(self.portal, request) result = view.uploadFile(file) self.assertEqual(result, 'http://nohost/plone/foo.pdf/edit') myUpload = makeFileUpload(file, 'test.gif') myUpload.method = 'POST' myUpload.form = {'ajax_call': True} headers, output, request = makeResponse(myUpload) view = VNCCollabUtilView(self.portal, request) result = view.uploadFile(file) self.assertEqual(result, 'http://nohost/plone/foo.pdf-1/edit')
def __init__(self, id, title, properties={}): """ZAnnotation constructor""" # Initialize properties if properties: self._doUpdate(properties) else: self.type = None self.annotates = None self.context = None self.language = None self.creator = None self.created = None self.date = None self.body = None self.root = None self.inreplyto = None self.encoding = '' return File.__init__(self, id, title,'','text/html')
def _createZODBClone(self): return File(self.getId(), '', self._readFile(1))
def __setstate__(self, dict): mimetype = dict.get('mimetype', None) if IMimetype.providedBy(mimetype): dict['mimetype'] = str(mimetype) dict['binary'] = not not mimetype.binary File.__setstate__(self, dict)
def __setstate__(self, dict): mimetype = dict.get("mimetype", None) if IMimetype.providedBy(mimetype): dict["mimetype"] = str(mimetype) dict["binary"] = not not mimetype.binary File.__setstate__(self, dict)
def __init__(self, *args, **kwargs): self._parameters = {} return File.__init__(self, *args, **kwargs)
import appy.pod try: renderer = appy.pod.renderer.Renderer(**rendererParams) renderer.run() except appy.pod.PodError, pe: if not os.path.exists(tempFileName): # In some (most?) cases, when OO returns an error, the result is # nevertheless generated. raise PloneMeetingError(POD_ERROR % str(pe)) # Open the temp file on the filesystem f = file(tempFileName, 'rb') if forBrowser: # Create a OFS.Image.File object that will manage correclty HTTP # headers, etc. from OFS.Image import File theFile = File('dummyId', 'dummyTitle', f, content_type=mimeTypes[self.getPodFormat()]) res = theFile.index_html(self.REQUEST, self.REQUEST.RESPONSE) # Before, I used the code below to set the HTTP headers. # But I've noticed that with some browsers (guess which one?) the # returned document could not be opened. Worse: the browser crashed # completely in some cases. So now I rely on File.index_html # instead. One caveat: the title of the file is "generateDocument-X' # and not a friendly title as before... #response = obj.REQUEST.RESPONSE #response.setHeader('Content-type', mimeTypes[self.getPodFormat()]) #response.setHeader('Content-disposition', # 'inline;filename="%s.%s"' % ( # obj.Title(), self.getPodFormat())) else: # I must return the raw document content. res = f.read()
def postResults(self, REQUEST): """ Record the results of a test run. o Create a folder with properties representing the summary results, and files containing the suite and the individual test runs. o REQUEST will have the following form fields: result -- one of "failed" or "passed" totalTime -- time in floating point seconds for the run numTestPasses -- count of test runs which passed numTestFailures -- count of test runs which failed numCommandPasses -- count of commands which passed numCommandFailures -- count of commands which failed numCommandErrors -- count of commands raising non-assert errors suite -- Colorized HTML of the suite table testTable.<n> -- Colorized HTML of each test run """ completed = DateTime() result_id = 'result_%s' % completed.strftime( '%Y%m%d_%H%M%S' ) self._setObject( result_id, ZuiteResults( result_id ) ) result = self._getOb( result_id ) rfg = REQUEST.form.get reg = REQUEST.environ.get result._updateProperty( 'completed' , completed ) result._updateProperty( 'passed' , rfg( 'result' ).lower() == 'passed' ) result._updateProperty( 'time_secs' , float( rfg( 'totalTime', 0 ) ) ) result._updateProperty( 'tests_passed' , int( rfg( 'numTestPasses', 0 ) ) ) result._updateProperty( 'tests_failed' , int( rfg( 'numTestFailures', 0 ) ) ) result._updateProperty( 'commands_passed' , int( rfg( 'numCommandPasses', 0 ) ) ) result._updateProperty( 'commands_failed' , int( rfg( 'numCommandFailures', 0 ) ) ) result._updateProperty( 'commands_with_errors' , int( rfg( 'numCommandErrors', 0 ) ) ) result._updateProperty( 'user_agent' , reg( 'HTTP_USER_AGENT', 'unknown' ) ) result._updateProperty( 'remote_addr' , reg( 'REMOTE_ADDR', 'unknown' ) ) result._updateProperty( 'http_host' , reg( 'HTTP_HOST', 'unknown' ) ) result._updateProperty( 'server_software' , reg( 'SERVER_SOFTWARE', 'unknown' ) ) result._updateProperty( 'product_info' , self._listProductInfo() ) result._setObject( 'suite.html' , File( 'suite.html' , 'Test Suite' , unquote( rfg( 'suite' ) ) , 'text/html' ) ) test_ids = [ x for x in REQUEST.form.keys() if x.startswith( 'testTable' ) ] test_ids.sort() for test_id in test_ids: body = unquote( rfg( test_id ) ) result._setObject( test_id , File( test_id , 'Test case: %s' % test_id , body , 'text/html' ) ) testcase = result._getOb( test_id ) # XXX: this is silly, but we have no other metadata. testcase._setProperty( 'passed' , _PINK_BACKGROUND.search( body ) is None , 'boolean' )
try: proxy.invokeFactory('Section', 'test-folder') except xmlrpclib.ProtocolError: print sys.exc_info()[1] except xmlrpclib.Fault: print "The id is invalid - it is already in use." # most likely proxy = xmlrpclib.ServerProxy(url + '/test-folder') proxy.setTitle('Test folder') proxy.setDescription('This is a test folder') # create image proxy = xmlrpclib.ServerProxy(url) # reset #wrappedData = xmlrpclib.Binary(open('screenshot.png').read()) filename = 'screenshot.png' data = open(filename).read() fileData = File(filename, filename, data, 'application/octet-stream') fileData.filename = filename wrappedData = fileData try: proxy.invokeFactory('Image', 'screenshot.png') except xmlrpclib.ProtocolError: print sys.exc_info()[1] except xmlrpclib.Fault: print "The id is invalid - it is already in use." # most likely proxy = xmlrpclib.ServerProxy(url + '/screenshot.png') proxy.setTitle('This is an image') try: proxy.setImage(wrappedData) # XXX this fails except: print sys.exc_info()[1]
def load_file(filename): id = os.path.basename(filename) ob = File(id, '', '') ob.update_data(file(filename, 'rb').read()) return ob
def PUT(self, REQUEST, RESPONSE): """from Products.CMFDefault.File""" OFS_File.PUT(self, REQUEST, RESPONSE) self.reindexObject()
def _discovering_dist_ids(project): # for each file in the project we # extract the distutils name project_path = '/'.join(project.getPhysicalPath()) files = cat(**{ 'portal_type': ['PSCFile', 'PSCFileLink'], 'path': project_path }) ids = [] for file_ in files: portal_type = file_.portal_type if portal_type == 'PSCFileLink': # the file is somewhere else, let's scan it file_ = file_.getObject() file_ = DistantFile(file_.getExternalURL()) else: file_ = file_.getObject() # trying to get back from old # storage # ExternalStorage here # if WAS_EXTERNAL_STORAGE and portal_type != 'PSCFileLink': from Products.ExternalStorage.ExternalStorage import\ ExternalStorage storage = ExternalStorage( prefix=EXTERNAL_STORAGE_PATHS[0], archive=False, rename=False, ) # transferring old data to new AT container fs = file_.schema['downloadableFile'] old = fs.storage fs.storage = storage portal_url = getToolByName(file_, 'portal_url') real_file = os.path.join( *portal_url.getRelativeContentPath(file_)) for path in EXTERNAL_STORAGE_PATHS: final_file = os.path.join(path, real_file) if os.path.exists(final_file): break if not os.path.exists(final_file): logging.info( '******** could not get %s on the file system !!' % real_file) continue #raise ValueError('File not found %s' % final_file) fs.storage = old dfile = file_.getDownloadableFile() filename = dfile.filename data = open(final_file).read() if data == '': logging.info('empty file ! %s' % final_file) #f = File(filename, filename, open(final_file)) elif portal_type != 'PSCFileLink': storage = AttributeStorage() fs = file_.schema['downloadableFile'] old = fs.storage fs.storage = storage dfile = file_.getDownloadableFile() data = dfile.get_data() filename = dfile.filename fs.storage = old #file_.getDownloadableFile().data = data #f = File(filename, filename, StringIO(data)) if portal_type != 'PSCFileLink': #file_.setDownloadableFile(f) file_.schema = PSCFileSchema if filename == '' and data == '': logging.info('file empty for %s' % file_) else: if filename is None: filename = file_.getId() file_.setDownloadableFile( File(filename, filename, StringIO(data))) id_ = extract_distutils_id(file_) if id_ is not None and id_ not in ids: ids.append(id_) return ids
def __iter__(self): for item in self.previous: yield item metadata = {} if self.metadata: metadataFile = open(self.metadata, 'rb') reader = csv.DictReader(metadataFile, delimiter=self.delimiter) if reader.fieldnames is None: raise ValueError("Metadata CSV is empty.") if len(reader.fieldnames) is 1 and \ self.delimiter not in reader.fieldnames[0]: msg = "Metadata CSV does not use the specified delimiter: %s" raise ValueError(msg % self.delimiter) if 'path' not in reader.fieldnames: msg = "Metadata CSV file does not have a 'path' column." raise ValueError(msg) if self.strict: field_count = len(reader.fieldnames) for row in reader: if self.strict and field_count != len(row): msg = "Found a row in Metadata CSV that has a different \ count of fields compared to first row: %s" raise ValueError(msg % row) path = row['path'] data = row.copy() del data['path'] metadata[path] = data if self.requireMetadata and not metadata: m = "Metadata is required, but metadata file %s not given or empty" raise ValueError(m % self.metadata) if not os.path.exists(self.directory): raise ValueError("Directory %s does not exist" % self.directory) for dirpath, dirnames, filenames in os.walk(self.directory): dirnames.sort() filenames.sort() wrapData = self.wrapData # Create folders first, if necessary for dirname in dirnames: dirPath = os.path.join(dirpath, dirname) zodbPath = self.getZODBPath(dirPath) if self.ignored(zodbPath)[1]: continue _type = self.folderType item = {'_type': self.folderType, '_path': zodbPath} if zodbPath in metadata: item.update(metadata[zodbPath]) yield item # Then import files for filename in filenames: filePath = os.path.join(dirpath, filename) zodbPath = self.getZODBPath(filePath) if self.ignored(zodbPath)[1]: continue if self.metadata and \ os.path.abspath(filePath) == os.path.abspath(self.metadata): continue if self.requireMetadata and zodbPath not in metadata: continue if zodbPath in metadata and \ 'portal_type' in metadata[zodbPath] and \ metadata[zodbPath]['portal_type'] in ['News Item', 'Document', 'Event']: # if portal_type is given in metadata.csv, use it! _type = metadata[zodbPath]['portal_type'] mimeType = 'text/html' fieldname = 'text' wrapData = False # if the file is an image: use the image field of the # news item, otherwise use the text field if _type == 'News Item': basename, extension = os.path.splitext(filename) if extension and \ extension.lower() in mimetypes.types_map and \ mimetypes.types_map[extension.lower()].startswith('image'): mimeType = mimetypes.types_map[extension.lower()] fieldname = self.imageField # the same of news wrapData = self.wrapData else: # else make it File or Image _type = self.fileType fieldname = self.fileField mimeType = self.defaultMimeType # Try to guess mime type and content type basename, extension = os.path.splitext(filename) if extension and extension.lower() in mimetypes.types_map: mimeType = mimetypes.types_map[extension.lower()] if mimeType.startswith('image'): _type = self.imageType fieldname = self.imageField # read in main content of this item infile = open(filePath, 'rb') if wrapData: fileData = File(filename, filename, infile, mimeType) fileData.filename = filename else: fileData = infile.read() infile.close() item = { '_type': _type, '_path': zodbPath, '_mimetype': mimeType, fieldname: fileData } if zodbPath in metadata: item.update(metadata[zodbPath]) yield item
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())
def _read_data(self, file): if IXWFFileReader.isImplementedBy(self.storage_plugin): return self.storage_plugin.reader(file) return File._read_data(self, file)
def sync_contacts(context, ldap_records, set_owner=False): """Synchronize the given ldap results """ # Statistics created = 0 modified = 0 skipped = 0 failed = 0 deleted = 0 dn_contact_id_mapping = {} ct = getToolByName(context, 'portal_catalog') mapper = get_ldap_attribute_mapper() dummy_contact = createContent('ftw.contacts.Contact') dummy_contact_folder = createContent('ftw.contacts.ContactFolder') # 1st pass: create or update profiles for dn, entry in ldap_records: if not dn: continue dn = dn.decode('unicode_escape').encode('iso8859-1').decode('utf-8') # Only entries with a contact id contact_id = entry.get(mapper.id(), [None, ])[0] if not contact_id: skipped += 1 logger.debug("Skipping entry '%s'. No contact id." % dn) continue # Get the normalzied name for the contact with the given id. This has # to be done on a dummy folder because existing content would influence # the resulting id. contact_id = INameChooser(dummy_contact_folder).chooseName( contact_id, dummy_contact) dn_contact_id_mapping[dn] = contact_id contact = context.unrestrictedTraverse(contact_id, None) changed = False is_new_object = False # Check if we really got the wanted object. if not IContact.providedBy(contact): contact = None # Create contact if contact is None: try: contact = createContent('ftw.contacts.Contact') contact.id = contact_id addContentToContainer(context, contact) is_new_object = True # invalid id except BadRequest: failed += 1 logger.warn("Could not create contact '%s' (invalid id)." % contact_id) continue # Update/set field values IContactSchema(contact).ldap_dn = dn field_mapping = dict(getFields(IContactSchema)) for ldap_name, field_name in mapper.mapping().items(): field = field_mapping.get(field_name, None) if field is None: raise NotImplementedError() value = entry.get(ldap_name, [''])[0] current_value = field.get(contact) if IBlobWrapper.providedBy(current_value): current_value = current_value.data if current_value != value: # Handle images if INamedImageField.providedBy(field) and value: infile = StringIO(value) filename = '%s.jpg' % contact_id value = File(filename, filename, infile, 'image/jpeg') value.filename = filename field.set(contact, value) changed = True # Update/set fields with custom updaters custom_updaters = getAdapters((contact, entry), provided=ILDAPCustomUpdater) for name, updater in custom_updaters: changed = updater.update() if is_new_object: if set_owner: # Grant owner role to contact contact.__ac_local_roles__ = None contact.manage_setLocalRoles(contact_id, ['Owner']) contact.reindexObjectSecurity() notify(ObjectCreatedEvent(contact)) aq_contact = context.get(contact_id) ct.catalog_object(aq_contact, '/'.join(aq_contact.getPhysicalPath())) created += 1 logger.debug("Created new contact '%s (%s)'." % (contact_id, dn)) elif changed: contact.reindexObject() notify(ObjectModifiedEvent(contact)) modified += 1 logger.debug("Modified contact '%s' (%s)." % (contact_id, dn)) total = len(ldap_records) unchanged = total - skipped - modified - created - failed # 2nd pass: set references # TODO # 3rd pass: delete contacts which have an ldap_id but are not in LDAP. all_contacts = ct.unrestrictedSearchResults( portal_type='ftw.contacts.Contact', path=dict(query='/'.join(context.getPhysicalPath()), depth=1)) to_be_deleted = {} for contact in all_contacts: obj = contact.getObject() ldap_dn = IContactSchema(obj).ldap_dn if ldap_dn and ldap_dn not in dn_contact_id_mapping: parent_path = '/'.join(obj.getPhysicalPath()[:-1]) id_ = obj.getPhysicalPath()[-1] if parent_path not in to_be_deleted: to_be_deleted[parent_path] = [] to_be_deleted[parent_path].append(id_) logger.debug("Deleting contact '%s'" % id_) # Disable link integrity check while deleting contacts ptool = getToolByName(context, 'portal_properties') props = getattr(ptool, 'site_properties') old_check = props.getProperty('enable_link_integrity_checks', False) props.enable_link_integrity_checks = False for parent_path, ids in to_be_deleted.items(): parent = context.unrestrictedTraverse(parent_path) deleted += len(ids) parent.manage_delObjects(ids) # Reenable previous link integrity setting props.enable_link_integrity_checks = old_check return dict( created=created, modified=modified, unchanged=unchanged, total=total, skipped=skipped, failed=failed, deleted=deleted, )
def __iter__(self): for item in self.previous: yield item metadata = {} if self.metadata: metadataFile = open(self.metadata, 'rb') reader = csv.DictReader(metadataFile, delimiter=self.delimiter) if reader.fieldnames is None: raise ValueError("Metadata CSV is empty.") if len(reader.fieldnames) is 1 and self.delimiter not in reader.fieldnames[0]: raise ValueError("Metadata CSV does not use the specified delimiter: %s" %(self.delimiter)) if 'path' not in reader.fieldnames: raise ValueError("Metadata CSV file does not have a 'path' column.") if self.strict: field_count = len(reader.fieldnames) for row in reader: if self.strict and field_count != len(row): raise ValueError("Found a row in Metadata CSV that has a different count of fields \ compared to first row: %s" %(row)) path = row['path'] data = row.copy() del data['path'] metadata[path] = data if self.requireMetadata and not metadata: raise ValueError("Metadata is required, but metadata file %s not given or empty" % self.metadata) if not os.path.exists(self.directory): raise ValueError("Directory %s does not exist" % self.directory) for dirpath, dirnames, filenames in os.walk(self.directory): wrapData = self.wrapData # Create folders first, if necessary for dirname in dirnames: dirPath = os.path.join(dirpath, dirname) zodbPath = self.getZODBPath(dirPath) if self.ignored(zodbPath)[1]: continue _type = self.folderType item = {'_type': self.folderType, '_path': zodbPath} if zodbPath in metadata: item.update(metadata[zodbPath]) yield item # Then import files for filename in filenames: filePath = os.path.join(dirpath, filename) zodbPath = self.getZODBPath(filePath) if self.ignored(zodbPath)[1]: continue if self.metadata and os.path.abspath(filePath) == os.path.abspath(self.metadata): continue if self.requireMetadata and zodbPath not in metadata: continue if zodbPath in metadata and 'portal_type' in metadata[zodbPath] and \ metadata[zodbPath]['portal_type'] in ['News Item', 'Document', 'Event']: # if portal_type is given in metadata.csv, use it! _type = metadata[zodbPath]['portal_type'] mimeType = 'text/html' fieldname = 'text' wrapData = False #if the file is an image: use the image field of the news item, otherwise use the text field if _type == 'News Item': basename, extension = os.path.splitext(filename) if extension and extension.lower() in mimetypes.types_map and mimetypes.types_map[extension.lower()].startswith('image'): mimeType = mimetypes.types_map[extension.lower()] fieldname = self.imageField # the same of news wrapData = self.wrapData else: # else make it File or Image _type = self.fileType fieldname = self.fileField mimeType = self.defaultMimeType # Try to guess mime type and content type basename, extension = os.path.splitext(filename) if extension and extension.lower() in mimetypes.types_map: mimeType = mimetypes.types_map[extension.lower()] if mimeType.startswith('image'): _type = self.imageType fieldname = self.imageField # read in main content of this item infile = open(filePath, 'rb') if wrapData: fileData = File(filename, filename, infile, mimeType) fileData.filename = filename else: fileData = infile.read() infile.close() item = {'_type': _type, '_path': zodbPath, '_mimetype': mimeType, fieldname: fileData} if zodbPath in metadata: item.update(metadata[zodbPath]) yield item
def _read_data( self, file ): if IXWFFileReader.isImplementedBy( self.storage_plugin ): return self.storage_plugin.reader( file ) return File._read_data( self, file )
def create_content(self, data, context): registry = getUtility(IRegistry) settings = registry.forInterface(IConfiguration, check=False) # check api key if not data["api_key"]['value'] == settings.api_key: return # get mappings self.type_mapping = self.create_mapping(settings.type_mapping) self.field_mapping = self.create_mapping(settings.field_mapping) self.view_mapping = self.create_mapping(settings.view_mapping) # Map Portal Types data["portal_type"]["value"] = self.type_mapping.get( data["portal_type"]["value"], data["portal_type"]["value"]) # Check if Type is available portal_types = getToolByName(context, "portal_types") types = portal_types.listContentTypes() if data["portal_type"]["value"] not in types: return # TODO Topic to Collection / FormGen / PloneGlossary # ContentProxy if data["portal_type"]["value"] in ["FormFolder", "ContentProxy"]: return # # ToDo PloneBoard # if "Ploneboard" in data["portal_type"]["value"]: # return # ToDo check and Fix Collection if "Criterion" in data["portal_type"]["value"]: return if "Criteria" in data["portal_type"]["value"]: return # if "parent_path" in data.keys(): if "neue-inhalte" in data["parent_path"]["value"]: return if "veranstaltungen-elsass" in data["parent_path"]["value"]: return # TODO FI ALIAS Contenttype # TODO FI GLOSSARY Contenttype # TODO FI FormGen Contenttype if data["portal_type"]["value"] in ["Alias"]: return # # # TODO Image Sizes portal = getToolByName(context, 'portal_url').getPortalObject() try: if "parent_path" not in data.keys(): parent = portal else: parent = portal.restrictedTraverse( str(data["parent_path"]["value"])) except: print "################################" print "ERROR GETTING PARENT: %s" % (str( data["parent_path"]["value"])) return if data["portal_type"]["value"] == "Topic": print "HOTTOPIC" if data["portal_type"]["value"] == "PloneboardComment": creator = data["creators"]["value"][0] obj = parent.addComment(data["title"]["value"], data["text"]["value"], creator=creator) obj.setCreationDate(DateTime(data["creation_date"]["value"])) if "attachments" not in data.keys(): return for at in data["attachments"]["value"]: at_file = File(str(at["id"]), safe_unicode(at["filename"]), base64.b64decode(str(at["data"])), str(at["contentType"])) obj.addAttachment(at_file) return else: if data['id']["value"] not in parent.objectIds(): parent.invokeFactory( type_name=data["portal_type"]["value"], id=data['id']["value"], ) obj = parent[data['id']['value']] if obj.portal_type == "Event": self.set_event_fields(obj, data) # elif obj.portal_type == "News Item": # import pdb; pdb.set_trace() else: self.set_fields(obj, data) # self.set_workflow_history(obj, data) self.set_local_roles(obj, data) self.set_exclude_from_nav(obj, data) self.set_default_view(obj, data) self.set_portlets(obj, data) self.set_portlet_blacklists(obj, data) self.set_creator_and_owner(obj, data) # obj.reindexObjectSecurity()