Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customized Titles in featured items on Homepage #1460

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
6 changes: 6 additions & 0 deletions home/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Meta:


class EmbeddedQuestionnaireBlock(blocks.StructBlock):
title = blocks.CharBlock(required=False)
direct_display = blocks.BooleanBlock(required=False)


Expand All @@ -63,6 +64,7 @@ def get_context(self, value, parent_context=None):
context.update({
'direct_display': value['direct_display'],
'questionnaire': poll.specific,
'title': value['title'] or poll.specific.title,
})
return context

Expand All @@ -80,6 +82,7 @@ def get_context(self, value, parent_context=None):
context.update({
'direct_display': value['direct_display'],
'questionnaire': survey.specific,
'title': value['title'] or survey.specific.title
})
return context

Expand All @@ -97,6 +100,7 @@ def get_context(self, value, parent_context=None):
context.update({
'direct_display': value['direct_display'],
'questionnaire': quiz.specific,
'title': value['title'] or quiz.specific.title,
})
return context

Expand All @@ -123,6 +127,7 @@ class Meta:


class ArticleBlock(blocks.StructBlock):
title = blocks.CharBlock(required=False)
display_section_title = blocks.BooleanBlock(required=False)
article = PageChooserBlock(target_model='home.Article')

Expand All @@ -133,6 +138,7 @@ def get_context(self, value, parent_context=None):
context.update({
'display_section_title': value['display_section_title'],
'article': article.specific,
'title': value['title'] or article.specific.title
})
return context

Expand Down
34 changes: 34 additions & 0 deletions home/migrations/0050_auto_20221109_0738.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 3.1.14 on 2022-11-09 07:38

from django.db import migrations
import home.blocks
import messaging.blocks
import wagtail.core.blocks
import wagtail.core.fields
import wagtail.images.blocks
import wagtailmarkdown.blocks


class Migration(migrations.Migration):

dependencies = [
('home', '0049_auto_20221018_0943'),
]

