Store UI settings in flask session for guest user

pull/1539/merge
Ozzieisaacs 4 years ago
parent 6dfa171b4e
commit cc0b0196f4

@ -24,6 +24,7 @@ var $list = $("#list").isotope({
}); });
$("#desc").click(function() { $("#desc").click(function() {
var page = $(this).data("id");
$.ajax({ $.ajax({
method:"post", method:"post",
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
@ -39,6 +40,7 @@ $("#desc").click(function() {
}); });
$("#asc").click(function() { $("#asc").click(function() {
var page = $(this).data("id");
$.ajax({ $.ajax({
method:"post", method:"post",
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",

@ -23,7 +23,7 @@ import sys
import datetime import datetime
import itertools import itertools
import uuid import uuid
import json from flask import session as flask_session
from binascii import hexlify from binascii import hexlify
from flask import g from flask import g
@ -71,15 +71,16 @@ def get_sidebar_config(kwargs=None):
"visibility": constants.SIDEBAR_HOT, 'public': True, "page": "hot", "visibility": constants.SIDEBAR_HOT, 'public': True, "page": "hot",
"show_text": _('Show Hot Books'), "config_show": True}) "show_text": _('Show Hot Books'), "config_show": True})
sidebar.append({"glyph": "glyphicon-download", "text": _('Downloaded Books'), "link": 'web.books_list', sidebar.append({"glyph": "glyphicon-download", "text": _('Downloaded Books'), "link": 'web.books_list',
"id": "download", "visibility": constants.SIDEBAR_DOWNLOAD, 'public': True, "page": "download", "id": "download", "visibility": constants.SIDEBAR_DOWNLOAD, 'public': (not g.user.is_anonymous),
"show_text": _('Show Downloaded Books'), "config_show": True}) "page": "download", "show_text": _('Show Downloaded Books'),
"config_show": content})
sidebar.append( sidebar.append(
{"glyph": "glyphicon-star", "text": _('Top Rated Books'), "link": 'web.books_list', "id": "rated", {"glyph": "glyphicon-star", "text": _('Top Rated Books'), "link": 'web.books_list', "id": "rated",
"visibility": constants.SIDEBAR_BEST_RATED, 'public': True, "page": "rated", "visibility": constants.SIDEBAR_BEST_RATED, 'public': True, "page": "rated",
"show_text": _('Show Top Rated Books'), "config_show": True}) "show_text": _('Show Top Rated Books'), "config_show": True})
sidebar.append({"glyph": "glyphicon-eye-open", "text": _('Read Books'), "link": 'web.books_list', "id": "read", 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", "visibility": constants.SIDEBAR_READ_AND_UNREAD, 'public': (not g.user.is_anonymous),
"show_text": _('Show read and unread'), "config_show": content}) "page": "read", "show_text": _('Show read and unread'), "config_show": content})
sidebar.append( sidebar.append(
{"glyph": "glyphicon-eye-close", "text": _('Unread Books'), "link": 'web.books_list', "id": "unread", {"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", "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="") denied_column_value = Column(String, default="")
allowed_column_value = Column(String, default="") allowed_column_value = Column(String, default="")
remote_auth_token = relationship('RemoteAuthToken', backref='user', lazy='dynamic') remote_auth_token = relationship('RemoteAuthToken', backref='user', lazy='dynamic')
#series_view = Column(String(10), default="list")
view_settings = Column(JSON, default={}) view_settings = Column(JSON, default={})
@ -286,6 +286,9 @@ class Anonymous(AnonymousUserMixin, UserBase):
self.denied_column_value = data.denied_column_value self.denied_column_value = data.denied_column_value
self.allowed_column_value = data.allowed_column_value self.allowed_column_value = data.allowed_column_value
self.view_settings = data.view_settings self.view_settings = data.view_settings
# Initialize flask_session once
if 'view' not in flask_session:
flask_session['view']={}
def role_admin(self): def role_admin(self):
@ -303,6 +306,16 @@ class Anonymous(AnonymousUserMixin, UserBase):
def is_authenticated(self): def is_authenticated(self):
return False 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 # Baseclass representing Shelfs in calibre-web in app.db
class Shelf(Base): class Shelf(Base):

@ -234,9 +234,8 @@ def admin_required(f):
def unconfigured(f): def unconfigured(f):
""" """
Checks if current_user.role == 1 Checks if calibre-web instance is not configured
""" """
@wraps(f) @wraps(f)
def inner(*args, **kwargs): def inner(*args, **kwargs):
if not config.db_configured: if not config.db_configured:
@ -464,25 +463,15 @@ def toggle_archived(book_id):
@web.route("/ajax/view", methods=["POST"]) @web.route("/ajax/view", methods=["POST"])
@login_required @login_required_if_no_ano
def update_view(): def update_view():
to_save = request.get_json() to_save = request.get_json()
try: try:
for element in to_save: 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]: for param in to_save[element]:
current_user.view_settings[element][param] = to_save[element][param] current_user.set_view_property(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
except Exception as e: 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 "Invalid request", 400
return "1", 200 return "1", 200

Loading…
Cancel
Save