예제 #1
0
def connect_callback(request):
    if request.user.is_anonymous():
        return HttpResponseRedirect(reverse('homepage'))
    
    try:
        if request.user.facebook_profile:
            return HttpResponseRedirect(reverse('homepage'))
    except:
        pass
    
    code = request.GET.get('code')
    
    if not code:
        return HttpResponseRedirect(reverse('homepage'))
    
    fb_component = FacebookComponent(app_id=settings.FACEBOOK_APP_ID, app_secret=settings.FACEBOOK_APP_SECRET)
        
    response = fb_component.connect_facebook(token=code, request=request)
    
    if response == False:
        request.session['sys_message'] = 'Sua conta do facebook já está associada a outra conta do prometo.me.'
    elif response == None:
        request.session['sys_message'] = 'Não foi possível efetuar a associação da sua conta do facebook no momento.'
        
    return HttpResponseRedirect(reverse('dashboard'))
예제 #2
0
def user_register(request, type):
    context = RequestContext(request)

    initial = {}
    fb_cadastro = False

    access_token = request.session.get("fb_access_token")
    fb_profile = None

    if access_token:
        fb_component = FacebookComponent(app_id=settings.FACEBOOK_APP_ID, app_secret=settings.FACEBOOK_APP_SECRET)

        fb_profile = fb_component.get_facebook_profile(access_token)

        if fb_profile:
            initial["nome"] = fb_profile.get("name", "")
            initial["email"] = fb_profile.get("email", "")
            fb_cadastro = True

    if request.method == "POST":
        for key in request.POST.keys():
            initial.update({key: request.POST.get(key)})

        if type == "voluntario":
            user_form = VoluntarioForm(initial)
        else:
            user_form = BeneficiarioForm(initial)
    else:
        if type == "voluntario":
            user_form = VoluntarioForm(initial=initial)
        else:
            user_form = BeneficiarioForm(initial=initial)

    user_form.fb_cadastro = fb_cadastro

    if access_token and fb_profile:
        user_form.fields["nome"].widget.attrs["disabled"] = True
        user_form.fields["nome"].widget.attrs["required"] = False
        user_form.fields["email"].widget.attrs["disabled"] = True
        user_form.fields["email"].widget.attrs["required"] = False

    if request.method == "POST":
        if user_form.is_valid():
            user = user_form.save(access_token=access_token, fb_profile=fb_profile)
            if user:
                user = authenticate(
                    email=user_form.cleaned_data.get("email"), password=user_form.cleaned_data.get("senha")
                )
                if user:
                    login(request, user)
                    return redirect(reverse("dashboard"))

    context.update({"form": user_form, "type": type})

    return render_to_response("cadastro.html", context)
예제 #3
0
def facebook_setup(request):
    context = RequestContext(request)

    token = request.GET.get("code")

    fb_component = FacebookComponent(app_id=settings.FACEBOOK_APP_ID, app_secret=settings.FACEBOOK_APP_SECRET)

    access_token = fb_component.get_access_token(
        code=token, redirect_uri=request.build_absolute_uri("/facebook/authentication_callback")
    )

    request.session["fb_access_token"] = access_token

    return HttpResponseRedirect(reverse("user_register"))