1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-28 12:30:00 +00:00

Store UI settings in flask session for guest user

This commit is contained in:
Ozzieisaacs 2020-09-27 19:12:10 +02:00
parent 6dfa171b4e
commit cc0b0196f4
3 changed files with 25 additions and 21 deletions

View File

@ -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",

View File

@ -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):

View File

@ -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