Beispiel #1
0
def email_login(request, template="registration/login.html", extra_context=None):
    """A generic view that you can use instead of the default auth.login view, for email logins.
       On GET:
            will render the specified template with and pass the an empty EmailLoginForm as login_form
            with its context, that you can use for the login.
       On POST:
            will try to validate and authenticate the user, using the EmailLoginForm. Upon successful
            login, will redirect to whatever the standard LOGIN_REDIRECT_URL is set to, or the 'next'
            parameter, if specified."""

    if request.method == 'POST':
        login_form = EmailLoginForm(data=request.POST)
        if login_form.is_valid():
            # The user has been authenticated, so log in and redirect
            user = login(request, login_form.user)
            # Redirect to page pointed to by the 'next' param, or else just the first page
            next_page = request.REQUEST.get('next', settings.LOGIN_REDIRECT_URL)
            return HttpResponseRedirect(next_page)
    else:
        login_form = EmailLoginForm()

    context = { 'login_form':login_form, 'next':request.GET.get('next') }
    if extra_context is None: extra_context = {}
    for key, value in extra_context.items():
        if callable(value):
            context[key] = value()
        else:
            context[key] = value

    return render_to_response(template, context, context_instance=RequestContext(request))
Beispiel #2
0
def email_login(request, template="registration/login.html", extra_context=None):
    """A generic view that you can use instead of the default auth.login view, for email logins.
       On GET:
            will render the specified template with and pass the an empty EmailLoginForm as login_form
            with its context, that you can use for the login.
       On POST:
            will try to validate and authenticate the user, using the EmailLoginForm. Upon successful
            login, will redirect to whatever the standard LOGIN_REDIRECT_URL is set to, or the 'next'
            parameter, if specified."""

    if request.method == 'POST':
        login_form = EmailLoginForm(data=request.POST)
        if login_form.is_valid():
            # The user has been authenticated, so log in and redirect
            user = login(request, login_form.user)
            # Redirect to page pointed to by the 'next' param, or else just the first page
            next_page = request.REQUEST.get('next', settings.LOGIN_REDIRECT_URL)
            return HttpResponseRedirect(next_page)
    else:
        login_form = EmailLoginForm()

    context = { 'form':login_form, 'next':request.GET.get('next') }
    if extra_context is None: extra_context = {}
    for key, value in extra_context.items():
        if callable(value):
            context[key] = value()
        else:
            context[key] = value

    return render_to_response(template, context, context_instance=RequestContext(request))
Beispiel #3
0
def email_login(request, template="registration/login.html", extra_context=None):
    """A generic view that you can use instead of the default auth.login view, for email logins.
       On GET:
            will render the specified template with and pass the an empty EmailLoginForm as login_form
            with its context, that you can use for the login.
       On POST:
            will try to validate and authenticate the user, using the EmailLoginForm. Upon successful
            login, will redirect to whatever the standard LOGIN_REDIRECT_URL is set to, or the 'next'
            parameter, if specified."""

    if request.method == 'POST':

        login_form = EmailLoginForm(data=request.POST)
        if login_form.is_valid():
            # The user has been authenticated, so log in and redirect
            user = login(request, login_form.user)
            # Redirect to page pointed to by the 'next' param, or else just the first page
            #next_page = request.REQUEST.get('next', settings.LOGIN_REDIRECT_URL)

            ####### send an email to admins #######
            #if not settings.DEBUG:
                #subject = 'New login'
                #message = request.user.username+' logged in with '+request.user.email
                #send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, ['*****@*****.**'], fail_silently=True)
            ####### end #######

            date = []
            date.append(str(datetime.now()))

            with open(os.path.join(settings.STATIC_ROOT, 'login_track.txt'), "a") as text_file:
                text_file.write(date[0]+'    '+request.user.username+' logged in with '+request.user.email+'\n')

            # check if first login 
            if request.user.get_profile().is_first_login:
                # send to the tutorial
                return HttpResponseRedirect(reverse('webapp_helloworld'))

            #return HttpResponseRedirect(next_page)
            return HttpResponseRedirect(reverse('webapp_index'))
    else:

        login_form = EmailLoginForm()

    context = { 'form':login_form, 'next':request.GET.get('next') }
    if extra_context is None: 
        extra_context = {}
    for key, value in extra_context.items():
        if callable(value):
            context[key] = value()
        else:
            context[key] = value

    return render_to_response(template, context, context_instance=RequestContext(request))
Beispiel #4
0
def login(request):
    form = EmailLoginForm()

    if request.method == 'POST':
        form = EmailLoginForm(request.POST)
        if form.is_valid():
            # Remove previous tokens
            email = form.cleaned_data['email']
            EmailToken.objects.filter(email=email).delete()

            # Create new
            token = EmailToken(email=email)
            token.save()

            sendEmailToken(request, token)
            return redirect(reverse_lazy('login_mailsent'))

    return render(request, 'login.html', {
        'form': form,
    })