from django.db import models
from django import forms

from django.template.defaultfilters import slugify

from tinymce.widgets import TinyMCE  # Réactivé

class Cat_Blog(models.Model) :
	cb_titre = models.CharField("Titre", max_length = 128, unique = True)
	cb_titre_slgify = models.CharField("Titre Slugify", max_length = 128, blank = True, editable = False)

	class Meta :
		verbose_name = 'Catégories'
		verbose_name_plural = 'Catégories'

	def save(self, *args, **kwargs):
		self.cb_titre_slgify = slugify(self.cb_titre)
		super().save(*args, **kwargs)  # Python 3 moderne

	def __str__(self):
		return '%s' % (self.cb_titre)

class Blog(models.Model) : #Architecture pour le blog
	b_titre = models.CharField("Titre", max_length = 128, unique = True)
	b_titre_slugify = models.CharField("Titre Slugify", max_length = 128, blank = True, editable = False)
	b_mots_clefs = models.CharField("Mots clefs", max_length = 512, blank = True)
	b_cat = models.ManyToManyField(Cat_Blog, blank = True)
	b_description = models.TextField("Description", blank = True)
	b_description_img = models.URLField("Url de la miniature", max_length = 128, blank = True)
	b_contenu = models.TextField("Contenu", blank = True)
	b_right = models.TextField("Contenu à droite", blank = True)
	b_publier = models.BooleanField("Publié", default = False)
	b_reading = models.IntegerField("Nb Lectures", default = 0)
	b_publdate = models.DateTimeField("Publié le", blank = True, null = True)

	class Meta :
		verbose_name = 'Gestion du Blog'
		verbose_name_plural = 'Gestion du Blog'
		ordering = ['-b_publdate']

	def save(self, *args, **kwargs):
		self.b_titre_slugify = slugify(self.b_titre)
		super().save(*args, **kwargs)  # Python 3 moderne

	def __str__(self):
		return '%s' % (self.b_titre)


class Blog_search_Form(forms.Form):
	b_search = forms.CharField(label='Vous recherchez un article ?', max_length=100)

class Blog_Admin_Form(forms.ModelForm):  # Réactivé
	class Meta:
		model = Blog
		exclude = ['b_titre_slugify','b_reading']
		widgets = {
			'b_contenu': TinyMCE(),
			'b_right': TinyMCE(),
		}