from django.shortcuts import render
from django.views import generic
from django.shortcuts import get_object_or_404

from catalog.models import Book, Author, BookInstance, Genre

def index(request):

	# Generate counts of some of the main objects
	num_books = Book.objects.all().count()
	num_instances = BookInstance.objects.all().count()

	# Available books 
	num_instances_available = BookInstance.objects.filter(status__exact='a').count()

	# The 'all()' is implied by default
	num_authors = Author.objects.count()

	# Number of visits to this view, as counted in the session variable.
	num_visits = request.session.get('num_visits', 0)
	request.session['num_visits'] = num_visits + 1

	context = {
		'num_books':num_books,
		'num_instances':num_instances,
		'num_instances_available':num_instances_available,
		'num_authors':num_authors,
		'num_visits':num_visits,
	}

	# Render the HTML template index.html with the data in the context variable
	return render(request, 'index.html', context=context)

class BookListView(generic.ListView):
	model = Book 
	paginate_by = 10

class BookDetailView(generic.DetailView):
	model = Book 
	def book_detail_view(request,primary_key):
		book = get_object_or_404(Book, pk=primary_key)
		return render(request, 'catalog/book_detail.html',
			context={'book': book})

class AuthorListView(generic.ListView):
	model = Author
	paginate_by = 10

class AuthorDetailView(generic.DetailView):
	model = Author
	def author_detail_view(request,primary_key):
		author = get_object_or_404(Author, pk=primary_key)
		return render(request,'catalog/author_detail.html',
			context={'author':author})




