diff --git a/.gitignore b/.gitignore index ff08f874b..d757b816c 100644 --- a/.gitignore +++ b/.gitignore @@ -29,9 +29,12 @@ cps/cache *.bak *.log.* .key +.venv settings.yaml gdrive_credentials client_secrets.json gmail.json /.key + +pages/ diff --git a/cps/admin.py b/cps/admin.py index 022acc8e1..91b2f7c8f 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -1981,7 +1981,7 @@ def _handle_edit_user(to_save, content, languages, translations, kobo_support): return redirect(url_for('admin.admin')) val = [int(k[5:]) for k in to_save if k.startswith('show_')] - sidebar, __ = get_sidebar_config() + sidebar, __, __, __ = get_sidebar_config() for element in sidebar: value = element['visibility'] if value in val and not content.check_visibility(value): diff --git a/cps/constants.py b/cps/constants.py index 87ce6f59f..505958dd5 100644 --- a/cps/constants.py +++ b/cps/constants.py @@ -68,6 +68,7 @@ ROLE_EDIT_SHELFS = 1 << 6 ROLE_DELETE_BOOKS = 1 << 7 ROLE_VIEWER = 1 << 8 +ROLE_SEND_TO_EREADER = 1 << 9 ALL_ROLES = { "admin_role": ROLE_ADMIN, @@ -78,6 +79,7 @@ "edit_shelf_role": ROLE_EDIT_SHELFS, "delete_role": ROLE_DELETE_BOOKS, "viewer_role": ROLE_VIEWER, + "send_to_ereader": ROLE_SEND_TO_EREADER, } DETAIL_RANDOM = 1 << 0 diff --git a/cps/editpage.py b/cps/editpage.py new file mode 100644 index 000000000..1942bfd23 --- /dev/null +++ b/cps/editpage.py @@ -0,0 +1,108 @@ +import os +import flask +from flask import Blueprint, Flask, abort, request +from functools import wraps +from pathlib import Path +from flask_login import current_user, login_required +from werkzeug.exceptions import NotFound + +from .render_template import render_title_template +from . import logger, config, ub +from .constants import CONFIG_DIR as _CONFIG_DIR + +log = logger.create() + +editpage = Blueprint('editpage', __name__) + +def edit_required(f): + @wraps(f) + def inner(*args, **kwargs): + if current_user.role_edit() or current_user.role_admin(): + return f(*args, **kwargs) + abort(403) + + return inner + +def _get_checkbox(dictionary, field, default): + new_value = dictionary.get(field, default) + convertor = lambda y: y == "on" + new_value = convertor(new_value) + + return new_value + +@editpage.route("/admin/page/", methods=["GET", "POST"]) +@login_required +@edit_required +def edit_page(file): + doc = "" + title = "" + name = "" + icon = "file" + is_enabled = True + order = 0 + position = "0" + + page = ub.session.query(ub.Page).filter(ub.Page.id == file).first() + + try: + title = page.title + name = page.name + icon = page.icon + is_enabled = page.is_enabled + order = page.order + position = page.position + except AttributeError: + if file != "new": + abort(404) + + if request.method == "POST": + to_save = request.form.to_dict() + title = to_save.get("title", "").strip() + name = to_save.get("name", "").strip() + icon = to_save.get("icon", "").strip() + position = to_save.get("position", "").strip() + order = int(to_save.get("order", 0)) + content = to_save.get("content", "").strip() + is_enabled = _get_checkbox(to_save, "is_enabled", True) + + if page: + page.title = title + page.name = name + page.icon = icon + page.is_enabled = is_enabled + page.order = order + page.position = position + ub.session_commit("Page edited {}".format(file)) + else: + new_page = ub.Page(title=title, name=name, icon=icon, is_enabled=is_enabled, order=order, position=position) + ub.session.add(new_page) + ub.session_commit("Page added {}".format(file)) + + if (file == "new"): + file = str(new_page.id) + dir_config_path = os.path.join(_CONFIG_DIR, 'pages') + file_name = Path(name + '.md') + file_path = dir_config_path / file_name + os.makedirs(dir_config_path, exist_ok=True) + + try: + with open(file_path, 'w') as f: + f.write(content) + f.close() + except Exception as ex: + log.error(ex) + + if file != "new": + try: + dir_config_path = Path(_CONFIG_DIR) / 'pages' + file_path = dir_config_path / f"{name}.md" + + with open(file_path, 'r') as f: + doc = f.read() + except NotFound: + log.error("'%s' was accessed but file doesn't exists." % file) + + else: + doc = "## New file\n\nInformation" + + return render_title_template("edit_page.html", title=title, name=name, icon=icon, is_enabled=is_enabled, order=order, position=position, content=doc, file=file) diff --git a/cps/kobo.py b/cps/kobo.py index e80873d0f..63741a620 100644 --- a/cps/kobo.py +++ b/cps/kobo.py @@ -56,7 +56,7 @@ KOBO_FORMATS = {"KEPUB": ["KEPUB"], "EPUB": ["EPUB3", "EPUB"]} KOBO_STOREAPI_URL = "https://storeapi.kobo.com" -KOBO_IMAGEHOST_URL = "https://kbimages1-a.akamaihd.net" +KOBO_IMAGEHOST_URL = "https://cdn.kobo.com/book-images" SYNC_ITEM_LIMIT = 100 diff --git a/cps/listpages.py b/cps/listpages.py new file mode 100644 index 000000000..8016f5ee1 --- /dev/null +++ b/cps/listpages.py @@ -0,0 +1,28 @@ +import flask +import json +from flask import Blueprint, jsonify, make_response,abort +from flask_login import current_user, login_required +from functools import wraps +from flask_babel import gettext as _ + +from .render_template import render_title_template +from . import ub, db + +listpages = Blueprint('listpages', __name__) + +def edit_required(f): + @wraps(f) + def inner(*args, **kwargs): + if current_user.role_edit() or current_user.role_admin(): + return f(*args, **kwargs) + abort(403) + + return inner + +@listpages.route("/admin/pages/", methods=["GET"]) +@login_required +@edit_required +def show_list(): + pages = ub.session.query(ub.Page).order_by(ub.Page.position).order_by(ub.Page.order).all() + + return render_title_template('list_pages.html', title=_("Pages List"), page="book_table", pages=pages) diff --git a/cps/main.py b/cps/main.py index 286b2b278..ad990bb7c 100644 --- a/cps/main.py +++ b/cps/main.py @@ -36,6 +36,9 @@ def main(): from .gdrive import gdrive from .editbooks import editbook from .about import about + from .page import page + from .listpages import listpages + from .editpage import editpage from .search import search from .search_metadata import meta from .shelf import shelf @@ -65,6 +68,9 @@ def main(): limiter.limit("3/minute",key_func=request_username)(opds) app.register_blueprint(jinjia) app.register_blueprint(about) + app.register_blueprint(page) + app.register_blueprint(listpages) + app.register_blueprint(editpage) app.register_blueprint(shelf) app.register_blueprint(admi) app.register_blueprint(remotelogin) diff --git a/cps/metadata_provider/douban.py b/cps/metadata_provider/douban.py index 8e27e82e8..39c71cc72 100644 --- a/cps/metadata_provider/douban.py +++ b/cps/metadata_provider/douban.py @@ -169,7 +169,8 @@ def _parse_single_book(self, ), ) - html = etree.HTML(r.content.decode("utf8")) + decode_content = r.content.decode("utf8") + html = etree.HTML(decode_content) match.title = html.xpath(self.TITTLE_XPATH)[0].text match.cover = html.xpath( @@ -184,7 +185,7 @@ def _parse_single_book(self, if len(tag_elements): match.tags = [tag_element.text for tag_element in tag_elements] else: - match.tags = self._get_tags(html.text) + match.tags = self._get_tags(decode_content) description_element = html.xpath(self.DESCRIPTION_XPATH) if len(description_element): diff --git a/cps/page.py b/cps/page.py new file mode 100644 index 000000000..b3e42faad --- /dev/null +++ b/cps/page.py @@ -0,0 +1,38 @@ +import os +import flask +import markdown +from flask import abort +from pathlib import Path +from flask_babel import gettext as _ +from werkzeug.exceptions import NotFound + +from . import logger, config, ub +from .render_template import render_title_template +from .constants import CONFIG_DIR as _CONFIG_DIR + +page = flask.Blueprint('page', __name__) + +log = logger.create() + +@page.route('/page/', methods=['GET']) +def get_page(file): + page = ub.session.query(ub.Page)\ + .filter(ub.Page.name == file)\ + .filter(ub.Page.is_enabled)\ + .first() + + if not page: + log.error(f"'{file}' was accessed but is not enabled or it's not in database.") + abort(404) + + try: + dir_config_path = Path(_CONFIG_DIR) / 'pages' + file_path = dir_config_path / f"{file}.md" + with open(file_path, 'r') as f: + temp_md = f.read() + body = markdown.markdown(temp_md) + + return render_title_template('page.html', body=body, title=page.title, page=page.name) + except NotFound: + log.error("'%s' was accessed but file doesn't exists." % file) + abort(404) diff --git a/cps/render_template.py b/cps/render_template.py index 68b464593..0f6e68ef3 100644 --- a/cps/render_template.py +++ b/cps/render_template.py @@ -104,14 +104,24 @@ def get_sidebar_config(kwargs=None): g.shelves_access = ub.session.query(ub.Shelf).filter( or_(ub.Shelf.is_public == 1, ub.Shelf.user_id == current_user.id)).order_by(ub.Shelf.name).all() - return sidebar, simple + top_pages = ub.session.query(ub.Page)\ + .filter(ub.Page.position == "1")\ + .filter(ub.Page.is_enabled)\ + .order_by(ub.Page.order) + bottom_pages = ub.session.query(ub.Page)\ + .filter(ub.Page.position == "0")\ + .filter(ub.Page.is_enabled)\ + .order_by(ub.Page.order) + + return sidebar, simple, top_pages, bottom_pages # Returns the template for rendering and includes the instance name def render_title_template(*args, **kwargs): - sidebar, simple = get_sidebar_config(kwargs) + sidebar, simple, top_pages, bottom_pages = get_sidebar_config(kwargs) try: return render_template(instance=config.config_calibre_web_title, sidebar=sidebar, simple=simple, + top_pages=top_pages, bottom_pages=bottom_pages, accept=constants.EXTENSIONS_UPLOAD, *args, **kwargs) except PermissionError: diff --git a/cps/static/css/caliBlur.css b/cps/static/css/caliBlur.css index cf7437615..6cff9f0e6 100644 --- a/cps/static/css/caliBlur.css +++ b/cps/static/css/caliBlur.css @@ -229,6 +229,10 @@ } } +body h2 { + color: hsla(0, 0%, 100%, .45); +} + body > div.navbar.navbar-default.navbar-static-top > div > div.navbar-collapse.collapse > ul > li > a[href*=advanced_search] { display: none } @@ -411,7 +415,7 @@ body.blur .row-fluid .col-sm-10 { font-family: plex-icons-new, serif; margin-right: 30px; margin-left: 15px; - vertical-align: bottom; + vertical-align: top; display: inline-block; font-weight: 400; font-size: 18px; @@ -447,9 +451,9 @@ body.shelforder > div.container-fluid > div.row-fluid > div.col-sm-10:before { font-style: normal; font-weight: 400; line-height: 1; - font-size: 6vw; + font-size: 3vw; position: fixed; - left: 240px; + left: 160px; top: 180px; width: calc(20% - 55px); text-align: center @@ -2447,7 +2451,7 @@ label { } body > div.container-fluid > div > div.col-sm-10 > div.col-sm-8 { - margin-left: calc(20%); + margin-left: calc(10%); width: calc(80% - 30px); padding: 60px 0 } @@ -7944,3 +7948,36 @@ div.comments[data-readmore] { transition: height 300ms; overflow: hidden } + +div.col-sm-10 > h2.book_table, div.col-sm-10 > a.session { + padding-left: 40px; + padding-right: 10px; +} + +div.col-sm-10 > h2.book_table { + color: white; +} + +div.col-sm-10 > table.table { + margin-left: 40px; + width: calc(100% - 50px); +} + +div.col-sm-10 > div.custom_page { + padding-left: 40px; + padding-right: 10px; + color: white; +} + +div.col-sm-10 > div.custom_page > h2 { + color: white; +} + +div.list-group-item > div.row { + margin-left: 50px; + margin-top: -50px; +} + +div.list-group-item > div.row > div.col-lg-2 { + width: 100px; +} \ No newline at end of file diff --git a/cps/tasks/thumbnail.py b/cps/tasks/thumbnail.py index dd9ee1e08..7cf90b68f 100644 --- a/cps/tasks/thumbnail.py +++ b/cps/tasks/thumbnail.py @@ -218,7 +218,7 @@ def generate_book_thumbnail(self, book, thumbnail): filename = self.cache.get_cache_file_path(thumbnail.filename, constants.CACHE_TYPE_THUMBNAILS) if img.height > height: width = get_resize_width(thumbnail.resolution, img.width, img.height) - img.resize(width=width, height=height, filter='lanczos') + img.resize(width=width, height=height)#, filter='lanczos') img.format = thumbnail.format img.save(filename=filename) else: diff --git a/cps/templates/admin.html b/cps/templates/admin.html index ac124fe84..761254f39 100644 --- a/cps/templates/admin.html +++ b/cps/templates/admin.html @@ -20,6 +20,7 @@

{{_('Users')}}

{{_('Upload')}} {% endif %} {{_('Download')}} + {{_('Send to eReader')}} {{_('View Books')}} {{_('Edit')}} {{_('Delete')}} @@ -38,6 +39,7 @@

{{_('Users')}}

{{ display_bool_setting(user.role_upload()) }} {% endif %} {{ display_bool_setting(user.role_download()) }} + {{ display_bool_setting(user.role_send_to_ereader()) }} {{ display_bool_setting(user.role_viewer()) }} {{ display_bool_setting(user.role_edit()) }} {{ display_bool_setting(user.role_delete_books()) }} @@ -159,6 +161,7 @@

{{_('Configuration')}}

{{_('Edit Calibre Database Configuration')}} {{_('Edit Basic Configuration')}} {{_('Edit UI Configuration')}} + {{_('List Pages')}} {% if feature_support['scheduler'] %} diff --git a/cps/templates/detail.html b/cps/templates/detail.html index 304306630..8c9d2df69 100755 --- a/cps/templates/detail.html +++ b/cps/templates/detail.html @@ -43,6 +43,8 @@ {% endif %} {% endif %} + {% endif %} + {% if current_user.role_send_to_ereader() %} {% if current_user.kindle_mail and entry.email_share_list %} {% if entry.email_share_list.__len__() == 1 %} diff --git a/cps/templates/edit_page.html b/cps/templates/edit_page.html new file mode 100644 index 000000000..e612a133b --- /dev/null +++ b/cps/templates/edit_page.html @@ -0,0 +1,45 @@ +{% extends "layout.html" %} +{% block body %} +
+
{{_('Back')}}
+

{{_('Edit page')}}

+
+ +
+ + +
+
+ + +
+
+ + + {{_('Icons list')}} +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + {{_('Cancel')}} +
+
+{% endblock %} diff --git a/cps/templates/layout.html b/cps/templates/layout.html index 1bee1c1d1..c4716615e 100644 --- a/cps/templates/layout.html +++ b/cps/templates/layout.html @@ -142,6 +142,9 @@

{{_('Uploading...')}}

+
+ + +
diff --git a/cps/templates/user_table.html b/cps/templates/user_table.html index 3f998f951..baa6a08d7 100644 --- a/cps/templates/user_table.html +++ b/cps/templates/user_table.html @@ -144,6 +144,7 @@

{{_(title)}}

{{ user_checkbox_row("role", "passwd_role", _('Change Password'), visiblility, all_roles)}} {{ user_checkbox_row("role", "upload_role",_('Upload'), visiblility, all_roles)}} {{ user_checkbox_row("role", "download_role", _('Download'), visiblility, all_roles)}} + {{ user_checkbox_row("role", "send_to_ereader", _('Send to eReader'), visiblility, all_roles)}} {{ user_checkbox_row("role", "viewer_role", _('View'), visiblility, all_roles)}} {{ user_checkbox_row("role", "edit_role", _('Edit'), visiblility, all_roles)}} {{ user_checkbox_row("role", "delete_role", _('Delete'), visiblility, all_roles)}} diff --git a/cps/ub.py b/cps/ub.py index 3e478f99f..43382dbc4 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -160,6 +160,9 @@ def role_delete_books(self): def role_viewer(self): return self._has_role(constants.ROLE_VIEWER) + def role_send_to_ereader(self): + return self._has_role(constants.ROLE_SEND_TO_EREADER) + @property def is_active(self): return True @@ -230,7 +233,7 @@ class User(UserBase, Base): id = Column(Integer, primary_key=True) name = Column(String(64), unique=True) email = Column(String(120), unique=True, default="") - role = Column(SmallInteger, default=constants.ROLE_USER) + role = Column(Integer, default=constants.ROLE_USER) password = Column(String) kindle_mail = Column(String(120), default="") shelf = relationship('Shelf', backref='user', lazy='dynamic', order_by='Shelf.name') @@ -535,6 +538,16 @@ class Thumbnail(Base): generated_at = Column(DateTime, default=lambda: datetime.datetime.utcnow()) expiration = Column(DateTime, nullable=True) +class Page(Base): + __tablename__ = 'page' + + id = Column(Integer, primary_key=True) + title = Column(String) + name = Column(String) + icon = Column(String) + order = Column(Integer) + position = Column(String) + is_enabled = Column(Boolean, default=True) # Add missing tables during migration of database def add_missing_tables(engine, _session): @@ -558,7 +571,8 @@ def add_missing_tables(engine, _session): trans = conn.begin() conn.execute("insert into registration (domain, allow) values('%.%',1)") trans.commit() - + if not engine.dialect.has_table(engine.connect(), "page"): + Page.__table__.create(bind=engine) # migrate all settings missing in registration table def migrate_registration_table(engine, _session): @@ -731,7 +745,7 @@ def migrate_Database(_session): conn.execute(text("CREATE TABLE user_id (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," "name VARCHAR(64)," "email VARCHAR(120)," - "role SMALLINT," + "role INTEGER," "password VARCHAR," "kindle_mail VARCHAR(120)," "locale VARCHAR(2)," diff --git a/requirements.txt b/requirements.txt index c28f2019c..7fd72b4cc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,3 +18,4 @@ flask-wtf>=0.14.2,<1.3.0 chardet>=3.0.0,<4.1.0 advocate>=1.0.0,<1.1.0 Flask-Limiter>=2.3.0,<3.6.0 +markdown>=3.5.1