def twitter(request): """ Actually setup/login an account relating to a twitter user after the oauth process is finished successfully """ client = OAuthTwitter( request, settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET_KEY, settings.TWITTER_REQUEST_TOKEN_URL, ) user_info = client.get_user_info() user = authenticate(twitter_id=user_info['id']) if user is None: profile = TwitterProfile(twitter_id=user_info['id'], ) user = User() request.session['socialregistration_profile'] = profile request.session['socialregistration_user'] = user request.session['next'] = _get_next(request) return HttpResponseRedirect(reverse('socialregistration_setup')) login(request, user) return HttpResponseRedirect(_get_next(request))
def authenticate(self, twitter_id=None): try: return TwitterProfile.objects( twitter_id=twitter_id #site=Site.objects.get_current() ).user except TwitterProfile.DoesNotExist: return None
def twitter( request, account_inactive_template='socialregistration/account_inactive.html', extra_context=dict(), client_class=None): """ Actually setup/login an account relating to a twitter user after the oauth process is finished successfully """ client = client_class( request, settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET_KEY, settings.TWITTER_REQUEST_TOKEN_URL, ) try: user_info = client.get_user_info() except 1: return render_to_response(account_inactive_template, extra_context, context_instance=RequestContext(request)) if request.user.is_authenticated(): # Handling already logged in users connecting their accounts try: profile = TwitterProfile.objects.get(twitter_id=user_info['id']) except TwitterProfile.DoesNotExist: # There can only be one profile! profile = TwitterProfile.objects.create( user=request.user, twitter_id=user_info['id'], username=user_info['screen_name']) _connect(request.user, profile, client) return HttpResponseRedirect(_get_next(request)) user = authenticate(twitter_id=user_info['id']) if user is None: profile = TwitterProfile(twitter_id=user_info['id'], username=user_info['screen_name']) user = User() request.session['socialregistration_profile'] = profile request.session['socialregistration_user'] = user # Client is not pickleable with the request on it client.request = None request.session['socialregistration_client'] = client request.session['socialregistration_info'] = user_info request.session['next'] = _get_next(request) return HttpResponseRedirect(reverse('socialregistration_setup')) if not user.is_active: return render_to_response(account_inactive_template, extra_context, context_instance=RequestContext(request)) _login(request, user, TwitterProfile.objects.get(user=user), client) return HttpResponseRedirect(_get_next(request))
def twitter(request, account_inactive_template='socialregistration/account_inactive.html', extra_context=dict()): """ Actually setup/login an account relating to a twitter user after the oauth process is finished successfully """ client = OAuthTwitter( request, settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET_KEY, settings.TWITTER_REQUEST_TOKEN_URL, ) user_info = client.get_user_info() if request.user.is_authenticated(): # Handling already logged in users connecting their accounts try: profile = TwitterProfile.objects(twitter_id=user_info['id']) except TwitterProfile.DoesNotExist: # There can only be one profile! profile = TwitterProfile(user=request.user, twitter_id=user_info['id']) profile.save() return HttpResponseRedirect(_get_next(request)) user = getuser(twitter_id=user_info['id']) if user is None: profile = TwitterProfile(twitter_id=user_info['id']) user = User() request.session['socialregistration_profile'] = profile request.session['socialregistration_user'] = user request.session['next'] = _get_next(request) return HttpResponseRedirect(reverse('socialregistration_setup')) if not user.is_active: return render_to_response( account_inactive_template, extra_context, context_instance=RequestContext(request) ) login(request, user) return HttpResponseRedirect(_get_next(request))
def twitter(request, account_inactive_template='socialregistration/account_inactive.html', extra_context=dict()): """ Actually setup/login an account relating to a twitter user after the oauth process is finished successfully """ client = OAuthTwitter( request, settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET_KEY, settings.TWITTER_REQUEST_TOKEN_URL, ) user_info = client.get_user_info() if request.user.is_authenticated(): # Handling already logged in users connecting their accounts try: profile = TwitterProfile.all().filter('twitter_id = ',user_info['id']).fetch(1)[0] except IndexError: # There can only be one profile! profile = TwitterProfile(user=request.user, twitter_id=user_info['id'], username=user_info['screen_name'], real_name=user_info['name'], pic_url = user_info['profile_image_url'], ) profile.save() return HttpResponseRedirect(_get_next(request)) user = authenticate(twitter_id=user_info['id']) if user is None: profile = TwitterProfile( twitter_id=user_info['id'], username=user_info['screen_name'], real_name=user_info['name'], pic_url = user_info['profile_image_url'], ) user = User(username=profile.real_name) request.session['social_suggested_username'] = user_info['screen_name'] request.session['socialregistration_profile'] = profile request.session['socialregistration_user'] = user request.session['next'] = _get_next(request) return HttpResponseRedirect(reverse('socialregistration_setup')) if not user.is_active: return render_to_response( account_inactive_template, extra_context, context_instance=RequestContext(request) ) login(request, user) return HttpResponseRedirect(_get_next(request))
def authenticate(self, twitter_id=None): try: twitter_profile = TwitterProfile.all() twitter_profile.filter('twitter_id = ', twitter_id) #twitter_profile.filter('site = ',Site.objects.get_current()) twitter_profile = twitter_profile.fetch(1) auth_user = twitter_profile[0].user return auth_user #TwitterProfile.objects.get( #twitter_id=twitter_id, #site=Site.objects.get_current() #).user except: return None
def authenticate(self, twitter_id=None): try: twitter_profile = TwitterProfile.all() twitter_profile.filter('twitter_id = ',twitter_id) #twitter_profile.filter('site = ',Site.objects.get_current()) twitter_profile = twitter_profile.fetch(1) auth_user = twitter_profile[0].user return auth_user #TwitterProfile.objects.get( #twitter_id=twitter_id, #site=Site.objects.get_current() #).user except: return None
def authenticate(self, twitter_id=None): return TwitterProfile.all().filter('twitter_id =', twitter_id).get()
from django.utils.translation import ugettext as _ from django.core import urlresolvers from django.template import defaultfilters from django.conf import settings from django.contrib.comments.managers import CommentManager from django.core.cache import cache from music.models import Track, Artist from tagging.fields import TagField from notification import models as notification from actstream.models import Follow from actstream import action from socialregistration.models import TwitterProfile, FacebookProfile TwitterProfile.add_to_class('access_token', models.TextField(help_text='only useful if your app wants to tweet while the browser is not authenticated via twitter', null=True, blank=True)) TwitterProfile.add_to_class('token_secret', models.TextField(help_text='only useful if your app wants to tweet while the browser is not authenticated via twitter', null=True, blank=True)) TwitterProfile.add_to_class('avatar_url', models.TextField(null=True, blank=True)) FacebookProfile.add_to_class('avatar_url', models.TextField(null=True, blank=True)) TwitterProfile.add_to_class('nick', models.TextField(null=True, blank=True)) FacebookProfile.add_to_class('nick', models.TextField(null=True, blank=True)) TwitterProfile.add_to_class('url', models.TextField(null=True, blank=True)) FacebookProfile.add_to_class('url', models.TextField(null=True, blank=True)) def new_user_unicode(self): return '%s %s' % (self.first_name, self.last_name) User.__unicode__ = new_user_unicode def new_user_get_absolute_url(self): return urlresolvers.reverse('user_details', args=(self.username,)) User.get_absolute_url = new_user_get_absolute_url
def twitter( request, account_inactive_template='socialregistration/account_inactive.html', extra_context=dict()): """ Actually setup/login an account relating to a twitter user after the oauth process is finished successfully """ client = OAuthTwitter( request, settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET_KEY, settings.TWITTER_REQUEST_TOKEN_URL, ) user_info = client.get_user_info() try: oauth_token = request.session['oauth_api.twitter.com_access_token'][ 'oauth_token'] except KeyError: try: oauth_token = request.session['oauth_twitter.com_access_token'][ 'oauth_token'] except: oauth_token = '' try: oauth_token_secret = request.session[ 'oauth_api.twitter.com_access_token']['oauth_token_secret'] except KeyError: try: oauth_token_secret = request.session[ 'oauth_twitter.com_access_token']['oauth_token_secret'] except: oauth_token_secret = '' if 'socialregistration_connect_object' in request.session and request.session[ 'socialregistration_connect_object'] != None: # this exists so that social credentials can be attached to any arbitrary object using the same callbacks. # Under normal circumstances it will not be used. Put an object in request.session named 'socialregistration_connect_object' and it will be used instead. # After the connection is made it will redirect to request.session value 'socialregistration_connect_redirect' or settings.LOGIN_REDIRECT_URL or / try: # get the profile for this Twitter ID and type of connected object profile = TwitterProfile.objects.get( twitter_id=user_info['id'], content_type=ContentType.objects.get_for_model( request.session['socialregistration_connect_object']. __class__), object_id=request.session['socialregistration_connect_object']. pk) except TwitterProfile.DoesNotExist: TwitterProfile.objects.create(content_object=request.session['socialregistration_connect_object'], twitter_id=user_info['id'], \ screenname=user_info['screen_name'], consumer_key=oauth_token, consumer_secret=oauth_token_secret) del request.session['socialregistration_connect_object'] else: if request.user.is_authenticated(): # Handling already logged in users connecting their accounts try: profile = TwitterProfile.objects.get( twitter_id=user_info['id'], content_type=ContentType.objects.get_for_model(User)) except TwitterProfile.DoesNotExist: # There can only be one profile! profile = TwitterProfile.objects.create( content_object=request.user, twitter_id=user_info['id'], screenname=user_info['screen_name'], consumer_key=oauth_token, consumer_secret=oauth_token_secret) return HttpResponseRedirect(_get_next(request)) user = authenticate(twitter_id=user_info['id']) if user is None: request.session['socialregistration_profile'] = TwitterProfile( twitter_id=user_info['id'], screenname=user_info['screen_name'], consumer_key=oauth_token, consumer_secret=oauth_token_secret) request.session['socialregistration_user'] = User() request.session['next'] = _get_next(request) return HttpResponseRedirect(reverse('socialregistration_setup')) if not user.is_active: return render_to_response(account_inactive_template, extra_context, context_instance=RequestContext(request)) login(request, user) return HttpResponseRedirect(_get_next(request))