-
Notifications
You must be signed in to change notification settings - Fork 2
/
socialtasks.py
366 lines (307 loc) · 11.8 KB
/
socialtasks.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
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
import base64
import os
import os.path
import json
import urllib
import urllib2
import time
import datetime
from flask import Flask, session, request, redirect, render_template, jsonify
from flaskext.sqlalchemy import SQLAlchemy
from models import app, db, Fbuser, Task, Comment
import decorators
FBAPI_APP_ID = os.environ.get('FACEBOOK_APP_ID')
Flask.secret_key = 'pm1yfQmmbZUiAP8Ll/JG9XJWNiebOVyyz1T0nlVED3uE4lpv'
app = Flask(__name__)
def get_me():
return fb_call('me', args={'access_token': session['access_token']})
def oauth_login_url(next_url=None):
fb_login_uri = ("https://www.facebook.com/dialog/oauth"
"?client_id=%s&redirect_uri=%s" %
(app.config['FBAPI_APP_ID'],
next_url if next_url else get_home()))
if app.config['FBAPI_SCOPE']:
fb_login_uri += "&scope=%s" % ",".join(app.config['FBAPI_SCOPE'])
return fb_login_uri
class ensure_fb_auth:
def __init__(self, func):
'''
Ensure that that user has a valid Facebook presence. If not, redirect
the user to the Facebook authentication page, while attempting to have
them link back to the current page.
'''
self.func = func
self.__name__ = func.__name__
self.__doc__ = func.__doc__
def __call__(self, **args):
access_token, expires = None, None
if ('access_token' in session and 'expires' in session) and \
int(session['expires']) > time.time():
access_token = session['access_token']
expires = int(session['expires'])
elif request.args.get('code', None):
auth_response = fbapi_auth(request.args.get('code'))
session['access_token'] = access_token = auth_response[0]
session['expires'] = expires = int(auth_response[1])
if not access_token or expires <= time.time():
return redirect(oauth_login_url(
get_permalink_path(request.path)))
else:
me = get_me()
user_id = str(me['id'])
user = Fbuser.query.filter_by(facebook_id=user_id).first()
if not user:
user = Fbuser(user_id)
db.session.add(user)
db.session.commit()
return self.func(**args)
def get_permalink_path(path):
'''
Deal with the mixed environment of AJAXified partials and full paths.
'''
if 'ajax' in path or 'task' in path:
path = get_fully_qualified_path('/permalink' + path)
else:
path = get_fully_qualified_path(path)
if not path:
path = get_fully_qualified_path('/')
return path
def simple_dict_serialisation(params):
return "&".join(map(lambda k: "%s=%s" % (k, params[k]), params.keys()))
def base64_url_encode(data):
return base64.urlsafe_b64encode(data).rstrip('=')
def fbapi_get_string(path, domain=u'graph', params=None, access_token=None,
encode_func=urllib.urlencode):
"""Make an API call"""
if not params:
params = {}
params[u'method'] = u'GET'
if access_token:
params[u'access_token'] = access_token
for k, v in params.iteritems():
if hasattr(v, 'encode'):
params[k] = v.encode('utf-8')
url = u'https://' + domain + u'.facebook.com' + path
params_encoded = encode_func(params)
url = url + params_encoded
result = urllib2.urlopen(url).read()
return result
def fbapi_auth(code):
params = {'client_id': app.config['FBAPI_APP_ID'],
'redirect_uri': get_home(),
'client_secret': app.config['FBAPI_APP_SECRET'],
'code': code}
result = fbapi_get_string(path=u"/oauth/access_token?", params=params,
encode_func=simple_dict_serialisation)
pairs = result.split("&", 1)
result_dict = {}
for pair in pairs:
(key, value) = pair.split("=")
result_dict[key] = value
return (result_dict["access_token"],
time.time() + int(result_dict["expires"]))
def fbapi_get_application_access_token(id):
token = fbapi_get_string(
path=u"/oauth/access_token",
params=dict(grant_type=u'client_credentials', client_id=id,
client_secret=app.config['FB_APP_SECRET']),
domain=u'graph')
token = token.split('=')[-1]
if not str(id) in token:
print 'Token mismatch: %s not in %s' % (id, token)
return token
def fql(fql, token, args=None):
if not args:
args = {}
args["query"], args["format"], args["access_token"] = fql, "json", token
return json.loads(
urllib2.urlopen("https://api.facebook.com/method/fql.query?" +
urllib.urlencode(args)).read())
def fb_call(call, args=None):
return json.loads(urllib2.urlopen("https://graph.facebook.com/" + call +
'?' + urllib.urlencode(args)).read())
app.config.from_object(__name__)
app.config.from_object('conf.Config')
def get_home():
return get_fully_qualified_path('/')
def get_fully_qualified_path(short_path='/'):
if len(short_path) == 0: short_path = '/'
if short_path[-1] != '/': short_path += '/'
if request.host.find('localhost') != -1:
return 'http://' + request.host + short_path
else:
return 'https://' + request.host + short_path
@app.route('/typeahead/json/', methods=['GET'])
def typeahead():
if 'access_token' in session and int(session['expires']) >= time.time():
user_friends = fb_call('me/friends',
args={'access_token':
session['access_token']})
return jsonify(user_friends)
else:
return jsonify({})
@app.route('/close/', methods=['GET', 'POST'])
def close():
return render_template('close.html')
@app.route('/', methods=['GET', 'POST'])
@ensure_fb_auth
def root(content=None):
access_token = session['access_token']
if access_token:
if not content:
content = home()
me = get_me()
return render_template(
'index.html',
app=app,
me=me,
appId=FBAPI_APP_ID,
token=access_token,
content=content)
else:
return redirect(oauth_login_url(next_url=get_home()))
@app.route('/task/create/', methods=['GET', 'POST'])
@ensure_fb_auth
def create_task():
return render_template('create_task.html')
@app.route('/task/make/', methods=['POST'])
@ensure_fb_auth
def make_task(content=None):
from sqlalchemy import func
size = len(Task.query.all())+1
access_token = session['access_token']
content = request.form['content']
if access_token:
me = fb_call('me', args={'access_token': access_token})
user_id = me['id']
task = Task(size,
datetime.datetime.today(),
str(me['id']),
request.form['title'],
content,
False)
assignees = parse_message_content(content)
for assignee_name in assignees:
task.add_assignee(fbuser_from_name(assignee_name))
# task.add_assignee(Fbuser.query.filter_by(
# facebook_id=str(me['id'])).first_or_404())
db.session.add(task)
db.session.commit()
return "Success"
raise Exception
def parse_message_content(content):
words = content.split()
assignees = set([])
for word in words:
if len(word) == 0: continue
if word[0] == '@':
assignees.add(word[1:])
return assignees
def fbuser_from_name(name):
import re
name = re.sub('\s', '', name).lower()
me = get_me()
myname = re.sub('\s', '', me['name']).lower()
if name == myname:
return Fbuser.query.filter_by(facebook_id= str(me['id'])).first_or_404()
app_friends = fql(
"SELECT uid, name "
"FROM user "
"WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"
, session['access_token'])
for user in app_friends:
name_compressed = re.sub('\s', '', user['name']).lower()
if name_compressed == name:
return Fbuser.query.filter_by(facebook_id=str(user['uid'])).first_or_404()
return None
@app.route('/task/comment/', methods=['POST'])
@ensure_fb_auth
def make_comment(content=None):
from sqlalchemy import func
comment_id = len(Comment.query.all())+1
access_token = session['access_token']
contents = request.form['content']
if access_token:
me = fb_call('me', args={'access_token': access_token})
author = me['id']
creation_time = datetime.datetime.today()
task_id = request.form['task_id']
comment = Comment(comment_id,
task_id,
datetime.datetime.today(),
author,
contents)
db.session.add(comment)
db.session.commit()
return render_template('make_comment.html',
me=me,
author=author,
contents=contents,
creation_time=creation_time)
raise Exception
@app.route('/task/<t_id>/', methods=['GET'])
@ensure_fb_auth
def view_task(t_id):
print "this is id: " + t_id
task = Task.query.filter_by(task_id = t_id).first_or_404()
comments = []
for comment in task.comments:
author = fb_call(comment.author,
args={'access_token': session['access_token']})
author_name = author['name']
c_dict = {'author': comment.author,
'contents': comment.contents,
'creation_time': comment.creation_time,
'author_name': author_name}
comments.append(c_dict)
me = get_me()
return render_template('view_task.html',
me=me,
task=task,
comments=comments)
@app.route('/ajax/home/', methods=['GET'])
@ensure_fb_auth
def home():
access_token = session['access_token']
me = fb_call('me', args={'access_token': access_token})
app = fb_call(FBAPI_APP_ID, args={'access_token': access_token})
friends = fb_call('me/friends',
args={'access_token': access_token, 'limit': 4})
user_id = str(me['id'])
user = Fbuser.query.filter_by(facebook_id=user_id).first()
user_assigned_tasks = user.tasks
for task in user_assigned_tasks:
print task.contents
assigned_to_user = []
all_tasks = Task.query.all()
for task in all_tasks:
for assignee in task.assignees:
if assignee.facebook_id == user_id:
assigned_to_user.append(task)
print "here are the tasks assigned to the user"
for task in assigned_to_user:
print task.contents
#if you are assignee, assigner list of task contents, list of fb_id of
#everyone assigned.
return render_template('home.html', me=me, app=app,
friends=friends,
user_assigned=user_assigned_tasks,
assigned_to_user=assigned_to_user)
@app.route('/permalink/<path:ajax_path>')
def permalink(ajax_path):
return root(render_template('permalink_load.html', ajax_path=ajax_path))
@app.route('/experiment/piglatin/', methods=['GET', 'POST'])
def pig():
def piglatin(inp):
if len(inp) < 3:
return inp+"ay"
else:
return inp[1:]+inp[0]+"ay"
inp = request.args.get('inp', None)
return render_template('experiment.html', out=piglatin(inp));
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
if app.config.get('FBAPI_APP_ID') and app.config.get('FBAPI_APP_SECRET'):
app.run(host='0.0.0.0', port=port)
else:
print 'Cannot start application without Facebook App Id and Secret set'