links in book and author detail

This commit is contained in:
ange 2018-05-23 17:50:01 +02:00
parent d4f23ab507
commit e9ed5ec76f
6 changed files with 72 additions and 7 deletions

View File

@ -36,7 +36,6 @@ class Book(db.Model):
class Author(db.Model): class Author(db.Model):
__tablename__ = 'authors' __tablename__ = 'authors'
id = db.Column(db.Integer(), primary_key=True) id = db.Column(db.Integer(), primary_key=True)
book_id = db.Column(db.Integer(), db.ForeignKey('books.id'))
author_name = db.Column(db.String(50)) author_name = db.Column(db.String(50))

View File

@ -0,0 +1,20 @@
{% extends 'base.html' %}
{% block main %}
<div class="container">
<h1 class="header">{{ author.author_name }}</h1>
<p>Book(s): {% for book in author.books %}
<li> <a href="{{url_for('show_book_by_id', id=book.id)}}">{{ book.title }}</a> </li>
{% endfor %}</p>
<br>
<br>
<a href="{{ url_for('edit_author_by_id', id=author.id )}}">edit</a>
</div>
{% endblock %}

View File

@ -7,11 +7,11 @@
<img src="../uploads/cover/{{ book.cover }}" width="200"> <img src="../uploads/cover/{{ book.cover }}" width="200">
<p>Author(s): {% for author in book.author %} <p>Author(s): <ul>{% for author in book.authors %}
<li> {{ author.author_name }}</li> <li><a href="{{url_for('show_author_by_id', id=author.id)}}">{{ author.author_name }}</a> </li>
{% endfor %}</p> {% endfor %}</ul></p>
<a href="../uploads/{{ book.file }}">download {{ book.fileformat }}</a> <a href="../uploads/{{ book.file }}">download {{ book.fileformat }}</a>
<br> <br>
@ -20,3 +20,5 @@
</div> </div>
{% endblock %} {% endblock %}

View File

@ -28,9 +28,9 @@
<td><img src="../uploads/cover/{{ book.cover }}" width="80"></td> <td><img src="../uploads/cover/{{ book.cover }}" width="80"></td>
<td><a href="books/{{ book.id }}">{{ book.title }}</a></td> <td><a href="books/{{ book.id }}">{{ book.title }}</a></td>
<td> {% for author in book.author %} <td> {% for author in book.authors %}
<li> {{ author.author_name }}</li> <li><a href="{{url_for('show_author_by_id', id=author.id)}}">{{ author.author_name }}</a> </li>
{% endfor %}</td> {% endfor %}</td>
<td>{{ book.fileformat }}</td> <td>{{ book.fileformat }}</td>

View File

@ -65,6 +65,7 @@ def show_book_by_id(id):
else: else:
return render_template('show_book_detail.html', book=book) return render_template('show_book_detail.html', book=book)
@app.route('/books/<int:id>/delete', methods=['POST', 'GET']) @app.route('/books/<int:id>/delete', methods=['POST', 'GET'])
def remove_book_by_id(id): def remove_book_by_id(id):
book_to_edit = Book.query.filter_by(id=id).first() book_to_edit = Book.query.filter_by(id=id).first()
@ -159,6 +160,46 @@ def flash_errors(form):
error error
)) ))
#Authors
@app.route('/authors/<int:id>')
def show_author_by_id(id):
author = Author.query.get(id)
if not author:
abort (404)
else:
return render_template('show_author_detail.html', author=author)
@app.route('/authors/<int:id>/edit', methods=['POST', 'GET'])
def edit_author_by_id(id):
book_to_edit = Book.query.filter_by(id=id).first()
user_form = UserForm_Edit(title = book_to_edit.title, author =book_to_edit.author)
if request.method == 'POST':
if user_form.validate_on_submit():
# check if the post request has the file part
title = user_form.title.data # You could also have used request.form['name']
author = user_form.author.data # You could also have used request.form['email']
# save user to database
#book = Book(title, author, filename, cover, file_extension)
book_to_edit.title = title
db.session.commit()
book = Book.query.filter_by(title=title).first()
author_table = Author.query.filter_by(user_id=book.id).delete()
for this_author in author:
this_author = Author(this_author.get('author_name'))
book.author.append(this_author)
db.session.commit()
flash("%s updated" % (title))
return redirect(url_for('show_books'))
return render_template('edit_book_detail.html', book=book_to_edit, form=user_form)
### ###
# The API # The API
@ -212,3 +253,6 @@ def page_not_found(error):
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True,host="0.0.0.0",port="8080") app.run(debug=True,host="0.0.0.0",port="8080")

2
run.py
View File

@ -1,3 +1,3 @@
#! /usr/bin/env python #! /usr/bin/env python
from app import app from app import app
app.run(debug=True,host="0.0.0.0",port=8080) app.run(debug=True,host="0.0.0.0",port=8000)