def subscribe(request, code, backend):
    try:
        email = get_signer().unsign(code)
    except BadSignature:
        messages.error(request, _('We are sorry. This link is broken.'))
        return redirect('newsletter_subscription_form')

    if backend.subscribe(email):
        messages.success(request, _('Your subscription has been activated.'))

    form = backend.subscription_details_form(email, request=request)
    if form is None:
        return redirect('newsletter_subscription_form')

    elif request.method == 'POST':
        if form.is_valid():
            messages.success(
                request,
                _('Thank you! The subscription details have been updated.'))
            form.save()

            return redirect('.')

    return render(request, 'newsletter_subscription/subscribe.html', {
        'email': email,
        'form': form,
    })
def subscribe(request, code, backend):
    try:
        email = get_signer().unsign(code)
    except BadSignature:
        messages.error(request, _('We are sorry. This link is broken.'))
        return redirect('newsletter_subscription_form')

    if backend.subscribe(email):
        messages.success(request, _('Your subscription has been activated.'))

    form = NewsletterForm(
        request.POST or None,
        backend=backend,
        request=request,
        initial={
            'email': request.user.email,
        } if request.user.is_authenticated else None,
    )
    if form is None:
        return redirect('newsletter_subscription_form')

    elif request.method == 'POST':
        if form.is_valid():
            messages.success(
                request,
                _('Thank you! The subscription details have been updated.'))
            form.save()

            return redirect(request.path)

    return render(request, 'newsletter_subscription/subscribe.html', {
        'email': email,
        'form': form,
    })
def resubscribe(request, code, backend):
    try:
        email = get_signer().unsign(code)
    except BadSignature:
        messages.error(request, _('We are sorry. This link is broken.'))
        return redirect('newsletter_subscription_form')

    if backend.is_subscribed(email):
        messages.info(request, _('Your subscription is already active.'))

    return redirect('newsletter_subscription_subscribe', code=code)
def resubscribe(request, code, backend):
    try:
        email = get_signer().unsign(code)
    except BadSignature:
        messages.error(request, _('We are sorry. This link is broken.'))
        return redirect('newsletter_subscription_form')

    if backend.is_subscribed(email):
        messages.info(
            request, _('Your subscription is already active.'))

    return redirect('newsletter_subscription_subscribe', code=code)
def unsubscribe(request, code, backend):
    try:
        email = get_signer().unsign(code)
    except BadSignature:
        messages.error(request, _('We are sorry. This link is broken.'))
        return redirect('newsletter_subscription_form')

    if backend.is_subscribed(email):
        backend.unsubscribe(email)

    return render(request, 'newsletter_subscription/unsubscribe.html', {
        'email': email,
        'form': form,
    })