Skip to content

Commit e89a4fd

Browse files
authored
Add files via upload
1 parent 702944b commit e89a4fd

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

Diff for: day63/main.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from flask import Flask, render_template, request, redirect, url_for
2+
from flask_sqlalchemy import SQLAlchemy
3+
4+
app = Flask(__name__)
5+
6+
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///books.db"
7+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
8+
db = SQLAlchemy(app)
9+
10+
11+
class Book(db.Model):
12+
id = db.Column(db.Integer, primary_key=True)
13+
title = db.Column(db.String(250), unique=True, nullable=False)
14+
author = db.Column(db.String(250), nullable=False)
15+
rating = db.Column(db.Float, nullable=False)
16+
17+
18+
db.create_all()
19+
20+
21+
@app.route('/')
22+
def home():
23+
all_books = db.session.query(Book).all()
24+
return render_template("index.html", books=all_books)
25+
26+
27+
@app.route("/add", methods=["GET", "POST"])
28+
def add():
29+
if request.method == "POST":
30+
new_book = Book(
31+
title=request.form["title"],
32+
author=request.form["author"],
33+
rating=request.form["rating"]
34+
)
35+
db.session.add(new_book)
36+
db.session.commit()
37+
return redirect(url_for('home'))
38+
return render_template("add.html")
39+
40+
41+
@app.route("/edit", methods=["GET", "POST"])
42+
def edit():
43+
if request.method == "POST":
44+
book_id = request.form["id"]
45+
book_to_update = Book.query.get(book_id)
46+
book_to_update.rating = request.form["rating"]
47+
db.session.commit()
48+
return redirect(url_for('home'))
49+
book_id = request.args.get('id')
50+
book_selected = Book.query.get(book_id)
51+
return render_template("edit_rating.html", book=book_selected)
52+
53+
54+
@app.route("/delete")
55+
def delete():
56+
book_id = request.args.get('id')
57+
58+
book_to_delete = Book.query.get(book_id)
59+
db.session.delete(book_to_delete)
60+
db.session.commit()
61+
return redirect(url_for('home'))
62+
63+
64+
if __name__ == "__main__":
65+
app.run(debug=True)
66+

Diff for: day63/templates/add.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Add Book</title>
6+
</head>
7+
<body>
8+
<form action="{{ url_for('add') }}" method="POST">
9+
<label>Book Name</label>
10+
<input name="title" type="text">
11+
<label>Book Author</label>
12+
<input name="author" type="text">
13+
<label>Rating</label>
14+
<input name="rating" type="text">
15+
<button type="submit">Add Book</button>
16+
</form>
17+
</body>
18+
</html>

Diff for: day63/templates/edit_rating.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Edit Rating</title>
6+
</head>
7+
<body>
8+
<form action="{{ url_for('edit') }}" method="POST">
9+
<p>Book Name: {{book.title}}</p>
10+
<p>Current Rating {{book.rating}}</p>
11+
<input hidden="hidden" name="id" value="{{book.id}}">
12+
<input name="rating" type="text" placeholder="New Rating">
13+
<button type="submit">Change Rating</button>
14+
</form>
15+
</body>
16+
</html>

Diff for: day63/templates/index.html

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Library</title>
6+
</head>
7+
<body>
8+
<h1>My Library</h1>
9+
{% if books == []: %}
10+
<p>Library is empty.</p>
11+
{% endif %}
12+
<ul>
13+
{% for book in books %}
14+
<li>
15+
<a href="{{ url_for('delete', id=book.id) }}">Delete</a>
16+
{{book.title}} - {{book.author}} - {{book.rating}}/10
17+
<a href="{{ url_for('edit', id=book.id) }}">Edit Rating</a>
18+
</li>
19+
{% endfor %}
20+
</ul>
21+
<a href="{{ url_for('add') }}">Add New Book</a>
22+
</body>
23+
</html>

0 commit comments

Comments
 (0)