-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
217 lines (161 loc) · 6.46 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import os
from flask import Flask, request, render_template, redirect, session
from lib.database_connection import get_flask_database_connection
from lib.booking_repository import BookingRepository
from lib.booking import Booking
from lib.space import Space
from lib.space_repository import SpaceRepository
from lib.user import User
from lib.user_repository import UserRepository
from datetime import datetime
# Create a new Flask app
app = Flask(__name__)
app.secret_key = 'super secret key'
# == Your Routes Here ==
# GET /index
# Returns the homepage
# Try it:
# ; open http://localhost:5001/index
@app.route('/', methods=['GET'])
def get_index():
return render_template('index.html')
#------------------- LOGIN ROUTES -------------------#
@app.route('/login')
def login():
return render_template('login.html')
@app.route('/login', methods=['POST'])
def login_post():
connection = get_flask_database_connection(app)
repo = UserRepository(connection)
username = request.form['username']
password = request.form['password']
if repo.check_password(username, password) == True:
user = repo.get_user_details(username)
# Set the user ID in session
session['user_id'] = user.id
session['username'] = user.username
# return render_template('spaces.html')
return redirect(location="/spaces")
else:
errors = "Incorrect username or password"
return render_template('login.html', errors=errors)
@app.route('/logout', methods=['GET'])
def logout():
session['user_id'] = None
return render_template('index.html')
#------------------- SPACES ROUTES -------------------#
@app.route('/spaces')
def get_spaces():
connection = get_flask_database_connection(app)
repository = SpaceRepository(connection)
list_of_spaces = repository.all()
list_of_spaces_reverse = list_of_spaces[::-1]
return render_template('spaces.html', spaces=list_of_spaces_reverse)
@app.route('/create_space')
def get_create_space():
return render_template('/create_space.html')
@app.route('/create_space', methods=["POST"])
def create_space():
connection = get_flask_database_connection(app)
repository = SpaceRepository(connection)
name = request.form["name"]
description = request.form["description"]
price = request.form["price"]
user_id = session["user_id"]
space = Space(None, name, description, price, user_id)
# try:
space = repository.create(space)
print(space)
return render_template("/create_space_success.html", id = space)
# except Exception as e:
# error = str(e)
# return render_template("/create_space.html", error=error)
# @app.route('/create_space_success')
# def create_space_successful():
# return render_template('/create_space_success.html)')
@app.route('/spaces/<int:id>')
def view_space(id):
connection = get_flask_database_connection(app)
repository = SpaceRepository(connection)
space = repository.find(id)
return render_template("/view_space.html", space=space)
#------------------- REGISTER ROUTES -------------------#
@app.route('/register')
def get_register_page():
return render_template('/register.html')
@app.route('/register', methods=["POST"])
def register_user():
connection = get_flask_database_connection(app)
repository = UserRepository(connection)
username = request.form["username"]
email = request.form["email"]
password = request.form["password"]
user = User(None, username, email, password)
try:
repository.create_user(user)
return render_template("/register_success.html")
except Exception as e:
error = str(e)
return render_template("/register.html", error=error)
@app.route('/register_success')
def register_successful():
return render_template('/register_success.html)')
#------------------- BOOKING ROUTES -------------------#
@app.route('/booking/<int:space_id>', methods=["GET"])
def get_booking_form(space_id):
return render_template('booking_form.html', user_id = session["user_id"], space_id = space_id)
@app.route('/user_bookings/', methods=['GET'])
def get_all_by_id():
connection = get_flask_database_connection(app)
repo = BookingRepository(connection)
users_bookings = repo.all_by_id(session["user_id"])
print(users_bookings)
return render_template('user_bookings.html', users_bookings=users_bookings)
@app.route('/post_booking/<int:space_id>', methods=["POST"])
def post_booking(space_id):
connection = get_flask_database_connection(app)
repo = BookingRepository(connection)
date = request.form['date_booked']
date_str = request.form['date_booked']
date = datetime.strptime(date_str, '%Y-%m-%d').date()
booking = Booking(
None,
date,
'pending',
session["user_id"],
space_id
)
try:
booking = repo.create(booking)
print(repo.is_date_unavailable(booking))
return redirect(f'/booking_complete/{booking.id}')
except Exception as e:
error = str(e)
# pass error in html
print(repo.is_date_unavailable(booking))
return render_template('booking_form.html', space_id=space_id, error=error)
# This page is seen by the renter
@app.route('/booking_complete/<int:id>', methods=['GET'])
def get_booking(id):
connection = get_flask_database_connection(app)
repo = BookingRepository(connection)
booking = repo.get_booking(id)
print(booking)
return render_template('booking.html', booking = booking)
@app.route('/booking_confirmed/<int:booking_id>', methods=["GET"])
def put_booking(booking_id):
connection = get_flask_database_connection(app)
repo = BookingRepository(connection)
booking = repo.confirm_booking(booking_id)
get_all_by_id = repo.all_by_id(session["user_id"])
return render_template('user_bookings.html', users_bookings=get_all_by_id)
# Could not build url for endpoint '/booking_confirmed' with values ['booking_id']. Did you mean 'get_booking_form' instead?
# space needs to display available bookings (get) via user id
# user_id to find their spaces -> space_id to find available bookings -> for that space
# These lines start the server if you run this file directly
# They also start the server configured to use the test database
# if started in test mode.
if __name__ == '__main__':
app.run(debug=True, port=int(os.environ.get('PORT', 5001)))
app.config['SESSION_TYPE'] = 'filesystem'
session.init_app(app)