-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
71 lines (56 loc) · 2.53 KB
/
views.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
from itertools import imap, ifilter
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login, authenticate
from django.conf import settings
from djangotoolbox.http import JSONResponse
from socialregistration.models import FacebookProfile
import facebook as fb_sdk
from utils import fb_account_setup, now_timestamp, fql_query, skinny_video_post
def canvas(request):
"""services facebook canvas"""
signed_request = request.GET.get('signed_request')
# redirect early if not from facebook
if not signed_request:
return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
# process signed_request if from facebook
data = fb_sdk.parse_signed_request(signed_request)
# TODO should prolly be a server error or something like that
if not data:
return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
oauth_token = data.get('oauth_token')
# request authorization / extended permissions
if not oauth_token:
return render_to_response('canvas_authorize.html', context_instance=RequestContext(request))
# authentication / start session
uid = data.get('user_id')
user = authenticate(uid=uid)
if user:
login(request, user)
else:
user = User()
facebook_profile = FacebookProfile(uid=uid)
fb_account_setup(request, user, facebook_profile)
request.session['oauth_token'] = oauth_token
if request.session.has_key('facebook_timestamp'):
del request.session['facebook_timestamp']
return render_to_response('canvas.html', context_instance=RequestContext(request))
@login_required
def videos_increment(request, limit=30):
graph = fb_sdk.GraphAPI(request.session['oauth_token'])
sources = []
# always return at least two to avoid
while len(sources) < 2:
timestamp = request.session.get('facebook_timestamp', now_timestamp())
fql = fql_query(timestamp, limit=limit)
# import ipdb; ipdb.set_trace()
# TODO secure this call with some exception handling + timeout
json_results = graph.fql(fql)
if not json_results: break
request.session['facebook_timestamp'] = json_results[-1]['created_time']
video_posts = imap(skinny_video_post, json_results)
sources += list(ifilter(None, video_posts))
return JSONResponse(sources)