-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
290 lines (227 loc) · 9.31 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from flask import Flask, render_template, request, redirect, url_for, session, flash, Blueprint,jsonify
import sqlite3
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
from checkout import checkout_blueprint
app = Flask(__name__)
app.register_blueprint(checkout_blueprint, url_prefix='/checkout')
app.secret_key = 'your_secret_key'
def get_user_by_username(username):
connection = sqlite3.connect('database/movie_ticket.db')
cursor = connection.cursor()
cursor.execute('SELECT * FROM users WHERE username=?', (username,))
user = cursor.fetchone()
connection.close()
return user
def get_movies():
connection = sqlite3.connect('database/movie_ticket.db')
cursor = connection.cursor()
cursor.execute('SELECT * FROM movies')
movies = cursor.fetchall()
connection.close()
return movies
def get_movie_by_id(movie_id):
connection = sqlite3.connect('database/movie_ticket.db')
cursor = connection.cursor()
cursor.execute('SELECT * FROM movies WHERE id=?', (movie_id,))
movie = cursor.fetchone()
connection.close()
return movie
def add_booking(user_id, movie_id, quantity, total):
connection = sqlite3.connect('database/movie_ticket.db')
cursor = connection.cursor()
# already booked the movie
cursor.execute('''
SELECT quantity, total FROM bookings
WHERE user_id = ? AND movie_id = ?
''', (user_id, movie_id))
existing_booking = cursor.fetchone()
if existing_booking:
# If existing, update the quantity and total
existing_quantity, existing_total = existing_booking
new_quantity = existing_quantity + quantity
new_total = existing_total + total
cursor.execute('''
UPDATE bookings
SET quantity = ?, total = ?
WHERE user_id = ? AND movie_id = ?
''', (new_quantity, new_total, user_id, movie_id))
else:
# insert a new one
cursor.execute('''
INSERT INTO bookings (user_id, movie_id, quantity, total)
VALUES (?, ?, ?, ?)
''', (user_id, movie_id, quantity, total))
connection.commit()
connection.close()
@app.route('/')
def home():
return render_template('index.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
# hashed_password = generate_password_hash(password)
connection = sqlite3.connect('database/movie_ticket.db')
cursor = connection.cursor()
try:
cursor.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, password))
connection.commit()
connection.close()
return redirect(url_for('login'))
except sqlite3.IntegrityError:
flash('Username already exists!')
connection.close()
return render_template('register.html')
return render_template('register.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = get_user_by_username(username)
# Compare the stored password with the input password
if user and user[2] == password:
session['user_id'] = user[0]
return redirect(url_for('movies'))
else:
flash('Invalid credentials!')
return redirect(url_for('login'))
return render_template('login.html')
@app.route('/logout')
def logout():
session.pop('user_id', None)
return redirect(url_for('home'))
@app.route('/booking_success')
def booking_success():
return render_template('booking_success.html')
@app.route('/movies')
def movies():
if 'user_id' not in session:
return redirect(url_for('login'))
movie_list = get_movies()
return render_template('movies.html', movies=movie_list)
@app.route('/book/<movie_id>', methods=['GET', 'POST'])
def book(movie_id):
if 'user_id' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
quantity = int(request.form['quantity'])
movie = get_movie_by_id(movie_id)
if quantity > movie[3]: # available_seats
flash('Not enough seats available!')
return redirect(url_for('book', movie_id=movie_id))
total = movie[2] * quantity
user_id = session['user_id']
movie_id = movie[0]
add_booking(user_id, movie_id, quantity, total)
connection = sqlite3.connect('database/movie_ticket.db')
cursor = connection.cursor()
new_available_seats = movie[3] - quantity
cursor.execute('UPDATE movies SET available_seats=? WHERE id=?', (new_available_seats, movie_id))
connection.commit()
connection.close()
return redirect(url_for('checkout'))
movie = get_movie_by_id(movie_id)
return render_template('book_ticket.html', movie=movie)
def add_status_column_if_missing():
connection = sqlite3.connect('database/movie_ticket.db')
cursor = connection.cursor()
# Check if the 'status' column exists in the 'bookings' table
cursor.execute("PRAGMA table_info(bookings);")
columns = [column[1] for column in cursor.fetchall()]
if 'status' not in columns:
# If 'status' column doesn't exist, add it
cursor.execute('ALTER TABLE bookings ADD COLUMN status TEXT;')
connection.commit()
connection.close()
add_status_column_if_missing()
@app.route('/decrement_quantity/<int:booking_id>', methods=['POST'])
def decrement_quantity(booking_id):
if 'user_id' not in session:
return redirect(url_for('login'))
conn = sqlite3.connect('database/movie_ticket.db')
cursor = conn.cursor()
# current booking details
cursor.execute("""
SELECT bookings.quantity, bookings.total, movies.price
FROM bookings
JOIN movies ON bookings.movie_id = movies.id
WHERE bookings.id = ?
""", (booking_id,))
booking = cursor.fetchone()
if booking:
current_quantity = booking[0]
current_total = booking[1]
price_per_ticket = booking[2]
# If quantity > 1, decrement the quantity
if current_quantity > 1:
new_quantity = current_quantity - 1
new_total = current_total - price_per_ticket
cursor.execute("""
UPDATE bookings
SET quantity = ?, total = ?
WHERE id = ?
""", (new_quantity, new_total, booking_id))
else:
# delete the booking entry
cursor.execute("DELETE FROM bookings WHERE id = ?", (booking_id,))
conn.commit()
conn.close()
return redirect(url_for('checkout'))
from flask import flash, redirect, url_for
@app.route('/checkout', methods=['GET', 'POST'])
def checkout():
if 'user_id' not in session:
return redirect(url_for('login'))
user_id = session['user_id']
try:
connection = sqlite3.connect('database/movie_ticket.db')
cursor = connection.cursor()
cursor.execute('''
SELECT b.quantity, b.total, m.name, b.id
FROM bookings b
JOIN movies m ON b.movie_id = m.id
WHERE b.user_id = ?
''', (user_id,))
bookings = cursor.fetchall()
if not bookings:
# flash('You have no bookings yet.')
return redirect(url_for('home')) # Redirect if no bookings found
total_amount = sum([booking[1] for booking in bookings])
connection.close()
except sqlite3.Error as e:
flash(f"Database error: {e}")
return redirect(url_for('home'))
if request.method == 'POST':
payment_method = request.form['payment_method']
try:
success = True
if success:
connection = sqlite3.connect('database/movie_ticket.db')
cursor = connection.cursor()
for booking in bookings:
cursor.execute('''
INSERT INTO purchases (booking_id, payment_method, amount, transaction_date, status)
VALUES (?, ?, ?, ?, ?)
''', (booking[3], payment_method, booking[1], datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'Success'))
# Update booking status to "Completed"
cursor.execute('''
UPDATE bookings
SET status = "Completed"
WHERE user_id = ? AND status != "Completed"
''', (user_id,))
connection.commit()
connection.close()
flash("Your payment has been processed successfully!")
return redirect(url_for('home')) # Redirect to homepage after payment success
else:
flash("Payment failed. Please try again later.")
return redirect(url_for('checkout')) # Stay on checkout if payment fails
except sqlite3.Error as e:
flash(f"Database error during payment processing: {e}")
return redirect(url_for('home')) # Redirect to homepage on DB error
return render_template('checkout.html', bookings=bookings, total_amount=total_amount)
if __name__ == '__main__':
app.run(debug=True)