-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·279 lines (242 loc) · 9.34 KB
/
server.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
from flask import Flask, render_template, session, request, redirect, flash
import json, sqlite3
import bcrypt
import pandas as pd
import sqlalchemy as sa
from pandas.io import sql
from forms import RegisterForm, LogInForm, PageDataForms
# ========ADMIN LOG IN FOR TESTING=============
#User: Zandrenel
#Pass: Pt,Ohs!179
#========Current Generic Log In========
#User: Apples
#Pass: Apple!10
#storing the data from the LORE section of the site has been giving trouble or easily messed up,
#after submission to fix that I plan to replace it with sqlite3, the storage and retrieval from the
#tables is much simpler in comparison
#This app/site was only created for personal practice and an educational course
#There is no intent to deploy this at any time in the future
app = Flask(__name__)
app.secret_key='sdfwsfwn132423nkj2n42jkndosih2o9gdfgs'
message=""
LoreTabs=[]
# Main route to HomePage
@app.route('/')
def index():
with open('data/sideBarLinks.json','r') as sideBar:
links=json.loads(sideBar.read())
with open('data/mainSite.json','r') as mainText:
text=json.loads(mainText.read())
return render_template('Template_base.html',links=links,text=text,message=message)
#Route to the LorePage
@app.route("/Lore",methods=['GET','POST'])
def Lore():
#with open('data/Lore.json','r') as LoreData:
# Lore=json.loads(LoreData.read())
form = PageDataForms()
Lore = LoreDataBase()
return render_template('Lore.html',Lore=Lore,form=form)
# 1.try casting te things as you SELECT them from table
# 2.try just fetch all, it will always return as a tuple
def LoreDataBase():
DataBase = sqlite3.connect("data/PageData.sqlite")
TD = pd.DataFrame(columns=["Tab","Header","Text","Visible"])
TD.to_sql("Lore",DataBase,if_exists="append")
print("TD: "+TD)
T = pd.DataFrame(columns=["Tab","Visible"])
T.to_sql("LoreTabs",DataBase,if_exists="append")
print("T: "+T)
Tabs=pd.read_sql_query("SELECT * FROM LoreTabs",DataBase)
Data=pd.read_sql_query("SELECT * FROM Lore",DataBase)
print("Tabs: ")
print(Tabs)
print("Data: ")
print(Data)
print("---------------Individually---------------")
print(Tabs.empty)
print(Data.empty)
if ((Data.empty) and (Tabs.empty)):
InitializationText=['Texting Text TAB','This is a Test:Header','This text should be the body',True]
DataInitialize=[InitializationText[0],InitializationText[3]]
print(InitializationText)
print(DataInitialize)
Tabs.loc[0,'Tab']=InitializationText[0]
Tabs.loc[0,'Header']=InitializationText[1]
Tabs.loc[0,'Text']=InitializationText[2]
Tabs.loc[0,'Visible']=InitializationText[3]
Data.loc[0,'Tab']=InitializationText[0]
Data.loc[0,'Visible']=InitializationText[3]
print(Tabs)
print(Data)
TabData={}
x=0
for tab in LoreTabs:
LoreTabs.pop(x)
x=x+1
x=0
for tab in Data['Tab']:
print(tab)
if (Data.loc[x,"Tab"]==Tabs.loc[x,"Tab"]):
LoreTabs.append(Data.loc[x,"Tab"])
TabData[tab]=({str(Data.loc[x,"Header"]):str(Data.loc[x,"Text"])})
x=x+1
print(TabData)
DataBase.close()
return TabData
@app.route("/LoreDataBaseTabs",methods=['GET'])
def DataBaseTabs():
return LoreTabs
@app.route("/Lore/<string:mode>/<string:name>",methods=['POST','GET'])
def LoreMod(mode,name):
DataBase = sqlite3.connect("data/PageData.sqlite")
print("Starting Lore Function "+mode)
if mode=='editTab':
print("-------Processing Edit Tab-----------")
temp ='editTab'+name #editTab+'+Tab'
#form='editTab'+name+"Form"
print(temp)
#newTab=request.form[temp]
#print(newTab)
Tabs=pd.read_sql_query("SELECT * FROM LoreTabs",DataBase)
print(Tabs)
Tabs["Tab"].replace(name, value=newTab, inplace=True)
Data=pd.read_sql_query("SELECT * FROM Lore",DataBase)
Data["Tab"].replace(name, value=newTab, inplace=True)
print(Tabs)
print(Data)
Tabs.to_sql("LoreTabs",DataBase,if_exists="replace")
Data.to_sql("Lore",DataBase,if_exists="replace")
print("Committed!")
DataBase.close()
DataBase = sqlite3.connect("data/PageData.sqlite")
Tabs=pd.read_sql_query("SELECT * FROM LoreTabs",DataBase)
print(Tabs)
print(Data)
DataBase.close()
return redirect('/Lore')
# will route to the tools tab
@app.route("/Tools")
def tools():
with open('data/mainSite.json','r') as mainText:
text=json.loads(mainText.read())
return render_template('Tools.html',text=text)
# Will route to the logIn tab
@app.route("/LogIn", methods=['GET'])
def logIn():
form=LogInForm()
return render_template('LogIn.html',form=form)
# Will process the log in, if the user exists, and if after decryting the password the passwords match, the current session will be
# set to this user, if they are an admin the session admin will be set to true.
@app.route("/processLogIn", methods=['POST'])
def processLogIn():
form = LogInForm()
if form.validate_on_submit():
print('Started')
_user=request.form['username']
_pass=request.form['password']
if (findUser(_user) and bcrypt.checkpw(_pass.encode('utf-8'),(returnUser(_user))[1].encode('utf-8'))):
print('users match')
session['Admin']=returnUser(_user)[3]
session['username']=_user
print('Log in Sucess')
return redirect('/')
else:
print('logInFailed')
return redirect("/LogIn")
raise("Could not Log In.")
# Will return a boolean of if the user exists
def findUser(_user):
print('finding User')
LogData = sqlite3.connect("data/Users.sqlite")
cursor = LogData.cursor();
LogData.row_factory = sqlite3.Row
cursor.execute('SELECT * FROM users WHERE userName=?', (_user,))
user_ = cursor.fetchone()
print(user_)
LogData.close()
print('tetsing equality of users')
if user_:
print('user true')
return True
else:
print('user false')
return False
# will return the actual user's data if it exists
def returnUser(_user):
print('finding User')
LogData = sqlite3.connect("data/Users.sqlite")
cursor = LogData.cursor();
LogData.row_factory = sqlite3.Row
cursor.execute('SELECT * FROM users WHERE userName=?', (_user,))
user_ = cursor.fetchone()
print(user_)
#print(user_[0])
#print(user_[1])
LogData.close()
print('tetsing equality of users')
if user_:
print('user true')
return user_
else:
print('user false')
return None
# will pop the user Session and admin status
@app.route('/LogOut')
def logOut():
session.pop('username', None)
session.pop('Admin',None)
return redirect('/')
# register template
@app.route("/Register",methods=['GET'])
def Register():
form = RegisterForm()
return render_template('Register.html',form=form)
# will process the registration, if they exist won't register, otherwise if validated will add to the dataBase
@app.route("/processRegistration", methods=['POST'])
def processRegistration():
form = RegisterForm()
if form.validate_on_submit():
_user=request.form['username']
#_user = findUser(_user)
LogData = sqlite3.connect("data/Users.sqlite")
cursor = LogData.cursor();
cursor.execute("CREATE TABLE IF NOT EXISTS users(userName, password, email, admin, level)")
LogData.close()
if not findUser(_user):
_pass=request.form['password'].encode()
salt = bcrypt.gensalt()
_pass = bcrypt.hashpw(_pass, salt)
_email=request.form['email']
LogData = sqlite3.connect("data/Users.sqlite")
cursor = LogData.cursor();
#cursor.execute("CREATE TABLE users(userName, password, email, admin, level)")
if _user == "Zandrenel":
cursor.execute("INSERT INTO users VALUES(?,?,?,?,?)",(_user,_pass.decode(),_email, True, 10))
else:
cursor.execute("INSERT INTO users VALUES(?,?,?,?,?)",(_user,_pass.decode(),_email, False, 1))
LogData.commit()
LogData.close()
flash('Registered successfully.')
print('User should have been successfullly added.')
return redirect('/')
else:
print('User exists, can\'t register')
flash("User name Already Exists.")
return render_template("Register.html")
else:
message="One of the fields was invalid"
return render_template("Register.html", form=form, message=message)
@app.route("/Contact")
def Contact():
with open('data/mainSite.json','r') as Main:
data = json.loads(Main.read())
return render_template('Contact.html',data=data)
@app.route("/Profile")
def profile():
if session['username']:
user=returnUser(session['username'])
return render_template("Profile.html",user=user)
else:
return redirect("/")
if __name__=='__main__':
app.run(debug=True)