Beispiel #1
0
def inviter(request):
    next = request.REQUEST.get('next', '/')
    if not request.facebook:
        return HttpResponseRedirect(next)
    errors = ""

    me = request.facebook.graph.get_object('me', fields='name, first_name, last_name, picture')
    friends = request.facebook.graph.get_connections('me', 'friends')
    
    facebook_data = {}
    facebook_data['name'] = me['name'] if me.get('name') else ''
    facebook_data['first_name'] = me['first_name'] if me.get('first_name') else ''
    facebook_data['last_name'] = me['last_name'] if me.get('last_name') else ''
    facebook_data['num_of_friends'] = len(friends.get('data'))
    facebook_data['thumb_url'] = me['picture'] if me.get('picture') else ''
    
#    friends_data = friends.get('data')
#    friends = [[friend['name'], friend['id']] for friend in friends_data]
#    friends = simplejson.dumps(friends)
    
    if request.method == 'POST':
        # test
        inviter_info = request.POST.copy()
        form = FacebookSignupForm(request.POST)
        if form.is_valid():
            user = form.save(facebook_data)
            # save facebook photos
            save_photos(user, request.facebook)
            facebook_user = FacebookUser.objects.create(
                user=user,
                facebook_id=request.facebook.uid,
                access_token=request.facebook.user['access_token'],
            )
            # save inviter info
        #    inviter_id = inviter_info.get('inviter_id', '')
            inviter_name = inviter_info.get('inviter_name', '')
        #    if inviter_id:
        #        facebook_user.inviter_id = inviter_id
            if inviter_name:
                facebook_user.inviter_name = inviter_name
            facebook_user.save()
            # auth and save temp books
            user = auth.authenticate(fb_uid=request.facebook.uid, fb_graphtoken=request.facebook.user['access_token'])
            auth.login(request, user)
            save_temp_books(request, user)
            return HttpResponseRedirect('/profile/')
        else:
            errors = form.errors
    
    form = FacebookSignupForm()
    return render_to_response('signup_facebook_inviter.html', {'form': form, 'facebook_data': facebook_data, 'errors': errors, 'friends':friends}, context_instance=RequestContext(request))
Beispiel #2
0
def signup(request):
    next = request.REQUEST.get('next', '/')
    if not request.facebook:
        return HttpResponseRedirect(next)
    errors = ""

    me = request.facebook.graph.get_object('me', fields='name, first_name, last_name, picture')
    friends = request.facebook.graph.get_connections('me', 'friends')
    
    facebook_data = {}
    facebook_data['name'] = me['name'] if me.get('name') else ''
    facebook_data['first_name'] = me['first_name'] if me.get('first_name') else ''
    facebook_data['last_name'] = me['last_name'] if me.get('last_name') else ''
    facebook_data['num_of_friends'] = len(friends.get('data'))
    facebook_data['thumb_url'] = me['picture'] if me.get('picture') else ''
    
    if request.method == 'POST':
        form = FacebookSignupForm(request.POST)
        if form.is_valid():
            user = form.save(facebook_data)
            # save facebook photos
            save_photos(user, request.facebook)
            facebook_user = FacebookUser.objects.create(
                user=user,
                facebook_id=request.facebook.uid,
                access_token=request.facebook.user['access_token']
            )
            facebook_user.save()
            user = auth.authenticate(fb_uid=request.facebook.uid, fb_graphtoken=request.facebook.user['access_token'])
            auth.login(request, user)
            save_temp_books(request, user)
            return HttpResponseRedirect('/profile/')
        else:
            errors = form.errors
    
    form = FacebookSignupForm()
    return render_to_response('signup_facebook.html', {'form': form, 'facebook_data': facebook_data, 'errors': errors}, context_instance=RequestContext(request))