def check_dexterity_blobs(context): """ Check Dexterity content for damaged blob fields Return True if purge needed """ # Assume dexterity contennt inherits from Item if isinstance(context, DexterityContent): # Iterate through all Python object attributes for key, value in context.__dict__.items(): # Ignore non-contentish attributes to speed up us a bit if not key.startswith("_"): if INamedFile.providedBy(value): try: value.getSize() except POSKeyError: print "Found damaged Dexterity plone.app.NamedFile \ %s on %s" % (key, context.absolute_url()) add_broken_link( url=context.absolute_url(), creation=context.creation_date, modification=context.modification_date, reason="sure" ) return True return False
def test_get_site_icon(self): registry = getUtility(IRegistry) registry['plone.site_icon'] = b64encode_file('foobar.gif', bimage) view = IconView(self.portal, self.request) self.request.form.update({'scale': '180'}) result = view._getFile() self.assertTrue(INamedFile.providedBy(result))
def get_mail_body(self, fields, request, context): """Returns the mail-body with footer. """ schema = get_fields(context) all_fields = [f for f in fields # TODO # if not (f.isLabel() or f.isFileField()) and not (getattr(self, # 'showAll', True) and f.getServerSide())] if not (INamedFile.providedBy(fields[f])) and not (getattr(self, 'showAll', True) and IFieldExtender(schema[f]).serverSide) ] # which fields should we show? if getattr(self, 'showAll', True): live_fields = all_fields else: live_fields = [ f for f in all_fields if f in getattr(self, 'showFields', ())] if not getattr(self, 'includeEmpties', True): all_fields = live_fields live_fields = [f for f in all_fields if fields[f]] for f in all_fields: value = fields[f] if value: live_fields.append(f) #bare_fields = [schema[f] for f in live_fields] bare_fields = dict([(f, fields[f]) for f in live_fields]) bodyfield = self.body_pt # pass both the bare_fields (fgFields only) and full fields. # bare_fields for compatability with older templates, # full fields to enable access to htmlValue replacer = DollarVarReplacer(fields).sub extra = { 'data': bare_fields, 'fields': dict([(i, j.title) for i, j in getFieldsInOrder(schema)]), 'mailer': self, 'body_pre': self.body_pre and replacer(self.body_pre), 'body_post': self.body_post and replacer(self.body_post), 'body_footer': self.body_footer and replacer(self.body_footer), } template = ZopePageTemplate(self.__name__) template.write(bodyfield) template = template.__of__(context) body = template.pt_render(extra_context=extra) # if isinstance(body, unicode): #body = body.encode("utf-8") #keyid = getattr(self, 'gpg_keyid', None) #encryption = gpg and keyid # if encryption: #bodygpg = gpg.encrypt(body, keyid) # if bodygpg.strip(): #body = bodygpg return body
def add_filename_to_metadata(ob, metadata, request, response): try: field = IPrimaryFieldInfo(ob).field except TypeError: return value = field.get(ob) if INamedFile.providedBy(value): filename = getattr(value, 'filename', None) if filename is not None: metadata.insert(0, 'filename:%s' % filename.encode('utf-8'))
def test_namedfile_setting(self): data = 'datattatatata' filename = FILENAME_TESTSTRING + u".xlsx" content_type = 'application/vnd.ms-excel' fileSetter = self.getFileSetter(ISchemaWithNamedFile) fileSetter.set(data, filename.encode('utf-8'), content_type) field = fileSetter.context.field self.assertTrue(INamedFile.providedBy(field)) self.assertEqual(field.filename, filename) self.assertEqual(field.contentType, content_type) self.assertEqual(field.data, data)
def check_dexterity_blobs(context): trouble = False for key, value in context.__dict__.items(): # Ignore non-contentish attributes to speed up us a bit if not key.startswith("_"): if INamedFile.providedBy(value): try: value.getSize() except POSKeyError: print "Found damaged Dexterity plone.app.NamedFile %s on %s" % (key, context.absolute_url()) trouble = True setattr(context, key, None) return trouble
def get_attachments(self, fields, request): """Return all attachments uploaded in form. """ attachments = [] for fname in fields: field = fields[fname] if INamedFile.providedBy(field) and (getattr(self, 'showAll', True) or fname in getattr(self, 'showFields', ())): data = field.data filename = field.filename mimetype, enc = guess_content_type(filename, data, None) attachments.append((filename, mimetype, enc, data)) return attachments
def getSavedFormInputForEdit(self, header=False, delimiter=','): """ returns saved as CSV text """ sbuf = StringIO() writer = csvwriter(sbuf, delimiter=delimiter) names = self.getColumnNames() if header: writer.writerow(names) for row in self.getSavedFormInput(): writer.writerow([ row[i].filename if INamedFile.providedBy( row.get(i, '')) else row.get(i, '') for i in names]) res = sbuf.getvalue() sbuf.close() return res
def check_dexterity_blobs(context): """ Check Dexterity content for damaged blob fields """ if isinstance(context, DexterityContent): for key, value in context.__dict__.items(): if not key.startswith("_"): if INamedFile.providedBy(value): try: value.getSize() except POSKeyError: logger.warn("Found damaged Dexterity " "plone.app.NamedFile %s on %s" % (key, context.absolute_url())) return True return False
def test_file_content_is_migrated(self): from plone.app.contenttypes.migration import FileMigrator from plone.namedfile.interfaces import INamedFile from Products.ATContentTypes.content.file import ATFile at_file = self.createATCTobject(ATFile, 'file') field = at_file.getField('file') field.set(at_file, 'dummydata') field.setFilename(at_file, 'dummyfile.txt') field.setContentType(at_file, 'text/dummy') migrator = self.get_migrator(at_file, FileMigrator) migrator.migrate() new_file = self.portal['file'] self.assertTrue(INamedFile.providedBy(new_file.file)) self.assertEqual(new_file.file.filename, 'dummyfile.txt') self.assertEqual(new_file.file.contentType, 'text/dummy') self.assertEqual(new_file.file.data, 'dummydata')
def getSavedFormInputForEdit(self, header=False, delimiter=','): """ returns saved as CSV text """ sbuf = StringIO() writer = csvwriter(sbuf, delimiter=delimiter) names = self.getColumnNames() if header: writer.writerow(names) for row in self.getSavedFormInput(): writer.writerow([ row[i].filename if INamedFile.providedBy(row.get(i, '')) else row.get(i, '') for i in names ]) res = sbuf.getvalue() sbuf.close() return res
def get_attachments(self, fields, request): """Return all attachments uploaded in form. """ attachments = [] for fname in fields: field = fields[fname] if INamedFile.providedBy(field) and ( getattr(self, 'showAll', True) or fname in getattr(self, 'showFields', ())): data = field.data filename = field.filename mimetype, enc = guess_content_type(filename, data, None) attachments.append((filename, mimetype, enc, data)) return attachments
def check_dexterity_blobs(context, report_file): """ Check Dexterity content for damaged blob fields XXX: NOT TESTED - THEORETICAL, GUIDELINING, IMPLEMENTATION Return True if purge needed """ # Assume dexterity contennt inherits from Item if isinstance(context, DexterityContent): # Iterate through all Python object attributes # XXX: Might be smarter to use zope.schema introspection here? for key, value in context.__dict__.items(): # Ignore non-contentish attributes to speed up us a bit if not key.startswith("_"): if INamedFile.providedBy(value): try: value.getSize() except POSKeyError: msg = "Found damaged Dexterity plone.app.NamedFile %s on %s" % (key, context.absolute_url()) print msg report_file.write(msg) return True return False
def check_dexterity_blobs(context, report_file): """ Check Dexterity content for damaged blob fields XXX: NOT TESTED - THEORETICAL, GUIDELINING, IMPLEMENTATION Return True if purge needed """ # Assume dexterity contennt inherits from Item if isinstance(context, DexterityContent): # Iterate through all Python object attributes # XXX: Might be smarter to use zope.schema introspection here? for key, value in context.__dict__.items(): # Ignore non-contentish attributes to speed up us a bit if not key.startswith("_"): if INamedFile.providedBy(value): try: value.getSize() except POSKeyError: msg = "Found damaged Dexterity plone.app.NamedFile %s on %s" % ( key, context.absolute_url()) print msg report_file.write(msg) return True return False
def test_get_site_icon(self): self.request.form.update({'scale': '180'}) result = self.view._getFile() self.assertTrue(INamedFile.providedBy(result))
def getData(self): """returns all important data data form {'column': {portlet: {key:value} } . . . . {'blackliststatus': {category:True}, {'order': ['portlet 1', 'portlet 2']} } } """ data = {} plone_portlet_manager = [] # gets all portlet managers used on this object annotations = getattr(self.object, '__annotations__', None) if not annotations: return data has_portlets = 'plone.portlets.contextassignments' in annotations has_blacklist = 'plone.portlets.categoryblackliststatus' in annotations if not has_portlets and not has_blacklist: return data if has_portlets: plone_portlet_manager += self.object.__annotations__[ 'plone.portlets.contextassignments'].keys() if has_blacklist: plone_portlet_manager += self.object.__annotations__[ 'plone.portlets.categoryblackliststatus'].keys() EXCLUDED_FIELDS = ['__name__', '__parent__'] # XXX this is a static list, replace by a configlet option # the list contains all not serializable portlets (__module__) blacklisted_portlets = [ 'collective.dancing.browser.portlets.channelsubscribe' ] for manager_name in plone_portlet_manager: column = queryUtility(IPortletManager, name=manager_name, context=self.object) if column is None: continue # ok we have a portlet manager data[manager_name] = {} # get blackliststatus blacklist = getMultiAdapter((self.object, column), ILocalPortletAssignmentManager) data[manager_name]['blackliststatus'] = {} blacklistdata = data[manager_name]['blackliststatus'] blacklistdata[GROUP_CATEGORY] = blacklist.getBlacklistStatus( GROUP_CATEGORY) blacklistdata[USER_CATEGORY] = blacklist.getBlacklistStatus( USER_CATEGORY) blacklistdata[CONTENT_TYPE_CATEGORY] = \ blacklist.getBlacklistStatus(CONTENT_TYPE_CATEGORY) blacklistdata[CONTEXT_CATEGORY] = \ blacklist.getBlacklistStatus(CONTEXT_CATEGORY) portlets = getMultiAdapter(( self.object, column, ), IPortletAssignmentMapping, context=self.object) # portlets order - dicts are unsorted data[manager_name]['order'] = ','.join(portlets._order) for portlet_id in portlets.keys(): portlet_assignment = portlets[portlet_id] # continue if portlet is blacklisted if portlet_assignment.__module__ in blacklisted_portlets: continue # we habe a portlet data[manager_name][portlet_assignment.__name__] = {} data[manager_name][portlet_assignment.__name__]['module'] = \ portlet_assignment.__class__.__module__ data[manager_name][portlet_assignment.__name__]['assignment_class_name'] = \ portlet_assignment.__class__.__name__ # portlet settings (visibility) settings = IPortletAssignmentSettings(portlet_assignment).data data[manager_name][ portlet_assignment.__name__]['settings'] = settings # get all data for field in portlet_assignment.__dict__.keys(): if field not in EXCLUDED_FIELDS: field_value = getattr(portlet_assignment, field, '') # image field - image.portlet integration if isinstance(field_value, OFSImage): # same way as in AT field serializer field_value = { 'module': OFSImage.__module__, 'klass_name': OFSImage.__name__, 'kwargs': { 'file': base64.encodestring(field_value.data), 'id': field_value.id(), 'title': field_value.title } } elif (HAS_NAMEDFILE and INamedFile.providedBy(field_value)): klass = type(field_value) field_value = { 'module': klass.__module__, 'klass_name': klass.__name__, 'kwargs': { 'data': base64.encodestring(field_value.data), 'filename': field_value.filename, 'contentType': field_value.contentType } } data[manager_name][ portlet_assignment.__name__][field] = field_value return data
def getData(self): """returns all important data data form {'column': {portlet: {key:value} } . . . . {'blackliststatus': {category:True}, {'order': ['portlet 1', 'portlet 2']} } } """ data = {} # gets all portlet managers used on this object annotations = getattr(self.object, '__annotations__', None) if not annotations: return data if 'plone.portlets.contextassignments' not in annotations: return data plone_portlet_manager = self.object.__annotations__[ 'plone.portlets.contextassignments'].keys() EXCLUDED_FIELDS = ['__name__', '__parent__'] # XXX this is a static list, replace by a configlet option # the list contains all not serializable portlets (__module__) blacklisted_portlets = [ 'collective.dancing.browser.portlets.channelsubscribe'] for manager_name in plone_portlet_manager: column = queryUtility(IPortletManager, name=manager_name, context=self.object) if column is None: continue # ok we have a portlet manager data[manager_name] = {} # get blackliststatus blacklist = getMultiAdapter((self.object, column), ILocalPortletAssignmentManager) data[manager_name]['blackliststatus'] = {} blacklistdata = data[manager_name]['blackliststatus'] blacklistdata[GROUP_CATEGORY] = blacklist.getBlacklistStatus( GROUP_CATEGORY) blacklistdata[USER_CATEGORY] = blacklist.getBlacklistStatus( USER_CATEGORY) blacklistdata[CONTENT_TYPE_CATEGORY] = \ blacklist.getBlacklistStatus(CONTENT_TYPE_CATEGORY) blacklistdata[CONTEXT_CATEGORY] = \ blacklist.getBlacklistStatus(CONTEXT_CATEGORY) portlets = getMultiAdapter((self.object, column,), IPortletAssignmentMapping, context=self.object) # portlets order - dicts are unsorted data[manager_name]['order'] = ','.join(portlets._order) for portlet_id in portlets.keys(): portlet_assignment = portlets[portlet_id] # continue if portlet is blacklisted if portlet_assignment.__module__ in blacklisted_portlets: continue # we habe a portlet data[manager_name][portlet_assignment.__name__] = {} data[manager_name][portlet_assignment.__name__]['module'] = \ portlet_assignment.__class__.__module__ data[manager_name][portlet_assignment.__name__]['assignment_class_name'] = \ portlet_assignment.__class__.__name__ # portlet settings (visibility) settings = IPortletAssignmentSettings(portlet_assignment).data data[manager_name][portlet_assignment.__name__]['settings'] = settings # get all data for field in portlet_assignment.__dict__.keys(): if field not in EXCLUDED_FIELDS: field_value = getattr(portlet_assignment, field, '') # image field - image.portlet integration if isinstance(field_value, OFSImage): # same way as in AT field serializer field_value = { 'module': OFSImage.__module__, 'klass_name': OFSImage.__name__, 'kwargs': { 'file': base64.encodestring( field_value.data), 'id': field_value.id(), 'title': field_value.title}} elif (HAS_NAMEDFILE and INamedFile.providedBy( field_value)): klass = type(field_value) field_value = { 'module': klass.__module__, 'klass_name': klass.__name__, 'kwargs': { 'data': base64.encodestring( field_value.data), 'filename': field_value.filename, 'contentType': field_value.contentType}} data[manager_name][portlet_assignment.__name__][ field] = field_value return data
def get_mail_body(self, fields, request, context): """Returns the mail-body with footer. """ schema = get_fields(context) all_fields = [ f for f in fields # TODO # if not (f.isLabel() or f.isFileField()) and not (getattr(self, # 'showAll', True) and f.getServerSide())] if not (INamedFile.providedBy(fields[f])) and not (getattr(self, 'showAll', True) and IFieldExtender(schema[f]).serverSide) ] # which fields should we show? if getattr(self, 'showAll', True): live_fields = all_fields else: live_fields = [ f for f in all_fields if f in getattr(self, 'showFields', ()) ] if not getattr(self, 'includeEmpties', True): all_fields = live_fields live_fields = [f for f in all_fields if fields[f]] for f in all_fields: value = fields[f] if value: live_fields.append(f) #bare_fields = [schema[f] for f in live_fields] bare_fields = dict([(f, fields[f]) for f in live_fields]) bodyfield = self.body_pt # pass both the bare_fields (fgFields only) and full fields. # bare_fields for compatability with older templates, # full fields to enable access to htmlValue replacer = DollarVarReplacer(fields).sub extra = { 'data': bare_fields, 'fields': dict([(i, j.title) for i, j in getFieldsInOrder(schema)]), 'mailer': self, 'body_pre': self.body_pre and replacer(self.body_pre), 'body_post': self.body_post and replacer(self.body_post), 'body_footer': self.body_footer and replacer(self.body_footer), } template = ZopePageTemplate(self.__name__) template.write(bodyfield) template = template.__of__(context) body = template.pt_render(extra_context=extra) # if isinstance(body, unicode): #body = body.encode("utf-8") #keyid = getattr(self, 'gpg_keyid', None) #encryption = gpg and keyid # if encryption: #bodygpg = gpg.encrypt(body, keyid) # if bodygpg.strip(): #body = bodygpg return body