From cc0b0196f4d7fc38d07002ae65a03f897f405323 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sun, 27 Sep 2020 19:12:10 +0200 Subject: [PATCH] Store UI settings in flask session for guest user --- cps/static/js/filter_grid.js | 2 ++ cps/ub.py | 25 +++++++++++++++++++------ cps/web.py | 19 ++++--------------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/cps/static/js/filter_grid.js b/cps/static/js/filter_grid.js index cb826f34..362c6bfa 100644 --- a/cps/static/js/filter_grid.js +++ b/cps/static/js/filter_grid.js @@ -24,6 +24,7 @@ var $list = $("#list").isotope({ }); $("#desc").click(function() { + var page = $(this).data("id"); $.ajax({ method:"post", contentType: "application/json; charset=utf-8", @@ -39,6 +40,7 @@ $("#desc").click(function() { }); $("#asc").click(function() { + var page = $(this).data("id"); $.ajax({ method:"post", contentType: "application/json; charset=utf-8", diff --git a/cps/ub.py b/cps/ub.py index 148bdee4..06c65f38 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -23,7 +23,7 @@ import sys import datetime import itertools import uuid -import json +from flask import session as flask_session from binascii import hexlify from flask import g @@ -71,15 +71,16 @@ def get_sidebar_config(kwargs=None): "visibility": constants.SIDEBAR_HOT, 'public': True, "page": "hot", "show_text": _('Show Hot Books'), "config_show": True}) sidebar.append({"glyph": "glyphicon-download", "text": _('Downloaded Books'), "link": 'web.books_list', - "id": "download", "visibility": constants.SIDEBAR_DOWNLOAD, 'public': True, "page": "download", - "show_text": _('Show Downloaded Books'), "config_show": True}) + "id": "download", "visibility": constants.SIDEBAR_DOWNLOAD, 'public': (not g.user.is_anonymous), + "page": "download", "show_text": _('Show Downloaded Books'), + "config_show": content}) sidebar.append( {"glyph": "glyphicon-star", "text": _('Top Rated Books'), "link": 'web.books_list', "id": "rated", "visibility": constants.SIDEBAR_BEST_RATED, 'public': True, "page": "rated", "show_text": _('Show Top Rated Books'), "config_show": True}) sidebar.append({"glyph": "glyphicon-eye-open", "text": _('Read Books'), "link": 'web.books_list', "id": "read", - "visibility": constants.SIDEBAR_READ_AND_UNREAD, 'public': (not g.user.is_anonymous), "page": "read", - "show_text": _('Show read and unread'), "config_show": content}) + "visibility": constants.SIDEBAR_READ_AND_UNREAD, 'public': (not g.user.is_anonymous), + "page": "read", "show_text": _('Show read and unread'), "config_show": content}) sidebar.append( {"glyph": "glyphicon-eye-close", "text": _('Unread Books'), "link": 'web.books_list', "id": "unread", "visibility": constants.SIDEBAR_READ_AND_UNREAD, 'public': (not g.user.is_anonymous), "page": "unread", @@ -242,7 +243,6 @@ class User(UserBase, Base): denied_column_value = Column(String, default="") allowed_column_value = Column(String, default="") remote_auth_token = relationship('RemoteAuthToken', backref='user', lazy='dynamic') - #series_view = Column(String(10), default="list") view_settings = Column(JSON, default={}) @@ -286,6 +286,9 @@ 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 + # Initialize flask_session once + if 'view' not in flask_session: + flask_session['view']={} def role_admin(self): @@ -303,6 +306,16 @@ class Anonymous(AnonymousUserMixin, UserBase): def is_authenticated(self): return False + def get_view_property(self, page, prop): + if not flask_session['view'].get(page): + return None + return flask_session['view'][page].get(prop) + + def set_view_property(self, page, prop, value): + if not flask_session['view'].get(page): + flask_session['view'][page] = dict() + flask_session['view'][page][prop] = value + # Baseclass representing Shelfs in calibre-web in app.db class Shelf(Base): diff --git a/cps/web.py b/cps/web.py index 94371dfb..38031cc0 100644 --- a/cps/web.py +++ b/cps/web.py @@ -234,9 +234,8 @@ def admin_required(f): def unconfigured(f): """ - Checks if current_user.role == 1 + Checks if calibre-web instance is not configured """ - @wraps(f) def inner(*args, **kwargs): if not config.db_configured: @@ -464,25 +463,15 @@ def toggle_archived(book_id): @web.route("/ajax/view", methods=["POST"]) -@login_required +@login_required_if_no_ano def update_view(): to_save = request.get_json() try: for element in to_save: - if not current_user.view_settings.get(element): - current_user.view_settings[element]=dict() for param in to_save[element]: - current_user.view_settings[element][param] = to_save[element][param] - try: - flag_modified(current_user, "view_settings") - except AttributeError: - pass - ub.session.commit() - except InvalidRequestError: - log.error("Invalid request received: %r ", request, ) - return "Invalid request", 400 + current_user.set_view_property(element, param, to_save[element][param]) except Exception as e: - log.error("Could not save series_view_settings: %r %r", request, to_save) + log.error("Could not save view_settings: %r %r: e", request, to_save, e) return "Invalid request", 400 return "1", 200