forked from screenl/air_ticket_system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.py
144 lines (135 loc) · 6.14 KB
/
account.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
from . import sql
import pymysql
import re
import json
import os
import hashlib
from flask import (
Blueprint, flash, g, redirect, render_template, request, session, url_for
)
bp = Blueprint('account', __name__, url_prefix='/account')
@bp.route('/')
def account():
return 'hello'
@bp.route('/login/',methods=['GET','POST'])
def login():
'''
request(POST) data:
email (or username for staff)
password
type - the type of login
'''
if request.method=='POST' and 'password' in request.form:
type = request.form["type"]
password = hashlib.md5(request.form['password'].encode()).hexdigest()
if type != 'staff':
email = request.form['email']
else:
username = request.form['username']
#connection = pymysql.connect(host = config['host'], port = int(config['port']), user = config['user'], password = config['password'], database = 'air_ticket')
connection = sql.SQLConnection.Instance().conn
cursor = connection.cursor()
#print('SELECT * FROM % s WHERE email = % s AND password = % s', (type, email, password, ))
if type == 'customer':
cursor.execute('SELECT * FROM customer WHERE email = % s AND password = % s', (email, password, ))
elif type == 'agent':
cursor.execute('SELECT * FROM agent WHERE agent_email = % s AND password = % s', (email, password, ))
elif type == 'staff':
cursor.execute('SELECT * FROM airline_staff WHERE username = % s AND password = % s', (username, password, ))
account = cursor.fetchone()
if account:
print(account)
session['loggedin'] = True
session['type'] = type
if type == 'customer':
session['username'] = account['name']
session['email'] = account['email']
elif type == 'agent':
session['username'] = f"Agent {account['booking_agent_id']}"
elif type == 'staff':
session['username'] = account['username']
return redirect("/public/home")
else:
flash('Invalid username or password!')
return render_template('login.html')
@bp.route('/logout/')
def logout():
'''
remove user data from session
'''
print(session)
print(dict(session) == {})
session.clear()
print(session)
print(dict(session) == {})
return redirect(url_for('account.login'))
@bp.route('/register/',methods=['GET','POST'])
def register():
'''
request(POST) data:
a ton of user data (refer to sql)
type - the type of user
'''
flash('')
if request.method == 'POST':
type = request.form["type"]
#connection = pymysql.connect(host = config['host'], port = int(config['port']), user = config['user'], password = config['password'], database = "air_ticket")
connection = sql.SQLConnection.Instance().conn
cursor = connection.cursor()
if type == 'customer':
name = request.form['name']
email = request.form['email']
password = hashlib.md5(request.form['password'].encode()).hexdigest()
building_number = request.form['building_number']
street = request.form["street"]
city = request.form["city"]
state = request.form["state"]
phone_number = request.form["phone_number"]
passport_number = request.form["passport_number"]
passport_expiration = request.form["passport_expiration"]
passport_country = request.form["passport_country"]
date_of_birth = request.form["date_of_birth"]
cursor.execute('SELECT * FROM customer WHERE name = % s', (name, ))
account = cursor.fetchone()
if account:
flash('Account already exists !')
else:
cursor.execute(
'INSERT INTO customer (name, email, password, building_number, street, city, state, phone_number, passport_number, passport_expiration, passport_country, date_of_birth) VALUES (% s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
(name, email, password, building_number, street, city, state, phone_number, passport_number, passport_expiration, passport_country, date_of_birth, ))
connection.commit()
flash('You have successfully registered !')
return redirect(url_for('account.login'))
elif type == 'agent':
email = request.form['email']
password = hashlib.md5(request.form['password'].encode()).hexdigest()
cursor.execute('SELECT * FROM agent WHERE agent_email = % s', (email, ))
account = cursor.fetchone()
if account:
flash('Account already exists !')
else:
cursor.execute(
'INSERT INTO agent(agent_email, password) VALUES (% s, % s)',
(email, password, ))
connection.commit()
flash('You have successfully registered !')
return redirect(url_for('account.login'))
elif type == 'staff':
username = request.form['username']
airline_name = request.form['airline_name']
password = hashlib.md5(request.form['password'].encode()).hexdigest()
first_name = request.form['first_name']
last_name = request.form['last_name']
date_of_birth = request.form["date_of_birth"]
cursor.execute('SELECT * FROM airline_staff WHERE username = % s', (username, ))
account = cursor.fetchone()
if account:
flash('Account already exists !')
else:
cursor.execute(
'INSERT INTO airline_staff (username, airline_name, password, first_name, last_name, date_of_birth) VALUES (% s, % s, %s, %s, %s, %s)',
(username, airline_name, password, first_name, last_name, date_of_birth,))
connection.commit()
flash('You have successfully registered !')
return redirect(url_for('account.login'))
return render_template('register.html')