from django.shortcuts import render, get_object_or_404,redirect
from .models import Blog
from django.utils import timezone

def home(request):
    blogs = Blog.objects
    return render(request, 'home.html', {'blogs': blogs})

def detail(request, blog_id):
    blog_detail = get_object_or_404(Blog, pk= blog_id)
    return render(request, 'detail.html', {'blog': blog_detail})

def edit(request, blog_id):
    blog_edit= Blog.objects.get(pk=blog_id)
    return render(request, 'edit.html',{'blog':blog_edit})

def update(request, blog_id):
    blog = Blog.objects.get(pk=blog_id)
    blog.title = request.POST['title']
    blog.body = request.POST['body']
    blog.pub_date = timezone.datetime.now()
  
    blog.save()
    
    return redirect('home')

def delete(request, blog_id):
    blog = Blog.objects.get(pk= blog_id)
    blog.delete()
    return redirect('home')

def new(request):
    return render(request, 'new.html')

def create(request):
    blog = Blog()
    blog.title = request.GET['title']
    blog.body = request.GET['body']
    blog.pub_date = timezone.datetime.now()
    blog.save()
    return redirect('/blog/' + str(blog.id))

def comment_new(request,blog_id):
    post=get_object_or_404(Blog,pk=blog_id)
    if request.method=="POST":
        form=CommentForm(request.POST)
        if form.is_valid():
            comment=form.save(commit=False)
            comment.post=Blog.objects.get(pk=blog_id)
            comment.save()
            return redirect('detail',blog_id)
    else:
        form=CommentForm()
    return render(request, 'blog_form.html',{'form':form,})