From d284abb0741354546e3155ff241c0659ce669586 Mon Sep 17 00:00:00 2001 From: Mustafa Baser Date: Wed, 5 Jun 2024 11:59:26 +0300 Subject: [PATCH] fix(jans-cli-tui): asset management changes: Jans Service and asset types Signed-off-by: Mustafa Baser --- jans-cli-tui/cli_tui/plugins/020_fido/main.py | 2 +- .../cli_tui/plugins/130_assets/main.py | 56 ++++++++++++++----- .../wui_components/jans_path_browser.py | 12 ++-- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/jans-cli-tui/cli_tui/plugins/020_fido/main.py b/jans-cli-tui/cli_tui/plugins/020_fido/main.py index 71e44d27a7b..07f8fb10e7a 100755 --- a/jans-cli-tui/cli_tui/plugins/020_fido/main.py +++ b/jans-cli-tui/cli_tui/plugins/020_fido/main.py @@ -87,7 +87,7 @@ async def coroutine(): if result.lower() == 'yes': self.requested_parties_container.remove_item(kwargs['selected']) self.app.stop_progressing() - + return result asyncio.ensure_future(coroutine()) diff --git a/jans-cli-tui/cli_tui/plugins/130_assets/main.py b/jans-cli-tui/cli_tui/plugins/130_assets/main.py index d602b589a70..ba62076bcf4 100644 --- a/jans-cli-tui/cli_tui/plugins/130_assets/main.py +++ b/jans-cli-tui/cli_tui/plugins/130_assets/main.py @@ -22,10 +22,10 @@ from wui_components.jans_vetrical_nav import JansVerticalNav from wui_components.jans_cli_dialog import JansGDialog from wui_components.jans_spinner import Spinner +from wui_components.jans_drop_down import DropDownWidget from wui_components.jans_path_browser import jans_file_browser_dialog, BrowseType - class Plugin(DialogUtils): """This is a general class for plugins """ @@ -62,8 +62,10 @@ def init_plugin(self) -> None: """The initialization for this plugin """ + self.app.create_background_task(self.get_initial_data()) self.app.create_background_task(self.get_assets()) + def set_center_frame(self) -> None: """center frame content """ @@ -113,9 +115,7 @@ def save_asset(dialog: Dialog) -> None: self.app.show_message(common_strings.error, HTML(_("Please fill Description and Display Name")), tobefocused=dialog) return - for prop in data['jansModuleProperty'][:]: - if not prop: - data['jansModuleProperty'].remove(prop) + data['jansService'] = [data.pop('jansService')] data.pop('document', None) form_data = {'assetFile': self.asset_file_path, 'document': data} @@ -144,6 +144,14 @@ async def coroutine(): def display_file_browser_dialog(): + file_browser_dialog = jans_file_browser_dialog( + common_data.app, + path=common_data.app.browse_path, + browse_type=BrowseType.file, + ok_handler=read_asset, + extensions=[f'.{e}' for e in common_data.asset_types] + ) + common_data.app.show_jans_dialog(file_browser_dialog) @@ -165,9 +173,18 @@ def read_asset(path): ) enabled_widget = common_data.app.getTitledCheckBox(_("Enabled"), name='jansEnabled', checked=data.get('jansEnabled'), style=cli_style.check_box) description_widget = common_data.app.getTitledText(_("Description"), name='description', value=data.get('description', ''), style=cli_style.edit_text_required) - #selected_widget = common_data.app.getTitledCheckBox(_("Selected"), name='selected', checked=data.get('selected'), style=cli_style.check_box) - jans_module_widget = common_data.app.getTitledText(_("Jans Module Properties"), name='jansModuleProperty', value='\n'.join(data.get('jansModuleProperty', [])), height=3, jans_list_type=True, style=cli_style.edit_text, jans_help=_("Enter property each line")) - #document_widget = common_data.app.getTitledText(_("Document"), name='document', height=3, value=data.get('document', ''), style=cli_style.edit_text) + + jans_serice_widget = self.app.getTitledWidget( + _("Jans Service"), + name='jansService', + widget=DropDownWidget( + values=[(s,s) for s in common_data.asset_services], + value=data.get('jansService', common_data.asset_services)[0], + select_one_option=False + ), + jans_help=_("Select Jans Service"), + style=cli_style.edit_text + ) save_button = Button(_("Save"), handler=save_asset) save_button.keep_dialog = True @@ -186,17 +203,12 @@ def read_asset(path): description_widget, jans_level_widget, enabled_widget, - #selected_widget, - jans_module_widget, - #document_widget, + jans_serice_widget, ], width=D()) edit_asset_dialog = JansGDialog(common_data.app, title=title, body=body, buttons=buttons, width=common_data.app.dialog_width) - file_browser_dialog = jans_file_browser_dialog(common_data.app, path=common_data.app.browse_path, browse_type=BrowseType.file, ok_handler=read_asset) - - common_data.app.show_jans_dialog(edit_asset_dialog) def delete_asset(self, **kwargs: Any) -> None: @@ -241,7 +253,7 @@ async def get_assets(self, pattern='') -> None: self.app.start_progressing(_("Retreiving assets from server...")) response = await get_event_loop().run_in_executor(self.app.executor, self.app.cli_requests, cli_args) self.app.stop_progressing() - + try: result = response.json() except Exception as e: @@ -266,3 +278,19 @@ async def get_assets(self, pattern='') -> None: self.assets_list_box.add_item((asset_info['inum'], asset_info['displayName'], asset_info['jansEnabled'], asset_info['creationDate'])) self.assets_container = self.assets_list_box + + + async def get_initial_data(self) -> None: + if not hasattr(common_data, 'asset_services'): + response = await get_event_loop().run_in_executor(common_data.app.executor, common_data.app.cli_requests, {'operation_id': 'get-asset-services'}) + try: + common_data.asset_services = response.json() + except Exception as e: + self.app.logger.error(f"Fail to get asset services: {e}") + + if not hasattr(common_data, 'asset_types'): + response = await get_event_loop().run_in_executor(common_data.app.executor, common_data.app.cli_requests, {'operation_id': 'get-asset-types'}) + try: + common_data.asset_types = response.json() + except Exception as e: + self.app.logger.error(f"Fail to get asset types: {e}") diff --git a/jans-cli-tui/cli_tui/wui_components/jans_path_browser.py b/jans-cli-tui/cli_tui/wui_components/jans_path_browser.py index 0ad6456a927..42c1b758b2d 100644 --- a/jans-cli-tui/cli_tui/wui_components/jans_path_browser.py +++ b/jans-cli-tui/cli_tui/wui_components/jans_path_browser.py @@ -35,7 +35,8 @@ def __init__(self, path: str, browse_type: BrowseType, height: int = 10, - hide_hidden_files: Optional[bool] = True + hide_hidden_files: Optional[bool] = True, + extensions: Optional[list] = [] ) -> None: """init for JansPathBrowserWidget""" @@ -44,7 +45,7 @@ def __init__(self, self.browse_type = browse_type self.height = height self.hide_hidden_files = hide_hidden_files - + self.extensions = extensions self.selected_line = 0 browsed_dir = Window(FormattedTextControl(lambda: self.path.as_posix())) path_selection_window = Window( @@ -108,6 +109,8 @@ def _update_path_content(self) -> None: dirs = [] for path_ in self.path.glob('*'): + if self.extensions and path_.is_file() and path_.suffix not in self.extensions: + continue if self.hide_hidden_files and path_.name.startswith('.'): continue if path_.is_dir(): @@ -171,7 +174,8 @@ def jans_file_browser_dialog( path: str = '/', browse_type: Optional[BrowseType] = BrowseType.save_as, ok_handler: Optional[Callable] = None, - hide_hidden_files: Optional[bool] = True + hide_hidden_files: Optional[bool] = True, + extensions: Optional[list] = [] ) -> JansGDialog: """Functo to create a Jans File Browser Dialog @@ -181,7 +185,7 @@ def jans_file_browser_dialog( ok_handler (collable, optional): Callable when OK button is pressed """ - browse_widget = JansPathBrowserWidget(path, browse_type, hide_hidden_files=hide_hidden_files) + browse_widget = JansPathBrowserWidget(path, browse_type, hide_hidden_files=hide_hidden_files, extensions=extensions) def call_ok_handler(dialog):