def unsubscribe(request, newsletter_slug):
    """
    Unsubscribe a user from the newsletter.
    
    Requires a POST with an email
    """
    newsletter = get_object_or_404(Newsletter, slug=newsletter_slug)
    if request.method != 'POST':
        if is_json_request:
            return JSONResponse({
                'success': False, 
                'message': 'Only POST methods are allowed.'})
        return HttpResponseNotAllowed(['POST',])
    if 'email' not in request.POST:
        if is_json_request:
            return JSONResponse({
                'success': False, 
                'message': 'No email field was included.'})
        raise Http404("email not included in POST")
    try:
        sub = Subscription.objects.get(email=request.POST['email'],
                                       newsletter=newsletter)
        sub.delete()
        unsubscription.send(sender=newsletter, email=request.POST['email'],
                                        newsletter=newsletter)
        send_notification([newsletter], [], request.POST['email'])
    except Subscription.DoesNotExist:
        pass # The user wasn't subscribed, so just fail gracefully.
    
    if is_json_request(request):
        return JSONResponse({'success': True, 'message': ''})
    return render_to_response('newsletters/unsubscribe.html', {
        'newsletter': newsletter
    }, RequestContext(request))
def sync_subscriptions(sub_form):
    """
    Do all the work of (un)subscribing newsletters for an account
    """
    old_subs = Subscription.objects.filter(email=sub_form.cleaned_data['email'])
    old_subs_nl = [item.newsletter for item in old_subs]
    new_subs = Newsletter.objects.filter(name__in=sub_form.get_subscriptions())
    
    unsubs = [sub for sub in old_subs if sub.newsletter not in new_subs]
    unsub_nl = [sub.newsletter for sub in unsubs]
    subs = [nl for nl in new_subs if nl not in old_subs_nl]
    
    for item in unsubs:
        unsubscription.send(
            sender=item.newsletter, 
            email=sub_form.cleaned_data['email'],
            newsletter=item.newsletter)
        item.delete()
    for item in subs:
        sub = Subscription.objects.create(
            email=sub_form.cleaned_data['email'],
            newsletter=item,
            confirmed=AUTO_CONFIRM,
        )
        subscription.send(
            sender=item,
            email=sub_form.cleaned_data['email'],
            newsletter=item,
        )
    send_notification(unsub_nl, subs, sub_form.cleaned_data['email'])
    return unsub_nl, subs