· 7 years ago · Sep 28, 2018, 06:12 AM
1from django.contrib.auth import authenticate, login, logout as django_logout
2from django.contrib.auth.models import User
3from django.http import HttpResponse, HttpResponseRedirect
4from django.shortcuts import render_to_response
5from django.contrib.auth.decorators import login_required
6from django.conf import settings
7from django.core.urlresolvers import reverse
8
9from twython import Twython
10
11# If you've got your own Profile setup, see the note in the models file
12# about adapting this to your own setup.
13from twitter_favs.models import TwitterProfile
14
15def logout(request, redirect_url=settings.LOGOUT_REDIRECT_URL):
16 """
17 Nothing hilariously hidden here, logs a user out. Strip this out if your
18 application already has hooks to handle this.
19 """
20 django_logout(request)
21 return HttpResponseRedirect(request.build_absolute_uri(redirect_url))
22
23def begin_auth(request):
24 """
25 The view function that initiates the entire handshake.
26 For the most part, this is 100% drag and drop.
27 """
28 # Instantiate Twython with the first leg of our trip.
29 twitter = Twython(
30 twitter_token = settings.TWITTER_KEY,
31 twitter_secret = settings.TWITTER_SECRET,
32 callback_url = request.build_absolute_uri(reverse('twitter_favs.views.thanks'))
33 )
34
35 # Request an authorization url to send the user to...
36 auth_props = twitter.get_authentication_tokens()
37
38 # Then send them over there, durh.
39 request.session['request_token'] = auth_props
40 return HttpResponseRedirect(auth_props['auth_url'])
41
42def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
43 """
44 A user gets redirected here after hitting Twitter and authorizing your
45 app to use their data.
46
47 ***
48 This is the view that stores the tokens you want
49 for querying data. Pay attention to this.
50 ***
51 """
52 # Now that we've got the magic tokens back from Twitter, we need to exchange
53 # for permanent ones and store them...
54 twitter = Twython(
55 twitter_token = settings.TWITTER_KEY,
56 twitter_secret = settings.TWITTER_SECRET,
57 oauth_token = request.session['request_token']['oauth_token'],
58 oauth_token_secret = request.session['request_token']['oauth_token_secret'],
59 )
60
61 # Retrieve the tokens we want...
62 authorized_tokens = twitter.get_authorized_tokens()
63
64 # If they already exist, grab them, login and redirect to a page displaying stuff.
65 try:
66 user = User.objects.get(username = authorized_tokens['screen_name'])
67 except User.DoesNotExist:
68 # We mock a creation here; no email, password is just the token, etc.
69 user = User.objects.create_user(authorized_tokens['screen_name'], "fjdsfn@jfndjfn.com", authorized_tokens['oauth_token_secret'])
70 profile = TwitterProfile()
71 profile.user = user
72 profile.oauth_token = authorized_tokens['oauth_token']
73 profile.oauth_secret = authorized_tokens['oauth_token_secret']
74 profile.save()
75
76 user = authenticate(
77 username = authorized_tokens['screen_name'],
78 password = authorized_tokens['oauth_token_secret']
79 )
80 login(request, user)
81 return HttpResponseRedirect(redirect_url)
82
83def user_timeline(request):
84 """
85 An example view with Twython/OAuth hooks/calls to fetch data about the user
86 in question. Pretty self explanatory if you read through it...
87 """
88 user = request.user.twitterprofile
89 twitter = Twython(
90 twitter_token = settings.TWITTER_KEY,
91 twitter_secret = settings.TWITTER_SECRET,
92 oauth_token = user.oauth_token,
93 oauth_token_secret = user.oauth_secret
94 )
95 user_tweets = twitter.getHomeTimeline()
96 return render_to_response('tweets.html', {'tweets': user_tweets})