diff --git a/cps/ub.py b/cps/ub.py index 9e053bd5..1aced154 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -23,6 +23,7 @@ import sys import datetime import itertools import uuid +import json from binascii import hexlify from flask import g @@ -41,7 +42,7 @@ except ImportError: oauth_support = False from sqlalchemy import create_engine, exc, exists, event 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.orm import backref, relationship, sessionmaker, Session from werkzeug.security import generate_password_hash @@ -218,8 +219,9 @@ 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(String, default="list") + #series_view = Column(String(10), default="list") + view_settings = Column(JSON, default={}) + if oauth_support: @@ -260,8 +262,8 @@ class Anonymous(AnonymousUserMixin, UserBase): self.allowed_tags = data.allowed_tags self.denied_column_value = data.denied_column_value self.allowed_column_value = data.allowed_column_value - self.series_view = data.series_view - self.view_settings = data.view_settings + # self.series_view = data.series_view + def role_admin(self): 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 `allowed_column_value` DEFAULT ''") session.commit() - try: - session.query(exists().where(User.series_view)).scalar() - except exc.OperationalError: - conn = engine.connect() - conn.execute("ALTER TABLE user ADD column `series_view` VARCHAR(10) DEFAULT 'list'") + #try: + # session.query(exists().where(User.series_view)).scalar() + #except exc.OperationalError: + # conn = engine.connect() + # conn.execute("ALTER TABLE user ADD column `series_view` VARCHAR(10) DEFAULT 'list'") try: session.query(exists().where(User.view_settings)).scalar() except exc.OperationalError: 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() \ is None: @@ -598,12 +601,13 @@ def migrate_Database(session): "locale VARCHAR(2)," "sidebar_view INTEGER," "default_language VARCHAR(3)," - "series_view VARCHAR(10)," + # "series_view VARCHAR(10)," "view_settings VARCHAR," "UNIQUE (nickname)," "UNIQUE (email))") 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," "sidebar_view, default_language FROM user") # delete old user table and rename new user_id table to user: diff --git a/cps/web.py b/cps/web.py index f74c87d0..c4fcfc19 100644 --- a/cps/web.py +++ b/cps/web.py @@ -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 sqlalchemy.exc import IntegrityError, InvalidRequestError, OperationalError 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 sqlalchemy.sql.functions import coalesce try: @@ -476,16 +477,21 @@ def update_view(): to_save = request.form.to_dict() allowed_view = ['grid', 'list'] if "series_view" in to_save and to_save["series_view"] in allowed_view: - current_user.series_view = to_save["series_view"] + 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() + except InvalidRequestError: + log.error("Invalid request received: %r ", request, ) + 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 - - try: - ub.session.commit() - except InvalidRequestError: - log.error("Invalid request received: %r ", request, ) - return "Invalid request", 400 return "", 200 @@ -831,10 +837,7 @@ def render_language_books(page, name, order): @web.route("/table") @login_required def books_table(): - try: - visibility = json.loads(current_user.view_settings) - except Exception: - visibility = {} + visibility = current_user.view_settings.get('table', {}) return render_title_template('book_table.html', title=_(u"Books list"), page="book_table", visiblility=visibility) @@ -876,8 +879,13 @@ def list_books(): def update_table_settings(): # vals = request.get_json() # ToDo: Save table settings - current_user.view_settings = request.data - ub.session.commit() + current_user.view_settings['table'] = json.loads(request.data) + try: + flag_modified(current_user, "view_settings") + ub.session.commit() + except InvalidRequestError: + log.error("Invalid request received: %r ", request, ) + return "Invalid request", 400 return "" @web.route("/author") @@ -918,7 +926,8 @@ def publisher_list(): @login_required_if_no_ano def series_list(): 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')) \ .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()