operations = [
migrations.AlterField(
model_name='article',
name='body',
field=wagtail.core.fields.StreamField([('heading', wagtail.core.blocks.CharBlock(form_classname='full title', template='blocks/heading.html')), ('paragraph', wagtail.core.blocks.RichTextBlock(features=['h2', 'h3', 'h4', 'bold', 'italic', 'ol', 'ul', 'hr', 'link', 'document-link', 'image'])), ('markdown', wagtailmarkdown.blocks.MarkdownBlock(icon='code')), ('paragraph_v1_legacy', home.blocks.RawHTMLBlock(icon='code')), ('image', wagtail.images.blocks.ImageChooserBlock(template='blocks/image.html')), ('list', wagtail.core.blocks.ListBlock(wagtailmarkdown.blocks.MarkdownBlock(icon='code'))), ('numbered_list', home.blocks.NumberedListBlock(wagtailmarkdown.blocks.MarkdownBlock(icon='code'))), ('page_button', wagtail.core.blocks.StructBlock([('page', wagtail.core.blocks.PageChooserBlock()), ('text', wagtail.core.blocks.CharBlock(max_length=255, required=False))])), ('embedded_poll', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('direct_display', wagtail.core.blocks.BooleanBlock(required=False)), ('poll', home.blocks.EmbeddedQuestionnaireChooserBlock(page_type=['questionnaires.Poll']))])), ('embedded_survey', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('direct_display', wagtail.core.blocks.BooleanBlock(required=False)), ('survey', home.blocks.EmbeddedQuestionnaireChooserBlock(page_type=['questionnaires.Survey']))])), ('embedded_quiz', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('direct_display', wagtail.core.blocks.BooleanBlock(required=False)), ('quiz', home.blocks.EmbeddedQuestionnaireChooserBlock(page_type=['questionnaires.Quiz']))])), ('media', home.blocks.MediaBlock(icon='media')), ('chat_bot', wagtail.core.blocks.StructBlock([('subject', wagtail.core.blocks.CharBlock()), ('button_text', wagtail.core.blocks.CharBlock()), ('trigger_string', wagtail.core.blocks.CharBlock()), ('channel', messaging.blocks.ChatBotChannelChooserBlock())])), ('download', wagtail.core.blocks.StructBlock([('available_text', wagtail.core.blocks.CharBlock(help_text='This text appears when it is possible for the user to install the app on their phone.')), ('unavailable_text', wagtail.core.blocks.CharBlock(form_classname='red-help-text', help_text='This text appears when the user is using a feature phone and thus cannot install the app (the button will be disabled in this case). [Currently not implemented]', required=False)), ('offline_text', wagtail.core.blocks.CharBlock(help_text="This text appears when the user is navigating the site via the offline app and thus it doesn't make sense to install the offline app again (the button will be disabled in this case).", required=False)), ('page', wagtail.core.blocks.PageChooserBlock(page_type=['wagtailcore.Page'])), ('description', wagtail.core.blocks.RichTextBlock(features=['h2', 'h3', 'h4', 'bold', 'italic', 'ol', 'ul', 'hr', 'link', 'document-link', 'image']))]))]),
),
migrations.AlterField(
model_name='homepage',
name='home_featured_content',
field=wagtail.core.fields.StreamField([('page_button', wagtail.core.blocks.StructBlock([('page', wagtail.core.blocks.PageChooserBlock()), ('text', wagtail.core.blocks.CharBlock(max_length=255, required=False))])), ('embedded_poll', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('direct_display', wagtail.core.blocks.BooleanBlock(required=False)), ('poll', home.blocks.EmbeddedQuestionnaireChooserBlock(page_type=['questionnaires.Poll']))])), ('embedded_survey', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('direct_display', wagtail.core.blocks.BooleanBlock(required=False)), ('survey', home.blocks.EmbeddedQuestionnaireChooserBlock(page_type=['questionnaires.Survey']))])), ('embedded_quiz', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('direct_display', wagtail.core.blocks.BooleanBlock(required=False)), ('quiz', home.blocks.EmbeddedQuestionnaireChooserBlock(page_type=['questionnaires.Quiz']))])), ('article', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('display_section_title', wagtail.core.blocks.BooleanBlock(required=False)), ('article', wagtail.core.blocks.PageChooserBlock(page_type=['home.Article']))])), ('download', wagtail.core.blocks.StructBlock([('available_text', wagtail.core.blocks.CharBlock(help_text='This text appears when it is possible for the user to install the app on their phone.')), ('unavailable_text', wagtail.core.blocks.CharBlock(form_classname='red-help-text', help_text='This text appears when the user is using a feature phone and thus cannot install the app (the button will be disabled in this case). [Currently not implemented]', required=False)), ('offline_text', wagtail.core.blocks.CharBlock(help_text="This text appears when the user is navigating the site via the offline app and thus it doesn't make sense to install the offline app again (the button will be disabled in this case).", required=False)), ('page', wagtail.core.blocks.PageChooserBlock(page_type=['wagtailcore.Page'])), ('description', wagtail.core.blocks.RichTextBlock(features=['h2', 'h3', 'h4', 'bold', 'italic', 'ol', 'ul', 'hr', 'link', 'document-link', 'image']))]))], blank=True, null=True),
),
migrations.AlterField(
model_name='offlinecontentindexpage',
name='body',
field=wagtail.core.fields.StreamField([('heading', wagtail.core.blocks.CharBlock(form_classname='full title', template='blocks/heading.html')), ('paragraph', wagtail.core.blocks.RichTextBlock(features=['h2', 'h3', 'h4', 'bold', 'italic', 'ol', 'ul', 'hr', 'link', 'document-link', 'image'])), ('markdown', wagtailmarkdown.blocks.MarkdownBlock(icon='code')), ('paragraph_v1_legacy', home.blocks.RawHTMLBlock(icon='code')), ('image', wagtail.images.blocks.ImageChooserBlock(template='blocks/image.html')), ('list', wagtail.core.blocks.ListBlock(wagtailmarkdown.blocks.MarkdownBlock(icon='code'))), ('numbered_list', home.blocks.NumberedListBlock(wagtailmarkdown.blocks.MarkdownBlock(icon='code'))), ('page_button', wagtail.core.blocks.StructBlock([('page', wagtail.core.blocks.PageChooserBlock()), ('text', wagtail.core.blocks.CharBlock(max_length=255, required=False))])), ('embedded_poll', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('direct_display', wagtail.core.blocks.BooleanBlock(required=False)), ('poll', home.blocks.EmbeddedQuestionnaireChooserBlock(page_type=['questionnaires.Poll']))])), ('embedded_survey', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('direct_display', wagtail.core.blocks.BooleanBlock(required=False)), ('survey', home.blocks.EmbeddedQuestionnaireChooserBlock(page_type=['questionnaires.Survey']))])), ('embedded_quiz', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('direct_display', wagtail.core.blocks.BooleanBlock(required=False)), ('quiz', home.blocks.EmbeddedQuestionnaireChooserBlock(page_type=['questionnaires.Quiz']))])), ('media', home.blocks.MediaBlock(icon='media')), ('chat_bot', wagtail.core.blocks.StructBlock([('subject', wagtail.core.blocks.CharBlock()), ('button_text', wagtail.core.blocks.CharBlock()), ('trigger_string', wagtail.core.blocks.CharBlock()), ('channel', messaging.blocks.ChatBotChannelChooserBlock())])), ('download', wagtail.core.blocks.StructBlock([('available_text', wagtail.core.blocks.CharBlock(help_text='This text appears when it is possible for the user to install the app on their phone.')), ('unavailable_text', wagtail.core.blocks.CharBlock(form_classname='red-help-text', help_text='This text appears when the user is using a feature phone and thus cannot install the app (the button will be disabled in this case). [Currently not implemented]', required=False)), ('offline_text', wagtail.core.blocks.CharBlock(help_text="This text appears when the user is navigating the site via the offline app and thus it doesn't make sense to install the offline app again (the button will be disabled in this case).", required=False)), ('page', wagtail.core.blocks.PageChooserBlock(page_type=['wagtailcore.Page'])), ('description', wagtail.core.blocks.RichTextBlock(features=['h2', 'h3', 'h4', 'bold', 'italic', 'ol', 'ul', 'hr', 'link', 'document-link', 'image']))]))]),
),
]
2 changes: 1 addition & 1 deletion home/templates/blocks/article.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% load generic_components %}

