from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect, Http404, HttpResponse
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.exceptions import ObjectDoesNotExist
from django.conf import settings 

# Create your views here.
from miniblog.models import Article, Author, Tag, Classification, RootClassification
from miniblog.forms import ArticleForm
from miniblog.forms import TagForm

def blog_note_show(request, article_id):
    article = get_object_or_404(Article, pk=article_id)
    #article.content = mistune.markdown(article.content)
    if request.is_ajax():
        return render(request, 'miniblog/blog_note_show_ajax.html', {"article": article})
    else:
        articles_list = Article.objects.all()
        articles_list = pagination(request, articles_list)
        classes_list = Classification.objects.all()
        rootClass_list = RootClassification.objects.all()
        return render(request, 'miniblog/blog_note_show.html', {
            "classes":classes_list, 
            "articles": articles_list, 
            "article": article,
            "current_class": "all",
            "root_classes": rootClass_list
        })

def blog_note_all(request):
    articles_list = Article.objects.all()
    articles_list = pagination(request, articles_list)
    # if is ajax request
    if request.is_ajax():
        return render(request, 'miniblog/blog_note_article_list_ajax.html', 
            {"articles": articles_list, "current_class": "all"})
    # not an ajax request should return the whole page
    else:
        classes_list = Classification.objects.all()
        article = articles_list[0]
        rootClass_list = RootClassification.objects.all()
        return render(request, 'miniblog/blog_note_show.html', {
            "classes":classes_list, 
            "articles": articles_list,
            "article": article,
            "current_class": "all",
            "root_classes": rootClass_list
        })
def blog_note_by_class(request, class_id):
    try:
        classobject = Classification.objects.get(pk = int(class_id))
        rootClass_list = RootClassification.objects.all()
        articles_list = classobject.article_set.all()
        articles_list = pagination(request, articles_list)
        if request.is_ajax():
            return render(request, 'miniblog/blog_note_article_list_ajax.html', 
                {"articles": articles_list, "current_class": class_id})
        else:
            classes_list = Classification.objects.all()
            article = articles_list[0]
            return render(request, 'miniblog/blog_note_show.html', {
                "classes":classes_list, 
                "articles": articles_list, 
                "current_class": class_id, 
                "article": article,
                "root_classes": rootClass_list
            })
    # if no article or no class is found
    except ObjectDoesNotExist:
        return blog_note_all(request)

def pagination(request, queryset):
    if settings.PAGINATION == True:
        pageNO = request.GET.get('page', 1)
        num_in_page = settings.NUM_IN_PAGE