-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
171 lines (134 loc) · 5.64 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
from flask import Flask, render_template, request, flash, redirect, url_for, session, logging
from data import Dishes
from flask_sqlalchemy import SQLAlchemy
from wtforms import Form, StringField, TextAreaField, PasswordField, validators, SubmitField, IntegerField
from passlib.hash import sha256_crypt
app = Flask(__name__)
app.config['SECRET_KEY'] = 'God save you from Forms'
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:1033@localhost/learning'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_CURSORCLASS'] = 'DictCursor'
db = SQLAlchemy(app)
Dishes = Dishes()
@app.route('/')
@app.route('/home')
def home():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/dishes')
def dishes():
return render_template('dishes.html', Dishes=Dishes)
@app.route('/dish/<string:id>')
def dish(id):
return render_template('dish.html', id=id)
# Register Form class
class RegisterForm(Form):
name = StringField('Name', [validators.DataRequired(), validators.Length(min=1, max=50)])
email = StringField('Email', [validators.DataRequired(), validators.Length(min=6, max=50)])
username = StringField('Username', [validators.Length(min=4, max=25)])
address = TextAreaField('Address', [validators.DataRequired(), validators.Length(max=1000)])
contact = IntegerField('Contact')
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Passwords do not match')
])
confirm = PasswordField('Confirm Password')
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
# Using WtForms for registration
if request.method == 'POST' and form.validate():
name = form.name.data
email = form.email.data
username = form.username.data
address = form.address.data
contact = form.contact.data
password = sha256_crypt.encrypt(str(form.password.data))
# Create db, execute query
db.create_all()
usr = Flask_Users(None, name, email, username, address, contact, password)
db.session.add(usr)
# Commit and close connection
db.session.commit()
flash('You are now registered', 'success')
return redirect(url_for('home'))
return render_template("register.html", form=form)
class Flask_Users(db.Model):# Calling User DB
__tablename__ = "flask_users"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
email = db.Column(db.String, nullable=False)
username = db.Column(db.String, nullable=False)
address = db.Column(db.String, nullable=False)
contact = db.Column(db.Integer, nullable=False,unique=True)
password = db.Column(db.String, nullable=False)
def __init__(self,id,name,email,username,address,contact,password):
self.id = id
self.name = name
self.email = email
self.username = username
self.address = address
self.contact = contact
self.password = password
def __repr__(self):
return f'Username:- {self.username}\n Email:- {self.email}\n Password:- {self.password}'
#REGISTRATION PROCESS ENDS DO NOT MOVE ANY CODE FROM ABOVE
# # Login Form class
# class LoginForm(Form):
# username = StringField('Username', [validators.DataRequired()])
# password = PasswordField('Password', [validators.DataRequired()])
# User Login
@app.route('/login', methods=['GET','POST'])
def login():
if request.method == 'POST':
# get form fields
username = request.form['username']
password_candidate = request.form['password']
# getting user
login_user = Flask_Users.query.filter_by(username=username).one()
if login_user is not None:
# Get stored hash
password = login_user.password # is this right way to get one field from login_user??
# Compare passwords
password_match = sha256_crypt.verify(password_candidate, password)
if password_match:
app.logger.info('PASSWORD MATCHED')
else:
app.logger.info('NO user')
return render_template('login.html')
# class Flask_Dishes(db.Model):
# id = db.Column(db.Integer, primary_key=True)
# title = db.Column(db.String,nullable=False)
# body = db.Column(db.String,nullable=False)
# price = db.Column(db.Integer,nullable=False)
#
# def __init__(self, id, title, body, price):
# self.id = id
# self.title = title
# self.body = body
# self.price = price
#
# def __repr__(self):
# return f'Name:- {self.title}\n Price:- {self.price}'
#
#
# class Flask_Orders(db.Model):
# id = db.Column(db.Integer, primary_key=True)
# user_id = db.Column(db.Integer, db.ForeignKey('Flask_Users.id'),nullable=False)
# dish_id = db.Column(db.Integer, db.ForeignKey('Flask_Dishes.id'), nullable=False)
# qty = db.Column(db.Integer, nullable=False)
# qty_price = db.Column(db.Integer)
# total_price = db.Column(db.Integer, nullable=False)
#
# def __init__(self, id, user_id, dish_id, qty, total_price):
# self.id = id
# self.user_id = user_id
# self.dish_id = dish_id
# self.total_price = total_price
#
# def __repr__(self):
# return f'Username:- {self.username}\n Email:- {self.email}\n Password:- {self.password}'
if __name__ == '__main__':
app.run(port=4995, debug=True)