Added save user view settings

pull/1539/head
Ozzieisaacs 4 years ago
parent 3ff3431b17
commit df01022f49

@ -23,6 +23,7 @@ import sys
import datetime import datetime
import itertools import itertools
import uuid import uuid
import json
from binascii import hexlify from binascii import hexlify
from flask import g from flask import g
@ -41,7 +42,7 @@ except ImportError:
oauth_support = False oauth_support = False
from sqlalchemy import create_engine, exc, exists, event from sqlalchemy import create_engine, exc, exists, event
from sqlalchemy import Column, ForeignKey from sqlalchemy import Column, ForeignKey
from sqlalchemy import String, Integer, SmallInteger, Boolean, DateTime, Float from sqlalchemy import String, Integer, SmallInteger, Boolean, DateTime, Float, JSON
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship, sessionmaker, Session from sqlalchemy.orm import backref, relationship, sessionmaker, Session
from werkzeug.security import generate_password_hash from werkzeug.security import generate_password_hash
@ -218,8 +219,9 @@ 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") #series_view = Column(String(10), default="list")
view_settings = Column(String, default="list") view_settings = Column(JSON, default={})
if oauth_support: if oauth_support:
@ -260,8 +262,8 @@ class Anonymous(AnonymousUserMixin, UserBase):
self.allowed_tags = data.allowed_tags self.allowed_tags = data.allowed_tags
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.series_view = data.series_view # self.series_view = data.series_view
self.view_settings = data.view_settings
def role_admin(self): def role_admin(self):
return False return False
@ -568,16 +570,17 @@ def migrate_Database(session):
conn.execute("ALTER TABLE user ADD column `denied_column_value` DEFAULT ''") conn.execute("ALTER TABLE user ADD column `denied_column_value` DEFAULT ''")
conn.execute("ALTER TABLE user ADD column `allowed_column_value` DEFAULT ''") conn.execute("ALTER TABLE user ADD column `allowed_column_value` DEFAULT ''")
session.commit() session.commit()
try: #try:
session.query(exists().where(User.series_view)).scalar() # session.query(exists().where(User.series_view)).scalar()
except exc.OperationalError: #except exc.OperationalError:
conn = engine.connect() # conn = engine.connect()
conn.execute("ALTER TABLE user ADD column `series_view` VARCHAR(10) DEFAULT 'list'") # conn.execute("ALTER TABLE user ADD column `series_view` VARCHAR(10) DEFAULT 'list'")
try: try:
session.query(exists().where(User.view_settings)).scalar() session.query(exists().where(User.view_settings)).scalar()
except exc.OperationalError: except exc.OperationalError:
conn = engine.connect() conn = engine.connect()
conn.execute("ALTER TABLE user ADD column `view_settings` VARCHAR DEFAULT '{}'") conn.execute("ALTER TABLE user ADD column `view_settings` JSON default '{}'")
session.commit()
if session.query(User).filter(User.role.op('&')(constants.ROLE_ANONYMOUS) == constants.ROLE_ANONYMOUS).first() \ if session.query(User).filter(User.role.op('&')(constants.ROLE_ANONYMOUS) == constants.ROLE_ANONYMOUS).first() \
is None: is None:
@ -598,12 +601,13 @@ def migrate_Database(session):
"locale VARCHAR(2)," "locale VARCHAR(2),"
"sidebar_view INTEGER," "sidebar_view INTEGER,"
"default_language VARCHAR(3)," "default_language VARCHAR(3),"
"series_view VARCHAR(10)," # "series_view VARCHAR(10),"
"view_settings VARCHAR," "view_settings VARCHAR,"
"UNIQUE (nickname)," "UNIQUE (nickname),"
"UNIQUE (email))") "UNIQUE (email))")
conn.execute("INSERT INTO user_id(id, nickname, email, role, password, kindle_mail,locale," conn.execute("INSERT INTO user_id(id, nickname, email, role, password, kindle_mail,locale,"
"sidebar_view, default_language, series_view) " # "sidebar_view, default_language, series_view) "
"sidebar_view, default_language) "
"SELECT id, nickname, email, role, password, kindle_mail, locale," "SELECT id, nickname, email, role, password, kindle_mail, locale,"
"sidebar_view, default_language FROM user") "sidebar_view, default_language FROM user")
# delete old user table and rename new user_id table to user: # delete old user table and rename new user_id table to user:

