# blogging/views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.template import loader
from django import forms
from django.utils import timezone
from blogging.forms import PostForm
from blogging.models import Post



def list_view(request):
	published = Post.objects.exclude(published_date__exact=None)
	posts = published.order_by('-published_date')
	context = {'posts': posts}
	return render(request, 'blogging/list.html', context)


def detail_view(request, post_id):
	published = Post.objects.exclude(published_date__exact=None)
	try:
		post = published.get(pk=post_id)
	except Post.DoesNotExist:
		raise Http404
	context = {'post': post}
	return render(request, 'blogging/detail.html', context)


def add_model(request):
	if request.method == "POST":
		form = PostForm(request.POST)
		if form.is_valid():
			model_instance = form.save(commit=False)
			model_instance.timestamp = timezone.now()
			model_instance.save()
			return redirect('/')
	else:
		form = PostForm()
		return render(request, "post.html", {'form': form})
