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

Feature: Move genre to new table #26

Merged
merged 8 commits into from
Oct 7, 2022
Merged
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
30 changes: 22 additions & 8 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_login import login_required, current_user
from flask_sqlalchemy import SQLAlchemy

from .models import User, Book, Author, Editorial
from .models import User, Book, Author, Editorial, Genre
from . import db

main = Blueprint('main', __name__)
Expand Down Expand Up @@ -33,9 +33,10 @@ def profile():
@main.route('/my_books')
def books():
if current_user.is_authenticated:
filtered_books = db.session.query(Book.id, Book.title, Book.rating, Book.genre, Book.id_author, Author.name.label('author_name'), Editorial.name.label('editorial_name'))\
filtered_books = db.session.query(Book.id, Book.title, Book.rating, Book.id_author, Author.name.label('author_name'), Editorial.name.label('editorial_name'), Genre.name.label('genre_name'))\
.join(Author)\
.join(Editorial)\
.join(Genre)\
.filter((Book.id_author == Author.id) & (Book.id_editorial == Editorial.id) & (Book.id_user == current_user.id))\
.order_by(Author.name.asc())

Expand All @@ -52,10 +53,11 @@ def books():
def show_author(author_id):
if current_user.is_authenticated:
current_author = db.session.query(Author.name, Author.country).filter(Author.id == author_id)
author_books = db.session.query(Book.id, Book.title, Book.genre, Book.year, Book.pages, Book.rating, Editorial.name.label('editorial_name'))\
author_books = db.session.query(Book.id, Book.title, Book.year, Book.pages, Book.rating, Editorial.name.label('editorial_name'), Genre.name.label('genre_name'))\
.filter(Book.id_author == author_id)\
.join(Author)\
.join(Editorial)
.join(Editorial)\
.join(Genre)

return render_template('show_author.html',
greeting = current_user.name,
Expand All @@ -67,10 +69,11 @@ def show_author(author_id):
@main.route('/show_book/<int:book_id>')
def show_book(book_id):
if current_user.is_authenticated:
current_book = db.session.query(Book.title, Book.genre, Book.year, Book.pages, Book.read, Book.shared, Book.rating, Author.name.label('author_name'), Author.country.label('author_country'), Editorial.name.label('editorial_name'))\
current_book = db.session.query(Book.title, Book.year, Book.pages, Book.read, Book.shared, Book.rating, Author.name.label('author_name'), Author.country.label('author_country'), Editorial.name.label('editorial_name'), Genre.name.label('genre_name'))\
.filter(Book.id == book_id)\
.join(Author)\
.join(Editorial)
.join(Editorial)\
.join(Genre)

return render_template('show_book.html',
greeting = current_user.name,
Expand Down Expand Up @@ -120,9 +123,20 @@ def add_book_post():

id_editorial = db.session.query(Editorial.id).filter(Editorial.name == editorial_name)

# Check genre existence
genre_name = request.form.get("genre_name")
genre_exists = Genre.query.filter_by(name=genre_name).first()

if not genre_exists:
genre = Genre(name=genre_name)
db.session.add(genre)
db.session.commit()
id_genre = db.session.query(Genre.id).filter(Genre.name == genre_name)

id_genre = db.session.query(Genre.id).filter(Genre.name == genre_name)

# Add Book
book_title = request.form.get('book_title')
book_genre = request.form.get('book_genre')
book_year = request.form.get('book_year')
book_pages = request.form.get('book_pages')
book_rating = request.form.get('book_rating')
Expand All @@ -138,7 +152,6 @@ def add_book_post():
is_shared = False

book = Book(title = book_title,
genre = book_genre,
year = book_year,
pages = book_pages,
read = is_read,
Expand All @@ -147,6 +160,7 @@ def add_book_post():
id_user = current_user.id,
id_author = id_author,
id_editorial = id_editorial,
id_genre = id_genre,
)
db.session.add(book)
db.session.commit()
Expand Down
22 changes: 13 additions & 9 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
from . import db

class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String(100), unique=True, nullable=False)
name = db.Column(db.String(100), nullable=False)
password_hash = db.Column(db.String(120))

class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(255), nullable=False)
genre = db.Column(db.String(255))
year = db.Column(db.Integer)
pages = db.Column(db.Integer)
read = db.Column(db.Boolean, default=False)
Expand All @@ -19,22 +18,27 @@ class Book(db.Model):
id_user = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
id_author = db.Column(db.Integer, db.ForeignKey('author.id'), nullable=False)
id_editorial = db.Column(db.Integer, db.ForeignKey('editorial.id'), nullable=False)
id_genre = db.Column(db.Integer, db.ForeignKey('genre.id'), nullable=False)

