def signup_form(request):
    login_form = AuthenticationForm(request)
    register_form = UserCreationForm()

    if request.method=='POST':
        action = request.POST.get('action',False)
        if action=='register':
            # create new dict for copying email into username because request.POST is immutable
            musiphile_email = request.POST.get('musiphile_email')
            password1 = request.POST.get('password1')
            password2 = request.POST.get('password2')

            data = {
                'username' : musiphile_email,
                'musiphile_email' : musiphile_email,
                'password1' : password1,
                'password2' : password2,
            }

            register_form = UserCreationForm(data=data)
            register_form.is_valid()

        elif action=='login':
            data = request.POST.copy()
            login_form = AuthenticationForm(data=data)
            login_form.is_valid()


    login_form.auto_id = 'login_%s'
    register_form.auto_id = 'signup_%s'


    return {'login_form':login_form, 'register_form':register_form}
Exemple #2
0
def ajax_signup(request):
    '''
    Similar to signup, except return json formatted request.
    '''
    if request.method!='POST':
        return HttpResponseNotAllowed(['POST'])

    login_form = AuthenticationForm()
    # create new dict for copying email into username because request.POST is immutable
    musiphile_email = request.POST.get('musiphile_email')
    password1 = request.POST.get('password1')
    password2 = request.POST.get('password2')
    data = {
            'username' : musiphile_email,
            'musiphile_email' : musiphile_email,
            'password1' : password1,
            'password2' : password2,
    }


    register_form = UserCreationForm(data)
    if register_form.is_valid():
        user = register_form.save()

        # log user in
        user = authenticate(musiphile_email=user.musiphile_email, password=request.POST['password1'])
        from django.contrib.auth import login
        login(request,user)
        
        save_session_playhistory(request,user)
        return HttpResponse(json.dumps({'status':'ok', 'user_id':user.id, 'user_name':user.username}), content_type="application/json")
   
    errors = {}
    c = RequestContext(request, {'show_error_message':True})
    t = loader.get_template('accounts/register_form.html')
    errors['message'] = t.render(c)
    errors['status'] = ['failed']
    return HttpResponse(json.dumps(errors), content_type="application/json")
Exemple #3
0
def signup(request, template_name='accounts/signup.html',redirect_field_name='next'):
    '''
    Override built-in django login to add following featured:
        - also pass register form
        - use email address to authenticate
        - if session contains a lightbox, save to user list
    '''
    redirect_to = request.REQUEST.get(redirect_field_name, '')
    # Light security check -- make sure redirect_to isn't garbage
    if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
        redirect_to = settings.LOGIN_REDIRECT_URL

    if request.method == "POST":
        action = request.POST.get('action', False)
        if action=='login':
            login_form = AuthenticationForm(data=request.POST)
            register_form = UserCreationForm()
            if login_form.is_valid():
                from django.contrib.auth import login
                user = login_form.get_user()
                login(request, user)
                if request.session.test_cookie_worked():
                    request.session.delete_test_cookie()

                save_session_playhistory(request,request.user)
                
                return HttpResponseRedirect(redirect_to)
        elif action=='register':
            login_form = AuthenticationForm(request)
            # create new dict for copying email into username because request.POST is immutable
            musiphile_email = request.POST.get('musiphile_email','')
            password1 = request.POST.get('password1','')
            password2 = request.POST.get('password2','')
            data = {
                    'username' : musiphile_email,
                    'musiphile_email' : musiphile_email,
                    'password1' : password1,
                    'password2' : password2,
            }


            register_form = UserCreationForm(data)
            if register_form.is_valid():
                user = register_form.save()

                # log user in
                user = authenticate(musiphile_email=user.musiphile_email, password=request.POST['password1'])
                from django.contrib.auth import login
                login(request,user)
                
                save_session_playhistory(request,user)
                return HttpResponseRedirect(redirect_to)
        else:
            return ResponseBadRequest()

    else: # request is get, render page
        login_form = AuthenticationForm(request)
        register_form = UserCreationForm()

    request.session.set_test_cookie()

    if Site._meta.installed:
        current_site = Site.objects.get_current()
    else:
        current_site = RequestSite(request)



    return render_to_response(template_name, {
        'login_form': login_form,
        'register_form': register_form,
        redirect_field_name: redirect_to,
        'site_name': current_site.name,
    }, context_instance=RequestContext(request))