from django.shortcuts import render
from django.template import loader
from django.urls import reverse

from django.http import HttpResponse, HttpResponseRedirect
from django.utils.html import strip_tags

from django.core.paginator import Paginator

from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth.models import User

from django.db.models import Q

#from blog.forms import *
from core.views import gen_page_base, gen_page_sys
from blog.models import *

def blog_index(request, bcat):

#print(bcat)

	template = loader.get_template('listing.html')
	page = gen_page_base()
	page.p_adresse = reverse('blog_index')
	page.p_titre = "Les articles du Blog"
	page.p_contenu = ""
	page.p_right = "Retrouvez les tous les articles de mon blog"
	page.p_description = strip_tags(page.p_right)
	page.p_mots_clefs = "blog, news, "
	

	b_search = request.GET.get('b_search')

	if b_search != None:
		page.blog_search = Blog_search_Form(initial={'b_search': b_search,})
		q= Q(b_titre__icontains=b_search) | Q(b_mots_clefs__icontains=b_search) | Q(b_description__icontains=b_search)
	else:
		page.blog_search = Blog_search_Form()
	if bcat == "index":
		if b_search != None:
			blog_art = Blog.objects.filter( b_publier = True ).filter(q).order_by( '-b_publdate' )[:5]
		else :
			blog_art = Blog.objects.filter( b_publier = True ).order_by( '-b_publdate' )[:5]
		page.blog_cat = Cat_Blog.objects.all()[:15]

	elif bcat == "all":
		blog_art = Blog.objects.filter( b_publier = True ).order_by( '-b_publdate' )
		page.blog_cat = Cat_Blog.objects.all()[:15]
	else :
		if b_search != None:
			blog_art = Blog.objects.filter( b_publier = True ).filter( b_cat__cb_titre_slgify = bcat ).filter(q).order_by( '-b_publdate' )
		else :
			blog_art = Blog.objects.filter( b_publier = True ).filter( b_cat__cb_titre_slgify = bcat ).order_by( '-b_publdate' )
		page.blog_cat = Cat_Blog.objects.filter( cb_titre_slgify = bcat )[:15]
		page.retour = "blog_index"

	paginator = Paginator(blog_art, 15)
	page.number = request.GET.get('page')

	page.blog_art = paginator.get_page(page.number)
	#print(page.blog_art.paginator.num_pages)
	
	page.blog_art.nbpage = range(page.blog_art.paginator.num_pages)

	for cat in page.blog_cat.all():
		page.p_mots_clefs = page.p_mots_clefs + cat.cb_titre + ', '

	page.blog_top10 = Blog.objects.filter( b_publier = True ).order_by( '-b_reading' )[:10]

	html = template.render({
		'page': page,
		'user': request.user,
		}, request)
		
	return HttpResponse(html)

def blog_play(request, bart):

	template = loader.get_template('read.html')
	page = gen_page_base()
	page.p_adresse = reverse('blog_index')
	page.blog_art = Blog.objects.filter(b_titre_slugify = bart)[:1]
	page.c_card_mp = "True"

	for art in page.blog_art:
		page.p_titre = art.b_titre
		page.p_contenu = art.b_description
		page.p_description = strip_tags(art.b_description)
		if art.b_right != "":
			page.p_right = art.b_right
		else:
			page.p_right = "&nbsp;"

		page.p_mots_clefs = ""
		for cat in art.b_cat.all():
			page.p_mots_clefs = page.p_mots_clefs + cat.cb_titre + ', '

		art.b_reading = art.b_reading + 1
		art.save()

		print(page)

	html = template.render({
		'page': page,
		'user': request.user,
		}, request)
		
	return HttpResponse(html)

def blog_update(request):
	arts = Blog.objects.all()
	for art in arts:
		print(art.b_titre_slugify)
		art.b_contenu = art.b_contenu.replace('é"','é').replace('à"','à').replace('ê"','ê').replace('è"','è').replace('ô"', 'ô').replace('«"', '«').replace('»"', '»')
		#art.b_contenu = art.b_contenu.replace('rsquo','|||').replace('|||','').replace('"',"'").replace("''","'")
		art.save()
	return HttpResponse("OK")