def __call__(self, name, content_type, data, obj_id): ctr = cmfutils.getToolByName(self.context, 'content_type_registry') type_ = ctr.findTypeName(name.lower(), '', '') or 'File' # otherwise I get ZPublisher.Conflict ConflictErrors # when uploading multiple files upload_lock.acquire() try: transaction.begin() obj = ploneutils._createObjectByType(type_, self.context, obj_id) ttool = getToolByName(self.context, 'portal_types') ctype = ttool[obj.portal_type] schema = ctype.lookupSchema() fields = getFieldsInOrder(schema) file_fields = [field for safe_name, field in fields if INamedFileField.providedBy(field) or INamedImageField.providedBy(field)] if len(file_fields) == 0: logger.info("An error happens : the dexterity content type %s " "has no file field, rawdata can't be created", obj.absolute_url()) for file_field in file_fields: if IPrimaryField.providedBy(file_field): break else: # Primary field can't be set ttw, # then, we take the first one file_field = file_fields[0] # TODO: use adapters if HAVE_BLOBS and INamedBlobImageField.providedBy(file_field): value = NamedBlobImage(data=data.read(), contentType=content_type, filename=unicode(obj_id, 'utf-8')) elif HAVE_BLOBS and INamedBlobFileField.providedBy(file_field): value = NamedBlobFile(data=data.read(), contentType=content_type, filename=unicode(obj_id, 'utf-8')) elif INamedImageField.providedBy(file_field): value = NamedImage(data=data.read(), contentType=content_type, filename=unicode(obj_id, 'utf-8')) elif INamedFileField.providedBy(file_field): value = NamedFile(data=data.read(), contentType=content_type, filename=unicode(obj_id, 'utf-8')) file_field.set(obj, value) obj.title = name obj.reindexObject() notify(ObjectInitializedEvent(obj)) notify(ObjectModifiedEvent(obj)) transaction.commit() finally: upload_lock.release() return obj
def set(self, data, filename, content_type): error = '' obj = self.context ttool = getToolByName(obj, 'portal_types') ctype = ttool[obj.portal_type] schema = ctype.lookupSchema() fields = getFieldsInOrder(schema) file_fields = [ field for name, field in fields if INamedFileField.providedBy(field) or INamedImageField.providedBy(field) ] if len(file_fields) == 0: error = u'serverError' logger.info( "An error happens : the dexterity content type %s " "has no file field, rawdata can't be created", obj.absolute_url()) for file_field in file_fields: if IPrimaryField.providedBy(file_field): break else: # Primary field can't be set ttw, # then, we take the first one file_field = file_fields[0] # TODO: use adapters if HAVE_BLOBS and INamedBlobImageField.providedBy(field): value = NamedBlobImage( data=data, contentType=content_type, filename=unicode(filename)) elif HAVE_BLOBS and INamedBlobFileField.providedBy(file_field): value = NamedBlobFile( data=data, contentType=content_type, filename=unicode(filename)) elif INamedImageField.providedBy(file_field): value = NamedImage( data=data, contentType=content_type, filename=unicode(file_field)) elif INamedFileField.providedBy(file_field): value = NamedFile( data=data, contentType=content_type, filename=unicode(filename)) file_field.set(obj, value) obj.reindexObject() notify(ObjectInitializedEvent(obj)) return error
def get_configured_fields(self): context = self.context tileType = queryUtility(ITileType, name=self.__name__) conf = self.get_tile_configuration() fields = getFieldsInOrder(tileType.schema) uuid = self.data.get('uuid', '') results = [] for name, field in fields: image_field = INamedImageField.providedBy(field) data = self.data[name] if not ((image_field and (data or uuid)) or (not image_field and data)): # If there's no data for this field, ignore it # special condition, if the field is an image field and # there is no uuid, then ignore it too continue if isinstance(data, RichTextValue): transformer = ITransformer(context, None) if transformer is not None: content = transformer(data, 'text/x-html-safe') else: content = data field = {'id': name, 'content': content, 'title': field.title} if not self._include_updated_field(field, conf.get(name)): continue results.append(field) return results
def __call__(self, context): context = getattr(context, 'context', context) portal = getToolByName(context, 'portal_url').getPortalObject() flt = [_infoDictForType(portal, tipe) for tipe in _listTypesForInterface(portal, IFileContent)] ilt = [_infoDictForType(portal, tipe) for tipe in _listTypesForInterface(portal, IImageContent)] items = [SimpleTerm('auto', 'auto', context.translate('label_default_portaltype_configuration', default=u'Default configuration (Content Type Registry).', domain='collective.quickupload')),] all_portal_types = [] for t in flt+ilt: portal_type = t['portal_type'] if portal_type not in all_portal_types: items.append(SimpleTerm(portal_type, portal_type, t['type_ui_info'])) all_portal_types.append(portal_type) for fti in portal.portal_types.objectValues(): if HAS_DEXTERITY and IDexterityFTI.providedBy(fti): fields = getFieldsInOrder(fti.lookupSchema()) for fieldname, field in fields: if INamedFileField.providedBy(field) or INamedImageField.providedBy(field): items.append(SimpleTerm(fti.getId(), fti.getId(), fti.Title())) break return SimpleVocabulary(items)
def _make_namedfile(value, field, widget): """Return a NamedImage or NamedFile instance, if it isn't already one - e.g. when it's base64 encoded data. """ if INamed.providedBy(value): return value string_types = (six.binary_type, six.text_type) if isinstance(value, string_types) and IBytes.providedBy(field): filename, data = b64decode_file(value) elif isinstance(value, dict) or isinstance(value, PersistentDict): filename = value['filename'] data = value['data'] if INamedBlobImageField.providedBy(field): value = NamedBlobImage(data=data, filename=filename) elif INamedImageField.providedBy(field): value = NamedImage(data=data, filename=filename) elif INamedBlobFileField.providedBy(field): value = NamedBlobFile(data=data, filename=filename) else: value = NamedFile(data=data, filename=filename) return value
def __call__(self, context): context = getattr(context, 'context', context) portal = getToolByName(context, 'portal_url').getPortalObject() items = [SimpleTerm('auto', 'auto', context.translate(_('label_default_portaltype_configuration', default=u'Default configuration (Content Type Registry).')))] archetype_tool = getToolByName(context, 'archetype_tool', None) if archetype_tool: flt = [_infoDictForType(portal, tipe) for tipe in _listTypesForInterface(portal, IFileContent)] ilt = [_infoDictForType(portal, tipe) for tipe in _listTypesForInterface(portal, IImageContent)] items.extend([SimpleTerm(t['portal_type'], t['portal_type'], t['type_ui_info']) for t in flt]) file_types = [t['portal_type'] for t in flt] items.extend([SimpleTerm(t['portal_type'], t['portal_type'], t['type_ui_info']) for t in ilt if t['portal_type'] not in file_types]) for fti in portal.portal_types.objectValues(): if HAS_DEXTERITY and IDexterityFTI.providedBy(fti): try: schema = fti.lookupSchema() except ImportError: # this dexterity type was changed/removed in an improper way # no need to punish, just fail gracefully continue fields = getFieldsInOrder(schema) for fieldname, field in fields: if INamedFileField.providedBy(field) or INamedImageField.providedBy(field): items.append(SimpleTerm(fti.getId(), fti.getId(), fti.Title())) break return SimpleVocabulary(items)
def __call__(self): e = {} ob = self.context title = ob.Title() if type(title) != unicode: title.decode('utf-8') e[u'title'] = title e[u'author_name'] = self._getAuthorName() try: info = IPrimaryFieldInfo(ob) except TypeError: info = None if info is None: e[u'type'] = 'link' else: field = info.field if IRichText.providedBy(field): e[u'type'] = 'rich' e[u'html'] = info.value elif INamedImageField.providedBy(field): e[u'type'] = 'photo' e[u'url'] = ob.absolute_url() image = field.get(ob) e[u'width'], e['height'] = image.getImageSize() else: e[u'type'] = 'link' return e
def set(self, data, filename, content_type): error = '' obj = self.context ttool = getToolByName(obj, 'portal_types') ctype = ttool[obj.portal_type] schema = ctype.lookupSchema() fields = getFieldsInOrder(schema) file_fields = [ field for name, field in fields if INamedFileField.providedBy(field) or INamedImageField.providedBy(field) ] if len(file_fields) == 0: error = u'serverError' logger.info( "An error happens : the dexterity content type %s " "has no file field, rawdata can't be created", obj.absolute_url()) for file_field in file_fields: if IPrimaryField.providedBy(file_field): break else: # Primary field can't be set ttw, # then, we take the first one file_field = file_fields[0] # TODO: use adapters if HAVE_BLOBS and INamedBlobImageField.providedBy(file_field): value = NamedBlobImage(data=data, contentType=content_type, filename=unicode(filename, 'utf-8')) elif HAVE_BLOBS and INamedBlobFileField.providedBy(file_field): value = NamedBlobFile(data=data, contentType=content_type, filename=unicode(filename, 'utf-8')) elif INamedImageField.providedBy(file_field): value = NamedImage(data=data, contentType=content_type, filename=unicode(filename, 'utf-8')) elif INamedFileField.providedBy(file_field): value = NamedFile(data=data, contentType=content_type, filename=unicode(filename, 'utf-8')) file_field.set(obj, value) return error
def get_configured_fields(self): tileType = queryUtility(ITileType, name=self.__name__) conf = self.get_tile_configuration() fields = getFieldsInOrder(tileType.schema) results = [] for name, field in fields: if (not INamedImageField.providedBy(field) and not self.data[name]) or \ (INamedImageField.providedBy(field) and not self.data[name] and not self.data.get('uuid')): # If there's no data for this field, ignore it # special condition, if the field is an image field and # there is no uuid, then ignore it too continue if isinstance(self.data[name], RichTextValue): transformer = ITransformer(self.context, None) if transformer is not None: content = transformer(self.data[name], 'text/x-html-safe') else: content = self.data[name] field = {'id': name, 'content': content, 'title': field.title} if name in conf: field_conf = conf[name] if ('visibility' in field_conf and field_conf['visibility'] == u'off'): # If the field was configured to be invisible, then just # ignore it continue if 'htmltag' in field_conf: # If this field has the capability to change its html tag # render, save it here field['htmltag'] = field_conf['htmltag'] if 'imgsize' in field_conf: field['scale'] = field_conf['imgsize'].split()[0] if 'position' in field_conf: field['position'] = field_conf['position'] results.append(field) return results
def export_images(self, imagesize): '''Returns the file (with the preview images ''' # Write ZIP archive zip_filename = tempfile.mktemp() ZIP = zipfile.ZipFile(zip_filename, 'w') #hack for new collection try: all_folder_contents = self.context.results(batch=False) except AttributeError: #for folder and old collection all_folder_contents = self.context.getFolderContents() for obj in all_folder_contents: obj = obj.getObject() try: #this is for archetype #imageformat is image/jpg so we are skipping the first part #this leaves us with png / jpg / gif or something else. imageformat = obj.getContentType() imageformat = imageformat.split("/") image_suffix = imageformat[1] #hack for news item image if image_suffix == 'html': image_suffix = 'jpg' if image_suffix == 'jpeg': image_suffix = 'jpg' full_image_name = obj.getId() + '.' + image_suffix img = obj.Schema().getField('image').getScale(obj,scale=imagesize) ZIP.writestr(self.context.getId() + '/' + full_image_name, str(img.data)) except: #this is for dexterity blob fields if IDexterityContent.providedBy(obj): for schemata in iterSchemata(obj): for name, field in getFields(schemata).items(): #checking for image field if INamedImageField.providedBy(field): #copied this line from somewhere field_value = field.get(field.interface(obj)) if field_value is not None: #field_value is not correct, this gets the image, not the scale ZIP.writestr(self.context.getId() + '/' + str((field_value.filename).encode("utf8")), str(field_value.data)) finally: pass ZIP.close() data = file(zip_filename).read() os.unlink(zip_filename) R = self.request.RESPONSE R.setHeader('content-type', 'application/zip') R.setHeader('content-length', len(data)) R.setHeader('content-disposition', 'attachment; filename="%s.zip"' % self.context.getId()) return R.write(data)
def export_images(self, imagesize): '''Returns zip file with images ''' # Write ZIP archive zip_filename = tempfile.mktemp() ZIP = zipfile.ZipFile(zip_filename, 'w') catalog = api.portal.get_tool(name='portal_catalog') folder_path = '/'.join(self.context.getPhysicalPath()) brains = catalog(Language="", show_inactive=True, path={'query': folder_path, 'depth': -1}) for obj in brains: obj = obj.getObject() try: #this is for archetype #imageformat is image/jpg so we are skipping the first part #this leaves us with png / jpg / gif or something else. imageformat = obj.getContentType() imageformat = imageformat.split("/") image_suffix = imageformat[1] #hack for news item image if image_suffix == 'html': image_suffix = 'jpg' if image_suffix == 'jpeg': image_suffix = 'jpg' full_image_name = obj.getId() + '.' + image_suffix img = obj.Schema().getField('image').getScale(obj,scale=imagesize) ZIP.writestr(self.context.getId() + '/' + full_image_name, str(img.data)) except: #this is for dexterity blob fields if IDexterityContent.providedBy(obj): for schemata in iterSchemata(obj): for name, field in getFields(schemata).items(): #checking for image field if INamedImageField.providedBy(field): #copied this line from somewhere field_value = field.get(field.interface(obj)) if field_value is not None: #field_value is not correct, this gets the image, not the scale ZIP.writestr(self.context.getId() + '/' + str((field_value.filename).encode("utf8")), str(field_value.data)) finally: pass ZIP.close() data = file(zip_filename).read() os.unlink(zip_filename) R = self.request.RESPONSE R.setHeader('content-type', 'application/zip') R.setHeader('content-length', len(data)) R.setHeader('content-disposition', 'attachment; filename="%s.zip"' % self.context.getId()) return R.write(data)
def get_configured_fields(self): context = self.context tileType = queryUtility(ITileType, name=self.__name__) conf = self.get_tile_configuration() fields = getFieldsInOrder(tileType.schema) uuid = self.data.get('uuid', '') results = [] for name, field in fields: image_field = INamedImageField.providedBy(field) data = self.data[name] if not ((image_field and (data or uuid)) or (not image_field and data)): # If there's no data for this field, ignore it # special condition, if the field is an image field and # there is no uuid, then ignore it too continue if isinstance(data, RichTextValue): transformer = ITransformer(context, None) if transformer is not None: content = transformer(data, 'text/x-html-safe') else: content = data field = {'id': name, 'content': content, 'title': field.title} if name in conf: field_conf = conf[name] if (field_conf.get('visibility', '') == u'off'): # If the field was configured to be invisible, then just # ignore it continue if 'htmltag' in field_conf: # If this field has the capability to change its html tag # render, save it here field['htmltag'] = field_conf['htmltag'] if 'imgsize' in field_conf: field['scale'] = field_conf['imgsize'].split()[0] if 'position' in field_conf: field['position'] = field_conf['position'] results.append(field) return results
def getRelativePaths(self): prefix = '/' + self.context.virtual_url_path() def fieldFilter(): portal_type = self.context.getPortalTypeName() fti = getUtility(IDexterityFTI, name=portal_type) schema = fti.lookupSchema() fields = getFieldsInOrder(schema) assignable = IBehaviorAssignable(self.context, None) for behavior in assignable.enumerateBehaviors(): if behavior.marker: new_fields = getFieldsInOrder(behavior.marker) if len(new_fields) > 0: fields = fields + new_fields obj_fields = [] for key, value in fields: is_image = INamedImageField.providedBy(value) is_file = INamedBlobFileField.providedBy(value) if is_image or is_file: obj_fields.append(value) return obj_fields for item in fieldFilter(): field = item.getName() value = item.get(self.context) if not value: continue if INamedImageField.providedBy(item): for size in self.getScales(): yield '{0}/@@images/{1}/{2}'.format( prefix, field, size, ) yield '{0}/@@download/{1}'.format(prefix, field) else: filename = value.filename if isinstance(filename, six.text_type): filename = filename.encode('utf-8') yield '{0}/view/{1}.{2}/@@download/{3}'.format( prefix, '++widget++form.widgets', field, filename) yield '{0}/@@download/{1}/{2}'.format(prefix, field, filename)
def setDexterityObject(self, obj, file_id): """ Set the file- or image-field of dexterity-based types This works with the types 'Image' and 'File' of plone.app.contenttypes and has fallbacks for other implementations of image- and file-types with dexterity. """ request = self.context.REQUEST field_name = '' info = '' try: # Use the primary field info = IPrimaryFieldInfo(obj, None) except TypeError: # ttw-types without a primary field throw a TypeError on # IPrimaryFieldInfo(obj, None) pass if info: field = info.field if INamedImageField.providedBy(field) or \ INamedFileField.providedBy(field): field_name = info.fieldname if not field_name: # Use the first field in the schema obj_schema = queryContentType(obj) obj_fields = getFieldsInOrder(obj_schema) for field_info in obj_fields: field = field_info[1] field_schema = getattr(field, 'schema', None) if field_schema and field_schema.getName() in [ 'INamedBlobImage', 'INamedImage', 'INamedBlobFile', 'INamedFile' ]: field_name = field_info[0] break if not field_name: return False else: # Create file/image setattr(obj, field_name, field._type(request['uploadfile'].read(), filename=unicode(file_id))) return True
def setDexterityObject(self, obj, file_id): """ Set the file- or image-field of dexterity-based types This works with the types 'Image' and 'File' of plone.app.contenttypes and has fallbacks for other implementations of image- and file-types with dexterity. """ request = self.context.REQUEST field_name = '' info = '' try: # Use the primary field info = IPrimaryFieldInfo(obj, None) except TypeError: # ttw-types without a primary field throw a TypeError on # IPrimaryFieldInfo(obj, None) pass if info: field = info.field if INamedImageField.providedBy(field) or \ INamedFileField.providedBy(field): field_name = info.fieldname if not field_name: # Use the first field in the schema obj_schema = queryContentType(obj) obj_fields = getFieldsInOrder(obj_schema) for field_info in obj_fields: field = field_info[1] field_schema = getattr(field, 'schema', None) if field_schema and field_schema.getName() in [ 'INamedBlobImage', 'INamedImage', 'INamedBlobFile', 'INamedFile' ]: field_name = field_info[0] break if not field_name: return False else: # Create file/image setattr( obj, field_name, field._type(request['uploadfile'].read(), filename=unicode(file_id))) return True
def fieldFilter(): portal_type = self.context.getPortalTypeName() fti = getUtility(IDexterityFTI, name=portal_type) schema = fti.lookupSchema() fields = getFieldsInOrder(schema) assignable = IBehaviorAssignable(self.context, None) for behavior in assignable.enumerateBehaviors(): if behavior.marker: new_fields = getFieldsInOrder(behavior.marker) if len(new_fields) > 0: fields = fields + new_fields obj_fields = [] for key, value in fields: is_image = INamedImageField.providedBy(value) is_file = INamedBlobFileField.providedBy(value) if is_image or is_file: obj_fields.append(value) return obj_fields
def __call__(self, context): context = getattr(context, 'context', context) portal = getToolByName(context, 'portal_url').getPortalObject() flt = [ _infoDictForType(portal, tipe) for tipe in _listTypesForInterface(portal, IFileContent) ] ilt = [ _infoDictForType(portal, tipe) for tipe in _listTypesForInterface(portal, IImageContent) ] items = [ SimpleTerm( 'auto', 'auto', context.translate( _('label_default_portaltype_configuration', default=u'Default configuration (Content Type Registry).' ))) ] items.extend([ SimpleTerm(t['portal_type'], t['portal_type'], t['type_ui_info']) for t in flt ]) file_types = [t['portal_type'] for t in flt] items.extend([ SimpleTerm(t['portal_type'], t['portal_type'], t['type_ui_info']) for t in ilt if t['portal_type'] not in file_types ]) for fti in portal.portal_types.objectValues(): if HAS_DEXTERITY and IDexterityFTI.providedBy(fti): fields = getFieldsInOrder(fti.lookupSchema()) for fieldname, field in fields: if INamedFileField.providedBy( field) or INamedImageField.providedBy(field): items.append( SimpleTerm(fti.getId(), fti.getId(), fti.Title())) break return SimpleVocabulary(items)
def getRelativePaths(self): prefix = '/' + self.context.virtual_url_path() def fieldFilter(): portal_type = self.context.getPortalTypeName() fti = getUtility(IDexterityFTI, name=portal_type) schema = fti.lookupSchema() fields = getFieldsInOrder(schema) assignable = IBehaviorAssignable(self.context, None) for behavior in assignable.enumerateBehaviors(): if behavior.marker: new_fields = getFieldsInOrder(behavior.marker) if len(new_fields) > 0: fields = fields + new_fields obj_fields = [] for key, value in fields: is_image = INamedImageField.providedBy(value) is_file = INamedBlobFileField.providedBy(value) if is_image or is_file: obj_fields.append(value) return obj_fields for item in fieldFilter(): field = item.getName() value = item.get(self.context) if not value: continue if INamedImageField.providedBy(item): for size in self.getScales(): yield '{0}/@@images/{1}/{2}'.format(prefix, field, size,) yield '{0}/@@download/{1}'.format(prefix, field) else: filename = value.filename if six.PY2 and isinstance(filename, six.text_type): filename = filename.encode('utf-8') yield '{0}/view/{1}.{2}/@@download/{3}'.format( prefix, '++widget++form.widgets', field, filename) yield '{0}/@@download/{1}/{2}'.format(prefix, field, filename)
def _deploy_content(self, obj, is_page=True): """ Deploy object as page. """ try: new_req, orig_req = fakeRequest(obj) except AttributeError: # not a valid obj to override request with new_req = None content = self._render_obj(obj) if content is None: return filename = obj.absolute_url_path().lstrip('/') # deploy additional views for content type if PLONE_APP_BLOB_INSTALLED and isinstance(obj, ATBlob): self._deploy_views([os.path.join(filename, 'view'), ], is_page=True) if is_page: filename = filename.rstrip('/') if self.add_index or IFolder.providedBy(obj): filename = os.path.join(filename, 'index.html') elif not filename.endswith('.htm') and not filename.endswith('.html'): filename = filename + '.html' elif isinstance(obj, ATImage) or \ hasattr(obj, 'getBlobWrapper') and \ 'image' in obj.getBlobWrapper().getContentType(): # create path to dump ATImage in original size if filename.rsplit('.', 1)[-1] in ('png', 'jpg', 'gif', 'jpeg'): filename = os.path.join(filename, 'image.%s' % ( filename.rsplit('.', 1)[-1])) else: filename = os.path.join(filename, 'image.jpg') filename, content = self._apply_image_transforms(filename, content) elif (hasattr(obj, 'getBlobWrapper') and 'image' not in obj.getBlobWrapper().getContentType()): # create path like for ATImage if len(filename.rsplit('.', 1)) > 1: filename = os.path.join(filename, 'file.%s' % ( filename.rsplit('.', 1)[-1])) else: filename = os.path.join(filename, 'file') self._write(filename, content) # deploy all sizes of images uploaded for the object if not getattr(obj, 'schema', None): return # For Dexterity objects if IDexterityContent.providedBy(obj): from plone.dexterity.interfaces import IDexterityFTI from zope.component import getUtility from zope.schema import getFieldsInOrder from plone.behavior.interfaces import IBehaviorAssignable fti = getUtility(IDexterityFTI, name=obj.portal_type) schema = fti.lookupSchema() fields = getFieldsInOrder(schema) for _, field in fields: if INamedImageField.providedBy(field): self._deploy_blob_dexterity_image_field(obj, field) elif INamedFileField.providedBy(field): self._deploy_blob_dexterity_file_field(obj, field) behavior_assignable = IBehaviorAssignable(obj) if behavior_assignable: behaviors = behavior_assignable.enumerateBehaviors() for behavior in behaviors: for k, v in getFieldsInOrder(behavior.interface): pass else: for field in obj.Schema().fields(): if (PLONE_APP_BLOB_INSTALLED and IBlobImageField.providedBy(field)) or \ field.type == 'image': self._deploy_blob_image_field(obj, field) elif PLONE_APP_BLOB_INSTALLED and IBlobField.providedBy(field): self._deploy_blob_file_field(obj, field) elif field.type == 'file' and obj.portal_type not in self.file_types: self._deploy_file_field(obj, field) else: continue if new_req is not None: restoreRequest(orig_req, new_req)
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 handleApply(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return utils._send_emails = False try: en_folder = self.context.restrictedTraverse('en') if 'organisations' in en_folder: org_folder = en_folder.restrictedTraverse('organisations') else: org_folder = content.create( en_folder, type='osha.hwccontent.organisationfolder', title='Organisations') if 'focalpoints' in en_folder: fop_folder = en_folder.restrictedTraverse('focalpoints') else: fop_folder = content.create( en_folder, type='osha.hwccontent.organisationfolder', title='Focalpoints') type_mapping = { u'Organisation': { 'type': 'osha.hwccontent.organisation', 'schema': dict(getFieldsInOrder(IOrganisation)), 'folder': org_folder, 'wf_actions': ('approve_phase_1', ), }, u'Focalpoint': { 'type': 'osha.hwccontent.focalpoint', 'schema': dict(getFieldsInOrder(IFocalPoint)), 'folder': fop_folder, 'wf_actions': ('publish', ), } } count = 0 for data in json.loads(data['json']): # Only keep the data that's in the main schema: type_info = type_mapping[data['_type']] schema = type_info['schema'] fields = {} if data['title'].startswith('MC-'): continue for name, field in schema.items(): if name in data: value = data[name] if value and INamedImageField.providedBy(field): content_type = data.get('_%s_content_type' % name, '') filename = data.get('_%s_filename' % name, None) value = NamedBlobImage(base64.b64decode(value), str(content_type), filename) elif value and IRichText.providedBy(field): content_type = data.get('_%s_content_type', None) value = RichTextValue(value, mimeType=content_type) elif name.find("email") >= 0: value = value.strip() fields[name] = value new_obj = content.create(container=type_info['folder'], type=type_info['type'], id=data['id'], **fields) for transition in type_info['wf_actions']: try: content.transition(new_obj, transition) except Exception: logger.exception( 'Could not execute %s transition for %s' % (transition, '/'.join(new_obj.getPhysicalPath()))) logger.info('Imported %s' % new_obj.getId()) count += 1 # Set status on this form page self.status = "%s partners imported" % count except Exception: # Enable emails again: utils._send_emails = True raise
def export_blobs(self, portal_type, blob_type, blacklist, whitelist): """Return a zip-file with file and/or images for the required export. """ all_fields = get_schema_info(portal_type, blacklist, whitelist) if blob_type == 'images': fields = [ i for i in all_fields if INamedImageField.providedBy(i[1]) or INamedBlobImageField.providedBy(i[1]) ] elif blob_type == 'files': fields = [ i for i in all_fields if INamedFileField.providedBy(i[1]) or INamedBlobFileField.providedBy(i[1]) ] elif blob_type == 'related': fields = [ i for i in all_fields if IRelationChoice.providedBy(i[1]) or IRelationList.providedBy(i[1]) ] tmp_file = NamedTemporaryFile() zip_file = zipfile.ZipFile(tmp_file, 'w') catalog = api.portal.get_tool('portal_catalog') query = {'portal_type': portal_type} query['path'] = {} query['path']['query'] = '/'.join(self.context.getPhysicalPath()) blobs_found = False if HAS_MULTILINGUAL and 'Language' in catalog.indexes(): query['Language'] = 'all' for brain in catalog(query): obj = brain.getObject() for fieldname, field in fields: # manually filter for fields # if fieldname not in ['primary_picture']: # continue blobs = [] value = field.get(field.interface(obj)) if not value: continue if blob_type != 'related': blobs = [value] elif IRelationChoice.providedBy(field) or \ IRelationList.providedBy(field): blobs = get_blobs_from_relations(value, field) for blob in blobs: if not blob: continue filename = str((blob.filename).encode('utf8')) zip_file.writestr( '{0}_{1}/{2}'.format( brain.UID, # or: brain.id.upper(), fieldname, filename), str(blob.data)) blobs_found = True zip_file.close() if not blobs_found: return 'No {0} found'.format(blob_type) data = file(tmp_file.name).read() response = self.request.response response.setHeader('content-type', 'application/zip') response.setHeader('content-length', len(data)) response.setHeader('content-disposition', 'attachment; filename="{0}.zip"'.format(blob_type)) return response.write(data)
invoiceSenderName=invoice.senderName, invoiceRecipient=invoice.invoiceRecipient, invoiceRecipientName=invoice.invoiceRecipientName, invoiceDate=invoice.invoiceDate, invoicePayCondition=invoice.invoicePayCondition, invoiceExpireDate=invoice.invoiceExpireDate, invoiceCurrency=invoice.invoiceCurrency, invoiceTotalVat=invoice.invoiceTotalVat, invoiceTotalAmount=invoice.invoiceTotalCost ) # Get the field containing data fields = getFieldsInOrder(IInvoice) file_fields = [field for name, field in fields if INamedFileField.providedBy(field) or INamedImageField.providedBy(field) ] for file_field in file_fields: if IPrimaryField.providedBy(file_field): break else: # Primary field can't be set ttw, # then, we take the first one file_field = file_fields[0] #import pdb #pdb.set_trace() value = NamedBlobFile(data=invoicefile, contentType=contenttype, filename=unicode(filename, 'utf-8'))
def export_blobs(self, portal_type, blob_type, blacklist, whitelist): """Return a zip-file with file and/or images for the required export. """ all_fields = get_schema_info(portal_type, blacklist, whitelist) if blob_type == 'images': fields = [ i for i in all_fields if INamedImageField.providedBy(i[1]) or INamedBlobImageField.providedBy(i[1])] elif blob_type == 'files': fields = [ i for i in all_fields if INamedFileField.providedBy(i[1]) or INamedBlobFileField.providedBy(i[1])] elif blob_type == 'related': fields = [ i for i in all_fields if IRelationChoice.providedBy(i[1]) or IRelationList.providedBy(i[1])] tmp_file = NamedTemporaryFile() zip_file = zipfile.ZipFile(tmp_file, 'w') catalog = api.portal.get_tool('portal_catalog') query = {'portal_type': portal_type} blobs_found = False if HAS_MULTILINGUAL and 'Language' in catalog.indexes(): query['Language'] = 'all' for brain in catalog(query): obj = brain.getObject() for fieldname, field in fields: # manually filter for fields # if fieldname not in ['primary_picture']: # continue blobs = [] value = field.get(field.interface(obj)) if not value: continue if blob_type != 'related': blobs = [value] elif IRelationChoice.providedBy(field) or \ IRelationList.providedBy(field): blobs = get_blobs_from_relations(value, field) for blob in blobs: if not blob: continue filename = str((blob.filename).encode('utf8')) zip_file.writestr( '{0}_{1}/{2}'.format( brain.UID, # or: brain.id.upper(), fieldname, filename), str(blob.data) ) blobs_found = True zip_file.close() if not blobs_found: return 'No {0} found'.format(blob_type) data = file(tmp_file.name).read() response = self.request.response response.setHeader('content-type', 'application/zip') response.setHeader('content-length', len(data)) response.setHeader( 'content-disposition', 'attachment; filename="{0}.zip"'.format(blob_type)) return response.write(data)
def __call__(self, name, content_type, data, obj_id): ctr = cmfutils.getToolByName(self.context, 'content_type_registry') type_ = ctr.findTypeName(name.lower(), '', '') or 'File' # otherwise I get ZPublisher.Conflict ConflictErrors # when uploading multiple files upload_lock.acquire() try: transaction.begin() obj = ploneutils._createObjectByType(type_, self.context, obj_id) ttool = getToolByName(self.context, 'portal_types') ctype = ttool[obj.portal_type] schema = ctype.lookupSchema() fields = getFieldsInOrder(schema) file_fields = [ field for safe_name, field in fields if INamedFileField.providedBy(field) or INamedImageField.providedBy(field) ] if len(file_fields) == 0: logger.info( "An error happens : the dexterity content type %s " "has no file field, rawdata can't be created", obj.absolute_url()) for file_field in file_fields: if IPrimaryField.providedBy(file_field): break else: # Primary field can't be set ttw, # then, we take the first one file_field = file_fields[0] # TODO: use adapters if HAVE_BLOBS and INamedBlobImageField.providedBy(file_field): value = NamedBlobImage(data=data.read(), contentType=content_type, filename=unicode(obj_id, 'utf-8')) elif HAVE_BLOBS and INamedBlobFileField.providedBy(file_field): value = NamedBlobFile(data=data.read(), contentType=content_type, filename=unicode(obj_id, 'utf-8')) elif INamedImageField.providedBy(file_field): value = NamedImage(data=data.read(), contentType=content_type, filename=unicode(obj_id, 'utf-8')) elif INamedFileField.providedBy(file_field): value = NamedFile(data=data.read(), contentType=content_type, filename=unicode(obj_id, 'utf-8')) file_field.set(obj, value) obj.title = name obj.reindexObject() notify(ObjectInitializedEvent(obj)) notify(ObjectModifiedEvent(obj)) transaction.commit() finally: upload_lock.release() return obj
def handleApply(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return utils._send_emails = False try: en_folder = self.context.restrictedTraverse('en') if 'organisations' in en_folder: org_folder = en_folder.restrictedTraverse('organisations') else: org_folder = content.create(en_folder, type='osha.hwccontent.organisationfolder', title='Organisations') if 'focalpoints' in en_folder: fop_folder = en_folder.restrictedTraverse('focalpoints') else: fop_folder = content.create(en_folder, type='osha.hwccontent.organisationfolder', title='Focalpoints') type_mapping = { u'Organisation': { 'type': 'osha.hwccontent.organisation', 'schema': dict(getFieldsInOrder(IOrganisation)), 'folder': org_folder, 'wf_actions': ('approve_phase_1',), }, u'Focalpoint': { 'type': 'osha.hwccontent.focalpoint', 'schema': dict(getFieldsInOrder(IFocalPoint)), 'folder': fop_folder, 'wf_actions': ('publish',), } } count = 0 for data in json.loads(data['json']): # Only keep the data that's in the main schema: type_info = type_mapping[data['_type']] schema = type_info['schema'] fields = {} if data['title'].startswith('MC-'): continue for name, field in schema.items(): if name in data: value = data[name] if value and INamedImageField.providedBy(field): content_type = data.get('_%s_content_type' % name, '') filename = data.get('_%s_filename' % name , None) value = NamedBlobImage(base64.b64decode(value), str(content_type), filename) elif value and IRichText.providedBy(field): content_type = data.get('_%s_content_type', None) value = RichTextValue(value, mimeType=content_type) elif name.find("email") >= 0: value = value.strip() fields[name] = value new_obj = content.create(container=type_info['folder'], type= type_info['type'], id=data['id'], **fields) for transition in type_info['wf_actions']: try: content.transition(new_obj, transition) except Exception: logger.exception('Could not execute %s transition for %s' % (transition, '/'.join(new_obj.getPhysicalPath()))) logger.info('Imported %s' % new_obj.getId()) count += 1 # Set status on this form page self.status = "%s partners imported" % count except Exception: # Enable emails again: utils._send_emails = True raise