Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect shelf to category. - Sponsor: Fundación Karisma #2998

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cps/render_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from flask_login import current_user
from sqlalchemy.sql.expression import or_

from . import config, constants, logger, ub
from . import config, constants, logger, ub, db, calibre_db
from .ub import User


Expand Down Expand Up @@ -106,13 +106,21 @@ def get_sidebar_config(kwargs=None):

return sidebar, simple

def get_category(categoryId=0):
categories = calibre_db.session.query(db.Tags)\
.order_by(db.Tags.name)
for category in categories:
if category.id == categoryId:
return category
return { 'name': '' }

# Returns the template for rendering and includes the instance name
def render_title_template(*args, **kwargs):
sidebar, simple = get_sidebar_config(kwargs)
try:
return render_template(instance=config.config_calibre_web_title, sidebar=sidebar, simple=simple,
accept=constants.EXTENSIONS_UPLOAD,
get_category=get_category,
*args, **kwargs)
except PermissionError:
log.error("No permission to access {} file.".format(args[0]))
Expand Down
7 changes: 7 additions & 0 deletions cps/shelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ def create_edit_shelf(shelf, page_title, page, shelf_id=False):
flash(_("Sorry you are not allowed to create a public shelf"), category="error")
return redirect(url_for('web.index'))
is_public = 1 if to_save.get("is_public") == "on" else 0
type = to_save.get("type", "")
category = to_save.get("category", "")
if config.config_kobo_sync:
shelf.kobo_sync = True if to_save.get("kobo_sync") else False
if shelf.kobo_sync:
Expand All @@ -334,6 +336,8 @@ def create_edit_shelf(shelf, page_title, page, shelf_id=False):
if check_shelf_is_unique(shelf_title, is_public, shelf_id):
shelf.name = shelf_title
shelf.is_public = is_public
shelf.type = type
shelf.category = category
if not shelf_id:
shelf.user_id = int(current_user.id)
ub.session.add(shelf)
Expand All @@ -356,8 +360,11 @@ def create_edit_shelf(shelf, page_title, page, shelf_id=False):
ub.session.rollback()
log.error_or_exception(ex)
flash(_("There was an error"), category="error")
categories = calibre_db.session.query(db.Tags)\
.order_by(db.Tags.name)
return render_title_template('shelf_edit.html',
shelf=shelf,
categories=categories,
title=page_title,
page=page,
kobo_sync_enabled=config.config_kobo_sync,
Expand Down
12 changes: 11 additions & 1 deletion cps/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,17 @@ <h3>{{_('Uploading...')}}</h3>
{% if current_user.is_authenticated or g.allow_anonymous %}
<li class="nav-head hidden-xs public-shelves">{{_('Shelves')}}</li>
{% for shelf in g.shelves_access %}
<li><a href="{{url_for('shelf.show_shelf', shelf_id=shelf.id)}}"><span class="glyphicon glyphicon-list shelf"></span> {{shelf.name|shortentitle(40)}}{% if shelf.is_public == 1 %} {{_('(Public)')}}{% endif %}</a></li>
{% if shelf.type == 0 %}
<li><a href="{{url_for('shelf.show_shelf', shelf_id=shelf.id)}}"><span class="glyphicon glyphicon-list shelf"></span> {{shelf.name|shortentitle(40)}}{% if shelf.is_public == 1 %} {{_('(Public)')}}{% endif %}</a></li>
{% else %}
<li><a href="{{url_for('web.books_list', data='category', sort_param='stored', book_id=shelf.category )}}"><span class="glyphicon glyphicon-list shelf"></span>
{{get_category(shelf.category).name|shortentitle(40)}}
{% if shelf.is_public == 1 %} {{_('(Public)')}}{% endif %}</a>
{% if not current_user.is_anonymous %}
<a href="{{url_for('shelf.show_shelf', shelf_id=shelf.id)}}"><span class="glyphicon glyphicon-edit shelf"></span> (Shelf)</a>
{% endif %}
</li>
{% endif %}
{% endfor %}
{% if not current_user.is_anonymous %}
<li id="nav_createshelf" class="create-shelf"><a href="{{url_for('shelf.create_shelf')}}">{{_('Create a Shelf')}}</a></li>
Expand Down
29 changes: 29 additions & 0 deletions cps/templates/shelf_edit.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{% extends "layout.html" %}
{% block header %}
<link href="{{ url_for('static', filename='css/libs/bootstrap-select.min.css') }}" rel="stylesheet" >
{% endblock %}

{% block body %}
<div class="discover">
<h1>{{title}}</h1>
Expand All @@ -14,6 +18,24 @@ <h1>{{title}}</h1>
<input type="checkbox" name="is_public" {% if shelf.is_public == 1 %}checked{% endif %}> {{_('Share with Everyone')}}
</label>
</div>
<div class="form-group">
<label for="type">Shelf type:</label>
<select id="type" name="type" class="selectpicker">
<option value="0"{% if shelf.type == 0 %} selected{% endif %}>Calibre web</option>
<option value="1"{% if shelf.type == 1 %} selected{% endif %}>Category shorcut</option>
</select>
</div>
<div class="form-group">
<label for="category">Category:</label>
<select id="category" name="category" class="selectpicker">
<option value="0">None</option>
{% if shelf.type == 1 %}
{% for category in categories %}
<option value="{{ category.id }}"{% if category.id == shelf.category %} selected{% endif %}>{{ category.name }}</option>
{% endfor %}
{% endif %}
</select>
</div>
{% endif %}
{% if kobo_sync_enabled and sync_only_selected_shelves %}
<div class="checkbox">
Expand All @@ -29,3 +51,10 @@ <h1>{{title}}</h1>
</form>
</div>
{% endblock %}

{% block js %}
<script src="{{ url_for('static', filename='js/libs/bootstrap-select.min.js')}}"></script>
{% if not current_user.locale == 'en' %}
<script src="{{ url_for('static', filename='js/libs/bootstrap-select/defaults-' + current_user.locale + '.min.js') }}" charset="UTF-8"></script>
{% endif %}
{% endblock %}
17 changes: 16 additions & 1 deletion cps/ub.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ class Shelf(Base):
uuid = Column(String, default=lambda: str(uuid.uuid4()))
name = Column(String)
is_public = Column(Integer, default=0)
type = Column(Integer, default=0)
category = Column(Integer, default=0)
user_id = Column(Integer, ForeignKey('user.id'))
kobo_sync = Column(Boolean, default=False)
books = relationship("BookShelf", backref="ub_shelf", cascade="all, delete-orphan", lazy="dynamic")
Expand Down Expand Up @@ -630,7 +632,20 @@ def migrate_shelfs(engine, _session):
trans = conn.begin()
conn.execute(text("ALTER TABLE book_shelf_link ADD column 'order' INTEGER DEFAULT 1"))
trans.commit()

try:
_session.query(exists().where(Shelf.type)).scalar()
except exc.OperationalError: # Database is not compatible, some columns are missing
with engine.connect() as conn:
trans = conn.begin()
conn.execute(text("ALTER TABLE shelf ADD column 'type' INTEGER DEFAULT 0"))
trans.commit()
try:
_session.query(exists().where(Shelf.category)).scalar()
except exc.OperationalError: # Database is not compatible, some columns are missing
with engine.connect() as conn:
trans = conn.begin()
conn.execute(text("ALTER TABLE shelf ADD column 'category' INTEGER DEFAULT 0"))
trans.commit()

def migrate_readBook(engine, _session):
try:
Expand Down