Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
484 lines (413 loc) · 16.3 KB
/
Copy pathserver.py
File metadata and controls
484 lines (413 loc) · 16.3 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
from bson.objectid import ObjectId
from flask import Flask, url_for, redirect, render_template, request, abort
from flask.ext import admin, login
from flask.ext.admin import helpers, expose
from service import Service
from werkzeug.security import generate_password_hash, check_password_hash
from wtforms import form, fields, validators
import pymongo
import requests
import creds
# Create Flask application
app = Flask(__name__, static_folder='static')
db = pymongo.MongoClient().yomote
# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = 'asdfyoloswag'
# the YoAPI
class Yo:
def __init__(self, token):
self.token = token
def number(self):
"""
Function to GET the the number of subscribers of the API user account.
Returns number of subscribers as an integer.
If request is unsuccessful, raises an error.
"""
number_url = "http://api.justyo.co/subscribers_count/?api_token=" + self.token
number = requests.get(number_url)
if number.status_code == requests.codes.ok:
return number.json()["result"]
else:
number.raise_for_status()
def yoall(self, *link):
"""
Function to send a Yo to all subscribers of the API user account.
If request is successful, returns true.
If request is unsuccessful, raises an error.
"""
yoall_data = {"api_token": self.token, "link": link}
yoall_url = "http://api.justyo.co/yoall/"
yoall = requests.post(yoall_url, data=yoall_data)
if yoall.status_code == requests.codes.created:
return True
else:
yoall.raise_for_status()
def youser(self, username, *link):
"""
Function to send a Yo to a specific username.
If request is successful, returns true.
If request is unsuccessful, raises an error.
"""
username = username.upper()
youser_data = {"api_token": self.token, "username": username, "link": link}
youser_url = "http://api.justyo.co/yo/"
youser = requests.post(youser_url, data=youser_data)
if youser.status_code == requests.codes.ok:
return True
else:
yoall.raise_for_status()
# Create user model.
class User():
"""
Fields
- _id
- yo_handle
- password
"""
def __init__(self, json):
if json is None:
self._none = True
else:
self._none = False
self._id = str(json['_id'])
self.yo_handle = json['yo_handle'].upper()
self.password = json['password']
def is_authenticated(self):
return not self._none
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return self._id if not self._none else None
# Required for administrative interface
def __unicode__(self):
return self.yo_handle if not self._none else ''
# Define the User password reset token database
class ResetPassword():
"""
Fields
- _id
- yo_handle
"""
def __init__(self, json):
if json is None:
self._none = True
else:
self._none = False
self._id = str(json['_id'])
self.yo_handle = json['yo_handle'].upper()
# Define login and registration forms (for flask-login)
class LoginForm(form.Form):
yo_handle = fields.TextField(validators=[validators.required()])
password = fields.PasswordField(validators=[validators.required()])
def validate_yo_handle(self, field):
user = self.get_user()
if user._none:
raise validators.ValidationError('Invalid user')
# we're comparing the plaintext pw with the the hash from the db
if not check_password_hash(user.password, self.password.data):
# to compare plain text passwords use
# if user.password != self.password.data:
raise validators.ValidationError('Invalid password')
def get_user(self):
cursor = db.users.find({'yo_handle': self.yo_handle.data.upper()})
if cursor.count() > 0:
return User(cursor.next())
else:
return User(None)
class RegistrationForm(form.Form):
yo_handle = fields.TextField(validators=[validators.required()])
password = fields.PasswordField(validators=[validators.required()])
def validate_yo_handle(self, field):
yh = self.yo_handle.data.upper()
if db.users.find({'yo_handle': yh}).count() > 0:
raise validators.ValidationError('Duplicate username')
class ResetForm(form.Form):
yo_handle = fields.HiddenField('',[validators.Required(),])
newpassword = fields.PasswordField(
'New Password', [validators.Required(),])
confirmpassword = fields.PasswordField(
'Confirm New Password',
[validators.Required(),
validators.EqualTo('newpassword',message='Password Must Match')])
class ForgetPasswordForm(form.Form):
yo_handle = fields.TextField('Yo Handle',[validators.Required(),])
# Initialize flask-login
def init_login():
login_manager = login.LoginManager()
login_manager.init_app(app)
# Create user loader function
@login_manager.user_loader
def load_user(user_id):
cursor = db.users.find({'_id': ObjectId(user_id)})
if cursor.count() > 0:
return User(cursor.next())
else:
return User(None)
# Create customized index view class that handles login & registration
class MyAdminIndexView(admin.AdminIndexView):
@expose('/')
def index(self):
if not login.current_user.is_authenticated():
return redirect(url_for('.login_view'))
return super(MyAdminIndexView, self).index()
@expose('/login/', methods=('GET', 'POST'))
def login_view(self):
# handle user login
form = LoginForm(request.form)
if helpers.validate_form_on_submit(form):
user = form.get_user()
login.login_user(user)
if login.current_user.is_authenticated():
return redirect(url_for('.index'))
link = "<p>Don\'t have an account?<br><a href='" + \
url_for('.register_view') + \
"'>Click here to register.</a></p>" + \
"<p>Forget your password?<br><a href='" + \
url_for('.get_token') + \
"'>Reset Here.</a></p>"
self._template_args['type'] = 'Log In'
self._template_args['form'] = form
self._template_args['link'] = link
return super(MyAdminIndexView, self).index()
@expose('/register/', methods=('GET', 'POST'))
def register_view(self):
form = RegistrationForm(request.form)
if helpers.validate_form_on_submit(form):
user = {
'yo_handle': form.yo_handle.data.upper(),
'password': generate_password_hash(form.password.data)
}
user['_id'] = db.users.insert(user)
user = User(user)
login.login_user(user)
return redirect(url_for('.index'))
link = ("<p>Already have an account?<br><a href='" +
url_for('admin.login_view') +
"'>Click here to log in.</a></p>")
self._template_args['type'] = 'Register'
self._template_args['form'] = form
self._template_args['link'] = link
return super(MyAdminIndexView, self).index()
@expose('/forgetpassword/',methods=('GET','POST'))
def get_token(self):
form=ForgetPasswordForm(request.values)
if helpers.validate_form_on_submit(form):
yosend = Yo(token=creds.yo_api_key)
uid = str(db.resettoken.insert(
{"yo_handle": str(form.yo_handle.data).upper()}))
yosend.youser(str(form.yo_handle.data).upper(),
"http://yomote.co/admin/reset/"+uid)
return redirect('/msg/Check%20your%20Yo%20to%20reset%20password!')
link = ("<p>Don\'t have an account?<br><a href='" +
url_for('admin.register_view') +
"'>Click here to register.</a></p>")
self._template_args['type'] = 'Reset Password'
self._template_args['form'] = form
self._template_args['link'] = link
return super(MyAdminIndexView, self).index()
@expose('/reset/<token>', methods=['GET', 'POST'])
def reset(self, token):
cursor = db.resettoken.find({'_id': ObjectId(token)})
count = cursor.count()
if count==1:
usrn = cursor.next()['yo_handle']
else:
return redirect('/sry/no%20such%20link%20exists')
form = ResetForm(request.values, yo_handle=usrn)
if request.method == 'POST' and form.validate():
handle = form.data['yo_handle'].upper()
psw = form.data['newpassword']
db.users.update({"yo_handle" : handle},
{'$set': {"password": generate_password_hash(psw)}})
return redirect('/')
self._template_args['type'] = 'Reset Password'
self._template_args['form'] = form
self._template_args['link'] = ''
return super(MyAdminIndexView, self).index()
return render_template("reset.html", form=form)
@expose('/logout/')
def logout_view(self):
login.logout_user()
return redirect(url_for('.index'))
# Flask views
@app.route('/')
def index():
services = map(Service,
db.services.find({'yo_handle': {'$exists': 1}}).limit(100))
is_logged = login.current_user.is_authenticated()
return render_template('index.html', services=services,
owner=(ObjectId(login.current_user._id) if is_logged else None),
c=('#' if is_logged else '/admin'), lg=is_logged)
@app.route('/code')
def get_code():
return redirect('https://github.com/CarletonDevX/yomote')
@app.route('/recent')
def recent_yos():
services = map(Service,
db.services.find(
{'yo_handle': {'$exists': 1}}
).sort('ts', pymongo.DESCENDING).limit(100))
is_logged = login.current_user.is_authenticated()
return render_template('services.html', services=services,
owner=(ObjectId(login.current_user._id) if is_logged else None))
@app.route('/hot')
def hot_yos():
services = map(Service,
db.services.find(
{'yo_handle': {'$exists': 1}}
).sort('rating', pymongo.DESCENDING).limit(100))
is_logged = login.current_user.is_authenticated()
return render_template('services.html', services=services,
owner=(ObjectId(login.current_user._id) if is_logged else None))
@app.route('/mine')
def my_yos():
if login.current_user.is_authenticated():
id_ = ObjectId(login.current_user._id)
services = map(Service,
db.services.find(
{'owner': id_}
).sort('ts', pymongo.DESCENDING))
return render_template('services.html', services=services,
owner=ObjectId(login.current_user._id))
@app.route('/search')
def search_yos():
search_terms = request.args['search'].strip().split()
id_ = ObjectId(login.current_user._id)
services = map(Service,
db.services.find(
{'$or':[
{'tags': {'$in': search_terms}},
{'dscrpt': {'$regex': '|'.join(search_terms)}},
{'name': {'$regex': '|'.join(search_terms)}}
], 'yo_handle': {'$exists': 1}}
).sort('rating', pymongo.DESCENDING).limit(100))
return render_template('services.html', services=services,
owner=(ObjectId(login.current_user._id) if is_logged else None))
@app.route('/create', methods=('GET',))
def new_service_render():
if not login.current_user.is_authenticated():
return redirect(url_for('admin.login_view'))
return render_template('create_service.html')
@app.route('/create', methods=('POST',))
def new_service_make():
if not login.current_user.is_authenticated():
return redirect(url_for('admin.login_view'))
data = {x: request.values.getlist(x) for x in list(request.values)}
data = {x: data[x][0]
if (len(data[x]) == 1 and x not in ['tags', 'fields'])
else data[x]
for x in data}
data['owner'] = ObjectId(login.current_user._id)
s = Service(data)
s.save(db)
print s._to_dict()
return render_template('msg.html', msg=("%s has been created!"%s.name))
@app.route('/delete/<service_id>', methods=('GET',))
def delete_yo(service_id):
if not login.current_user.is_authenticated():
return redirect(url_for('admin.login_view'))
oid = None
try:
oid = ObjectId(service_id)
except Exception, e:
return redirect('/sry/poorly%20formed%20url')
cursor = db.services.find({'_id': oid})
if cursor.count() == 0:
return redirect('/sry/no%20such%20service%20exists')
s = Service(cursor.next())
if ObjectId(login.current_user._id) != s.owner:
return redirect('/sry/no%20such%20service%20exists')
db.services.remove({'_id': s._id})
db.user_data.remove({'service': s._id})
return render_template('msg.html', msg=('%s has been deleted.'%s.name))
@app.route('/edit/<service_id>', methods=('GET',))
def edit_yo(service_id):
if not login.current_user.is_authenticated():
return redirect(url_for('admin.login_view'))
oid = None
try:
oid = ObjectId(service_id)
except Exception, e:
return redirect('/sry/poorly%20formed%20url')
cursor = db.services.find({'_id': oid})
if cursor.count() == 0:
return redirect('/sry/no%20such%20service%20exists')
s = Service(cursor.next())
if ObjectId(login.current_user._id) != s.owner:
return redirect('/sry/no%20such%20service%20exists')
return redirect('/sry/edit%20not%20yet%20implemented')
@app.route('/sry/<text>')
def sry(text):
return render_template('sry.html', text=text)
@app.route('/msg/<msg>')
def msg(msg):
return render_template('msg.html', msg=msg)
@app.route('/docs')
def docs():
return render_template('docs.html')
@app.route('/add/<service_id>', methods=('GET',))
def add_yo(service_id):
oid = None
try:
oid = ObjectId(service_id)
except Exception, e:
return redirect('/sry/poorly%20formed%20url')
cursor = db.services.find({'_id': oid, 'yo_handle': {'$exists': 1}})
if cursor.count() == 0:
return redirect('/sry/no%20such%20service%20exists')
s = Service(cursor.next())
if s.need_extra:
return render_template('add_params.html', s_id=s._id, fields=(s.fields or []))
else:
return redirect('http://www.justyo.co/%s/' % s.yo_handle)
@app.route('/add/<service_id>', methods=('POST',))
def add_yo2(service_id):
if not login.current_user.is_authenticated():
return redirect(url_for('admin.login_view'))
oid = None
try:
oid = ObjectId(service_id)
except Exception, e:
return redirect('/sry/poorly%20formed%20url')
cursor = db.services.find({'_id': oid, 'yo_handle': {'$exists': 1}})
if cursor.count() == 0:
return redirect('/sry/no%20such%20service%20exists')
s = Service(cursor.next())
if s.need_extra:
data = {x: request.form[x] for x in request.form if x in s.fields}
if len(data.keys()) != len(s.fields):
return render_template('add_params.html', s_id=s._id, fields=s.fields)
db.user_data.update({'user': ObjectId(login.current_user._id),
'service': s._id},
{'$set': {'data': data}}, upsert=True)
return redirect('http://www.justyo.co/%s/' % s.yo_handle)
else:
return redirect('http://www.justyo.co/%s/' % s.yo_handle)
@app.route('/yoback/<service_id>')
def yoback(service_id, methods=('POST',)):
print 'got yo'
oid = None
try:
oid = ObjectId(service_id)
except Exception, e:
return abort(404)
cursor = db.services.find({'_id': oid})
if cursor.count() == 0:
return abort(404)
s = Service(cursor.next())
data = {x: request.args[x] for x in request.args
if x in ['username', 'link', 'location']}
s.run(db, data)
db.services.update({'_id': s._id}, {'$inc': {'rating': 1}})
return 'yo'
# Initialize flask-login
init_login()
# Create admin
admin = admin.Admin(app, 'Accounts', index_view=MyAdminIndexView(),
base_template='my_master.html')
if __name__ == '__main__':
# Start app
app.run(debug=True)
# app.run(debug=True, host="0.0.0.0", port=80)