Skip to content

Commit 7ebabfa

Browse files
committed
Addding code of lecture5
1 parent 22df5dc commit 7ebabfa

File tree

104 files changed

+2162
-26
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+2162
-26
lines changed

lecture4/airline1/application.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import os
2+
3+
from flask import Flask, render_template, request
4+
from sqlalchemy import create_engine
5+
from sqlalchemy.orm import scoped_session, sessionmaker
6+
7+
app = Flask(__name__)
8+
9+
engine = create_engine(os.getenv("DATABASE_URL"))
10+
db = scoped_session(sessionmaker(bind=engine))
11+
12+
13+
@app.route("/")
14+
def index():
15+
flights = db.execute("SELECT * FROM flights").fetchall()
16+
return render_template("index.html", flights=flights)
17+
18+
19+
@app.route("/book", methods=["POST"])
20+
def book():
21+
"""Book a flight."""
22+
23+
# Get form information.
24+
name = request.form.get("name")
25+
try:
26+
flight_id = int(request.form.get("flight_id"))
27+
except ValueError:
28+
return render_template("error.html", message="Invalid flight number.")
29+
30+
# Make sure flight exists.
31+
if db.execute("SELECT * FROM flights WHERE id = :id", {"id": flight_id}).rowcount == 0:
32+
return render_template("error.html", message="No such flight with that id.")
33+
db.execute("INSERT INTO passengers (name, flight_id) VALUES (:name, :flight_id)",
34+
{"name": name, "flight_id": flight_id})
35+
db.commit()
36+
return render_template("success.html")
37+
38+
39+
@app.route("/flights")
40+
def flights():
41+
"""List all flights."""
42+
flights = db.execute("SELECT * FROM flights").fetchall()
43+
return render_template("flights.html", flights=flights)
44+
45+
46+
@app.route("/flights/<int:flight_id>")
47+
def flight(flight_id):
48+
"""List details about a single flight."""
49+
50+
# Make sure flight exists.
51+
flight = db.execute("SELECT * FROM flights WHERE id = :id", {"id": flight_id}).fetchone()
52+
if flight is None:
53+
return render_template("error.html", message="No such flight.")
54+
55+
# Get all passengers.
56+
passengers = db.execute("SELECT name FROM passengers WHERE flight_id = :flight_id",
57+
{"flight_id": flight_id}).fetchall()
58+
return render_template("flight.html", flight=flight, passengers=passengers)
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends "layout.html" %}
2+
3+
{% block title %}
4+
Error
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>Error</h1>
9+
{{ message }}
10+
{% endblock %}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% extends "layout.html" %}
2+
3+
{% block title %}
4+
Flight
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>Flight Details</h1>
9+
10+
<ul>
11+
<li>Origin: {{ flight.origin }}</li>
12+
<li>Destination: {{ flight.destination }}</li>
13+
<li>Duration: {{ flight.duration}} minutes</li>
14+
</ul>
15+
16+
<h2>Passengers</h2>
17+
<ul>
18+
{% for passenger in passengers %}
19+
<li>{{ passenger.name }}</li>
20+
{% else %}
21+
<li>No passengers.</li>
22+
{% endfor %}
23+
</ul>
24+
25+
{% endblock %}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% extends "layout.html" %}
2+
3+
{% block title %}
4+
Flights
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>All Flights</h1>
9+
10+
<ul>
11+
{% for flight in flights %}
12+
<li>
13+
<a href="{{ url_for('flight', flight_id=flight.id) }}">
14+
{{ flight.origin }} to {{ flight.destination }}
15+
</a>
16+
</li>
17+
{% endfor %}
18+
</ul>
19+
20+
{% endblock %}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{% extends "layout.html" %}
2+
3+
{% block title %}
4+
Flights
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>Book a Flight</h1>
9+
10+
<form action="{{ url_for('book') }}" method="post">
11+
12+
<div class="form-group">
13+
<select class="form-control" name="flight_id">
14+
{% for flight in flights %}
15+
<option value="{{ flight.id }}">{{ flight.origin }} to {{ flight.destination }}</option>
16+
{% endfor %}
17+
</select>
18+
</div>
19+
20+
<div class="form-group">
21+
<input class="form-control" name="name" placeholder="Passenger Name">
22+
</div>
23+
24+
<div class="form-group">
25+
<button class="btn btn-primary">Book Flight</button>
26+
</div>
27+
28+
</form>
29+
{% endblock %}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>{% block title %}{% endblock %}</title>
5+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
6+
</head>
7+
<body>
8+
<div class="container">
9+
{% block body %}
10+
{% endblock %}
11+
</div>
12+
</body>
13+
</html>
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends "layout.html" %}
2+
3+
{% block title %}
4+
Success!
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>Success!</h1>
9+
10+
You have successfully booked your flight.
11+
{% endblock %}
Binary file not shown.
974 Bytes
Binary file not shown.

