forked from ryan-radloff/magic-bookshelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
275 lines (225 loc) · 9.32 KB
/
main.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
from flask import Flask, render_template, redirect, url_for, request
from forms import LoginForm, RegisterForm, BookForm, ChangePasswordForm, RequestForm
from flask_bootstrap import Bootstrap
from flask_login import UserMixin, login_user, LoginManager, login_required, current_user, logout_user
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from dotenv import load_dotenv
from sqlalchemy import create_engine, Column, Integer, String, Boolean, ForeignKey
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import os
from os.path import join, dirname
# TODO: figure out whats wrong with python-dotenv because it returns every string with quotes
# dotenv_path = join(dirname(__file__), '.env')
load_dotenv()
app = Flask(__name__)
Base = declarative_base()
app.config['SECRET_KEY'] = '8BYkEfBA6O6donzWlSihBXox7C0sKR6b'
API_KEY=os.getenv('KEY')
Bootstrap(app)
Session = sessionmaker()
# # Sqlalchamy database
# # TODO: Change the SQL database when done
s = "mysql+mysqlconnector://{username}:{password}@{server}/bookshelf_db".format(username = "root", password = os.getenv('PASSWORD'), server = "104.197.168.142:3306")
engine = create_engine(s)
engine.connect()
Session.configure(bind=engine)
# app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+mysqlconnector://{username}:{password}@{server}/bookshelf_db".format(username = "root", password = "p@xs8Ddz4.YVT-QweUZE", server = "104.197.168.142:3306")
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# db = SQLAlchemy(app)
# Login
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
session = Session()
return session.query(User).filter(User.user_id == user_id).first()
##CREATE TABLE IN DB
class User(UserMixin, Base):
__tablename__ = 'users'
user_id = Column(Integer, primary_key=True)
email = Column(String(250))
username = Column(String(25), unique=True)
password = Column(String(1000))
credits = Column(Integer)
totalcredit = Column(Integer)
is_authenticated = True
def get_id(self):
return (self.user_id)
class Book(Base):
__tablename__ = 'books'
book_id = Column(Integer, primary_key=True)
isbn = Column(Integer)
owner = Column(Integer, ForeignKey('users.user_id'))
# Below required once, when creating DB.
# db.create_all()
class Transaction(Base):
__tablename__ = 'transactions'
transaction_id = Column(Integer, primary_key=True)
address_to = Column(String(100))
address_from = Column(String(100))
complete = Column(Boolean)
seller = Column(Integer, ForeignKey('users.user_id'))
buyer = Column(Integer, ForeignKey('users.user_id'))
book = Column(Integer, ForeignKey('books.book_id'))
@app.route('/', methods = ["GET", "POST"])
def index():
# Todo: index should know if a user is logged in or not
# and conditionally render clickables
# print(User.query.get(1))
return render_template('index.html', user=current_user, msg=request.args.get('msg'))
@app.route('/create_listing', methods=["GET", "POST"])
@login_required
def create_listing():
form = BookForm()
if form.validate_on_submit():
new_book = Book(
isbn=int(form.isbn.data),
owner=current_user.user_id
)
session = Session()
session.add(new_book)
session.commit()
new_transaction = Transaction(
seller=current_user.user_id,
book=new_book.book_id,
address_from=form.streetNameNum.data + " " + form.city.data + ", " + form.state.data + " " + form.zipcode.data
)
session.add(new_transaction)
user = session.query(User).filter(User.user_id==current_user.user_id).first()
user.credits += 1
user.totalcredit += 1
session.commit()
return redirect(url_for("index", msg="The Magic Bookshelf📚 applauds your generosity! You have earned [✨Arcane Dust x1]"))
# Very important, make sure to initialize the field of the OWNER's
# associated user's ID after the post request. To be handled later
return render_template('create_listing.html', form=form)
# def post_listing():
@app.route('/data/', methods=['POST', 'GET'], strict_slashes=False)
# apparently strict_slashes=False is required here if debug=True...
@login_required
def data():
if request.method == 'GET':
return "Can't access directly! Post through /create-listing"
if request.method == 'POST':
form_data = request.form
return render_template('submitted_data.html', form_data=form_data)
@app.route('/register', methods=["GET", "POST"])
def register():
if current_user.is_authenticated == True:
return redirect(url_for("index"))
form = RegisterForm()
if form.validate_on_submit():
session = Session()
if session.query(User).filter(User.email==form.email.data).first():
#User already exists
msg = "User with this email already exist, log in instead!"
return redirect(url_for('login'))
hashed_salted_password = generate_password_hash(
form.password.data,
method='pbkdf2:sha256',
salt_length=10
)
new_user = User(
email=form.email.data,
username=form.username.data,
password=hashed_salted_password,
credits=0,
totalcredit=0
)
session.add(new_user)
session.commit()
login_user(new_user)
return redirect(url_for("index"))
return render_template("register.html", form=form, current_user=current_user)
@app.route('/login', methods=["GET", "POST"])
def login():
# DO not login in if alredy logged in
if current_user.is_authenticated:
return redirect(url_for("index"))
form = LoginForm()
msg = ""
if form.validate_on_submit():
email = form.email.data
password = form.password.data
session = Session()
# User from database
new_user = session.query(User).filter(User.email==email).first()
# Wrong email
if (not new_user) or (not check_password_hash(new_user.password, password)):
msg = "Wrong Username or Password"
return render_template('login.html', form=form, msg = msg)
else:
login_user(new_user)
return redirect(url_for("index"))
return render_template('login.html', form=form, msg = msg)
@app.route("/<string:name>/profile")
@login_required
def show_profile(name):
# DO not login in if alredy logged in
if not current_user.is_authenticated:
return redirect(url_for("login"))
session = Session()
ob=session.query(Book).filter(Book.owner == current_user.user_id)
return render_template('profile.html', current_user=current_user, owned_books=ob, bkrsrc=url_for('.static', filename='bookshelf.png'))
@app.route("/change_password", methods=["GET", "POST"])
@login_required
def change_password():
form = ChangePasswordForm()
session = Session()
if form.validate_on_submit():
hashed_salted_password = generate_password_hash(
form.password.data,
method='pbkdf2:sha256',
salt_length=10
)
# Test the below line of code
user = session.query(User).filter(User.user_id==current_user.user_id).first()
user.password = hashed_salted_password
session.commit()
return redirect(url_for("index"))
return render_template("change_password.html", form = form)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
@app.route('/book_list', methods=["GET", "POST"])
def listings():
session = Session()
return render_template('book_list.html', query=session.query(Book))
@app.route('/<string:id>/listing', methods=["GET", "POST"])
def listing(id):
session = Session()
return render_template('book_info.html', book=session.query(Book).filter(Book.book_id==id).first())
@app.route('/request_book/<string:id>', methods=["GET", "POST"])
@login_required
def request_book(id):
session = Session()
trans = session.query(Transaction).filter(Transaction.book==id).first()
if (trans.address_to):
return redirect(url_for("listings"))
form = RequestForm()
if form.validate_on_submit():
address = form.streetNameNum.data + " " + form.city.data + ", " + form.state.data + " " + form.zipcode.data
trans.address_to = address
trans.buyer = current_user.user_id
addressf = trans.address_from
session.commit()
return redirect(url_for("index"))
# Very important, make sure to initialize the field of the OWNER's
# associated user's ID after the post request. To be handled later
return render_template('request_book.html', form=form)
#TODO: implement the about page
@app.route('/about', methods=["GET", "POST"])
def about():
session = Session()
return render_template("about.html", session.query(Books).count())
#TODO: implement the success page once distance matrix decides to work
@app.route('/success', methods=["GET", "POST"])
def success():
addrto = request.args.get('addrto', None)
addrfrom = request.args.get('addrfrom', None)
return render_template("success.html", addrfrom=addrfrom, addrto=addrto, key=API_KEY)
if __name__ == "__main__":
app.run(debug=True)