class SponsorsPage(TranslatablePage):
    """
    SponsorsPage page - allows for listing of site sponsors along with additional supporters and contributors
    Can be nested under the homepage or generic pages
    """

    parent_page_types = [
        "base.HomePage", "base.GenericPageWithSubNav", "base.GenericPage"
    ]
    subpage_types = ["base.GenericPageWithSubNav", "base.GenericPage"]

    body = StreamField([
        ("rich_text_section", blocks.RichTextWithTitleBlock()),
        ("sponsors_section",
         blocks.RichTextWithTitleBlock(
             template="streams/sponsors_block.html")),
        ("supports_section", blocks.SupportersBlock()),
    ],
                       null=True,
                       blank=True)

    content_panels = TranslatablePage.content_panels + [
        StreamFieldPanel("body"),
    ]

    search_fields = TranslatablePage.search_fields + [
        index.SearchField('body'),
    ]
class GenericPage(TranslatablePage):
    """
    Generic page class which allows rich text and quote components to be added.
    Similar page to GenericPageWithSubNav but does not include the sub-navigation component.
    """

    parent_page_types = [
        "base.HomePage", "base.GenericPageWithSubNav", "base.GenericPage"
    ]
    subpage_types = [
        "base.GenericPageWithSubNav", "base.GenericPage",
        "sponsors.SponsorsPage"
    ]

    introduction = RichTextField(blank=True, default="")

    body = StreamField([
        ("rich_text_section", blocks.RichTextWithTitleBlock()),
        ("quote_section", blocks.QuoteBlock()),
    ],
                       null=True,
                       blank=True)

    content_panels = TranslatablePage.content_panels + [
        FieldPanel("introduction"),
        StreamFieldPanel("body"),
    ]
class GenericPageWithSubNav(TranslatablePage):
    """
    Generic page class which allows rich text and quote components to be added.  Can only be added under the homepage but can be nested itself.
    Page includes a sub navigation on the left of the layout for quick links to content on the page. This is auto genereated based on the body components.
    """

    parent_page_types = [
        "base.HomePage", "base.GenericPageWithSubNav", "base.GenericPage"
    ]
    subpage_types = [
        "base.GenericPageWithSubNav", "base.GenericPage",
        "sponsors.SponsorsPage"
    ]

    navigation_title = models.CharField(max_length=120,
                                        null=True,
                                        blank=True,
                                        help_text=_("Title for Navigation"))

    body = StreamField([
        ("rich_text_section", blocks.RichTextWithTitleBlock()),
        ("quote_section", blocks.QuoteBlock()),
    ],
                       null=True,
                       blank=True)

    content_panels = TranslatablePage.content_panels + [
        FieldPanel("navigation_title"),
        StreamFieldPanel("body"),
    ]
Esempio n. 4
0
class CaseStudyPage(TranslatablePage):
    """
    A TranslatablePage class used for case studies pages. 
    """

    parent_page_types = ["case_studies.CaseStudiesListingPage"]
    subpage_types = []

    introduction = models.CharField(max_length=240)

    header_image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        related_name='+',
        on_delete=models.SET_NULL,
        help_text=_("Image dimensions should be 1912px wide × 714px high"))

    header_image_description = models.CharField(max_length=240)

    publication_date = models.DateField()

    collaborator = models.CharField(max_length=120)

    read_time = models.IntegerField(
        help_text=_("Time taken (in minutes) to read the case study"))

    section_tags = ClusterTaggableManager(
        through=CaseStudyGuidelinesSectionTag, blank=True)

    body = StreamField([
        ("rich_text_section", blocks.RichTextWithTitleBlock()),
        ("simple_rich_text_section", blocks.SimpleRichTextBlock()),
        ("quote_section", blocks.QuoteBlock()),
    ],
                       null=True,
                       blank=True)

    content_panels = TranslatablePage.content_panels + [
        ImageChooserPanel("header_image"),
        FieldPanel('header_image_description'),
        FieldPanel('publication_date'),
        FieldPanel('read_time'),
        FieldPanel('collaborator'),
        FieldPanel('section_tags'),
        FieldPanel('introduction'),
        StreamFieldPanel("body"),
    ]

    search_fields = TranslatablePage.search_fields + [
        index.SearchField('title'),
        index.SearchField('body'),
    ]

    def get_context(self, request, *args, **kwards):
        context = super().get_context(request, *args, **kwards)
        siblings = CaseStudyPage.objects.filter(
            language__code=request.LANGUAGE_CODE).order_by(
                '-publication_date').live()
        case_study_list = list(siblings.values_list('pk', flat=True))

        if self.pk in case_study_list:
            current_idx = case_study_list.index(self.pk)

            case_study_length = len(case_study_list) - 1  # 0 based index

            if current_idx + 1 <= case_study_length:
                context['next_page'] = siblings[current_idx + 1]

            if current_idx - 1 >= 0:
                context['prev_page'] = siblings[current_idx - 1]

        return context

    def save(self, *args, **kwards):
        try:
            clear_case_study_cache(self.language.code)
        except Exception:
            logging.error('Error deleting CaseStudyPage cache')
            pass

        return super().save(*args, **kwards)
