def step_1(request): ctx = {} if request.user.is_authenticated(): access_token = TroveAccessToken.objects.filter(user=request.user)[0] logger.debug(access_token) api = TroveAPI(TROVE['ID'], TROVE['SECRET'], TROVE['CALLBACK_URL'], access_token = access_token.oauth_token_as_string) trove_user_info = api.get_user_info() ids = trove_user_info['identities']['photos'] try: ids.extend(trove_user_info['identities']['status']) except: pass try: ids.extend(trove_user_info['identities']['checkins']) except: pass ## todo fixme: are these lines good or bad? if we prepopulate it we need to UN specify to the send the emails in step 2 #email = trove_user_info['trove_email'] #request.user.email = email #request.user.save() services = [id[0].lower() for id in ids ] ctx['services'] = services return render_to_response('yourtroveinabox/simpl/step1.html', context_instance=RequestContext(request,ctx))
def authenticate(self, oauth_token=None): if oauth_token is None: return None user_info = None try: api = TroveAPI(settings.TROVE_APP_KEY, settings.TROVE_APP_SECRET, ["photos"], oauth_token) user_info = api.get_user_info() except: return None try: trove_profile = UserProfile.objects.get(trove_user_id=user_info["trove_internal_id"]) return trove_profile.user except UserProfile.DoesNotExist: user = User() if user_info.has_key("trove_username"): user.username = user_info["trove_username"] else: user.username = user_info["trove_internal_id"] # We use the trove internal id if there is no user name user.first_name = user_info["first_name"] user.last_name = user_info["last_name"] user.save() trove_profile = UserProfile() trove_profile.trove_user_id = user_info["trove_internal_id"] trove_profile.trove_access_token_key = oauth_token.key trove_profile.trove_access_token_secret = oauth_token.secret trove_profile.user = user trove_profile.save() return user
def callback_view(request): if request.GET.has_key('bounceback'): request.session['bounceback'] = request.GET['bounceback'] if request.GET.has_key("code"): code = request.GET.get('code') api = TroveAPI(TROVE['ID'], TROVE['SECRET'], TROVE['CALLBACK_URL']) oauth_token = api.get_access_token(code) else: # this might be a /add callback response (instead of a loginwith) if request.user.is_authenticated(): return __bounceback__(request) else: return redirect('trove-login') try: at = TroveAccessToken.objects.get(oauth_token_as_string=oauth_token) except TroveAccessToken.DoesNotExist: # we have to create one! -- although this does run into issues with users having different access tokens user_info = api.get_user_info() try: u = User.objects.get(username=user_info['trove_internal_id']) except User.DoesNotExist: u = User() u.username = user_info['trove_internal_id'] try: if user_info['first_name'] is not None: u.first_name = to_unicode_or_bust(user_info['first_name']) else: u.first_name = " " except: u.first_name = " " try: if user_info['last_name'] is not None: u.last_name = to_unicode_or_bust(user_info['last_name']) else: u.last_name = "" except: u.last_name = "" u.set_unusable_password() u.email = "" u.is_active = True u.save() at = TroveAccessToken() at.user = u at.oauth_token_as_string = oauth_token at.save() user = authenticate(access_token=oauth_token) if user is not None: if user.is_active: login(request, user) return __bounceback__(request) else: return redirect('trove-login') else: return redirect('trove-login')
def login_view(request): if request.GET.has_key('bounceback'): request.session['bounceback'] = request.GET['bounceback'] if request.user.is_authenticated(): return __bounceback__(request) api = TroveAPI(TROVE['ID'], TROVE['SECRET'], TROVE['CALLBACK_URL']) url = api.get_authenticate_url() return HttpResponseRedirect(url)
def login_view(request): if request.GET.has_key('bounceback'): request.session['bounceback'] = request.GET['bounceback'] if request.user.is_authenticated(): return __bounceback__(request) api = TroveAPI(TROVE['KEY'], TROVE['SECRET'], TROVE['CONTENT_REQUESTED']) request_token = api.get_request_token() url = api.get_authorization_url(request_token) request.session['request_token'] = request_token return HttpResponseRedirect(url)
def callback_view(request): if request.GET.has_key('bounceback'): request.session['bounceback'] = request.GET['bounceback'] if request.GET.has_key("oauth_token_secret"): oauth_token = OAuthToken.from_string("oauth_token_secret=%s&oauth_token=%s" % (request.GET['oauth_token_secret'], request.GET['oauth_token'])) api = TroveAPI(TROVE['KEY'], TROVE['SECRET'], TROVE['CONTENT_REQUESTED'], oauth_token) elif request.session.has_key('request_token'): api = TroveAPI(TROVE['KEY'], TROVE['SECRET'], TROVE['CONTENT_REQUESTED']) oauth_token = api.get_access_token(request.session['request_token']) del request.session['request_token'] else: return redirect('trove-login') try: at = TroveAccessToken.objects.get(oauth_token_as_string=oauth_token.__str__()) except TroveAccessToken.DoesNotExist: # we have to create one! -- although this does run into issues with users having different access tokens user_info = api.get_user_info() try: u = User.objects.get(username=user_info['trove_internal_id']) except User.DoesNotExist: u = User() u.username = user_info['trove_internal_id'] try: u.first_name = user_info['first_name'] except: pass try: u.last_name = user_info['last_name'] except: pass u.set_unusable_password() u.email = "" u.is_active = True u.save() at = TroveAccessToken() at.user = u at.oauth_token_as_string = oauth_token.__str__() at.save() user = authenticate(access_token=oauth_token.__str__()) if user is not None: if user.is_active: login(request, user) return __bounceback__(request) else: return redirect('trove-login') else: return redirect('trove-login')
def specific_user_report(request, user_id): if not request.user.is_superuser: return HttpResponse(content="Access denied", status="403") u = User.objects.get(id=user_id) if request.method == "POST": try: unfinished_tasks = BackupTask.objects.filter(user=u,completed_on=None) for task in unfinished_tasks: task.result = 'manually cancelled' task.completed_on=datetime.datetime.utcnow() task.save() except: pass task = BackupPhotos() try: task.delay(u) except: logger.error('Unable to queue backup for user %s' % (u.id)) send_email_unable_to_queue_backup(u) return HttpResponse("Rerunning Backup for " + user_id) else: ctx = {} u = User.objects.get(id=user_id) ctx['user'] = u ctx['current_user'] = u ctx['backed_up_items'] = BackedupEntity.objects.filter(user=u) ctx['failed_backup_tasks'] = BackupTask.objects.filter(user=u).exclude(result="Success") ctx['successful_backup_tasks'] = BackupTask.objects.filter(user=u, result="Success") ctx['all_backup_tasks'] = BackupTask.objects.filter(user=u) access_tokens = TroveAccessToken.objects.filter(user=u) try: api = TroveAPI(TROVE['ID'], TROVE['SECRET'], TROVE['CALLBACK_URL'], access_token = access_tokens[0].oauth_token_as_string) trove_user_info = api.get_user_info() ctx['trove_user_info'] = trove_user_info ctx['photo_identites'] = trove_user_info['identities']['photos'] except: pass return render_to_response('yourtroveinabox/simpl/admin_user.html', context_instance=RequestContext(request,ctx))
def home(request): template = 'welcome.html' context_vars = {} context = None if request.user.is_authenticated(): return get_started_or_go_home(request) else: api = TroveAPI(settings.TROVE_APP_KEY, settings.TROVE_APP_SECRET, ['photos']) if request.GET: try: oauth_token_key = request.GET['oauth_token'] request_token = request.session['trove_request_token'] oauth_token = api.get_access_token(request_token) user = authenticate(oauth_token = oauth_token) if user is not None: if user.is_active: login(request, user) return get_started_or_go_home(request) else: # Return a 'disabled account' error message pass else: # TODO FIXME failed login pass except KeyError: pass else: try: request_token = request.session['trove_request_token'] except: request_token = api.get_request_token() request.session['trove_request_token']=request_token url = api.get_authorization_url(request_token) context_vars['trove_connect_url']=url context = RequestContext(request,context_vars) return render_to_response(template,context,mimetype="text/html")
def add_other_service(request, service): trove_token = request.user.troveaccesstoken_set.all()[0] access_token = trove_token.get_oauth_token() api = TroveAPI(TROVE['ID'], TROVE['SECRET'], TROVE['CALLBACK_URL'], access_token = access_token) url = api.get_url_for_service(service, redirect_url=ADD_SERVICE_BOUNCEBACK_URL) return HttpResponseRedirect(url)
def login_via_3rd_party(request, service): api = TroveAPI(TROVE['ID'], TROVE['SECRET'], TROVE['CALLBACK_URL']) url = api.login_and_auth_via_service(service) return HttpResponseRedirect(url)
#Some Sample code for how Trove Client APIs work from troveclient import TroveAPI # You need a consumer. Make one in the db. Consumers have call back URLs, which are not overridable (a-la Twitter and Fb) c = { client_id: 'some_key_here', secret: 'sssh, it\'s a secret!', redirect_uri: 'http://redirecturl.dev/callback' } # Initialize the TroveAPI with the consumer's key and secret api = TroveAPI(client_id, secret, redirect_uri) url = api.get_authenticate_url() print url raw_input() # Returns back the URL to redirect the user (Looks like 'http://brooklyn.vlku.com:8000/multi/login?next=/oauth/2authorize/%3Foauth_token%3DdtKaXn5JXRWNndTa') # callback URL includes a "code" that is passed in via the code parameter. We use that to get an access_token. at = api.get_access_token(code) # This returns back the access_token (at) as an OAuthToken. You want to save this somewhere for reuse later # access_token also conveniently makes this the access_token for the instantiated API # so you can call this easily right away with no setup results = api.get_photos() # but if you have to initialize a fresh TroveAPI (say for another hit with the token from the DB:) api = TroveAPI(c.key, c.secret, ['photo'], access_token=at)
#Some Sample code for how Trove Client APIs work from troveclient import TroveAPI # You need a consumer. Make one in the db. Consumers have call back URLs, which are not overridable (a-la Twitter and Fb) c = { key: 'some_key_here', secret: 'sssh, it\'s a secret!' } # Initialize the TroveAPI with the consumer's key and secret api = TroveAPI(c.key, c.secret, ['photo']) rt = api.get_request_token() # We get the request token -- returns back an 'OAuthToken' url = api.get_authorization_url(rt) print url raw_input() # Returns back the URL to redirect the user (Looks like 'http://brooklyn.vlku.com:8000/multi/login?next=/oauth/authorize/%3Foauth_token%3DdtKaXn5JXRWNndTa') # callback URL AUTHORIZES (SAME) token and redirects to a page for you to act on token to get access token # to promote a request token to an access token call this method at = api.get_access_token(rt) # This returns back the access_token (at) as an OAuthToken. You want to save this somewhere for reuse later # access_token also conveniently makes this the access_token for the instantiated API # so you can call this easily right away with no setup results = api.get_photos()