Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
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')
Ejemplo n.º 4
0
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')
Ejemplo n.º 5
0
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))