from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.views.decorators.http import require_POST
from django.utils.translation import ugettext as _

from .models import AddressBook
from .forms import AddressBookForm, AddressForm


@login_required
def details(request):

    ctx = {'address_book': request.user.address_book.all()}
    return TemplateResponse(request, 'userprofile/details.html', ctx)


@login_required
def orders(request):

    ctx = {'orders': request.user.orders.prefetch_related('groups')}
    return TemplateResponse(request, 'userprofile/orders.html', ctx)


def validate_address_and_render(request, address_form, address_book_form,
                                success_message):
    if address_form.is_valid() and address_book_form.is_valid():
        address = address_form.save()
        address_book_form.instance.address = address
        address_book_form.save()
        messages.success(request, success_message)
        return HttpResponseRedirect(reverse('profile:details'))

    return TemplateResponse(
        request,
        'userprofile/address-edit.html',
        {'address_form': address_form, 'address_book_form': address_book_form})


@login_required
def address_edit(request, slug, pk):

    address_book = get_object_or_404(AddressBook, pk=pk, user=request.user)
    address = address_book.address

    if not address_book.get_slug() == slug and request.method == 'GET':
        return HttpResponseRedirect(address_book.get_absolute_url())

    address_form = AddressForm(request.POST or None, instance=address)
    address_book_form = AddressBookForm(
        request.POST or None, instance=address_book)

    message = _('Address successfully updated.')

    return validate_address_and_render(
        request, address_form, address_book_form, success_message=message)


@login_required
def address_create(request):

    address_form = AddressForm(request.POST or None)
    address_book_form = AddressBookForm(
        request.POST or None, instance=AddressBook(user=request.user))

    message = _('Address successfully created.')

    return validate_address_and_render(
        request, address_form, address_book_form, success_message=message)


@login_required
def address_delete(request, slug, pk):

    address_book = get_object_or_404(AddressBook, pk=pk, user=request.user)

    if not address_book.get_slug() == slug:
        raise Http404

    if request.method == 'POST':
        address_book.address.delete()
        messages.success(request, _('Address successfully deleted.'))
        return HttpResponseRedirect(reverse('profile:details'))

    return TemplateResponse(request, 'userprofile/address-delete.html',
                            context={'object': address_book})


@login_required
@require_POST
def address_make_default(request, pk, purpose):
    user = request.user

    address_book = get_object_or_404(AddressBook, pk=pk, user=user)

    if purpose == 'shipping':
        user.default_shipping_address = address_book
    elif purpose == 'billing':
        user.default_billing_address = address_book
    else:
        raise AssertionError(
            '``purpose`` should be ``billing`` or ``shipping``')

    user.save()

    return redirect('profile:details')

@@ -434,16 +434,25 @@
 ressBook
+, Address
 %0Afrom .f
@@ -2073,33 +2073,32 @@
 ssage=message)%0A%0A
-%0A
 @login_required%0A
@@ -2339,38 +2339,94 @@
 created.')%0A%0A    
-return
+is_first_address = not Address.objects.exists()%0A    response =
  validate_addres
@@ -2514,16 +2514,362 @@
 essage)%0A
+    address_book = address_book_form.instance%0A    if address_book.pk and is_first_address:%0A        user = request.user%0A        user.default_shipping_address = address_book%0A        user.default_billing_address = address_book%0A        user.save(update_fields=%5B%0A            'default_shipping_address', 'default_billing_address'%5D)%0A    return response%0A
 %0A%0A@login
@@ -3571,25 +3571,24 @@
  user=user)%0A
-%0A
     if purpo
@@ -3654,24 +3654,86 @@
 ddress_book%0A
+        user.save(update_fields=%5B'default_shipping_address'%5D)%0A
     elif pur
@@ -3799,24 +3799,85 @@
 ddress_book%0A
+        user.save(update_fields=%5B'default_billing_address'%5D)%0A
     else:%0A  
@@ -3974,25 +3974,8 @@
 ')%0A%0A
-    user.save()%0A%0A
     

Set first address as a default billing & shipping