From d95838309efa1221980c47b5a9d0680537692ea2 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sat, 15 May 2021 10:45:51 +0200 Subject: [PATCH] Added config option for shelf sync behavior --- cps/admin.py | 10 +++++++++- cps/kobo.py | 4 ++-- cps/shelf.py | 6 +++--- cps/static/js/table.js | 9 ++++++++- cps/templates/shelf_edit.html | 10 +++------- cps/templates/user_edit.html | 7 ++++++- cps/templates/user_table.html | 28 +++++++++++++++++++++------- cps/ub.py | 17 +++++++++++++---- cps/web.py | 2 ++ 9 files changed, 67 insertions(+), 26 deletions(-) diff --git a/cps/admin.py b/cps/admin.py index 230672bf..3c3b2ee4 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -236,7 +236,7 @@ def edit_user_table(): custom_values = [] if not config.config_anonbrowse: allUser = allUser.filter(ub.User.role.op('&')(constants.ROLE_ANONYMOUS) != constants.ROLE_ANONYMOUS) - + kobo_support = feature_support['kobo'] and config.config_kobo_sync return render_title_template("user_table.html", users=allUser.all(), tags=tags, @@ -245,6 +245,7 @@ def edit_user_table(): languages=languages, visiblility=visibility, all_roles=constants.ALL_ROLES, + kobo_support=kobo_support, sidebar_settings=constants.sidebar_settings, title=_(u"Edit Users"), page="usertable") @@ -391,6 +392,8 @@ def edit_list_user(param): user.name = check_username(vals['value']) elif param =='email': user.email = check_email(vals['value']) + elif param =='kobo_only_shelves_sync': + user.kobo_only_shelves_sync = int(vals['value'] == 'true') elif param == 'kindle_mail': user.kindle_mail = valid_email(vals['value']) if vals['value'] else "" elif param.endswith('role'): @@ -558,6 +561,8 @@ def load_dialogtexts(element_id): texts["main"] = _('Are you sure you want to change the selected restrictions for the selected user(s)?') elif element_id == "sidebar_view": texts["main"] = _('Are you sure you want to change the selected visibility restrictions for the selected user(s)?') + elif element_id == "kobo_only_shelves_sync": + texts["main"] = _('Are you sure you want to change shelf sync behavior for the selected user(s)?') return json.dumps(texts) @@ -1314,6 +1319,7 @@ def _handle_new_user(to_save, content, languages, translations, kobo_support): content.denied_tags = config.config_denied_tags content.allowed_column_value = config.config_allowed_column_value content.denied_column_value = config.config_denied_column_value + content.kobo_only_shelves_sync = 0 # No default value for kobo sync shelf setting ub.session.add(content) ub.session.commit() flash(_(u"User '%(user)s' created", user=content.name), category="success") @@ -1381,6 +1387,8 @@ def _handle_edit_user(to_save, content, languages, translations, kobo_support): else: content.sidebar_view &= ~constants.DETAIL_RANDOM + content.kobo_only_shelves_sync = int(to_save.get("kobo_only_shelves_sync") == "on") or 0 + if to_save.get("default_language"): content.default_language = to_save["default_language"] if to_save.get("locale"): diff --git a/cps/kobo.py b/cps/kobo.py index c3bd82e5..9b022379 100644 --- a/cps/kobo.py +++ b/cps/kobo.py @@ -152,8 +152,8 @@ def HandleSyncRequest(): # in case of external changes (e.g: adding a book through Calibre). calibre_db.reconnect_db(config, ub.app_DB_path) - only_kobo_shelves = calibre_db.session.query(ub.Shelf).filter(ub.Shelf.user_id == current_user.id)\ - .filter(ub.Shelf.kobo_sync).count() > 0 + only_kobo_shelves = current_user.kobo_only_shelves_sync + # calibre_db.session.query(ub.Shelf).filter(ub.Shelf.user_id == current_user.id).filter(ub.Shelf.kobo_sync).count() > 0 if only_kobo_shelves: changed_entries = ( diff --git a/cps/shelf.py b/cps/shelf.py index 5da1eda3..431eeff8 100644 --- a/cps/shelf.py +++ b/cps/shelf.py @@ -240,8 +240,8 @@ def edit_shelf(shelf_id): # if shelf ID is set, we are editing a shelf def create_edit_shelf(shelf, title, page, shelf_id=False): - sync_all_books = calibre_db.session.query(ub.Shelf).filter(ub.Shelf.user_id == current_user.id)\ - .filter(ub.Shelf.kobo_sync).count() + sync_only_selected_shelves = current_user.kobo_only_shelves_sync + # calibre_db.session.query(ub.Shelf).filter(ub.Shelf.user_id == current_user.id).filter(ub.Shelf.kobo_sync).count() if request.method == "POST": to_save = request.form.to_dict() shelf.is_public = 1 if to_save.get("is_public") else 0 @@ -277,7 +277,7 @@ def create_edit_shelf(shelf, title, page, shelf_id=False): title=title, page=page, kobo_sync_enabled=config.config_kobo_sync, - sync_all_books=sync_all_books) + sync_only_selected_shelves=sync_only_selected_shelves) def check_shelf_is_unique(shelf, to_save, shelf_id=False): diff --git a/cps/static/js/table.js b/cps/static/js/table.js index 10f82d17..88694837 100644 --- a/cps/static/js/table.js +++ b/cps/static/js/table.js @@ -580,12 +580,19 @@ function singleUserFormatter(value, row) { return '' + this.buttontext + '' } -function checkboxFormatter(value, row, index){ +function checkboxFormatter(value, row){ if(value & this.column) return ''; else return ''; } +function singlecheckboxFormatter(value, row){ + if(value) + return ''; + else + return ''; +} + /* Do some hiding disabling after user list is loaded */ function loadSuccess() { diff --git a/cps/templates/shelf_edit.html b/cps/templates/shelf_edit.html index 6aec27ee..b3c81731 100644 --- a/cps/templates/shelf_edit.html +++ b/cps/templates/shelf_edit.html @@ -14,14 +14,10 @@ {% endif %} - {% if kobo_sync_enabled %} + {% if kobo_sync_enabled and sync_only_selected_shelves %}
-
{% endif %} diff --git a/cps/templates/user_edit.html b/cps/templates/user_edit.html index 846ad978..6eeb069c 100644 --- a/cps/templates/user_edit.html +++ b/cps/templates/user_edit.html @@ -66,7 +66,6 @@ {% endif %} -
{% for element in sidebar %} {% if element['config_show'] %} @@ -125,6 +124,12 @@
{% endif %} {% endif %} + {% if kobo_support and not content.role_anonymous() %} +
+ + +
+ {% endif %}
{{_('Save')}}
diff --git a/cps/templates/user_table.html b/cps/templates/user_table.html index f56279dd..598c6d3a 100644 --- a/cps/templates/user_table.html +++ b/cps/templates/user_table.html @@ -32,6 +32,21 @@ {%- endmacro %} +{% macro user_single_checkbox_row(parameter, show_text) -%} + +
+
+ {{_('Deny')}} +
+
+ {{_('Allow')}} +
+
+ {{show_text}} + +{%- endmacro %} + {% macro user_checkbox_row(parameter, array_field, show_text, element, value) -%}
- - {{_('Deny')}} - + {{_('Deny')}}
- - {{_('Allow')}} - + {{_('Allow')}}
{{show_text}} @@ -134,7 +145,10 @@ {{ user_checkbox_row("role", "viewer_role", _('View'), visiblility, all_roles)}} {{ user_checkbox_row("role", "edit_role", _('Edit'), visiblility, all_roles)}} {{ user_checkbox_row("role", "delete_role", _('Delete'), visiblility, all_roles)}} - {{ user_checkbox_row("role", "edit_shelf_role", _('Edit Public Shelfs'), visiblility, all_roles)}} + {{ user_checkbox_row("role", "edit_shelf_role", _('Edit Public Shelves'), visiblility, all_roles)}} + {% if kobo_support %} + {{ user_single_checkbox_row("kobo_only_shelves_sync", _('Sync Selected Shelves with Kobo'))}} + {% endif %} {{ user_checkbox_row("sidebar_view", "detail_random", _('Show Random Books in Detail View'), visiblility, sidebar_settings)}} {{ user_checkbox_row("sidebar_view", "sidebar_language", _('Show language selection'), visiblility, sidebar_settings)}} {{ user_checkbox_row("sidebar_view", "sidebar_read_and_unread", _('Show read/unread selection'), visiblility, sidebar_settings)}} diff --git a/cps/ub.py b/cps/ub.py index eb5090af..f473bb47 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -188,7 +188,7 @@ class User(UserBase, Base): allowed_column_value = Column(String, default="") remote_auth_token = relationship('RemoteAuthToken', backref='user', lazy='dynamic') view_settings = Column(JSON, default={}) - + kobo_only_shelves_sync = Column(Integer, default=1) if oauth_support: @@ -229,6 +229,7 @@ class Anonymous(AnonymousUserMixin, UserBase): self.denied_column_value = data.denied_column_value self.allowed_column_value = data.allowed_column_value self.view_settings = data.view_settings + self.kobo_only_shelves_sync = data.kobo_only_shelves_sync def role_admin(self): @@ -603,6 +604,13 @@ def migrate_Database(session): with engine.connect() as conn: conn.execute("ALTER TABLE user ADD column `view_settings` VARCHAR(10) DEFAULT '{}'") session.commit() + try: + session.query(exists().where(User.kobo_only_shelves_sync)).scalar() + except exc.OperationalError: + with engine.connect() as conn: + conn.execute("ALTER TABLE user ADD column `kobo_only_shelves_sync` SMALLINT DEFAULT 0") + session.commit() + try: # check if name is in User table instead of nickname session.query(exists().where(User.name)).scalar() @@ -622,15 +630,16 @@ def migrate_Database(session): "allowed_tags VARCHAR," "denied_column_value VARCHAR," "allowed_column_value VARCHAR," - "view_settings JSON," + "view_settings JSON," + "kobo_only_shelves_sync SMALLINT," "UNIQUE (name)," "UNIQUE (email))")) conn.execute(text("INSERT INTO user_id(id, name, email, role, password, kindle_mail,locale," "sidebar_view, default_language, denied_tags, allowed_tags, denied_column_value, " - "allowed_column_value, view_settings)" + "allowed_column_value, view_settings, kobo_only_shelves_sync)" "SELECT id, nickname, email, role, password, kindle_mail, locale," "sidebar_view, default_language, denied_tags, allowed_tags, denied_column_value, " - "allowed_column_value, view_settings FROM user")) + "allowed_column_value, view_settings, kobo_only_shelves_sync FROM user")) # delete old user table and rename new user_id table to user: conn.execute(text("DROP TABLE user")) conn.execute(text("ALTER TABLE user_id RENAME TO user")) diff --git a/cps/web.py b/cps/web.py index 9cffc931..7e5ebb75 100644 --- a/cps/web.py +++ b/cps/web.py @@ -1593,6 +1593,8 @@ def change_profile(kobo_support, local_oauth_check, oauth_status, translations, current_user.default_language = to_save["default_language"] if to_save.get("locale"): current_user.locale = to_save["locale"] + current_user.kobo_only_shelves_sync = int(to_save.get("kobo_only_shelves_sync") == "on") or 0 + except Exception as ex: flash(str(ex), category="error") return render_title_template("user_edit.html", content=current_user,