from blogapp.models import blogentry, blogcomment
from django.shortcuts import render, get_object_or_404
from django.template import RequestContext
from django import forms
import datetime

class BlogCommentForm(forms.Form):
    guestname = forms.CharField()
    blogcomment = forms.CharField(widget=forms.Textarea(attrs={'rows':3, 'cols':30}))


def listall(request):
	listall_entries = blogentry.objects.all().order_by('-date')[:10]
	context = {'blogentries': listall_entries}
	return render(request, 'blogapp/blogindex.html', context)

def detail(request, blogentry_id):
    blogdetail = get_object_or_404(blogentry, pk=blogentry_id)

    form = BlogCommentForm()
    context = {'blogentry': blogdetail, 'commentform': form}
    return render(request, 'blogapp/blogdetail.html', context)
	#if request.user.is_authenticated():
	#	return render_to_response('blogapp/blogdetail.html', {'blogentry': blogdetail, 'commentform': form}, context_instance=RequestContext(request))
	#else:
	#	return render_to_response('blogapp/blogdetail.html', {'blogentry': blogdetail}, context_instance=RequestContext(request))

#def addarticle(request):
#	if not request.user.is_staff:
#		return listall(request)
#
#	if request.method == 'POST':
#		form = NewsForm(request.POST)
#		if form.is_valid():
#			blog = blogentry()
#			blog.user = request.user
#			blog.newsheader = form.cleaned_data['header']
#			blog.newsbody = form.cleaned_data['body']
#			blog.date = datetime.datetime.now()
#			blog.save()
#
#
#		#return rather to the thread detail here
#		listall_entries = newsentry.objects.all().order_by('date')[:10]
#		return render_to_response('blogindex.html', {'listall_entries': listall_entries}, context_instance=RequestContext(request))
#	else:
#		form = NewsForm()
#		return render_to_response('blogindex.html', {'showaddnewsform' : form}, context_instance=RequestContext(request))
#
#def editnews(request, newsentry_id):
#	if not request.user.is_staff:
#		return listall(request)
#
#	newsdetail = get_object_or_404(newsentry, pk=newsentry_id)
#
#	if request.method == 'POST':
#		form = NewsForm(request.POST)
#		if form.is_valid():
#			newsdetail.newsheader = form.cleaned_data['newsheader']
#			newsdetail.newsbody = form.cleaned_data['newsbody']
#			newsdetail.save()
#			return detail(request, newsentry_id)
#
#
#	data = {'newsheader': newsdetail.newsheader, 'newsbody': newsdetail.newsbody}
#	form = NewsForm(data)
#	return render_to_response('newsedit.html', {'editform' : form}, context_instance=RequestContext(request))
#
#d#ef showfrontpage(request):
#0	newsdetail = get_object_or_404(newsentry, pk=1)
#	return render_to_response('newsapp/newsdetail.html', {'newsentry': newsdetail, 'isfrontpage': True}, context_instance=RequestContext(request))
#
#
#d#ef getincludes(request):
#	eventlist = evententry.objects.all().order_by('-date')[:10]
#	appointmentlist = event.objects.all().filter(eventend__gte = datetime.datetime.now()).order_by('eventstart')[:5]
#	return {'eventlist' : eventlist, 'appointmentlist' : appointmentlist}
