def _save_form_files(self, directory, path, form): filename = op.join(directory, secure_filename(form.upload.data.filename)) if op.exists(filename): flash(gettext('File "%(name)s" already exists.', name=filename), 'error') else: self.save_file(filename, form.upload.data) self.on_file_upload(directory, path, filename)
def rename(self): """ Rename view method """ form = self.name_form() path = form.path.data if path: base_path, full_path, path = self._normalize_path(path) return_url = self._get_dir_url('.index', op.dirname(path)) else: return redirect(self.get_url('.index')) if not self.can_rename: flash(gettext('Renaming is disabled.'), 'error') return redirect(return_url) if not self.is_accessible_path(path): flash(gettext('Permission denied.'), 'error') return redirect(self._get_dir_url('.index')) if not op.exists(full_path): flash(gettext('Path does not exist.'), 'error') return redirect(return_url) if self.validate_form(form): try: dir_base = op.dirname(full_path) filename = secure_filename(form.name.data) os.rename(full_path, op.join(dir_base, filename)) self.on_rename(full_path, dir_base, filename) flash(gettext('Successfully renamed "%(src)s" to "%(dst)s"', src=op.basename(path), dst=filename)) except Exception as ex: flash(gettext('Failed to rename: %(error)s', error=ex), 'error') return redirect(return_url) else: helpers.flash_errors(form, message='Failed to rename: %(error)s') return self.render(self.rename_template, form=form, path=op.dirname(path), name=op.basename(path), dir_url=return_url)