def test_asset_paths_as_key(self): """Tests the asset_paths.as_key function.""" self.assertEqual("both/delims", asset_paths.as_key(BOTH_DELIMS)) self.assertEqual("end/delim", asset_paths.as_key(END_DELIM)) self.assertEqual("start/delim", asset_paths.as_key(START_DELIM)) self.assertEqual("assets/img/abs_img_path.jpg", asset_paths.as_key(ABS_KEY)) self.assertEqual("assets/img/rel_img_path.jpg", asset_paths.as_key(REL_KEY))
def test_asset_paths_as_key(self): """Tests the asset_paths.as_key function.""" self.assertEqual( "both/delims", asset_paths.as_key(BOTH_DELIMS)) self.assertEqual( "end/delim", asset_paths.as_key(END_DELIM)) self.assertEqual( "start/delim", asset_paths.as_key(START_DELIM)) self.assertEqual( "assets/img/abs_img_path.jpg", asset_paths.as_key(ABS_KEY)) self.assertEqual( "assets/img/rel_img_path.jpg", asset_paths.as_key(REL_KEY))
def get_manage_asset(self): """Show an upload/delete dialog for assets.""" path = self.request.get('key') key = asset_paths.as_key(path) if not asset_paths.AllowedBases.is_path_allowed(path): raise ValueError('Cannot add/edit asset with key "%s" ' % key + 'which is not under a valid asset path') fs = self.app_context.fs.impl delete_url = None delete_method = None delete_message = None auto_return = False if fs.isfile(fs.physical_to_logical(key)): delete_url = self._get_delete_url(FilesItemRESTHandler.URI, key, 'delete-asset') delete_method = 'delete' else: # Sadly, since we don't know the name of the asset when we build # the form, the form can't update itself to show the uploaded # asset when the upload completes. Rather than continue to # show a blank form, bring the user back to the assets list. auto_return = True details = AssetItemRESTHandler.get_schema_details(path) json, ann = details.json, details.annotations from_action = self.request.get('from_action') exit_url = self.canonicalize_url( dashboard_utils.build_assets_url(from_action)) rest_url = self.canonicalize_url(AssetItemRESTHandler.URI) form_html = oeditor.ObjectEditor.get_html_for( self, json, ann, key, rest_url, exit_url, save_method='upload', save_button_caption='Upload', auto_return=auto_return, delete_url=delete_url, delete_method=delete_method, delete_message=delete_message, required_modules=AssetItemRESTHandler.REQUIRED_MODULES, additional_dirs=[ os.path.join(dashboard_utils.RESOURCES_DIR, 'js') ]) template_values = {} template_values['page_title'] = self.format_title('Manage Asset') template_values['main_content'] = form_html self.render_page(template_values, in_action=from_action)
def post(self): is_valid, payload, upload = self._validate_post() if is_valid: key = payload['key'] base = asset_paths.as_key(payload['base']) if key == base: # File name not given on setup; we are uploading a new file. filename = os.path.split(self.request.POST['file'].filename)[1] physical_path = os.path.join(base, filename) is_overwrite_allowed = False else: # File name already established on setup; use existing # file's name and uploaded file's data. physical_path = key is_overwrite_allowed = True self._handle_post(physical_path, is_overwrite_allowed, upload)
def get_manage_asset(self): """Show an upload/delete dialog for assets.""" path = self.request.get('key') key = asset_paths.as_key(path) if not asset_paths.AllowedBases.is_path_allowed(path): raise ValueError('Cannot add/edit asset with key "%s" ' % key + 'which is not under a valid asset path') fs = self.app_context.fs.impl delete_url = None delete_method = None delete_message = None auto_return = False if fs.isfile(fs.physical_to_logical(key)): delete_url = self._get_delete_url( FilesItemRESTHandler.URI, key, 'delete-asset') delete_method = 'delete' else: # Sadly, since we don't know the name of the asset when we build # the form, the form can't update itself to show the uploaded # asset when the upload completes. Rather than continue to # show a blank form, bring the user back to the assets list. auto_return = True details = AssetItemRESTHandler.get_schema_details(path) json, ann = details.json, details.annotations from_action = self.request.get('from_action') exit_url = self.canonicalize_url( dashboard_utils.build_assets_url(from_action)) rest_url = self.canonicalize_url(AssetItemRESTHandler.URI) form_html = oeditor.ObjectEditor.get_html_for( self, json, ann, key, rest_url, exit_url, save_method='upload', save_button_caption='Upload', auto_return=auto_return, delete_url=delete_url, delete_method=delete_method, delete_message=delete_message, required_modules=AssetItemRESTHandler.REQUIRED_MODULES, additional_dirs=[os.path.join(dashboard_utils.RESOURCES_DIR, 'js')]) template_values = {} template_values['page_title'] = self.format_title('Manage Asset') template_values['main_content'] = form_html self.render_page(template_values, in_action=from_action)