support for removing books from shelfs

This commit is contained in:
Jan Broer 2015-10-12 03:09:56 +02:00
parent 597c1ef2a8
commit 2e4117eac2
2 changed files with 77 additions and 24 deletions

View File

@ -47,26 +47,9 @@
<ul class="list-unstyled more-stuff"> <ul class="list-unstyled more-stuff">
{% if g.user.shelf.all() or g.public_shelfes %}
<li>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="glyphicon glyphicon-list"></span> add this book to a shelf <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
{% for shelf in g.user.shelf %}
<li><a href="{{ url_for('add_to_shelf', book_id=entry.id, shelf_id=shelf.id) }}">{{shelf.name}}</a></li>
{% endfor %}
{% for shelf in g.public_shelfes %}
<li><a href="{{ url_for('add_to_shelf', book_id=entry.id, shelf_id=shelf.id) }}">{{shelf.name}}</a></li>
{% endfor %}
</ul>
</div>
</li>
{% endif %}
{% if g.user.kindle_mail %} {% if g.user.kindle_mail %}
<li><a href="{{url_for('send_to_kindle', book_id=entry.id)}}"><div class="btn btn-default"><span class="glyphicon glyphicon-send"></span> Send ebook to kindle</div></a></li> <li><a href="{{url_for('send_to_kindle', book_id=entry.id)}}"><div class="btn btn-info"><span class="glyphicon glyphicon-send"></span> Send to Kindle</div></a></li>
{% endif %} {% endif %}
@ -80,8 +63,8 @@
</li> --> </li> -->
<li> <li>
<div class="btn-group"> <div class="btn-group">
<button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown"> <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="glyphicon glyphicon-download"></span> download this ebook <span class="caret"></span> <span class="glyphicon glyphicon-download"></span> Download <span class="caret"></span>
</button> </button>
<ul class="dropdown-menu" role="menu"> <ul class="dropdown-menu" role="menu">
{% for format in entry.data %} {% for format in entry.data %}
@ -91,11 +74,56 @@
</div> </div>
</li> </li>
<li><a target="_blank" href="{{url_for('read_book', book_id=entry.id)}}"><div class="btn btn-default"><span class="glyphicon glyphicon-eye-open"></span> read this book online</div></a></li> <li><a target="_blank" href="{{url_for('read_book', book_id=entry.id)}}"><div class="btn btn-primary"><span class="glyphicon glyphicon-eye-open"></span> Read online</div></a></li>
{% if g.user.shelf.all() or g.public_shelfes %}
<li>
<div class="btn-group">
<button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="glyphicon glyphicon-list"></span> Add to shelf <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
{% for shelf in g.user.shelf %}
{% if not shelf.id in books_shelfs and shelf.is_public != 1 %}
<li><a href="{{ url_for('add_to_shelf', book_id=entry.id, shelf_id=shelf.id) }}">{{shelf.name}}</a></li>
{% endif %}
{% endfor %}
{% for shelf in g.public_shelfes %}
{% if not shelf.id in books_shelfs %}
<li><a href="{{ url_for('add_to_shelf', book_id=entry.id, shelf_id=shelf.id) }}">{{shelf.name}}</a></li>
{% endif %}
{% endfor %}
</ul>
</div>
</li>
{% endif %}
{% if books_shelfs %}
<li>
<div class="btn-group">
<button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="glyphicon glyphicon-list"></span> Remove from shelf <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
{% for shelf in g.user.shelf %}
{% if shelf.id in books_shelfs and shelf.is_public != 1 %}
<li><a href="{{ url_for('remove_from_shelf', book_id=entry.id, shelf_id=shelf.id) }}">{{shelf.name}}</a></li>
{% endif %}
{% endfor %}
{% for shelf in g.public_shelfes %}
{% if shelf.id in books_shelfs %}
<li><a href="{{ url_for('remove_from_shelf', book_id=entry.id, shelf_id=shelf.id) }}">{{shelf.name}}</a></li>
{% endif %}
{% endfor %}
</ul>
</div>
</li>
{% endif %}
{% if g.user.role %} {% if g.user.role %}
<li><a href="{{ url_for('edit_book', book_id=entry.id) }}"><div class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Edit book</div></a></li> <li><a href="{{ url_for('edit_book', book_id=entry.id) }}"><div class="btn btn-xs btn-warning"><span class="glyphicon glyphicon-edit"></span> Edit book</div></a></li>
<li><a href="{{ url_for('edit_book', book_id=entry.id) }}"><div class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete book</div></a></li> <li><a href="{{ url_for('edit_book', book_id=entry.id) }}"><div class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete book</div></a></li>
{% endif %} {% endif %}
{% endif %} {% endif %}
</ul> </ul>

View File

@ -206,7 +206,11 @@ def discover(page):
@app.route("/book/<int:id>") @app.route("/book/<int:id>")
def show_book(id): def show_book(id):
entries = db.session.query(db.Books).filter(db.Books.id == id).first() entries = db.session.query(db.Books).filter(db.Books.id == id).first()
return render_template('detail.html', entry=entries, title=entries.title) book_in_shelfs = []
shelfs = ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == id).all()
for entry in shelfs:
book_in_shelfs.append(entry.shelf)
return render_template('detail.html', entry=entries, title=entries.title, books_shelfs=book_in_shelfs)
@app.route("/category") @app.route("/category")
def category_list(): def category_list():
@ -348,6 +352,27 @@ def add_to_shelf(shelf_id, book_id):
ub.session.add(ins) ub.session.add(ins)
ub.session.commit() ub.session.commit()
flash("Book has been added to shelf: %s" % shelf.name, category="success")
#return redirect(url_for('show_book', id=book_id))
return redirect(request.environ["HTTP_REFERER"])
@app.route("/shelf/remove/<int:shelf_id>/<int:book_id>")
@login_required
def remove_from_shelf(shelf_id, book_id):
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
if not shelf.is_public and not shelf.user_id == int(current_user.id):
flash("Sorry you are not allowed to remove a book from this shelf: %s" % shelf.name)
return redirect(url_for('index'))
book_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id, ub.BookShelf.book_id == book_id).first()
#rem = ub.BookShelf(shelf=shelf.id, book_id=book_id)
ub.session.delete(book_shelf)
ub.session.commit()
flash("Book has been removed from shelf: %s" % shelf.name, category="success")
return redirect(request.environ["HTTP_REFERER"]) return redirect(request.environ["HTTP_REFERER"])
@app.route("/shelf/create", methods=["GET", "POST"]) @app.route("/shelf/create", methods=["GET", "POST"])