def from_request_data(self, field, request_data): """ Add some logic for figuring out whether or not to show the 'remove' checkbox. """ if request_data.get('default', [False])[0]: # we have a default value that can be removed self.show_remove_checkbox = True if request_data['name'] == ['']: remove = request_data.get('remove', [None])[0] is not None default = request_data.get('default', [None])[0] name = request_data.get('name', [None])[0] meta = {'remove': remove, 'default': default, 'name': name} return SchemaFile(None, None, None, metadata=meta) elif request_data['name'] == request_data['default']: return SchemaFile(None, None, None) else: key = request_data['name'][0] try: cache_tag, headers, f = self.filestore.get(key) except (KeyError, TypeError): return None # we have a photo from the filestore that can be removed self.show_remove_checkbox = True self.from_filestore = True headers = dict(headers) return SchemaFile(f, headers['Filename'], headers['Content-Type'])
def from_request_data(self, field, request_data): """ Differs from default for better handling of SchemaFile's metadata when no file is in the request, and to set a flag on self when we're working w/ a file in the tmp filestore. """ if request_data['name'] == ['']: remove = request_data.get('remove', [None])[0] is not None default = request_data.get('default', [None])[0] name = request_data.get('name', [None])[0] meta = {'remove': remove, 'default': default, 'name': name} return SchemaFile(None, None, None, metadata=meta) elif (request_data['name'] == request_data['default'] and request_data.get('file') is None): # no file submitted -> no change to the file value return SchemaFile(None, None, None) else: key = request_data['name'][0] try: cache_tag, headers, f = self.filestore.get(key) except (KeyError, TypeError): return None self.from_filestore = True headers = dict(headers) if headers.get('Filename', None) is None: # user pressed remove on edit view and submitted with no file return None return SchemaFile(f, headers['Filename'], headers['Content-Type'])
def add_sample_file(community, i): files = community['files'] title = generate_title('SampleF') log.info('adding file %s', title) filename = get_sample_filename(subdir='samplefiles') stream = open(filename, 'rb') class FakeFieldStorage: pass filename = 'sample%d-%s' % (i, os.path.basename(filename)) mimetype, _ = mimetypes.guess_type(filename) fs = SchemaFile(stream, filename, mimetype) request = testing.DummyRequest() request.environ.update(DEFAULT_ENV) request.POST = FauxPost(request.POST) converted = {} converted['title'] = title converted['file'] = fs converted['security_state'] = 'inherits' converted['tags'] = ['sample'] controller = AddFileFormController(files, request) response = controller.handle_submit(converted) file = _parse_add_response(request, response, files) stream.close() return file
def __init__(self, context, request): self.page_title = 'Edit %s' % context.title super(EditNewsItemFormController, self).__init__(context, request) photo = context.get('photo') if photo is not None: photo = SchemaFile(None, photo.__name__, photo.mimetype) self.photo = photo
def form_defaults(self): context = self.context attachments = [SchemaFile(None, x.__name__, x.mimetype) for x in context.values()] defaults = {'add_comment': context.text, 'attachments': attachments, } return defaults
def __init__(self, context, request): self.context = context self.request = request self.filestore = get_filestore(context, request, 'edit-profile') self.page_title = "Edit %s" % context.title photo = context.get('photo') if photo is not None: photo = SchemaFile(None, photo.__name__, photo.mimetype) self.photo = photo
def test_to_request_data_with_schemafile(self): from schemaish.type import File as SchemaFile field = DummyField() f = SchemaFile('1', '2', '3') filestore = DummyFileStore() widget = self._makeOne(filestore) result = widget.to_request_data(field, f) self.assertEqual(result['name'], ['2']) self.assertEqual(result['default'], ['2']) self.assertEqual(result['mimetype'], ['3'])
def makeSchemaFile(context, key): """ Factory to make SchemaFile objects for formish file store""" data = getattr(context, key) file_data = StringIO(data) file_name = context.filename mime_type = context.mimetype store = MemcacheFileStore(None, context, None) store.put(context.filename, StringIO(data), uuid.uuid4().hex, {'Content-Type': mime_type}) return SchemaFile(file_data, file_name, mime_type)
def from_request_data(self, field, request_data): """ Creates a File object if possible """ # XXX We could add a file converter that converts this to a string data? if request_data['name'] == ['']: return None elif request_data['name'] == request_data['default']: return SchemaFile( None, request_data.get('filename', [None])[0], None, {'old_name': list(i for i in request_data['name'])}) else: key = util.decode_file_resource_path(request_data['name'][0])[1] try: cache_tag, headers, f = self.filestore.get(key) except KeyError: return None headers = dict(headers) return SchemaFile(f, headers['Filename'], headers['Content-Type'])
def form_defaults(self): attachments = [SchemaFile(None, x.__name__, x.mimetype) for x in self.context['attachments'].values()] defaults = { 'title':self.context.title, 'tags':[], # initial values supplied by widget 'text':self.context.text, 'attachments':attachments, } if self.workflow is not None: defaults['security_state'] = self.workflow.state_of(self.context) return defaults
def form_defaults(self): context = self.context attachments = [ SchemaFile(None, x.__name__, x.mimetype) for x in context['attachments'].values() ] defaults = { 'title': context.title, 'tags': [], # initial values supplied by widget 'text': context.text, 'attachments': attachments, } return defaults
def form_defaults(self): context = self.context attachments = [ SchemaFile(None, x.__name__, x.mimetype) for x in context['attachments'].values() ] defaults = { 'title': context.title, 'tags': [], # initial values supplied by widget 'text': context.text, 'attachments': attachments, 'photo': self.photo, 'caption': context.caption, 'publication_date': context.publication_date, } return defaults
def test_handle_submit(self): from schemaish.type import File as SchemaFile from karl.models.interfaces import IObjectModifiedEvent from zope.interface import Interface from karl.models.interfaces import ITagQuery from karl.content.interfaces import IBlogEntry from repoze.lemonade.testing import registerContentFactory from karl.testing import DummyCatalog karl.testing.registerAdapter(DummyTagQuery, (Interface, Interface), ITagQuery) self._registerDummyWorkflow() context = DummyBlogEntry() context.sessions = DummySessions() context.__name__ = 'ablogentry' context.catalog = DummyCatalog() context['attachments'] = testing.DummyModel() from karl.models.interfaces import ISite from zope.interface import directlyProvides directlyProvides(context, ISite) converted = { 'title': 'foo', 'text': 'text', 'security_state': 'public', 'tags': 'thetesttag', 'attachments': [SchemaFile(None, None, None)], } registerContentFactory(DummyBlogEntry, IBlogEntry) L = karl.testing.registerEventListener( (Interface, IObjectModifiedEvent)) karl.testing.registerDummySecurityPolicy('testeditor') request = self._makeRequest() controller = self._makeOne(context, request) response = controller.handle_submit(converted) self.assertEqual(response.location, 'http://example.com/ablogentry/') self.assertEqual(len(L), 2) self.assertEqual(context.title, 'foo') self.assertEqual(context.text, 'text') self.assertEqual(context.modified_by, 'testeditor')