-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
305 lines (238 loc) · 7.73 KB
/
__init__.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""
This is the main script that runs the application
and handles URLS and paging.
"""
# Call vendor to add the dependencies to the classpath
import vendor
vendor.add('lib')
import random
import requests
import re
import json
from datetime import datetime
import pymongo
from bson.objectid import ObjectId
from utils import *
# Import the Flask Framework
from flask import Flask, render_template, request, jsonify, Response, redirect
app = Flask(__name__)
# Create mongoconnection
from private.mongoClientConnection import MongoClientConnection
client = MongoClientConnection().connection.atchannel
# Get captcha secret
from private.captchasecret import captcha
secret = captcha().secret
# Hex regex
reg = re.compile("[a-f0-9]{24}")
# Limits
charLimit = 20
channelLimit = 20
# Testing parameter passing to url
@app.route('/', methods=['GET'])
def index():
channelCount = client.channels.count()
postCount = client.messages.count()
latestPosts = list( client.messages.find(sort=[("time", -1)], limit=5) )
for i in range(len(latestPosts)):
post = latestPosts[i]["message"]
if len(post) > charLimit:
latestPosts[i]["message"] = latestPosts[i]["message"][:charLimit] + "..."
newestChannels = list( client.channels.find(limit=5, sort=[("time", -1)]) )
popularChannels = list( client.channels.find(limit=5, sort=[("seq", -1)]) )
return render_template("index.html",
channelCount=channelCount,
postCount=postCount,
latestPosts=latestPosts,
newestChannels=newestChannels,
popularChannels=popularChannels
)
@app.route('/<channel>', methods=['GET'])
def channel(channel="main"):
limit = 50
if not channelDoesExist(client, channel):
return "This channel does not exist", 404
description = client.channels.find_one({"_id": channel})["description"]
messages = get_posts(client, channel, 0, limit)
return render_template("channel.html",
messages = messages,
channel=channel,
description=description,
limit=limit
)
@app.route('/comments/<ID>', methods=['GET'])
def comments(ID=None):
if ID is None:
return "This post does no exist", 404
limit = request.args.get("limit")
start = request.args.get("start")
if limit is None:
limit = 50
if start is None:
start = 0
if client.messages.find({"_id": ObjectId(ID)}).count() <= 0:
return "The post with ID " + ID + " does not exist", 404
mainPost = get_one_post(client, ID)
comments = get_comments(client, ID, start, limit)
return render_template("comments.html",
mainPost=mainPost,
messages=comments,
inComments=True
)
@app.route('/submitchannel.html', methods=['GET'])
def submitchannel():
return render_template("submitchannel.html")
# Need to get rid of this l8er
@app.route('/channels.html', methods=['GET'])
def channelsRedirect():
return redirect("/channels")
@app.route('/channels/', methods=['GET'])
@app.route('/channels/<sort>/', methods=['GET'])
@app.route('/channels/<sort>/<int:page>', methods=['GET'])
def channels(sort="popular",page=0):
start = page*channelLimit
if sort == "latest":
channels = client.channels.find(sort=[("time", -1)], skip=start, limit=channelLimit)
else:
channels = client.channels.find(sort=[("seq", -1)], skip=start, limit=channelLimit)
return render_template("channels.html",
channels=list(channels) or [],
page=page,
limit=channelLimit,
sort=sort
)
@app.route('/about.html', methods=['GET'])
def about():
return render_template("about.html")
@app.route('/rules.html', methods=['GET'])
def rules():
return render_template("rules.html")
"""
Add a message to the database
"""
@app.route('/addPost', methods=['POST'])
def add_post():
if "name" in request.form and "message" in request.form and "time" in request.form and "channel" in request.form:
name = request.form["name"].strip()
message = request.form["message"].strip()
time = request.form["time"].strip()
channel = request.form["channel"].strip()
# Check validity
if name == "":
return "Invalid username"
if message == "":
return "Invalid message"
if not time.isdigit():
return "Invalid time"
if channel == "":
return "Invalid channel name"
# Increment the number of posts in the channel.
# The post number for the current post is the new number of posts.
count_update = client.channels.find_and_modify({"_id": channel}, {"$inc": {"seq": 1}}, new=True)
if count_update is None:
return "This channel does not exist"
# Actually add the post
ID = client.messages.insert({
"name": name,
"message": message,
"time": int(time),
"channel": channel,
"postNumber": int(count_update["seq"])
})
# Return empty string upon seccession
return ""
else:
return "Unsucessful insertion: Did not receive either the username, channel, message, or time of post."
# Add a Channel to the database
@app.route('/addChannel', methods=['POST'])
def add_channel():
if "channel" in request.form and "time" in request.form and "description" in request.form:
# Do google captcha magic first
if not "g-recaptcha-response" in request.form or request.form["g-recaptcha-response"] == "":
return "Please prove you are a human."
data = {
"response": request.form["g-recaptcha-response"],
"secret": secret,
"remoteip": request.remote_addr
}
response = requests.post("https://www.google.com/recaptcha/api/siteverify", data)
if not response.json()["success"]:
return "Sorry, Google does not think you are a human."
channel = request.form["channel"].strip()
time = int(request.form["time"].strip())
description = request.form["description"].strip()
if not channel.isalnum():
return "The channel name must contain only aphanumeric characters"
if description == "":
return "Please enter a channel description"
if channelDoesExist(client, channel):
return "This channel already exists"
client.channels.insert({
"_id": channel,
"seq": 0,
"time": time,
"description": description
})
return ""
else:
return "Unsucessful creation: the channel name or current date is not provided"
"""
Route for handling get requests.
"""
@app.route('/getPosts', methods=['GET'])
def get_posts_handler():
channel = request.args.get("channel")
start = request.args.get("start")
length = request.args.get("length")
if start is None or not start.isdigit():
return "Invalid starting index"
if length is None or not length.isdigit():
return "Invalid length"
if channel is None or channel.strip() == "":
return "Invalid channel name"
channel = channel.strip()
if not channelDoesExist(client, channel):
return "Channel does not exist"
start = int(start)
length = int(length)
messages = get_posts(client, channel, start, length)
return jsonify(
html=getPostsHTML(messages),
messagesCount=len(messages),
largestPostNumber=messages[-1]["postNumber"],
smallestPostNumber=messages[0]["postNumber"]
)
@app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
return 'Sorry, Nothing at this URL.', 404
@app.errorhandler(500)
def page_not_found(e):
"""Return a custom 500 error."""
return 'Sorry, unexpected error: {}'.format(e), 500
@app.context_processor
def utility_processor():
def randFaceNum():
return random.randrange(1,4);
def randID():
return '%09x' % random.randrange(16**9)
def prettifyTime(time):
"""
yyyy/mm/dd (Day) hh:mm:ss
hh is from 00-23
"""
return datetime.fromtimestamp(time/1000.0).strftime("%Y/%m/%d (%a) %H:%M:%S")
def randColor():
return random.choice([
"#bbffff", # light blue
"#b694da", # some purple
"#bde0b1", # light green
"#a0e1b4", # darker green
"#a63333", # reddish
"#d090c4", # pinkish
"#fffed9", # yellowish,
"#ffd39b", # lighty orange
"#74bbfb" # darker blue
])
return dict(randID=randID, prettifyTime=prettifyTime, randFaceNum=randFaceNum, randColor=randColor)
if __name__ == '__main__':
app.run()