-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_copy.py
289 lines (234 loc) · 9.6 KB
/
run_copy.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
import bottle
import conf
#-----------------------------------------------------------------------------
# Get our components
# You may eventually wish to put these in their own directories and then load
# Each file separately
# For the template, we will keep them together
import model
#-----------------------------------------------------------------------------
'''
This file is the one to run to start your webserver
Keep it clean and keep it simple, you're going to have
Up to 5 people running around breaking this constantly
If it's all in one file, then things are going to be hard to fix
If in doubt, `import this`
'''
#-----------------------------------------------------------------------------
#Testing if this way runs the wsgi server
from bottle import route, get, post, request, redirect, static_file, error, template, response
import html
import model
#-----------------------------------------------------------------------------
'''
This file will handle our typical Bottle requests and responses
You should not have anything beyond basic page loads, handling forms and
maybe some simple program logic
'''
app = application = bottle.Bottle()
@app.route('/', method='GET')
def login():
if request.get_cookie("username",secret='m14AGroup6'):
username = request.get_cookie("username", secret='m14AGroup6')
return model.homepage(username)
else:
print("going to login")
return model.login()
#-----------------------------------------------------------------------------
@app.post('/')
def do_login():
username = html.escape(request.forms.get('username'))
if len(username) > 10:
return model.error()
password = html.escape(request.forms.get('password'))
if len(password) > 20:
return model.error()
#response.set_cookie("username",username, secret='m14AGroup6')
return model.login_check(username, password)
#-----------------------------------------------------------------------------
@app.post('/homepage')
def unit_add():
if request.get_cookie("username"):
username = request.get_cookie("username",secret='m14AGroup6')
unit_add = html.escape(request.forms.get('unit'))
if len(unit_add) != 8:
return model.error()
return model.addUnit(unit_add, username)
else:
return model.login()
#-----------------------------------------------------------------------------
@app.get('/forgot-password')
def forgot():
return model.forgot_password()
#-----------------------------------------------------------------------------
@app.get('/reset-password')
def forgot():
return model.reset_password()
#-----------------------------------------------------------------------------
@app.post('/forgot-pwd')
def reset_pass():
email = html.escape(request.forms.get("email"))
if len(email)>50:
return model.error("")
return model.reset_pass(email)
#-----------------------------------------------------------------------------
@app.get('/sign-up')
def sign_up():
return model.signup()
#-----------------------------------------------------------------------------
@app.get('/sign-uperror')
def sign_up():
return model.signup_error()
#-----------------------------------------------------------------------------
@app.get('/sign-up', method='POST')
def do_sign_up():
username = html.escape(request.forms.get('username'))
if len(username) > 10:
return model.error("Length Error")
password = html.escape(request.forms.get('password'))
if len(password) > 20:
return model.error("Length Error")
email = html.escape(request.forms.get('email'))
if len(email)>50:
return model.error("Length Error")
#Need to remove
if email is None:
email = "test123@gmail.com"
return model.signup_check(username, password, email)
#-----------------------------------------------------------------------------
@app.get('/homepage/<subject>')
def list_of_topics(subject):
if request.get_cookie("username",secret='m14AGroup6'):
username = request.get_cookie("username",secret='m14AGroup6')
return model.listTopics(subject, username)
else:
return model.login()
#-----------------------------------------------------------------------------
@app.post('/homepage/send/<subject>')
@app.post('/homepage/send/homepage/<subject>')
def get_post(subject):
if request.get_cookie("username",secret='m14AGroup6'):
title = html.escape(request.forms.get('title'))
if len(title) > 100:
return model.error("Length Error")
content = html.escape(request.forms.get('content'))
print(content)
if len(content) > 250:
return model.error("Length Error")
username = request.get_cookie("username",secret='m14AGroup6')
return model.new_post(subject, title, content, username)
else:
return model.login()
#-----------------------------------------------------------------------------
@app.post('/homepage/<subject>/<value>')
def topic(subject, value):
if request.get_cookie("username",secret='m14AGroup6'):
title = html.escape(request.forms.get('title'))
if len(title) > 100:
return model.error("Length Error")
username = request.get_cookie("username",secret='m14AGroup6')
return model.content(subject, title, username)
else:
return model.login()
#-----------------------------------------------------------------------------
@app.post('/homepage/comment/<subject>/<value>')
def add_comment(subject, value):
if request.get_cookie("username",secret='m14AGroup6'):
comment = html.escape(request.forms.get("comment"))
username = request.get_cookie("username",secret='m14AGroup6')
title = html.escape(request.forms.get("title"))
unit = html.escape(request.forms.get("unit"))
if len(title) > 100 or len(username)>10 or len(comment)>250:
return model.error("Length Error")
if unit!=subject:
return model.error("Length Error")
return model.new_comment(subject, title, comment,username)
else:
return model.login()
#-----------------------------------------------------------------------------
@app.get('/messages')
def list_users():
print("list of users")
if request.get_cookie("username",secret='m14AGroup6'):
username = request.get_cookie("username",secret='m14AGroup6')
return model.overview_messages(username)
else:
return model.login()
#-----------------------------------------------------------------------------
@app.post('/messages/<user>')
def view_chat(user):
print("view chat")
if request.get_cookie("username",secret='m14AGroup6'):
username = request.get_cookie("username",secret='m14AGroup6')
user_to_chat = html.escape(request.forms.get("message_user"))
if len(user_to_chat) > 10:
return model.error("Length Error")
return model.get_messages(username, user_to_chat)
else:
return model.login()
#-----------------------------------------------------------------------------
@app.post('/messages/send')
def new_message():
print("Enter controller for messages post")
if request.get_cookie("username",secret='m14AGroup6'):
username = request.get_cookie("username",secret='m14AGroup6')
user_to_chat = html.escape(request.forms.get("message_user"))
message = html.escape(request.forms.get("message"))
if len(message) > 100 or len(user_to_chat)>10:
return model.error("Length Error")
print("Going to model")
return model.new_message(username, user_to_chat, message)
else:
return model.login()
#-----------------------------------------------------------------------------
@app.get('/signout')
def signout():
response.delete_cookie("username",secret='m14AGroup6')
username = request.get_cookie("username")
print(username)
return model.login()
#-----------------------------------------------------------------------------
# Static file paths
#-----------------------------------------------------------------------------
# Allow image loading
@app.route('/img/<picture:path>')
def serve_pictures(picture):
return static_file(picture, root='img/')
#-----------------------------------------------------------------------------
#Allow everything just incase
@app.route('/<filename:path>')
def server_static(filename):
return static_file(filename, root='/')
#-----------------------------------------------------------------------------
# Allow javascript
@app.route('/js/<js:path>')
def serve_js(js):
return static_file(js, root='js/')
#-----------------------------------------------------------------------------
# Allow CSS
@app.route('/css/<css:path>')
def serve_css(css):
return static_file(css, root='css/')
#-----------------------------------------------------------------------------
@app.error(404)
@app.error(500)
@app.error(502)
@app.error(400)
@app.error(401)
@app.error(403)
@app.error(503)
def error404(error):
print(error)
return model.error("")
#-----------------------------------------------------------------------------
# It might be a good idea to move the following settings to a config file and then load them
# Change this to your IP address or 0.0.0.0 when actually hosting
host = conf.ip_conf() #'localhost'10.86.163.196
# Test port, change to the appropriate port to host
port = 80
# Turn this off for production
debug = False
#-----------------------------------------------------------------------------
#Run the server
if __name__ == "__main__":
app.run(host=host, port=port, debug=debug)