from django.db import models from django.utils import timezone from wagtail.admin.panels import FieldPanel, MultiFieldPanel, TabbedInterface, ObjectList from wagtail.contrib.routable_page.models import RoutablePageMixin, path from wagtail.documents import get_document_model from wagtail.fields import RichTextField, StreamField from wagtail.models import Page from .blocks import PersonBlock class HomePage(RoutablePageMixin, Page): heading_text = RichTextField(verbose_name="Hlavní text stránky") # --- Donations --- donation_text = RichTextField(verbose_name="Text pro dary") # --- Contact info --- address = models.CharField(verbose_name="Sídlo", max_length=128) branch = models.CharField(verbose_name="Pobočka", max_length=128) email = models.EmailField(verbose_name="Email", max_length=128) ds_id = models.CharField(verbose_name="Datová schránka", max_length=128) # --- People --- director = StreamField( [("person", PersonBlock())], verbose_name="Ředitel", use_json_field=True, blank=True, null=True, ) director_description = models.TextField( verbose_name="Ředitel - popis" ) academic_council_description = models.TextField( verbose_name="Akademická rada - popis" ) controller = StreamField( [("person", PersonBlock())], verbose_name="Kontrolor", use_json_field=True, blank=True, null=True, ) controller_description = models.TextField( verbose_name="Kontrolor - popis" ) council_members = StreamField( [("person", PersonBlock())], verbose_name="Správní rada", use_json_field=True, blank=True, null=True, ) council_members_description = models.TextField( verbose_name="Správní rada - popis" ) volunteers = StreamField( [("person", PersonBlock())], verbose_name="Dobrovolníci", use_json_field=True, blank=True, null=True, ) volunteers_description = models.TextField( verbose_name="Dobrovolníci - popis" ) employees = StreamField( [("person", PersonBlock())], verbose_name="Zaměstnanci", use_json_field=True, blank=True, null=True, ) employees_description = models.TextField( verbose_name="Zaměstnanci - popis" ) subpage_types = [ "home.HomeArticlesPage", "home.HomeEventsPage", "home.HomeDocumentsPage", ] intro_panels = Page.content_panels + [ FieldPanel("heading_text", icon="pilcrow"), FieldPanel("donation_text", icon="pilcrow"), MultiFieldPanel( [ FieldPanel("address", icon="home"), FieldPanel("branch", icon="home"), FieldPanel("email", icon="pilcrow"), FieldPanel("ds_id", icon="mail"), ], heading="Kontaktní údaje", ) ] people_panels = [ FieldPanel("director_description", icon="pilcrow"), FieldPanel("director", icon="user"), FieldPanel("academic_council_description"), FieldPanel("controller_description", icon="pilcrow"), FieldPanel("controller", icon="user"), FieldPanel("council_members_description", icon="pilcrow"), FieldPanel("council_members", icon="group"), FieldPanel("volunteers_description", icon="pilcrow"), FieldPanel("volunteers", icon="group"), FieldPanel("employees_description", icon="pilcrow"), FieldPanel("employees", icon="group"), ] edit_handler = TabbedInterface([ ObjectList(intro_panels, heading="Základy"), ObjectList(people_panels, heading="Lidé"), ObjectList(Page.promote_panels, heading="Propagace"), ]) # Articles @property def articles_page(self) -> "HomeArticlesPage": return HomeArticlesPage.objects.live().first() @property def latest_articles(self) -> "QuerySet": return HomeArticlePage.objects.order_by("-date").live().all()[:3] # Events @property def events_page(self) -> "HomeEventsPage": return HomeEventsPage.objects.live().first() @property def latest_events(self) -> "QuerySet": return HomeEventPage.objects.order_by("-date").live().all()[:8] # Documents @property def documents_page(self) -> "HomeDocumentsPage": return HomeDocumentsPage.objects.live().first() @property def latest_documents(self) -> "QuerySet": return HomeDocumentPage.objects.order_by("-date").live().all()[:4] # Feed @path("feeds/atom/") def view_feed(self, request): # Avoid circular import from .feeds import LatestArticlesFeed # noqa return LatestArticlesFeed()(request, self.articles_page.id) class Meta: verbose_name = "Domovská stránka" class HomeArticlesPage(RoutablePageMixin, Page): content = RichTextField(verbose_name="Obsah", blank=True, null=True) parent_page_type = ["home.HomePage"] subpage_types = ["home.HomeArticlePage"] content_panels = Page.content_panels + [ FieldPanel("content", icon="pilcrow"), ] @property def articles(self): return HomeArticlePage.objects.live().all() class Meta: verbose_name = "Rozcestník článků" class HomeEventsPage(RoutablePageMixin, Page): content = RichTextField(verbose_name="Obsah", blank=True, null=True) parent_page_type = ["home.HomePage"] subpage_types = ["home.HomeEventPage"] content_panels = Page.content_panels + [ FieldPanel("content", icon="pilcrow"), ] @property def events(self): return HomeEventPage.objects.live().all() class Meta: verbose_name = "Rozcestník akcí" class HomeDocumentsPage(RoutablePageMixin, Page): content = RichTextField(verbose_name="Obsah", blank=True, null=True) parent_page_type = ["home.HomePage"] subpage_types = ["home.HomeDocumentPage"] content_panels = Page.content_panels + [ FieldPanel("content", icon="pilcrow"), ] @property def documents(self): return HomeDocumentPage.objects.live().all() class Meta: verbose_name = "Rozcestník dokumentů" class HomeContentPageMixin(RoutablePageMixin, Page): tags = models.ManyToManyField("Tag", verbose_name="Štítky") author = models.CharField( verbose_name="Autor", max_length=128, blank=True, null=True, ) date = models.DateField(verbose_name="Datum vytvoření", default=timezone.now) content = RichTextField(verbose_name="Obsah") parent_page_type = ["home.HomeArticlesPage"] content_panels = Page.content_panels + [ FieldPanel("author", icon="user"), FieldPanel("date", icon="calendar"), FieldPanel("content", icon="pilcrow"), ] @property def shortened_perex(self) -> str: if len(self.perex) > 310: return self.perex[:300] + "..." return self.perex class Meta: abstract = True class HomeArticlePage(HomeContentPageMixin): perex = models.TextField(verbose_name="Perex") content_panels = Page.content_panels + [ FieldPanel("author", icon="user"), FieldPanel("date", icon="calendar"), FieldPanel("perex", icon="pilcrow"), FieldPanel("content", icon="pilcrow"), ] class Meta: verbose_name = "Článek" class HomeEventPage(HomeContentPageMixin): date = models.DateField(verbose_name="Datum konání", blank=True, null=True) location = models.CharField( verbose_name="Místo konání", blank=True, null=True, max_length=64 ) content = RichTextField(verbose_name="Obsah", blank=True, null=True) content_panels = Page.content_panels + [ FieldPanel("date", icon="calendar"), FieldPanel("location", icon="globe"), FieldPanel("author", icon="user"), FieldPanel("content", icon="pilcrow"), ] class Meta: verbose_name = "Akce" class HomeDocumentPage(HomeContentPageMixin): document = models.ForeignKey( get_document_model(), on_delete=models.PROTECT, verbose_name="Dokument" ) content = RichTextField(verbose_name="Obsah", blank=True, null=True) subpage_types = ["home.HomeDocumentPage"] content_panels = Page.content_panels + [ FieldPanel("document", icon="doc-full"), FieldPanel("author", icon="user"), FieldPanel("date", icon="calendar"), FieldPanel("content", icon="pilcrow"), ] class Meta: verbose_name = "Dokument" class Tag(models.Model): name = models.CharField(verbose_name="Jméno", max_length=32) class Meta: verbose_name = "Štítek" verbose_name_plural = "Štítky"