{% if article %}
{% render_article_card page=article display_section_title=display_section_title font_color=article.top_level_section.font_color %}
{% render_article_card page=article title=title display_section_title=display_section_title font_color=article.top_level_section.font_color %}
{% endif %}
10 changes: 6 additions & 4 deletions home/templatetags/generic_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def primary_button(title, extra_classnames='', href=None, icon_path=None, icon=N


@register.inclusion_tag('generic_components/article_card.html', takes_context=True)
def render_article_card(context, page, is_first_content=False, display_section_title=False, background_color=None, font_color=None):
def render_article_card(context, page, title=None, is_first_content=False, display_section_title=False, background_color=None, font_color=None):
theme_settings = globals_.theme_settings

font_color = font_color or theme_settings.article_card_font_color
Expand All @@ -42,7 +42,8 @@ def render_article_card(context, page, is_first_content=False, display_section_t
'is_first_content': is_first_content,
'display_section_title': display_section_title,
'background_color': background_color,
'font_color': font_color
'font_color': font_color,
'title': title or page.title
})
return context

Expand All @@ -64,7 +65,7 @@ def render_section_card(context, page, is_first_content=False, background_color=


@register.inclusion_tag('generic_components/questionnaire_card.html', takes_context=True)
def render_questionnaire_card(context, page, background_color=None, font_color=None):
def render_questionnaire_card(context, page, title=None, background_color=None, font_color=None):
theme_settings = globals_.theme_settings

font_color = font_color or theme_settings.section_listing_questionnaire_font_color
Expand All @@ -73,7 +74,8 @@ def render_questionnaire_card(context, page, background_color=None, font_color=N
context.update({
'questionnaire': page,
'background_color': background_color,
'font_color': font_color
'font_color': font_color,
'title': title or page.title
})
return context

Expand Down
100 changes: 99 additions & 1 deletion home/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
from wagtail_localize.operations import TranslationCreator

from home.wagtail_hooks import limit_page_chooser
from home.factories import SectionFactory, ArticleFactory, HomePageFactory, MediaFactory, LocaleFactory
from home.factories import SectionFactory, SectionIndexFactory, ArticleFactory, HomePageFactory, MediaFactory, LocaleFactory
from wagtail_factories import SiteFactory, PageFactory
from bs4 import BeautifulSoup

from questionnaires.factories import PollFactory, SurveyFactory, QuizFactory


class LimitPageChooserHookTests(TestCase):
def setUp(self):
Expand Down Expand Up @@ -98,3 +100,99 @@ def test_media_block_translation_of_bengali_language(self):
self.assertEqual(response.status_code, 200)
self.assertContains(response, f"উপরের ভিডিও দেখা না গেলে <a href=\"{self.en_article.body[0].value.url}\" download> এর পরিবর্তে এটা </a> ডাউনলোড করুন", count=1)
self.assertContains(response, f"উপরের অডিও শুনতে না পেলে <a href=\"{self.en_article.body[1].value.url}\" download> এর পরিবর্তে এটা </a> ডাউনলোড করুন", count=1)


class HomePageFeaturedItemTest(TestCase):
def setUp(self):
self.site = Site.objects.get(is_default_site=True)
self.home_page = self.site.root_page.specific
self.section_index_page = SectionIndexFactory(parent=self.home_page)
self.section = SectionFactory(parent=self.section_index_page)
self.article = ArticleFactory(parent=self.section)
self.poll = PollFactory(parent=self.section)
self.survey = SurveyFactory(parent=self.section)
self.quiz = QuizFactory(parent=self.section)

def test_home_page_featured_item_with_empty_title(self):
self.home_page.home_featured_content.append((
'article', {
'article': self.article,
'display_section_title': True,
}
))
self.home_page.home_featured_content.append((
'embedded_poll', {
'poll': self.poll,
'direct_display': False,
}
))
self.home_page.home_featured_content.append((
'embedded_survey', {
'survey': self.survey,
'direct_display': False,
}
))
self.home_page.home_featured_content.append((
'embedded_quiz', {
'quiz': self.quiz,
'direct_display': False,
}
))
self.home_page.save()

response = self.client.get(self.home_page.url)
parsed_response = BeautifulSoup(response.content)
article_title = parsed_response.find("p", {"class": "article-title"}).text
poll_title = parsed_response.find("div", {"class": "block-embedded_poll"}).find("p").findNext("p").text
survey_title = parsed_response.find("div", {"class": "block-embedded_survey"}).find("p").findNext("p").text
quiz_title = parsed_response.find("div", {"class": "block-embedded_quiz"}).find("p").findNext("p").text

self.assertEqual(response.status_code, 200)
self.assertEqual(article_title, self.home_page.home_featured_content[0].value['article'].title)
self.assertEqual(poll_title, self.home_page.home_featured_content[1].value['poll'].title)
self.assertEqual(survey_title, self.home_page.home_featured_content[2].value['survey'].title)
self.assertEqual(quiz_title, self.home_page.home_featured_content[3].value['quiz'].title)

def test_home_page_featured_item_with_new_title(self):
self.home_page.home_featured_content.append((
'article', {
'title': 'new article title',
'article': self.article,
'display_section_title': True,
}
))
self.home_page.home_featured_content.append((
'embedded_poll', {
'title': 'new poll title',
'poll': self.poll,
'direct_display': False,
}
))
self.home_page.home_featured_content.append((
'embedded_survey', {
'title': 'new survey title',
'survey': self.survey,
'direct_display': False,
}
))
self.home_page.home_featured_content.append((
'embedded_quiz', {
'title': 'new quiz title',
'quiz': self.quiz,
'direct_display': False,
}
))
self.home_page.save()

response = self.client.get(self.home_page.url)
parsed_response = BeautifulSoup(response.content)
article_title = parsed_response.find("p", {"class": "article-title"}).text
poll_title = parsed_response.find("div", {"class": "block-embedded_poll"}).find("p").findNext("p").text
survey_title = parsed_response.find("div", {"class": "block-embedded_survey"}).find("p").findNext("p").text
quiz_title = parsed_response.find("div", {"class": "block-embedded_quiz"}).find("p").findNext("p").text

self.assertEqual(response.status_code, 200)
self.assertEqual(article_title, "new article title")
self.assertEqual(poll_title, "new poll title")
self.assertEqual(survey_title, "new survey title")
self.assertEqual(quiz_title, "new quiz title")
2 changes: 1 addition & 1 deletion iogt/templates/generic_components/article_card.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{{ article.top_level_section.title|upper }}
</p>
{% endif %}
<p class="article-title">{{ article.title }}</p>
<p class="article-title">{{ title }}</p>
{% if article.index_page_description %}
<p class="sm-paragraph">{{ article.index_page_description }}</p>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion iogt/templates/generic_components/questionnaire_card.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<a href="{% pageurl questionnaire %}" style="color:{{ font_color }}; background:{{ background_color }};">
<div>
<p>{% translate questionnaire.get_type|upper %}</p>
<p>{{ questionnaire.title }}</p>
<p>{{ title }}</p>
{% include 'questionnaires/tags/index_page_description_lines.html' with questionnaire=questionnaire %}
</div>
{% include 'home/tags/angle_bracket.html' %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<div class="questionnaire-container container"
style="color:{{ font_color }}; background:{{ background_color }};">
<h1 class="title polls-widget__title">{{ questionnaire.title }}</h1>
<h1 class="title polls-widget__title">{{ title }}</h1>
{% if user.is_authenticated or request.is_preview or questionnaire.allow_anonymous_submissions %}
{% if form %}
{{ questionnaire.description }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<div class="questionnaire-container container"
style="color:{{ font_color }}; background:{{ background_color }};">
<h1 class="title {{ questionnaire.get_type }}-page__title">{{ questionnaire.title }}</h1>
<h1 class="title {{ questionnaire.get_type }}-page__title">{{ title }}</h1>
{% if user.is_authenticated or request.is_preview or questionnaire.allow_anonymous_submissions %}
{% if form %}
{{ questionnaire.description }}
Expand Down
Loading