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_edit(self, title, content_type, precondition='', filedata=None, REQUEST=None): """ Changes the title and content type attributes of the File or Image. """ if self.wl_isLocked(): raise ResourceLockedError("File is locked.") self.title = str(title) self.content_type = str(content_type) if precondition: self.precondition = str(precondition) elif self.precondition: del self.precondition if filedata is not None: if isinstance(filedata, text_type): filedata = filedata.encode(self._get_encoding()) self.update_data(filedata, content_type, len(filedata)) else: self.ZCacheable_invalidate() notify(ObjectModifiedEvent(self)) if REQUEST: message = "Saved changes." return self.manage_main(self, REQUEST, manage_tabs_message=message)
def manage_cutObjects(self, ids=None, REQUEST=None): """Put a reference to the objects named in ids in the clip board""" if ids is None and REQUEST is not None: raise BadRequest('No items specified') elif ids is None: raise ValueError('ids must be specified') if isinstance(ids, str): ids = [ids] oblist = [] for id in ids: ob = self._getOb(id) if ob.wl_isLocked(): raise ResourceLockedError('Object "%s" is locked' % ob.getId()) if not ob.cb_isMoveable(): raise CopyError('Not Supported') m = Moniker(ob) oblist.append(m.dump()) cp = (1, oblist) cp = _cb_encode(cp) if REQUEST is not None: resp = REQUEST['RESPONSE'] resp.setCookie('__cp', cp, path='%s' % cookie_path(REQUEST)) REQUEST['__cp'] = cp return self.manage_main(self, REQUEST) return cp
def manage_delObjects(self, ids=[], REQUEST=None): """Delete a subordinate object The objects specified in 'ids' get deleted. """ if isinstance(ids, string_types): ids = [ids] if not ids: raise BadRequest('No items specified') try: p = self._reserved_names except Exception: p = () for n in ids: if n in p: raise BadRequest('Not Deletable') while ids: id = ids[-1] v = self._getOb(id, self) try: if v.wl_isLocked(): raise ResourceLockedError('Object "%s" is locked.' % v.getId()) except AttributeError: pass if v is self: raise BadRequest('%s does not exist' % escape(ids[-1], True)) self._delObject(id) del ids[-1] if REQUEST is not None: return self.manage_main(self, REQUEST)
def manage_edit(self, data, title, SUBMIT='Change', REQUEST=None): """ Replace contents with 'data', title with 'title'. """ self._validateProxy(REQUEST) if self.wl_isLocked(): raise ResourceLockedError(self._locked_error_text) self.title = str(title) if isinstance(data, TaintedString): data = data.quoted() if hasattr(data, 'read'): data = data.read() try: self.munge(data) except ParseError as e: if REQUEST: return self.manage_main(self, REQUEST, manage_tabs_message=e, manage_tabs_type='warning') else: raise self.ZCacheable_invalidate() if REQUEST: message = "Saved changes." return self.manage_main(self, REQUEST, manage_tabs_message=message)
def manage_upload(self, file='', REQUEST=None): """ Replace the contents of the document with the text in 'file'. Store `file` as a native `str`. """ self._validateProxy(REQUEST) if self.wl_isLocked(): if REQUEST is not None: return self.manage_main( self, REQUEST, manage_tabs_message=self._locked_error_text, manage_tabs_type='warning') raise ResourceLockedError(self._locked_error_text) if REQUEST is not None and not file: return self.manage_main(self, REQUEST, manage_tabs_message='No file specified', manage_tabs_type='warning') if hasattr(file, 'read'): file = file.read() if PY3 and isinstance(file, binary_type): file = file.decode('utf-8') if PY2 and isinstance(file, text_type): file = file.encode('utf-8') self.munge(file) self.ZCacheable_invalidate() if REQUEST is not None: message = "Content uploaded." return self.manage_main(self, REQUEST, manage_tabs_message=message)
def manage_upload(self, file='', REQUEST=None): """ Replace the contents of the document with the text in 'file'. Store `file` as a native `str`. """ self._validateProxy(REQUEST) if self.wl_isLocked(): if REQUEST is not None: return self.manage_main( self, REQUEST, manage_tabs_message=self._locked_error_text, manage_tabs_type='warning') raise ResourceLockedError(self._locked_error_text) if REQUEST is not None and not file: return self.manage_main(self, REQUEST, manage_tabs_message='No file specified', manage_tabs_type='warning') self.munge(safe_file_data(file)) self.ZCacheable_invalidate() if REQUEST is not None: message = "Content uploaded." return self.manage_main(self, REQUEST, manage_tabs_message=message)
def ZPythonScript_edit(self, params, body): self._validateProxy() if self.wl_isLocked(): raise ResourceLockedError("The script is locked via WebDAV.") if not isinstance(body, str): body = body.read() if self._params != params or self._body != body or self._v_change: self._params = str(params) self.write(body)
def manage_renameObject(self, id, new_id, REQUEST=None): """Rename a particular sub-object. """ try: self._checkId(new_id) except Exception: raise CopyError('Invalid Id') ob = self._getOb(id) if ob.wl_isLocked(): raise ResourceLockedError('Object "%s" is locked' % ob.getId()) if not ob.cb_isMoveable(): raise CopyError('Not Supported') self._verifyObjectPaste(ob) try: ob._notifyOfCopyTo(self, op=1) except ConflictError: raise except Exception: raise CopyError('Rename Error') notify(ObjectWillBeMovedEvent(ob, self, id, self, new_id)) try: self._delObject(id, suppress_events=True) except TypeError: self._delObject(id) warnings.warn( "%s._delObject without suppress_events is discouraged." % self.__class__.__name__, DeprecationWarning) ob = aq_base(ob) ob._setId(new_id) # Note - because a rename always keeps the same context, we # can just leave the ownership info unchanged. try: self._setObject(new_id, ob, set_owner=0, suppress_events=True) except TypeError: self._setObject(new_id, ob, set_owner=0) warnings.warn( "%s._setObject without suppress_events is discouraged." % self.__class__.__name__, DeprecationWarning) ob = self._getOb(new_id) notify(ObjectMovedEvent(ob, self, id, self, new_id)) notifyContainerModified(self) ob._postCopy(self, op=1) if REQUEST is not None: return self.manage_main(self, REQUEST)
def ZPythonScriptHTML_upload(self, REQUEST, file=''): """Replace the body of the script with the text in file.""" if self.wl_isLocked(): raise ResourceLockedError("The script is locked via WebDAV.") if not isinstance(file, str): if not file: raise ValueError('File not specified') file = file.read() self.write(file) message = 'Saved changes.' return self.ZPythonScriptHTML_editForm(self, REQUEST, manage_tabs_message=message)
def manage_edit(self, data, title, SUBMIT='Change', REQUEST=None): """ Replace contents with 'data', title with 'title'. """ self._validateProxy(REQUEST) if self.wl_isLocked(): raise ResourceLockedError('This item is locked.') self.title = str(title) if isinstance(data, TaintedString): data = data.quoted() if not isinstance(data, basestring): data = data.read() self.munge(data) self.ZCacheable_invalidate() if REQUEST: message = "Saved changes." return self.manage_main(self, REQUEST, manage_tabs_message=message)
def manage_upload(self, file='', REQUEST=None): """ Replace the contents of the document with the text in 'file'. """ self._validateProxy(REQUEST) if self.wl_isLocked(): raise ResourceLockedError('This DTML Method is locked.') if not isinstance(file, str): if REQUEST and not file: raise ValueError('No file specified') file = file.read() self.munge(file) self.ZCacheable_invalidate() if REQUEST: message = "Saved changes." return self.manage_main(self, REQUEST, manage_tabs_message=message)
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 manage_upload(self, file='', REQUEST=None): """ Replaces the current contents of the File or Image object with file. The file or images contents are replaced with the contents of 'file'. """ if self.wl_isLocked(): raise ResourceLockedError("File is locked.") data, size = self._read_data(file) content_type = self._get_content_type(file, data, self.__name__, 'application/octet-stream') self.update_data(data, content_type, size) notify(ObjectModifiedEvent(self)) if REQUEST: message = "Saved changes." return self.manage_main(self, REQUEST, manage_tabs_message=message)
def pt_editAction(self, REQUEST, title, text, content_type, expand=0): """Change the title and document.""" if self.wl_isLocked(): raise ResourceLockedError("File is locked.") self.expand = expand # The ZMI edit view uses utf-8! So we can safely assume # that 'title' and 'text' are utf-8 encoded strings - hopefully self.pt_setTitle(title, 'utf-8') self.pt_edit(text, content_type, True) REQUEST.set('text', self.read()) # May not equal 'text'! REQUEST.set('title', self.title) message = "Saved changes." if getattr(self, '_v_warnings', None): message = ("<strong>Warning:</strong> <i>%s</i>" % '<br>'.join(self._v_warnings)) return self.pt_editForm(manage_tabs_message=message)
def manage_upload(self, file='', REQUEST=None): """ Replace the contents of the document with the text in 'file'. """ self._validateProxy(REQUEST) if self.wl_isLocked(): raise ResourceLockedError('This document has been locked.') if REQUEST and not file: raise ValueError('No file specified') if hasattr(file, 'read'): file = file.read() if PY3 and isinstance(file, binary_type): file = file.decode('utf-8') if PY2 and isinstance(file, text_type): file = file.encode('utf-8') self.munge(file) self.ZCacheable_invalidate() if REQUEST: message = "Content uploaded." return self.manage_main(self, REQUEST, manage_tabs_message=message)
def apply(self, obj, creator=None, depth='infinity', token=None, result=None, url=None, top=1): """ Apply, built for recursion (so that we may lock subitems of a collection if requested """ if result is None: result = StringIO() url = urlfix(self.request['URL'], 'LOCK') url = urlbase(url) iscol = isDavCollection(obj) if iscol and url[-1] != '/': url = url + '/' errmsg = None exc_ob = None lock = None try: lock = LockItem(creator, self.owner, depth, self.timeout, self.type, self.scope, token) if token is None: token = lock.getLockToken() except ValueError: errmsg = "412 Precondition Failed" exc_ob = HTTPPreconditionFailed() except Exception: errmsg = "403 Forbidden" exc_ob = Forbidden() try: if not IWriteLock.providedBy(obj): if top: # This is the top level object in the apply, so we # do want an error errmsg = "405 Method Not Allowed" exc_ob = MethodNotAllowed() else: # We're in an infinity request and a subobject does # not support locking, so we'll just pass pass elif obj.wl_isLocked(): errmsg = "423 Locked" exc_ob = ResourceLockedError() else: method = getattr(obj, 'wl_setLock') vld = getSecurityManager().validate(None, obj, 'wl_setLock', method) if vld and token and (lock is not None): obj.wl_setLock(token, lock) else: errmsg = "403 Forbidden" exc_ob = Forbidden() except Exception: errmsg = "403 Forbidden" exc_ob = Forbidden() if errmsg: if top and ((depth in (0, '0')) or (not iscol)): # We don't need to raise multistatus errors raise exc_ob elif not result.getvalue(): # We haven't had any errors yet, so our result is empty # and we need to set up the XML header result.write('<?xml version="1.0" encoding="utf-8" ?>\n' '<d:multistatus xmlns:d="DAV:">\n') result.write('<d:response>\n <d:href>%s</d:href>\n' % url) result.write(' <d:status>HTTP/1.1 %s</d:status>\n' % errmsg) result.write('</d:response>\n') if depth == 'infinity' and iscol: for ob in obj.objectValues(): if hasattr(obj, '__dav_resource__'): uri = urljoin(url, absattr(ob.getId())) self.apply(ob, creator, depth, token, result, uri, top=0) if not top: return token, result if result.getvalue(): # One or more subitems probably failed, so close the multistatus # element and clear out all succesful locks result.write('</d:multistatus>') transaction.abort() # This *SHOULD* clear all succesful locks return token, result.getvalue()