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_do_dont_block_input_types_count(self):
     child_blocks = ictgs_blocks.DosAndDontsBlock().child_blocks
     self.assertEquals(len(child_blocks), 3)
 def test_do_dont_block_template(self):
     self.assertEquals(ictgs_blocks.DosAndDontsBlock().get_template(),
                       'streams/do_dont_list.html')
 def test_do_dont_block_input_types(self):
     child_blocks = ictgs_blocks.DosAndDontsBlock().child_blocks
     assert type(child_blocks['title']) is blocks.CharBlock
     assert type(child_blocks['dos']) is blocks.ListBlock
     assert type(child_blocks['donts']) is blocks.ListBlock