示例#1
0
def socialregistration_friends(request,
    template_name='socialregistration/friends.html', extra_context=None):
    context = {}

    friends = []
    conditions = []
    user = request.user
    
    for facebookprofile in user.facebookprofile_set.all():
        print facebookprofile
        friendlist = request.facebook.graph.request(request.facebook.user['uid'] + '/friends')
        facebook_ids = [x['id'] for x in friendlist['data']]
        conditions.append(Q(facebookprofile__uid__in=facebook_ids))

    if user.twitterprofile_set.count():
        client = OAuthTwitter(
            request, settings.TWITTER_CONSUMER_KEY,
            settings.TWITTER_CONSUMER_SECRET_KEY,
            settings.TWITTER_REQUEST_TOKEN_URL,
        )
    for twitterprofile in user.twitterprofile_set.all():
        res = simplejson.loads(client.query('http://api.twitter.com/1/statuses/friends.json'))
        twitter_ids = [x['id'] for x in res]
        conditions.append(Q(twitterprofile__twitter_id__in=twitter_ids))

    for gfcprofile in user.gfcprofile_set.all():
        container = gfc.my_opensocial_container(request)
        res = container.fetch_friends()
        gfc_ids = [x['id'] for x in res]
        conditions.append(Q(gfcprofile__uid__in=gfc_ids))

    context['friends'] = User.objects.filter(reduce(operator.or_,conditions))
    context['follows'] = [f.actor for f in user.follow_set.all()]

    context.update(extra_context or {})
    return shortcuts.render_to_response(template_name, context,
        context_instance=template.RequestContext(request))
示例#2
0
def gfc_callback(request,
    template_name='gfc/callback.html', extra_context=None):
    context = {}
    context.update(extra_context or {})

    st = request.COOKIES.get('fcauth' + settings.GOOGLE_SITE_ID, False)
    if not st:
        context['error'] = 'nocookie'
        return shortcuts.render_to_response(template_name, context,
            context_instance=template.RequestContext(request))

    uid = st[st.find('~')+1:st.rfind('~')]

    user = auth.authenticate(uid=uid)

    container = gfc.my_opensocial_container(request)
    req = opensocial.FetchPersonRequest(uid, ['@all'])
    result = container.send_request(req)

    if user is None:
        user = User(
            first_name=result['displayName'],
            last_name=result['displayName']
        )
        profile = GfcProfile(uid=uid, url=result['urls'][0]['value'], user=user)
        request.session['socialregistration_user'] = user
        request.session['socialregistration_profile'] = profile
        return http.HttpResponseRedirect(urlresolvers.reverse('socialregistration_setup'))

    if not user.is_active:
        return shortcuts.render_to_response(account_inactive_template, context,
            context_instance=RequestContext(request))

    auth.login(request, user)

    return http.HttpResponseRedirect(_get_next(request))
