from django.db import models from django.utils.translation import ugettext_lazy as _ from modelcluster.models import ClusterableModel, ParentalKey from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel from wagtail.wagtailcore.fields import RichTextField from wagtail.wagtailcore.models import Orderable, Page from wagtail.wagtailimages.edit_handlers import ImageChooserPanel from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel from wagtail.wagtailsnippets.models import register_snippet from kfet.cms.context_processors import get_articles class KFetPage(Page): no_header = models.BooleanField( verbose_name=_('Sans en-tête'), default=False, help_text=_( "Coché, l'en-tête (avec le titre) de la page n'est pas affiché." ), ) content = RichTextField(verbose_name=_('Contenu')) custom_template = models.CharField( verbose_name=_('Template personnalisé'), max_length=255, blank=True, ) content_panels = Page.content_panels + [ FieldPanel('no_header'), FieldPanel('content', classname='full'), InlinePanel('team_groups', label=_("Groupes de K-Fêt-eux-ses")), ] settings_panels = Page.settings_panels + [ FieldPanel('custom_template'), ] class Meta: verbose_name = _('page K-Fêt') verbose_name_plural = _('pages K-Fêt') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.template = "kfetcms/base.html" def get_context(self, request, *args, **kwargs): context = super().get_context(request, *args, **kwargs) page = context['page'] if not page.seo_title: page.seo_title = page.title if self.slug == "carte": context.update(get_articles(request)) return context def get_template(self, request, *args, **kwargs): return self.custom_template or ( super().get_template(request, *args, **kwargs)) class KFetPageGroupTeam(Orderable, models.Model): page = ParentalKey(KFetPage, related_name='team_groups') group = models.ForeignKey( 'kfetcms.GroupTeam', verbose_name=_('Groupe de K-Fêt-eux-ses'), related_name='+', ) title = models.CharField( verbose_name=_('Titre du groupe'), max_length=255, blank=True, ) content = RichTextField( verbose_name=_('Texte de présentation du groupe'), ) show_only = models.IntegerField( verbose_name=_('Montrer seulement'), blank=True, null=True, default=None, help_text=_( 'Nombre de membres du groupe affichés initialement. Laisser vide ' 'pour tou-te-s les afficher.' ), ) panels = [ FieldPanel('title', classname='full'), FieldPanel('show_only', classname='full'), FieldPanel('content', classname='full'), SnippetChooserPanel('group'), ] @register_snippet class GroupTeam(ClusterableModel): name = models.CharField( verbose_name=_('Nom'), max_length=255, ) class Meta: verbose_name = _('Groupe de K-Fêt-eux-ses') verbose_name_plural = _('Groupes de K-Fêt-eux-ses') def __str__(self): return self.name panels = [ FieldPanel('name', classname='full'), InlinePanel('members', label=_('Membres du groupe')), ] @register_snippet class MemberTeam(Orderable, models.Model): group = ParentalKey( GroupTeam, verbose_name=_("Groupe de K-Fêt-eux-ses"), on_delete=models.CASCADE, related_name='members', ) first_name = models.CharField( verbose_name=_('Prénom'), max_length=255, blank=True, default='', ) last_name = models.CharField( verbose_name=_('Nom'), max_length=255, blank=True, default='', ) nick_name = models.CharField( verbose_name=_('Alias'), max_length=255, blank=True, default='', ) photo = models.ForeignKey( 'wagtailimages.Image', verbose_name=_('Photo'), on_delete=models.SET_NULL, null=True, blank=True, related_name='+', ) class Meta: verbose_name = _('K-Fêt-eux-se') def __str__(self): return self.get_full_name() panels = [ FieldPanel('first_name'), FieldPanel('last_name'), FieldPanel('nick_name'), FieldPanel('group'), ImageChooserPanel('photo'), ] def get_full_name(self): full_name = '{} {}'.format(self.first_name, self.last_name) return full_name.strip()