コード例 #1
0
ファイル: views.py プロジェクト: markpasc/leapfrog
def favorite_flickr(request):
    if request.method != 'POST':
        resp = HttpResponse('POST is required', status=405, content_type='text/plain')
        resp['Allow'] = ('POST',)
        return resp

    user = request.user
    if not user.is_authenticated():
        return HttpResponse('Authentication required to respond', status=400, content_type='text/plain')
    try:
        person = user.person
    except Person.DoesNotExist:
        return HttpResponse('Real reader account required to respond', status=400, content_type='text/plain')

    try:
        photo_id = request.POST['photo']
    except KeyError:
        photo_id = False
    if not photo_id:
        return HttpResponse("Parameter 'photo' is required", status=400, content_type='text/plain')

    # TODO: get only one account once we enforce (service,person) uniqueness
    accounts = person.accounts.filter(service='flickr.com')
    for account in accounts:
        try:
            call_flickr('flickr.favorites.add', sign=True, auth_token=account.authinfo, photo_id=photo_id)
        except Exception, exc:
            log.warning("Error favoriting photo %s for Flickr user %s: %s", photo_id, account.display_name, str(exc))
            return HttpResponse('Error favoriting photo: %s' % str(exc), status=400, content_type='text/plain')
コード例 #2
0
ファイル: views.py プロジェクト: markpasc/leapfrog
def complete_flickr(request):
    try:
        frob = request.GET['frob']
    except KeyError:
        raise ValueError("Redirect back from Flickr did not include a frob")

    result = call_flickr('flickr.auth.getToken', sign=True, frob=frob)

    try:
        nsid = result['auth']['user']['nsid']
    except KeyError:
        raise ValueError("Result of Flickr getToken call did not have a user NSID")

    try:
        token = result['auth']['token']['_content']
    except KeyError:
        raise ValueError("Result of Flickr getToken call did not include a token")

    person = None
    if not request.user.is_anonymous():
        person = request.user.person
    account = account_for_flickr_id(nsid, person=person)
    if request.user.is_anonymous():
        person = account.person
        if person.user is None:
            random_name = ''.join(choice(string.letters + string.digits) for i in range(20))
            while User.objects.filter(username=random_name).exists():
                random_name = ''.join(choice(string.letters + string.digits) for i in range(20))
            person.user = User.objects.create_user(random_name, '*****@*****.**' % random_name)
            person.save()

        person.user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(request, person.user)
    else:
        # If the account already existed (because some other user follows
        # that account and had imported objects by them, say), "merge" it
        # onto the signed-in user. (This does mean you can intentionally
        # move an account by signing in as a different django User and re-
        # associating that account, but that's appropriate.)
        account.person = person

    account.authinfo = token
    account.save()

    return HttpResponseRedirect(reverse('home'))