from django.shortcuts import render_to_response
from django.http import Http404, HttpResponse, HttpResponseRedirect
import datetime
from django import forms
from django.contrib.auth.forms import UserCreationForm

def hello(request):
	return HttpResponse("Hello world")

def current_datetime(request):
	now = datetime.datetime.now()
	return render_to_response('current_datetime.html', {'current_date':now})

def hours_ahead(request, offset):
	try:
		offset = int(offset)
	except ValueError:
		raise Http404()
	dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
	html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
	return HttpResponse(html)

def display_meta1(request):
	values = request.META.items()
	values.sort()
	html = []
	for k, v in values:
		html.append('<tr><td>%s</td><td>%s</td></tr>' % (k, v))
	return HttpResponse('<table>%s</table>' % '\n'.join(html))

def display_meta(request):
	dicts = request.META.items()
	dicts.sort()
	return render_to_response('display_meta.html', {'dicts':dicts})

def register(request):
	if request.method == 'POST':
		form = UserCreationForm(request.POST)
		if form.is_valid():
			new_user = form.save()
			return HttpResponseRedirect("/books")
	else:
		form = UserCreationForm()
	return render_to_response("registration/register.html",{
		'form': form,
	})