示例#3
0
def socialregistration_userdata(request, form_class=UserDataForm,
    template_name='socialregistration/userdata.html', extra_context=None):
    context = {}

    if 'socialregistration_profile' in request.session:
        profile = request.session['socialregistration_profile']
        user = request.session['socialregistration_user']
    elif request.user.is_authenticated():
        if request.user.facebookprofile_set.count():
            profile = request.user.facebookprofile_set.all()[0]
        elif request.user.twitterprofile_set.count():
            profile = request.user.twitterprofile_set.all()[0]
        if request.user.gfcprofile_set.count():
            profile = request.user.gfcprofile_set.all()[0]
        user = request.user
    else:
        return http.HttpResponseRedirect(urlresolvers.reverse(
            'acct_signup'))

    if request.method == 'POST':
        form = form_class(request.POST)
        if form.is_valid():
            request.session['socialregistration_userdata'] = form.cleaned_data
            
            user = request.session['socialregistration_user']
            profile = request.session['socialregistration_profile']
            userdata = request.session['socialregistration_userdata']

            user.first_name = userdata.get('first_name', '')
            user.last_name = userdata.get('last_name', '')
            user.email = userdata.get('email', '')
            user_slug = defaultfilters.slugify('%s %s' % (
                user.first_name,
                user.last_name
            ))
            if User.objects.filter(username=user_slug).count() > 0:
                i = 1
                user_slug_test = user_slug + unicode(i)
                while User.objects.filter(username=user_slug_test).count() >0:
                    i += 1
                    user_slug_test = user_slug + str(i)
                user.username = user_slug_test
            else:
                user.username = user_slug
            user.save()

            user.playlistprofile.user_location = userdata.get('location', '')
            user.playlistprofile.avatar_url = userdata.get('avatar_url', '')
            user.playlistprofile.user = user
            user.playlistprofile.save()

            profile.user = user
            profile.avatar_url = userdata.get('avatar_url', '')
            profile.url = userdata.get('url', '')
            profile.nick = userdata.get('nick', '')
            profile.save()

            if 'socialregistration_user' in request.session: 
                del request.session['socialregistration_user']
            if 'socialregistration_profile' in request.session: 
                del request.session['socialregistration_profile']
            if 'socialregistration_userdata' in request.session: 
                del request.session['socialregistration_userdata']

            user = profile.authenticate()
            auth.login(request, user)
            request.user = user

            if not user:
                logger.info('NOT USER IN REGISTRATION!')
                return shortcuts.render_to_response('socialregistration/fail.html', context,
                    context_instance=template.RequestContext(request))

            friends = []
            conditions = []
            
            for facebookprofile in user.facebookprofile_set.all():
                print facebookprofile
                friendlist = request.facebook.graph.request(request.facebook.user['uid'] + '/friends')
                facebook_ids = [x['id'] for x in friendlist['data']]
                conditions.append(Q(facebookprofile__uid__in=facebook_ids))

            if user.twitterprofile_set.count():
                client = OAuthTwitter(
                    request, settings.TWITTER_CONSUMER_KEY,
                    settings.TWITTER_CONSUMER_SECRET_KEY,
                    settings.TWITTER_REQUEST_TOKEN_URL,
                )
            for twitterprofile in user.twitterprofile_set.all():
                res = simplejson.loads(client.query('http://api.twitter.com/1/statuses/friends.json'))
                twitter_ids = [x['id'] for x in res]
                conditions.append(Q(twitterprofile__twitter_id__in=twitter_ids))

            for gfcprofile in user.gfcprofile_set.all():
                container = gfc.my_opensocial_container(request)
                res = container.fetch_friends()
                gfc_ids = [x['id'] for x in res]
                conditions.append(Q(gfcprofile__uid__in=gfc_ids))
            
            for u in User.objects.filter(reduce(operator.or_,conditions)):
                follow(user, u)

            return http.HttpResponseRedirect(urlresolvers.reverse('socialregistration_friends'))
    else:
        if profile.__class__.__name__ == 'FacebookProfile':
            upstream = request.facebook.graph.request(request.facebook.user['uid'])
            initial = {
                'first_name': upstream.get('first_name', ''),
                'last_name': upstream.get('last_name', ''),
                'email': upstream.get('email', ''),
                'avatar_url': 'http://graph.facebook.com/%s/picture' % request.facebook.user['uid'],
                'nick': '%s %s' % (upstream.get('first_name', ''), upstream.get('last_name', '')),
                'url': upstream.get('link', ''),
            }
            if 'location' in upstream:
                initial['location'] = upstream['location']['name']
            else:   
                initial['location'] = ''

        elif profile.__class__.__name__ == 'TwitterProfile':
            client = OAuthTwitter(
                request, settings.TWITTER_CONSUMER_KEY,
                settings.TWITTER_CONSUMER_SECRET_KEY,
                settings.TWITTER_REQUEST_TOKEN_URL,
            )
            upstream = client.get_user_info()
            initial = {
                'first_name': '',
                'last_name': upstream.get('name', ''),
                'location': upstream.get('location', ''),
                'email': '',
                'avatar_url': upstream['profile_image_url'],
                'url': 'http://twitter.com/%s' % upstream['screen_name'],
                'nick': upstream['screen_name'],
            }

        elif profile.__class__.__name__ == 'GfcProfile':
            container = gfc.my_opensocial_container(request)
            req = opensocial.FetchPersonRequest(profile.uid, ['@all'])
            upstream = container.send_request(req)
            initial = {
                'first_name': upstream.get('displayName', ''),
                'last_name': '',
                'location': '',
                'email': '',
                'avatar_url': upstream['thumbnailUrl'],
                'url': upstream['urls'][0]['value'],
                'nick': upstream['displayName'],
            }
        form = form_class(initial=initial)

    context['form'] = form
    context['step'] = 'userdata'

    context.update(extra_context or {})
    return shortcuts.render_to_response(template_name, context,
        context_instance=template.RequestContext(request))