Skip to content

Commit

Permalink
Merge pull request #36 from boris/fix/some-cosmetic
Browse files Browse the repository at this point in the history
Fix: some cosmetic changes
  • Loading branch information
Boris Quiroz authored Jun 14, 2023
2 parents 447a46d + 7294983 commit 5eddf79
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 122 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ run: ## Run the Flask app in local (debug) mode
run-db: ## Start the DB service in docker
docker run -d -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=$(MYSQL_ROOT_PASSWORD) -d mysql:5.7

tunnel: ## Exposes the local environment on test.lenore.me. This requires a `make run` first.
tunnel: ## Exposes the local environment on test.lenore.me. This will run `make run` in the background first.
$(MAKE) run &
docker run cloudflare/cloudflared:latest tunnel --no-autoupdate --metrics 0.0.0.0:60123 run --token $(CF_TOKEN)
4 changes: 2 additions & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import yaml
from os import path
from flask import Flask
from flask import Flask, redirect, url_for, render_template, request, abort
from flask_bootstrap import Bootstrap5
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
Expand Down Expand Up @@ -34,7 +34,7 @@


# Import models
from .models import User
from .models import User, Book, Author, Editorial, Genre


# Login manager
Expand Down
12 changes: 12 additions & 0 deletions app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
from . import db

class BookForm(FlaskForm):
def __init__(self, *args, author_id=None, editorial_id=None, genre_id=None, **kwargs):
super().__init__(*args, **kwargs)
self.author_id = author_id
self.editorial_id = editorial_id
self.genre_id = genre_id

book_title = StringField('Título', validators=[InputRequired()])
author_name = StringField('Autor', validators=[InputRequired()])
author_country = StringField('País')
Expand All @@ -23,6 +29,12 @@ class BookForm(FlaskForm):
book_tags = StringField('Tags')
submit = SubmitField('Agregar')

def populate_obj(self, obj):
super().populate_obj(obj)
obj.author_id = self.author_id
obj.editorial_id = self.editorial_id
obj.genre_id = self.genre_id


class LoginForm(FlaskForm):
email = StringField('Email', [DataRequired()])
Expand Down
141 changes: 41 additions & 100 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from flask_login import login_required, current_user
from flask_sqlalchemy import SQLAlchemy

from .models import User, Book, Author, Editorial, Genre
# This is for debug
import traceback

from app.models import User, Book, Author, Editorial, Genre
from . import db
from .forms import BookForm

Expand Down Expand Up @@ -112,101 +115,26 @@ def delete_book(book_id):


@main.route('/edit_book/<int:book_id>', methods = ['GET', 'POST'])
@login_required
def edit_book(book_id):
if current_user.is_authenticated:
book = db.session.query(Book.id, 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(Genre)

author = db.session.query(Author.name, Author.country).filter(Author.id == Book.id_author)
editorial = db.session.query(Editorial.name).filter(Editorial.id == Book.id_editorial)
genre = db.session.query(Genre.name).filter(Genre.id == Book.id_genre)

#book = Book.query.get(book_id)
#author = Author.query.get(book.id_author)
#editorial = Editorial.query.get(book.id_editorial)
#genre = Genre.query.get().filter(genre_id == book.id_genre)

form = BookForm(obj=book)

if form.validate_on_submit():
# From here up to "END", everything can be optimized in different private methods
if request.form['book_is_read'].lower() == 'si':
is_read = True
else:
is_read = False

if request.form['book_is_shared'].lower() == 'si':
is_shared = True
else:
is_shared = False

# Check if Author exists
author_name = request.form['author_name']
author_country = request.form['author_country']

author_exists = Author.query.filter_by(name=author_name).first()

if not author_exists:
author = Author(country=author_country, name=author_name)
db.session.add(author)
db.session.commit()
id_author = db.session.query(Author.id).filter(Author.name == author_name)

id_author = db.session.query(Author.id).filter(Author.name == author_name)

# Check editorial details and existence
editorial_name = request.form['editorial_name']
editorial_exists = Editorial.query.filter_by(name=editorial_name).first()

if not editorial_exists:
editorial = Editorial(name=editorial_name)
db.session.add(editorial)
db.session.commit()
id_editorial = db.session.query(Editorial.id).filter(Editorial.name == editorial_name)

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

# Check genre existence
genre_name = request.form['book_genre']
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)
# End of existance checks

book.title = form.book_title.data
book.year = form.book_year.data
book.pages = form.book_pages.data
book.read = form.book_is_read.data
book.shared = form.book_is_shared.data
book.id_user = current_user.id

author.name = form.author_name.data
editorial.name = form.editorial_name.data
genre.name = form.genre_name.data

db.session.commit()


return redirect(url_for('main.profile'))


return render_template('edit_book.html',
book_id = book_id,
greeting = current_user.name,
book = book,
form = form,
)
else:
return redirect(url_for('auth.login'))
book = Book.query.get_or_404(book_id)
form = BookForm(author_id=book.id_author, editorial_id=book.id_editorial, genre_id=book.id_genre)
if form.validate_on_submit():
book.title = form.book_title.data
book.author.name = form.author_name.data
book.author.country = form.author_country.data
book.pages = form.book_pages.data
book.editorial.name = form.editorial_name.data
book.year = form.book_year.data
book.genre.name = form.book_genre.data
book.read = form.book_is_read.data == 'Si'
book.shared = form.book_is_shared.data == 'Si'
book.rating = form.book_rating.data
book.tags = form.book_tags.data
db.session.commit()
flash('El libro se ha actualizado exitosamente.', 'success')
return redirect(url_for('main.show_book', book_id=book_id))
return render_template('edit_book.html', form=form, book_id=book_id, book=book)