class GuidancePage(CacheClearMixin, TranslatablePage):
    """
    A TranslatablePage class used for content pages (GuidancePages) within each guidelines section
    (GuidelinesSectionPages)
    """

    parent_page_types = ["guidelines.GuidelinesSectionPage"]
    subpage_types = []

    introduction = RichTextField(blank=True, default="")

    body = StreamField([
        ("content_section", blocks.RichTextWithTitleBlock()),
        ("dos_and_donts", blocks.DosAndDontsBlock()),
    ],
                       null=True,
                       blank=True)

    content_panels = TranslatablePage.content_panels + [
        FieldPanel("introduction"),
        StreamFieldPanel("body"),
    ]

    more_information_module = models.ForeignKey(
        'modules.MoreInformationModule',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+')

    links_module = models.ForeignKey('modules.LinksModule',
                                     null=True,
                                     blank=True,
                                     on_delete=models.SET_NULL,
                                     related_name='+')

    Sidebar_panels = [
        SnippetChooserPanel("more_information_module"),
        SnippetChooserPanel("links_module")
    ]

    edit_handler = TabbedInterface([
        ObjectList(content_panels, heading='Content'),
        ObjectList(Sidebar_panels, heading='Sidebar'),
        ObjectList(TranslatablePage.settings_panels, heading='Settings'),
        ObjectList(TranslatablePage.promote_panels, heading='Promote')
    ])

    search_fields = Page.search_fields + [
        index.SearchField('body'),
    ]

    def get_context(self, request, *args, **kwards):
        context = super().get_context(request, *args, **kwards)
        guidelines = GuidelinesListingPage.objects.ancestor_of(
            self).live().first()
        section = GuidelinesSectionPage.objects.ancestor_of(
            self).live().first()

        prev_page = self.get_prev_siblings().live().first()
        next_page = self.get_next_siblings().live().first()

        if prev_page == None:
            prev_page = section.specific
            prev_page.title = prev_page.subtitle

        if next_page == None:
            next_page = section.get_next_siblings().live().first()

        context['prev_page'] = prev_page
        context['next_page'] = next_page

        context['guidelines'] = guidelines
        context['section'] = section

        return context

    def clear_from_caches(self):
        target = "sections_and_pages_for_listing"
        try:
            listing_id = self.get_parent().get_parent().id
            target = make_template_fragment_key(target, [listing_id])
            cache.delete(target)
        except Exception:
            logging.warning('Error deleting %s cache', target)

        target = "pages_for_section"
        try:
            section_id = self.get_parent().id
            target = make_template_fragment_key(target, [section_id])
            cache.delete(target)
        except Exception:
            logging.warning('Error deleting %s cache', target)
class GuidancePage(TranslatablePage):
    """
    A TranslatablePage class used for the many content pages for each guidelines subsection
    """

    parent_page_types = ["guidelines.GuidelinesSectionPage"]
    subpage_types = []

    introduction = RichTextField(blank=True, default="")

    body = StreamField([
        ("content_section", blocks.RichTextWithTitleBlock()),
        ("dos_and_donts", blocks.DosAndDontsBlock()),
    ],
                       null=True,
                       blank=True)

    content_panels = TranslatablePage.content_panels + [
        FieldPanel("introduction"),
        StreamFieldPanel("body"),
    ]

    more_information_module = models.ForeignKey(
        'modules.MoreInformationModule',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+')

    links_module = models.ForeignKey('modules.LinksModule',
                                     null=True,
                                     blank=True,
                                     on_delete=models.SET_NULL,
                                     related_name='+')

    Sidebar_panels = [
        SnippetChooserPanel("more_information_module"),
        SnippetChooserPanel("links_module")
    ]

    edit_handler = TabbedInterface([
        ObjectList(content_panels, heading='Content'),
        ObjectList(Sidebar_panels, heading='Sidebar'),
        ObjectList(TranslatablePage.settings_panels, heading='Settings'),
        ObjectList(TranslatablePage.promote_panels, heading='Promote')
    ])

    search_fields = Page.search_fields + [
        index.SearchField('body'),
    ]

    def get_context(self, request, *args, **kwards):
        context = super().get_context(request, *args, **kwards)
        guidelines = GuidelinesListingPage.objects.ancestor_of(
            self).live().first()
        context['guidelines_title'] = guidelines.title
        context['section'] = GuidelinesSectionPage.objects.ancestor_of(
            self).live().first()

        prev_page = self.get_prev_siblings().live().first()
        next_page = self.get_next_siblings().live().first()

        if prev_page == None:
            prev_page = self.get_parent().specific
            prev_page.title = prev_page.subtitle

        if next_page == None:
            next_page = self.get_parent().get_next_siblings().live().first()

        context['prev_page'] = prev_page
        context['next_page'] = next_page

        return context

    def save(self, *args, **kwards):
        try:
            section = self.get_parent()
            clear_guidelines_listing_cache(self.language.code)
            clear_guidelines_section_cache(section.id)
        except Exception:
            logging.error('Error deleting GuidancePage cache')
            pass

        return super().save(*args, **kwards)
 def test_rich_text_block_template(self):
     self.assertEquals(ictgs_blocks.RichTextWithTitleBlock().get_template(),
                       'streams/richtext_block.html')
 def test_rich_text_block_count(self):
     child_blocks = ictgs_blocks.RichTextWithTitleBlock().child_blocks
     self.assertEquals(len(child_blocks), 2)
 def test_rich_text_block(self):
     child_blocks = ictgs_blocks.RichTextWithTitleBlock().child_blocks
     assert type(child_blocks['title']) is blocks.CharBlock
     assert type(child_blocks['content']) is blocks.RichTextBlock