class Author(db.Model):
id = db.Column(db.Integer, primary_key=True)
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(255), nullable=False)
country = db.Column(db.String(255))

class Editorial(db.Model):
id = db.Column(db.Integer, primary_key=True)
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(255), nullable=False)

class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(255), primary_key=True)

class Genre(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(255), primary_key=True)

# many-to-many books/tags
tags = db.Table('books_tags',
db.Column('id_tag', db.Integer, db.ForeignKey('tag.id'), primary_key=True),
db.Column('id_book', db.Integer, db.ForeignKey('book.id'), primary_key=True)
)

class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), primary_key=True)
2 changes: 1 addition & 1 deletion app/templates/add_book.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h1 class="title is-1">Agregar nuevo libro</h1>
<form method="POST" action="/add_book" style="width: 50%">
<input class="input is-medium" type="text" name="book_title" placeholder="Título"><br></br>
<input class="input is-medium" type="text" name="author_name" placeholder="Autor"><br></br>
<input class="input is-medium" type="text" name="book_genre" placeholder="Género"><br></br>
<input class="input is-medium" type="text" name="genre_name" placeholder="Género"><br></br>
<input class="input is-medium" type="text" name="author_country" placeholder="País"><br></br>
<input class="input is-medium" type="text" name="editorial_name" placeholder="Editorial"><br></br>
<input class="input is-medium" type="text" name="book_year" placeholder="Año"><br></br>
Expand Down
2 changes: 1 addition & 1 deletion app/templates/my_books.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h1 class="title">Mis Libros</h1>
<th><a href="{{ url_for('main.show_author', author_id = book.id_author) }}">{{ book.author_name }}</a></th>
<th><a href="{{ url_for('main.show_book', book_id = book.id) }}">{{ book.title }}</a></th>
<th>{{ book.rating * '<i class="bi bi-star-fill"></i>' |safe }}</th>
<th>{{ book.genre }}</th>
<th>{{ book.genre_name }}</th>
<th>{{ book.editorial_name }}</th>
</tr>
{% endfor %}
Expand Down
16 changes: 8 additions & 8 deletions app/templates/show_book.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
{% for item in book %}
<div class="container has-text-justified">
<h1 class="title">{{ item[0] }}</h1>
<h2 class="text-muted">{{ item[7] }} ({{ item[8] }})</h2>
<h2 class="text-muted">{{ item[6] }} ({{ item[7] }})</h2>
<br></br>
<p><b>Género: </b>{{ item[1] }}</p>
<p><b>Editorial: </b>{{ item[9] }}</p>
<p><b>Año: </b>{{ item[2] }}</p>
<p><b>¿Leído?: </b>{% if item[4] %}<i class="bi bi-check-square-fill"></i>{%else%}<i class="bi bi-x-square-fill"></i>{%endif%}</p>
<p><b>¿Prestado?: </b>{% if item[5] %}<i class="bi bi-check-square-fill"></i>{%else%}<i class="bi bi-x-square-fill"></i>{%endif%}</p>
<p><b>Número de páginas: </b>{{ item[3] }}</p>
<p><b>Puntuación: </b>{{ item[6] * '<i class="bi bi-star-fill"></i>' |safe}}</p>
<p><b>Género: </b>{{ item[9] }}</p>
<p><b>Editorial: </b>{{ item[7] }}</p>
<p><b>Año: </b>{{ item[1] }}</p>
<p><b>¿Leído?: </b>{% if item[3] %}<i class="bi bi-check-square-fill"></i>{%else%}<i class="bi bi-x-square-fill"></i>{%endif%}</p>
<p><b>¿Prestado?: </b>{% if item[4] %}<i class="bi bi-check-square-fill"></i>{%else%}<i class="bi bi-x-square-fill"></i>{%endif%}</p>
<p><b>Número de páginas: </b>{{ item[2] }}</p>
<p><b>Puntuación: </b>{{ item[5] * '<i class="bi bi-star-fill"></i>' |safe}}</p>
</div>
{% endfor %}

Expand Down
Binary file modified migrations/__pycache__/env.cpython-39.pyc
Binary file not shown.
36 changes: 0 additions & 36 deletions migrations/versions/11ddd75a4034_.py

This file was deleted.

35 changes: 0 additions & 35 deletions migrations/versions/24c8b37053aa_.py

This file was deleted.

33 changes: 0 additions & 33 deletions migrations/versions/2f3143ca16e9_.py

This file was deleted.

39 changes: 0 additions & 39 deletions migrations/versions/5c34faeea863_.py

This file was deleted.

44 changes: 0 additions & 44 deletions migrations/versions/68152efb549c_.py

This file was deleted.

Loading