def pt_upload(self, REQUEST, file='', encoding='utf-8'): """Replace the document with the text in file.""" if self.wl_isLocked(): raise ResourceLockedError("File is locked.") if not file: return self.pt_editForm(manage_tabs_message='No file specified', manage_tabs_type='warning') if hasattr(file, 'read'): text = file.read() filename = file.filename else: filename = None text = file if isinstance(text, binary_type): content_type = guess_type(filename, text) (text, source_encoding) = convertToUnicode(text, content_type, preferred_encodings) elif isinstance(text, text_type): content_type = guess_type(filename, text.encode('utf-8')) self.pt_edit(text, content_type) return self.pt_editForm(manage_tabs_message='Saved changes')
def manage_addPageTemplate(self, id, title='', text='', encoding='utf-8', submit=None, REQUEST=None, RESPONSE=None): "Add a Page Template with optional file content." filename = '' content_type = 'text/html' if REQUEST and REQUEST.has_key('file'): file = REQUEST['file'] filename = file.filename text = file.read() headers = getattr(file, 'headers', None) if headers and headers.has_key('content_type'): content_type = headers['content_type'] else: content_type = guess_type(filename, text) else: if hasattr(text, 'read'): filename = getattr(text, 'filename', '') headers = getattr(text, 'headers', None) text = text.read() if headers and headers.has_key('content_type'): content_type = headers['content_type'] else: content_type = guess_type(filename, text) # ensure that we pass unicode to the constructor to # avoid further hassles with pt_edit() if not isinstance(text, unicode): text = unicode(text, encoding) zpt = ZopePageTemplate(id, text, content_type, output_encoding=encoding) zpt.pt_setTitle(title, encoding) self._setObject(id, zpt) zpt = getattr(self, id) if RESPONSE: if submit == " Add and Edit ": RESPONSE.redirect(zpt.absolute_url() + '/pt_editForm') else: RESPONSE.redirect(self.absolute_url() + '/manage_main') else: return zpt
def PUT(self, REQUEST, RESPONSE): """ Handle HTTP PUT requests """ self.dav__init(REQUEST, RESPONSE) self.dav__simpleifhandler(REQUEST, RESPONSE, refresh=1) text = REQUEST.get('BODY', '') content_type = guess_type('', text) self.pt_edit(text, content_type) RESPONSE.setStatus(204) return RESPONSE
def pt_upload(self, REQUEST, file='', encoding='utf-8'): """Replace the document with the text in file.""" if self.wl_isLocked(): raise ResourceLockedError("File is locked.") if isinstance(file, str): filename = None text = file else: if not file: raise ValueError('File not specified') filename = file.filename text = file.read() content_type = guess_type(filename, text) self.pt_edit(text, content_type) return self.pt_editForm(manage_tabs_message='Saved changes')
def pt_upload(self, REQUEST, file='', encoding='utf-8'): """Replace the document with the text in file.""" if self.wl_isLocked(): raise ResourceLockedError("File is locked via WebDAV") if isinstance(file, str): filename = None text = file else: if not file: raise ValueError('File not specified') filename = file.filename text = file.read() content_type = guess_type(filename, text) # if not content_type in ('text/html', 'text/xml'): # raise ValueError('Unsupported mimetype: %s' % content_type) self.pt_edit(text, content_type) return self.pt_editForm(manage_tabs_message='Saved changes')
def _put(self, text): zpt = self._createZPT() content_type = guess_type('', text) zpt.pt_edit(text, content_type) return zpt