from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
from django.shortcuts import render
from django.utils.translation import ugettext as _

from books.forms import BookForm
from common.models import BookType


def index(request):
    book_list = BookType.objects.all()
    args = {'book_list': book_list}
    if 'success_msg' in request.session:
        args['success_msg'] = {
            'book_added': _("The book was added successfully."),
            'book_removed': _("The book was removed successfully."),
            'books_removed': _("The books were removed successfully."),
        }[request.session['success_msg']]
        del request.session['success_msg']
    return render(request, 'books/index.html', args)


def add_book(request):
    if request.method == 'POST':
        form = BookForm(request.POST)
        if form.is_valid():
            form.save()
            request.session['success_msg'] = 'book_added'
            return HttpResponseRedirect(reverse('index'))
    else:
        form = BookForm()
    return render(request, 'books/add.html', {'form': form})


def edit_book(request, book_id):
    if request.method == 'POST':
        pass
    else:
        book = BookType.objects.get(id=book_id)
        form = BookForm(instance=book)
        return render(request, 'books/edit.html', {'form': form})


def remove_book(request, book_ids):
    book_list = BookType.objects.filter(id__in=book_ids.split(','))
    if request.method == 'POST':
        request.session['success_msg'] = 'book_removed' if len(book_list) == 1 else 'books_removed'
        book_list.delete()
        return HttpResponseRedirect(reverse('index'))
    else:
        return render(request, 'books/remove.html', {'book_list': book_list})

@@ -475,32 +475,99 @@
 uccessfully.%22),%0A
+            'book_edited': _(%22The book was edited successfully.%22),%0A
             'boo
@@ -1233,107 +1233,312 @@
     
-if request.method == 'POST':%0A        pass%0A    else:%0A        book = BookType.objects.get(id=book_id)
+book = BookType.objects.get(id=book_id)%0A    if request.method == 'POST':%0A        form = BookForm(request.POST, instance=book)%0A        if form.is_valid():%0A            form.save()%0A            request.session%5B'success_msg'%5D = 'book_edited'%0A            return HttpResponseRedirect(reverse('index'))%0A    else:
 %0A   
@@ -1569,28 +1569,24 @@
 tance=book)%0A
-    
     return r

Implement book editing