From ba580db6b07d6fcb4877259f13052ddee34afb90 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 27 Sep 2022 11:03:55 +0200 Subject: [proxy] make some (possibly confusing) web UI parts only display to advanced users --- src/hydrilla/proxy/state.py | 2 + src/hydrilla/proxy/state_impl/concrete_state.py | 102 +++++++++++---------- src/hydrilla/proxy/tables.sql | 3 + src/hydrilla/proxy/web_ui/items.py | 1 - src/hydrilla/proxy/web_ui/options.py | 10 +- src/hydrilla/proxy/web_ui/root.py | 6 +- .../proxy/web_ui/templates/base.html.jinja | 21 +++-- .../templates/items/item_viewversion.html.jinja | 91 +++++++++--------- .../templates/items/library_viewversion.html.jinja | 32 +++---- .../templates/items/package_viewversion.html.jinja | 38 +++----- .../proxy/web_ui/templates/options.html.jinja | 26 ++++++ .../web_ui/templates/repos/show_single.html.jinja | 21 +++-- 12 files changed, 193 insertions(+), 160 deletions(-) (limited to 'src/hydrilla/proxy') diff --git a/src/hydrilla/proxy/state.py b/src/hydrilla/proxy/state.py index ae80dfb..a94cd75 100644 --- a/src/hydrilla/proxy/state.py +++ b/src/hydrilla/proxy/state.py @@ -468,6 +468,7 @@ class HaketiloGlobalSettings: """....""" mapping_use_mode: MappingUseMode default_allow_scripts: bool + advanced_user: bool repo_refresh_seconds: int @@ -526,6 +527,7 @@ class HaketiloState(ABC): *, mapping_use_mode: t.Optional[MappingUseMode] = None, default_allow_scripts: t.Optional[bool] = None, + advanced_user: t.Optional[bool] = None, repo_refresh_seconds: t.Optional[int] = None ) -> None: """....""" diff --git a/src/hydrilla/proxy/state_impl/concrete_state.py b/src/hydrilla/proxy/state_impl/concrete_state.py index d08a821..04bb67f 100644 --- a/src/hydrilla/proxy/state_impl/concrete_state.py +++ b/src/hydrilla/proxy/state_impl/concrete_state.py @@ -58,21 +58,67 @@ from . import _operations here = Path(__file__).resolve().parent +def _prepare_database(connection: sqlite3.Connection) -> None: + cursor = connection.cursor() + + try: + cursor.execute( + ''' + SELECT + COUNT(name) + FROM + sqlite_master + WHERE + name = 'general' AND type = 'table'; + ''' + ) + + (db_initialized,), = cursor.fetchall() + + if not db_initialized: + cursor.executescript((here.parent / 'tables.sql').read_text()) + else: + cursor.execute( + ''' + SELECT + haketilo_version + FROM + general; + ''' + ) + + (db_haketilo_version,) = cursor.fetchone() + if db_haketilo_version != '3.0b1': + raise HaketiloException(_('err.proxy.unknown_db_schema')) + + cursor.execute('PRAGMA FOREIGN_KEYS;') + if cursor.fetchall() == []: + raise HaketiloException(_('err.proxy.no_sqlite_foreign_keys')) + + cursor.execute('PRAGMA FOREIGN_KEYS=ON;') + finally: + cursor.close() + + def load_settings(cursor: sqlite3.Cursor) -> st.HaketiloGlobalSettings: cursor.execute( ''' SELECT - default_allow_scripts, repo_refresh_seconds, mapping_use_mode + default_allow_scripts, + advanced_user, + repo_refresh_seconds, + mapping_use_mode FROM general ''' ) - (default_allow_scripts, repo_refresh_seconds, + (default_allow_scripts, advanced_user, repo_refresh_seconds, mapping_use_mode), = cursor.fetchall() return st.HaketiloGlobalSettings( default_allow_scripts = default_allow_scripts, + advanced_user = advanced_user, repo_refresh_seconds = repo_refresh_seconds, mapping_use_mode = st.MappingUseMode(mapping_use_mode) ) @@ -80,55 +126,8 @@ def load_settings(cursor: sqlite3.Cursor) -> st.HaketiloGlobalSettings: @dc.dataclass class ConcreteHaketiloState(base.HaketiloStateWithFields): def __post_init__(self) -> None: - sqlite3.enable_callback_tracebacks(True) - - self._prepare_database() - self.rebuild_structures() - def _prepare_database(self) -> None: - """....""" - cursor = self.connection.cursor() - - try: - cursor.execute( - ''' - SELECT - COUNT(name) - FROM - sqlite_master - WHERE - name = 'general' AND type = 'table'; - ''' - ) - - (db_initialized,), = cursor.fetchall() - - if not db_initialized: - cursor.executescript((here.parent / 'tables.sql').read_text()) - - else: - cursor.execute( - ''' - SELECT - haketilo_version - FROM - general; - ''' - ) - - (db_haketilo_version,) = cursor.fetchone() - if db_haketilo_version != '3.0b1': - raise HaketiloException(_('err.proxy.unknown_db_schema')) - - cursor.execute('PRAGMA FOREIGN_KEYS;') - if cursor.fetchall() == []: - raise HaketiloException(_('err.proxy.no_sqlite_foreign_keys')) - - cursor.execute('PRAGMA FOREIGN_KEYS=ON;') - finally: - cursor.close() - def import_items(self, malcontent_path: Path, repo_id: int = 1) -> None: with self.cursor(transaction=(repo_id == 1)) as cursor: # This method without the repo_id argument exposed is part of the @@ -305,6 +304,7 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields): *, mapping_use_mode: t.Optional[st.MappingUseMode] = None, default_allow_scripts: t.Optional[bool] = None, + advanced_user: t.Optional[bool] = None, repo_refresh_seconds: t.Optional[int] = None ) -> None: with self.cursor(transaction=True) as cursor: @@ -315,6 +315,8 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields): set_opt('mapping_use_mode', mapping_use_mode.value) if default_allow_scripts is not None: set_opt('default_allow_scripts', default_allow_scripts) + if advanced_user is not None: + set_opt('advanced_user', advanced_user) if repo_refresh_seconds is not None: set_opt('repo_refresh_seconds', repo_refresh_seconds) @@ -328,6 +330,8 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields): check_same_thread = False ) + _prepare_database(connection) + global_settings = load_settings(connection.cursor()) return ConcreteHaketiloState( diff --git a/src/hydrilla/proxy/tables.sql b/src/hydrilla/proxy/tables.sql index 4465a05..6ceea25 100644 --- a/src/hydrilla/proxy/tables.sql +++ b/src/hydrilla/proxy/tables.sql @@ -30,6 +30,7 @@ BEGIN TRANSACTION; CREATE TABLE general( haketilo_version VARCHAR NOT NULL, default_allow_scripts BOOLEAN NOT NULL, + advanced_user BOOLEAN NOT NULL, repo_refresh_seconds INTEGER NOT NULL, -- "mapping_use_mode" determines whether current mode is AUTO, -- WHEN_ENABLED or QUESTION. @@ -44,6 +45,7 @@ INSERT INTO general( rowid, haketilo_version, default_allow_scripts, + advanced_user, repo_refresh_seconds, mapping_use_mode ) @@ -51,6 +53,7 @@ VALUES( 1, '3.0b1', FALSE, + FALSE, 24 * 60 * 60, 'Q' ); diff --git a/src/hydrilla/proxy/web_ui/items.py b/src/hydrilla/proxy/web_ui/items.py index ec394ee..6195107 100644 --- a/src/hydrilla/proxy/web_ui/items.py +++ b/src/hydrilla/proxy/web_ui/items.py @@ -177,7 +177,6 @@ def show_item_version( f'items/{item_type.alt_name}_viewversion.html.jinja', display_info = display_info, version_display_info = this_info, - settings = state.get_settings(), **errors ) return flask.make_response(html, 200) diff --git a/src/hydrilla/proxy/web_ui/options.py b/src/hydrilla/proxy/web_ui/options.py index 9be1ae0..7d59375 100644 --- a/src/hydrilla/proxy/web_ui/options.py +++ b/src/hydrilla/proxy/web_ui/options.py @@ -41,11 +41,7 @@ bp = flask.Blueprint('options', __package__) @bp.route('/options', methods=['GET']) def options(errors: t.Mapping[str, bool] = {}) -> werkzeug.Response: - html = flask.render_template( - 'options.html.jinja', - settings = _app.get_haketilo_state().get_settings(), - **errors - ) + html = flask.render_template('options.html.jinja', **errors) return flask.make_response(html, 200) @bp.route('/options', methods=['POST']) @@ -64,6 +60,10 @@ def options_post() -> werkzeug.Response: state.update_settings(default_allow_scripts=True) elif action == 'block_scripts': state.update_settings(default_allow_scripts=False) + elif action == 'user_make_advanced': + state.update_settings(advanced_user=True) + elif action == 'user_make_simple': + state.update_settings(advanced_user=False) else: raise ValueError() diff --git a/src/hydrilla/proxy/web_ui/root.py b/src/hydrilla/proxy/web_ui/root.py index ed030d2..21c491b 100644 --- a/src/hydrilla/proxy/web_ui/root.py +++ b/src/hydrilla/proxy/web_ui/root.py @@ -64,11 +64,14 @@ def authenticate_by_referrer() -> t.Optional[werkzeug.Response]: flask.abort(403) -def get_current_endpoint() -> t.Optional[str]: +def get_current_endpoint() -> str: endpoint = flask.request.endpoint assert endpoint is not None return endpoint +def get_settings() -> st.HaketiloGlobalSettings: + return _app.get_haketilo_state().get_settings() + class WebUIAppImpl(_app.WebUIApp): def __init__(self): @@ -87,6 +90,7 @@ class WebUIAppImpl(_app.WebUIApp): } self.jinja_env.globals['get_current_endpoint'] = get_current_endpoint + self.jinja_env.globals['get_settings'] = get_settings self.jinja_env.globals['EnabledStatus'] = st.EnabledStatus self.jinja_env.globals['FrozenStatus'] = st.FrozenStatus self.jinja_env.globals['InstalledStatus'] = st.InstalledStatus diff --git a/src/hydrilla/proxy/web_ui/templates/base.html.jinja b/src/hydrilla/proxy/web_ui/templates/base.html.jinja index ff9fd71..493398a 100644 --- a/src/hydrilla/proxy/web_ui/templates/base.html.jinja +++ b/src/hydrilla/proxy/web_ui/templates/base.html.jinja @@ -20,6 +20,8 @@ code in a proprietary work, I am not going to enforce this in court. -#} +{% set settings = get_settings() %} + {% macro button_row(buttons_data, common_fields={}) %}
{% for classes, text, extra_fields in buttons_data %} @@ -236,17 +238,20 @@ code in a proprietary work, I am not going to enforce this in court. {% set active_endpoint = get_current_endpoint() %} {% set navigation_bar = [ - ('home', _('web_ui.base.nav.home')), - ('options.options', _('web_ui.base.nav.options')), - ('rules.rules', _('web_ui.base.nav.rules')), - ('items.packages', _('web_ui.base.nav.packages')), - ('items.libraries', _('web_ui.base.nav.libraries')), - ('repos.repos', _('web_ui.base.nav.repos')), - ('items.load_from_disk', _('web_ui.base.nav.load')) + ('home', _('web_ui.base.nav.home'), false), + ('options.options', _('web_ui.base.nav.options'), false), + ('rules.rules', _('web_ui.base.nav.rules'), false), + ('items.packages', _('web_ui.base.nav.packages'), false), + ('items.libraries', _('web_ui.base.nav.libraries'), true), + ('repos.repos', _('web_ui.base.nav.repos'), false), + ('items.load_from_disk', _('web_ui.base.nav.load'), false) ] %}