@ -39,6 +39,7 @@ from flask_babel import gettext as _
from flask_login import login_user, logout_user, login_required, current_user, confirm_login from flask_login import login_user, logout_user, login_required, current_user, confirm_login
from sqlalchemy.exc import IntegrityError, InvalidRequestError, OperationalError from sqlalchemy.exc import IntegrityError, InvalidRequestError, OperationalError
from sqlalchemy.sql.expression import text, func, true, false, not_, and_, or_ from sqlalchemy.sql.expression import text, func, true, false, not_, and_, or_
from sqlalchemy.orm.attributes import flag_modified
from werkzeug.exceptions import default_exceptions, InternalServerError from werkzeug.exceptions import default_exceptions, InternalServerError
from sqlalchemy.sql.functions import coalesce from sqlalchemy.sql.functions import coalesce
try: try:
@ -476,16 +477,21 @@ def update_view():
to_save = request.form.to_dict() to_save = request.form.to_dict()
allowed_view = ['grid', 'list'] allowed_view = ['grid', 'list']
if "series_view" in to_save and to_save["series_view"] in allowed_view: if "series_view" in to_save and to_save["series_view"] in allowed_view:
current_user.series_view = to_save["series_view"]
else:
log.error("Invalid request received: %r %r", request, to_save)
return "Invalid request", 400
try: try:
#visibility = json.loads(current_user.view_settings)
current_user.view_settings['series_view'] = to_save["series_view"]
# current_user.view_settings = json.dumps(visibility)
flag_modified(current_user, "view_settings")
ub.session.commit() ub.session.commit()
except InvalidRequestError: except InvalidRequestError:
log.error("Invalid request received: %r ", request, ) log.error("Invalid request received: %r ", request, )
return "Invalid request", 400 return "Invalid request", 400
except Exception:
log.error("Could not save series_view_settings: %r %r", request, to_save)
return "Invalid request", 400
else:
log.error("Invalid request received: %r %r", request, to_save)
return "Invalid request", 400
return "", 200 return "", 200
@ -831,10 +837,7 @@ def render_language_books(page, name, order):
@web.route("/table") @web.route("/table")
@login_required @login_required
def books_table(): def books_table():
try: visibility = current_user.view_settings.get('table', {})
visibility = json.loads(current_user.view_settings)
except Exception:
visibility = {}
return render_title_template('book_table.html', title=_(u"Books list"), page="book_table", return render_title_template('book_table.html', title=_(u"Books list"), page="book_table",
visiblility=visibility) visiblility=visibility)
@ -876,8 +879,13 @@ def list_books():
def update_table_settings(): def update_table_settings():
# vals = request.get_json() # vals = request.get_json()
# ToDo: Save table settings # ToDo: Save table settings
current_user.view_settings = request.data current_user.view_settings['table'] = json.loads(request.data)
try:
flag_modified(current_user, "view_settings")
ub.session.commit() ub.session.commit()
except InvalidRequestError:
log.error("Invalid request received: %r ", request, )
return "Invalid request", 400
return "" return ""
@web.route("/author") @web.route("/author")
@ -918,7 +926,8 @@ def publisher_list():
@login_required_if_no_ano @login_required_if_no_ano
def series_list(): def series_list():
if current_user.check_visibility(constants.SIDEBAR_SERIES): if current_user.check_visibility(constants.SIDEBAR_SERIES):
if current_user.series_view == 'list': # visibility = json.loads(current_user.view_settings)
if current_user.view_settings.get('series_view') == 'list':
entries = calibre_db.session.query(db.Series, func.count('books_series_link.book').label('count')) \ entries = calibre_db.session.query(db.Series, func.count('books_series_link.book').label('count')) \
.join(db.books_series_link).join(db.Books).filter(calibre_db.common_filters()) \ .join(db.books_series_link).join(db.Books).filter(calibre_db.common_filters()) \
.group_by(text('books_series_link.series')).order_by(db.Series.sort).all() .group_by(text('books_series_link.series')).order_by(db.Series.sort).all()

Loading…
Cancel
Save