kpsul/gestioncof/cms/models.py

211 lines
7.1 KiB
Python
Raw Normal View History

2017-08-07 23:31:27 +02:00
from django.db import models
from wagtail.wagtailcore.models import Page, Orderable
from wagtail.wagtailcore.fields import RichTextField, StreamField
from wagtail.wagtailcore import blocks
2017-08-09 00:07:56 +02:00
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailimages.blocks import ImageChooserBlock
2017-08-26 18:05:20 +02:00
from django.utils.translation import ugettext as _
2017-08-07 23:31:27 +02:00
2017-08-09 00:07:56 +02:00
from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel
from wagtail.wagtailsnippets.models import register_snippet
2017-08-07 23:31:27 +02:00
# Page pouvant afficher des actualités
class COFActuIndexMixin():
@property
def actus(self):
actus = COFActuPage.objects.live().descendant_of(self)
events = COFActuEventPage.objects.live().descendant_of(self)
genactus = list(actus) + list(events)
return genactus
2017-08-09 00:07:56 +02:00
# Racine du site du COF
class COFRootPage(Page, COFActuIndexMixin):
2017-08-09 00:07:56 +02:00
introduction = RichTextField("Introduction")
content_panels = Page.content_panels + [
FieldPanel('introduction', classname="full"),
]
subpage_types = ['COFActuIndexPage', 'COFPage', 'COFDirectoryPage']
class Meta:
verbose_name = "Racine site du COF"
verbose_name_plural = "Racines site du COF"
2017-08-09 00:07:56 +02:00
# Page lambda du site
2017-08-07 23:31:27 +02:00
class COFPage(Page):
2017-08-09 00:07:56 +02:00
body = StreamField([
('heading', blocks.CharBlock(classname="full title")),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
])
content_panels = Page.content_panels + [
StreamFieldPanel('body'),
]
subpage_types = ['COFDirectoryPage', 'COFPage']
2017-08-09 00:07:56 +02:00
parent_page_types = ['COFPage', 'COFRootPage']
class Meta:
verbose_name = "Page normale COF"
verbose_name_plural = "Pages normales COF"
2017-08-09 00:07:56 +02:00
# Actualités
class COFActuIndexPage(Page, COFActuIndexMixin):
subpage_types = ['COFActuPage', 'COFActuEventPage']
2017-08-09 00:07:56 +02:00
parent_page_types = ['COFRootPage']
class Meta:
verbose_name = "Index des actualités"
verbose_name_plural = "Indexs des actualités"
2017-08-09 00:07:56 +02:00
class COFActuPage(Page):
body = RichTextField("Contenu")
date = models.DateField("Date du post")
image = models.ForeignKey(
'wagtailimages.Image', verbose_name="Image à la Une",
2017-08-09 00:07:56 +02:00
null=True, blank=True,
on_delete=models.SET_NULL, related_name='+'
)
content_panels = Page.content_panels + [
FieldPanel('date'),
ImageChooserPanel('image'),
2017-08-09 00:07:56 +02:00
FieldPanel('body', classname="full"),
]
subpage_types = []
parent_page_types = ['COFActuIndexPage']
class Meta:
verbose_name = "Actualité simple"
verbose_name_plural = "Actualités simples"
# Évènements
class COFActuEventPage(Page):
chapo = models.TextField("Description rapide")
body = RichTextField("Description longue")
image = models.ForeignKey(
'wagtailimages.Image', verbose_name="Image à la Une",
null=True, blank=True,
on_delete=models.SET_NULL, related_name='+'
)
date_start = models.DateTimeField("Date et heure de début")
date_end = models.DateTimeField("Date et heure de fin", blank=True, default=None, null=True)
all_day = models.BooleanField("Toute la journée", default=False, blank=True)
is_event = True
2017-08-09 00:07:56 +02:00
content_panels = Page.content_panels + [
ImageChooserPanel('image'),
FieldPanel('chapo'),
FieldPanel('body', classname="full"),
FieldPanel("date_start"),
FieldPanel("date_end"),
FieldPanel("all_day"),
]
subpage_types = []
2017-08-09 00:07:56 +02:00
parent_page_types = ['COFActuIndexPage']
@property
def dates(self):
if self.date_end:
if self.date_end.date() == self.date_start.date():
if self.all_day:
2017-08-26 18:05:20 +02:00
return self.date_start.strftime(_("le %A %d %B %Y"))
else:
2017-08-26 18:05:20 +02:00
return _("le %s de %s à %s") % \
(self.date_start.strftime("%A %d %B %Y"),
self.date_start.strftime(_("%Hh%M")),
self.date_end.strftime(_("%Hh%M")))
else:
2017-08-26 18:05:20 +02:00
tmpl = "%A %d %B %Y"
diff_i = len(tmpl)
if self.date_end.year != self.date_start.year:
diff_i = len(tmpl)
elif self.date_end.month != self.date_start.month:
diff_i = len(tmpl) - 3
elif self.date_end.day != self.date_start.day:
diff_i = len(tmpl) - 6
common = tmpl[diff_i:]
diff = tmpl[:diff_i]
if self.all_day:
2017-08-26 18:05:20 +02:00
return _("du %s au %s %s") % \
(self.date_start.strftime(diff),
self.date_end.strftime(diff),
self.date_end.strftime(common))
else:
2017-08-26 18:05:20 +02:00
return _("du %s %s à %s au %s à %s") % \
(self.date_start.strftime(diff),
self.date_start.strftime(common),
self.date_start.strftime("%Hh%M"),
self.date_end.strftime(diff),
self.date_end.strftime("%Hh%M"))
else:
if self.all_day:
2017-08-26 18:05:20 +02:00
return self.date_start.strftime(_("le %A %d %B %Y"))
else:
2017-08-26 18:05:20 +02:00
return self.date_start.strftime(_("le %A %d %B %Y à %Hh%M"))
class Meta:
verbose_name = "Actu liée à un évènement"
verbose_name_plural = "Actus liées à des évènements"
2017-08-09 00:07:56 +02:00
# Annuaires (Clubs, partenaires, bonnes adresses)
class COFDirectoryPage(Page):
introduction = RichTextField("Introduction")
content_panels = Page.content_panels + [
FieldPanel('introduction'),
]
subpage_types = ['COFActuPage', 'COFDirectoryEntryPage']
2017-08-09 00:07:56 +02:00
parent_page_types = ['COFRootPage', 'COFPage']
@property
def entries(self):
2017-08-26 18:05:20 +02:00
entries = COFDirectoryEntryPage.objects.live()\
.descendant_of(self)\
.order_by("title")
return entries
class Meta:
verbose_name = "Annuaire (clubs, partenaires, bons plans...)"
verbose_name_plural = "Annuaires"
2017-08-09 00:07:56 +02:00
class COFDirectoryEntryPage(Page):
body = RichTextField("Description")
links = StreamField([
('lien', blocks.StructBlock([
('url', blocks.URLBlock(required=True)),
('texte', blocks.CharBlock()),
])),
('contact', blocks.StructBlock([
('email', blocks.EmailBlock(required=True)),
('texte', blocks.CharBlock()),
])),
])
2017-08-09 00:07:56 +02:00
image = models.ForeignKey(
'wagtailimages.Image', verbose_name="Image",
2017-08-09 00:07:56 +02:00
null=True, blank=True,
on_delete=models.SET_NULL, related_name='+'
)
content_panels = Page.content_panels + [
ImageChooserPanel('image'),
FieldPanel('body', classname="full"),
StreamFieldPanel("links"),
]
subpage_types = []
2017-08-09 00:07:56 +02:00
parent_page_types = ['COFDirectoryPage']
class Meta:
verbose_name = "Éntrée d'annuaire"
verbose_name_plural = "Éntrées d'annuaire"