lecture4/airline2/application.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from flask import Flask, render_template, request
2+
from models import *
3+
4+
app = Flask(__name__)
5+
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("DATABASE_URL")
6+
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
7+
db.init_app(app)
8+
9+
10+
@app.route("/")
11+
def index():
12+
flights = Flight.query.all()
13+
return render_template("index.html", flights=flights)
14+
15+
16+
@app.route("/book", methods=["POST"])
17+
def book():
18+
"""Book a flight."""
19+
20+
# Get form information.
21+
name = request.form.get("name")
22+
try:
23+
flight_id = int(request.form.get("flight_id"))
24+
except ValueError:
25+
return render_template("error.html", message="Invalid flight number.")
26+
27+
# Make sure the flight exists.
28+
flight = Flight.query.get(flight_id)
29+
if flight is None:
30+
return render_template("error.html", message="No such flight with that id.")
31+
32+
# Add passenger.
33+
passenger = Passenger(name=name, flight_id=flight_id)
34+
db.session.add(passenger)
35+
db.session.commit()
36+
return render_template("success.html")
37+
38+
39+
@app.route("/flights")
40+
def flights():
41+
"""List all flights."""
42+
flights = Flight.query.all()
43+
return render_template("flights.html", flights=flights)
44+
45+
46+
@app.route("/flights/<int:flight_id>")
47+
def flight(flight_id):
48+
"""List details about a single flight."""
49+
50+
# Make sure flight exists.
51+
flight = Flight.query.get(flight_id)
52+
if flight is None:
53+
return render_template("error.html", message="No such flight.")
54+
55+
# Get all passengers.
56+
passengers = Passenger.query.filter_by(flight_id=flight_id).all()
57+
return render_template("flight.html", flight=flight, passengers=passengers)

lecture4/airline2/models.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
3+
from flask import Flask
4+
from flask_sqlalchemy import SQLAlchemy
5+
6+
db = SQLAlchemy()
7+
8+
9+
class Flight(db.Model):
10+
__tablename__ = "flights"
11+
id = db.Column(db.Integer, primary_key=True)
12+
origin = db.Column(db.String, nullable=False)
13+
destination = db.Column(db.String, nullable=False)
14+
duration = db.Column(db.Integer, nullable=False)
15+
16+
17+
class Passenger(db.Model):
18+
__tablename__ = "passengers"
19+
id = db.Column(db.Integer, primary_key=True)
20+
name = db.Column(db.String, nullable=False)
21+
flight_id = db.Column(db.Integer, db.ForeignKey("flights.id"), nullable=False)
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends "layout.html" %}
2+
3+
{% block title %}
4+
Error
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>Error</h1>
9+
{{ message }}
10+
{% endblock %}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% extends "layout.html" %}
2+
3+
{% block title %}
4+
Flight
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>Flight Details</h1>
9+
10+
<ul>
11+
<li>Origin: {{ flight.origin }}</li>
12+
<li>Destination: {{ flight.destination }}</li>
13+
<li>Duration: {{ flight.duration}} minutes</li>
14+
</ul>
15+
16+
<h2>Passengers</h2>
17+
<ul>
18+
{% for passenger in passengers %}
19+
<li>{{ passenger.name }}</li>
20+
{% else %}
21+
<li>No passengers.</li>
22+
{% endfor %}
23+
</ul>
24+
25+
{% endblock %}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% extends "layout.html" %}
2+
3+
{% block title %}
4+
Flights
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>All Flights</h1>
9+
10+
<ul>
11+
{% for flight in flights %}
12+
<li>
13+
<a href="{{ url_for('flight', flight_id=flight.id) }}">
14+
{{ flight.origin }} to {{ flight.destination }}
15+
</a>
16+
</li>
17+
{% endfor %}
18+
</ul>
19+
20+
{% endblock %}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{% extends "layout.html" %}
2+
3+
{% block title %}
4+
Flights
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>Book a Flight</h1>
9+
10+
<form action="{{ url_for('book') }}" method="post">
11+
12+
<div class="form-group">
13+
<select class="form-control" name="flight_id">
14+
{% for flight in flights %}
15+
<option value="{{ flight.id }}">{{ flight.origin }} to {{ flight.destination }}</option>
16+
{% endfor %}
17+
</select>
18+
</div>
19+
20+
<div class="form-group">
21+
<input class="form-control" name="name" placeholder="Passenger Name">
22+
</div>
23+
24+
<div class="form-group">
25+
<button class="btn btn-primary">Book Flight</button>
26+
</div>
27+
28+
</form>
29+
{% endblock %}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>{% block title %}{% endblock %}</title>
5+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
6+
</head>
7+
<body>
8+
<div class="container">
9+
{% block body %}
10+
{% endblock %}
11+
</div>
12+
</body>
13+
</html>
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends "layout.html" %}
2+
3+
{% block title %}
4+
Success!
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>Success!</h1>
9+
10+
You have successfully booked your flight.
11+
{% endblock %}

0 commit comments

Comments
 (0)