@main.route('/my_books')
Expand Down Expand Up @@ -302,11 +230,24 @@ def show_genre(genre_id):
else:
return redirect(url_for('main.login'))

@main.route('/test_utf8/<int:editorial_id>')
def test_utf8(editorial_id):
editorial_name = Editorial.query.get(editorial_id)
print(editorial_name)
@main.route('/test/<int:book_id>', methods = ['GET', 'POST'])
def test(book_id):
book = db.session.query(Book.id, 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(Genre)

return str(editorial_name.name)
form = BookForm(obj=book)

if form.validate_on_submit():
form.populate_obj(book)
db.session.commit()

return redirect(url_for('main.profile'))

return render_template('edit_book.html',
greeting = current_user.name,
book = book,
form = form,
)
4 changes: 4 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Book(db.Model):
id_editorial = db.Column(db.Integer, db.ForeignKey('editorial.id'), nullable=False)
id_genre = db.Column(db.Integer, db.ForeignKey('genre.id'), nullable=False)

author = db.relationship('Author', backref='books')
editorial = db.relationship('Editorial', backref='books')
genre = db.relationship('Genre', backref='books')

class Author(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(255), nullable=False)
Expand Down
30 changes: 20 additions & 10 deletions app/templates/edit_book.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@
<div class="body">
<div class="container has-test-justified">
<div class="container">
debug: {{book}}
<h1 class="title is-1">Editar libro</h1>
{% for b in book %}
<form method="POST" action="/edit_book">
{{b.title}}
{{b.genre_name}}
{{b.author_name}}
{{b.author_country}}
</form>
{% endfor %}
</div>
<form method="POST" action="{{ url_for('main.edit_book', book_id=book.id) }}">
{{ form.hidden_tag() }}
<br>
<p>{{ form.book_title.label }} {{ form.book_title(value=book.title) }}</p>
<p>{{ form.author_name.label }} {{ form.author_name(value=book.author.name) }}</p>
<input type="hidden" name="author_id" value="{{ book.id_author }}">
<p>{{ form.author_country.label }} {{ form.author_country(value=book.author.country) }}</p>
<p>{{ form.book_pages.label }} {{ form.book_pages(value=book.pages) }}</p>
<p>{{ form.editorial_name.label }} {{ form.editorial_name(value=book.editorial.name) }}</p>
<input type="hidden" name="editorial_id" value="{{ book.id_editorial }}">
<p>{{ form.book_year.label }} {{ form.book_year(value=book.year) }}</p>
<p>{{ form.book_genre.label }} {{ form.book_genre(value=book.genre.name) }}</p>
<input type="hidden" name="genre_id" value="{{ book.id_genre }}">
<p>{{ form.book_is_read.label }} {{ form.book_is_read(checked=book_is_read) }}</p>
<p>{{ form.book_is_shared.label }} {{ form.book_is_shared(checked=book_is_shared) }}</p>
<p>{{ form.book_rating.label }} {{ form.book_rating(value=book.rating) }}</p>
<p>{{ form.book_tags.label }} {{ form.book_tags(value=book.tags) }}</p>
{{ form.submit }}
</form>
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion app/templates/my_books.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ <h1 class="title">Mis Libros</h1>
<table class="table is-fullwidth">
<tbody>
<tr class="has-background-success-light">
<th>Autor</th>
<th>Título</th>
<th>Autor</th>
<th>Puntuación</th>
<th>Género</th>
<th>Editorial</th>
Expand Down
15 changes: 7 additions & 8 deletions app/templates/show_book.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h1 class="title">{{ item[1] }}</h1>
<h2 class="text-muted">{{ item[7] }} ({{ item[8] }})</h2>
<br></br>
<p><b>Género: </b>{{ item[10] }}</p>
<p><b>Editorial: </b>{{ item[8] }}</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>
Expand All @@ -21,13 +21,12 @@ <h2 class="text-muted">{{ item[7] }} ({{ item[8] }})</h2>

<hr>

<div class="container has-text-justified">
<h4 class="text-muted">Footer Menu:</h3>
<ul>
<li><a href="{{ url_for('main.edit_book', book_id = item[0]) }}">Editar Libro</a></li>
<li><a href="{{ url_for('main.delete_book', book_id = item[0]) }}">Borrar Libro</a></li>
</ul>
</div>
<nav class="navbar fixed-bottom navbar-light bg-light">
<div class="text-nowrap">
<a href="{{ url_for('main.edit_book', book_id = item[0]) }}">Editar Libro</a>
<a href="{{ url_for('main.delete_book', book_id = item[0]) }}">Borrar Libro</a>
</div>
</nav>
{% endfor %}
</div>
{% endblock %}
9 changes: 9 additions & 0 deletions app/templates/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends 'base.html' %}

{% block content %}

{% for b in book %}
{{ b.title }}
{% endfor %}

{% endblock %}
Binary file modified migrations/versions/__pycache__/5f7d46cc782b_.cpython-39.pyc
Binary file not shown.

0 comments on commit 5eddf79

Please sign in to comment.