def _authenticate(self, request): user = request.session.get('user') if user: p_obj = dbapi.get_profile(user) if p_obj is not None: return twitter.Api(consumer_key=self.consumer_key, consumer_secret=self.consumer_secret, \ access_token_key=p_obj.oauth_token, access_token_secret=p_obj.oauth_secret) token = oauth.Token(request.session['request_token']['oauth_token'], request.session['request_token']['oauth_token_secret']) client = oauth.Client(self.consumer, token) resp, content = client.request(access_token_url, "POST") access_token_key = dict(urlparse.parse_qsl(content)) screen_name = access_token_key['screen_name'] request.session['screen_name'] = screen_name access_token = access_token_key['oauth_token'] access_token_secret = access_token_key['oauth_token_secret'] user = dbapi.get_user(screen_name) if user is None: user = dbapi.create_user(screen_name,'*****@*****.**' % screen_name, access_token_secret) dbapi.create_profile(user, access_token, access_token_secret) request.session['user'] = user return twitter.Api(consumer_key=self.consumer_key, consumer_secret=self.consumer_secret, \ access_token_key=access_token, access_token_secret=access_token_secret)
def home_page(request): user = request.session.get('user') if user: p_obj = dbapi.get_profile(user) if p_obj is not None: return HttpResponseRedirect(reverse('tweet-response')) context = RequestContext(request) if request.method=='POST': auth_url = tweet.get_authorize_url(request) return HttpResponseRedirect(auth_url) return render_to_response('index.html', context_instance=context)