Skip to content
Snippets Groups Projects
Forked from TO / Maják
1763 commits behind the upstream repository.
models.py 6.01 KiB
from django.core.paginator import Paginator
from django.db import models
from django.utils.translation import gettext_lazy
from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel, StreamFieldPanel
from wagtail.core import blocks
from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from wagtailmetadata.models import MetadataPageMixin

from shared.models import Article, PeoplePage, SharedSubpageMixin


class SubpageMixin:
    """Must be used in class definition before MetadataPageMixin!
    Obsolete, misto nej pouzivat SharedSubpageMixin
    TODO je pouze v migracich, po odstraneni z nich lze smazat
    """

    pass


class DistrictHomePage(MetadataPageMixin, Page):
    ### FIELDS

    content = StreamField(
        [
            ("title", blocks.CharBlock(label="nadpis", icon="title")),
        ],
        verbose_name="obsah stránky",
        blank=True,
    )

    facebook = models.URLField("Facebook URL", blank=True, null=True)
    forum = models.URLField("Fórum URL", blank=True, null=True)

    contact_email = models.EmailField("kontaktni email", max_length=250, blank=True)
    contact_phone = models.TextField("kontaktni telefon", max_length=250, blank=True)
    contact_newcomers = models.URLField(
        "URL pro zájemce o členství", blank=True, null=True
    )

    donation_page = models.URLField("URL pro příjem darů", blank=True, null=True)

    # Lide uvedeni v paticce
    footperson_coord = models.ForeignKey(
        "shared.PersonPage",
        verbose_name="Koordinátor",
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name="footperson_coord",
    )
    footperson_electman = models.ForeignKey(
        "shared.PersonPage",
        verbose_name="Volební manažer",
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name="footperson_electman",
    )
    footperson_media = models.ForeignKey(
        "shared.PersonPage",
        verbose_name="Kontakt pro média",
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name="footperson_media",
    )

    # settings
    matomo_id = models.IntegerField(
        "Matomo ID pro sledování návštěvnosti", blank=True, null=True
    )

    ### PANELS

    content_panels = Page.content_panels + [
        StreamFieldPanel("content"),
    ]

    promote_panels = [
        MultiFieldPanel(
            [
                FieldPanel("facebook"),
                FieldPanel("forum"),
            ],
            gettext_lazy("Common page configuration"),
        ),
    ]

    settings_panels = [
        FieldPanel("matomo_id"),
        FieldPanel("donation_page"),
        MultiFieldPanel(
            [
                FieldPanel("contact_email"),
                FieldPanel("contact_phone"),
                FieldPanel("contact_newcomers"),
            ],
            gettext_lazy("Kontakty"),
        ),
        MultiFieldPanel(
            [
                FieldPanel("footperson_coord"),
                FieldPanel("footperson_electman"),
                FieldPanel("footperson_media"),
            ],
            gettext_lazy("Lidé v zápatí stránky"),
        ),
    ]

    ### RELATIONS
    subpage_types = [
        "shared.PeoplePage",
        "DistrictArticles",
        "DistrictContact",
        "DistrictTags",
    ]

    ### OTHERS

    class Meta:
        verbose_name = "Web místního sdružení"

    def _first_subpage_of_type(self, page_type):
        return self.get_descendants().type(page_type).live().specific()[0]

    @property
    def articles(self):
        return self.get_descendants().type(Article).live().specific()

    @property
    def articles_page(self):
        return self._first_subpage_of_type(DistrictArticles)

    @property
    def people_page(self):
        return self._first_subpage_of_type(PeoplePage)

    @property
    def contact_page(self):
        return self._first_subpage_of_type(DistrictContact)

    @property
    def tags_page(self):
        return self._first_subpage_of_type(DistrictTags)

    @property
    def root_page(self):
        return self


class DistrictArticles(SharedSubpageMixin, MetadataPageMixin, Page):
    ### FIELDS

    max_items = models.IntegerField(
        "Maximalni pocet prvku na strance", blank=True, null=True
    )

    ### PANELS

    content_panels = Page.content_panels + [
        FieldPanel("max_items"),
    ]

    settings_panels = []

    subpage_types = [
        "shared.Article",
    ]

    ### OTHERS

    class Meta:
        verbose_name = "Aktuality"

    def get_context(self, request):
        context = super().get_context(request)
        context["articles"] = Paginator(
            self.get_children().live().specific(),
            self.max_items or 5,
        ).get_page(request.GET.get("page"))
        return context


class DistrictContact(SharedSubpageMixin, MetadataPageMixin, Page):
    class ContactItemBlock(blocks.StructBlock):
        name = blocks.CharBlock(label="Role")
        person = blocks.PageChooserBlock(
            label="Osoba",
            page_type=["shared.PersonPage"],
        )

        class Meta:
            label = "Kontakt"

    contact_persons = StreamField(
        [("item", ContactItemBlock())],
        verbose_name="Kontakty",
        blank=True,
    )

    content_panels = Page.content_panels + [
        StreamFieldPanel("contact_persons"),
    ]

    class Meta:
        verbose_name = "Kontakty"


class DistrictTags(SharedSubpageMixin, MetadataPageMixin, Page):

    settings_panels = []

    class Meta:
        verbose_name = "Stránka s tagy"

    def get_context(self, request):
        context = super().get_context(request)

        # Natrid clanky do hashtable dle tagu (v template by se to delalo podstatne hur)
        tags = {}
        for x in self.root_page.articles_page.get_children().live().specific():
            for y in x.tags.all():
                try:
                    tags[y.name].append(x)
                except KeyError:
                    tags[y.name] = [x]

        context["tags"] = tags
        return context