Skip to content

Commit

Permalink
#3672 Completes this work.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajrbyers committed Dec 8, 2023
1 parent 8d1c4cc commit 9e80606
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 25 deletions.
22 changes: 21 additions & 1 deletion src/core/homepage_elements/news/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class NewsForm(forms.Form):
help_text='Number of news articles to display on the homepage.'
)
display_images = forms.BooleanField(
help_text='When enabled the News Plugin will display news images.'
help_text='When enabled the News Plugin will display news images. '
'Note: this setting has no effect on the clean theme '
'which does not display images for news.',
required=False
)

def __init__(self, *args, **kwargs):
Expand All @@ -28,4 +31,21 @@ def __init__(self, *args, **kwargs):
self.fields['number_of_articles'].initial = number_of_articles.value
self.fields['display_images'].initial = True if display_images.value else False

def save(self, commit=True):
number_of_articles = self.cleaned_data.get('number_of_articles')
display_images = self.cleaned_data.get('display_images')

if commit:
setting_handler.save_setting(
'plugin:News',
'number_of_articles',
self.journal,
number_of_articles,
)
setting_handler.save_setting(
'plugin:News',
'display_images',
self.journal,
'On' if display_images else '',
)

10 changes: 9 additions & 1 deletion src/core/homepage_elements/news/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def yield_homepage_element_context(request, homepage_elements):
request.journal if request.journal else None).value
number_of_articles = int(
number_of_articles) if number_of_articles else 2
display_images = setting_handler.get_setting(
'plugin:News',
'display_images',
request.journal,
).value

news_items = comms_models.NewsItem.objects.filter(
(Q(content_type=request.model_content_type) & Q(
Expand All @@ -29,6 +34,9 @@ def yield_homepage_element_context(request, homepage_elements):
(Q(end_display__gte=timezone.now()) | Q(end_display=None))
).order_by('-posted')[:number_of_articles]

return {'news_items': news_items}
return {
'news_items': news_items,
'display_images': display_images,
}
else:
return {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

{% block body %}
<div class="large-12 columns box">
{% include "admin/elements/forms/errors.html" with form=form %}
<form method="POST">
{% csrf_token %}
{{ form|foundation }}
Expand Down
31 changes: 14 additions & 17 deletions src/core/homepage_elements/news/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,24 @@

@editor_user_required
def news_config(request):
plugin = models.Plugin.objects.get(name='News')
number_of_articles = setting_handler.get_plugin_setting(
plugin,
'number_of_articles',
request.journal,
create=True,
pretty='Number of Articles',
).value
number_of_articles = int(
number_of_articles
) if number_of_articles else 2
form = forms.NewsForm(journal=request.journal)
if request.POST:
number_of_articles = request.POST.get('number_of_articles')
setting_handler.save_plugin_setting(plugin, 'number_of_articles', number_of_articles, request.journal)
messages.add_message(request, messages.INFO, 'Number of articles updated.')
return redirect(reverse('home_settings_index'))
form = forms.NewsForm(
request.POST,
journal=request.journal,
)
if form.is_valid():
form.save()
messages.add_message(
request,
messages.INFO,
'News settings updated.'
)
return redirect(reverse('home_settings_index'))

template = 'news_settings.html'
context = {
'number_of_articles': number_of_articles,
'form': forms.NewsForm(journal=request.journal)
'form': form,
}

return render(request, template, context)
30 changes: 24 additions & 6 deletions src/themes/OLH/templates/journal/homepage_elements/news.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,33 @@
<div class="row column text-center">
<h2 class="title">{% trans 'Latest' %} {{ journal_settings.news.news_title }} {% trans "Posts" %}</h2>
</div>
<div class="row">
{% for item in news_items %}
<h5>{{ item.title }}</h5>
<h6>{{ item.byline }} {% trans "on" %} {{ item.posted|date:"Y-m-d" }}</h6>
<p>{{ item.body|striptags|truncatesmart:400 }}</p>
<a href="{% url 'core_news_item' item.pk %}">{% trans "Read More" %}</a>
<hr />
{% if display_images %}
{% if item.large_image_file or request.journal and request.journal.default_large_image or request.press.default_carousel_image %}
<div class="large-5 columns">
<a href="{% url 'core_news_item' item.pk %}"><img
class="news-image"
src="{{ item.best_image_url }}"
alt="{{ item.title }}"/></a>
</div>
{% endif %}
{% endif %}
{% if display_images %}
<div class="large-7 columns">
{% endif %}
<h5>{{ item.title }}</h5>
<h6>{{ item.byline }} {% trans "on" %} {{ item.posted|date:"Y-m-d" }}</h6>
<p>{{ item.body|striptags|truncatesmart:400 }}</p>
<a class="button" href="{% url 'core_news_item' item.pk %}">{% trans "Read More" %}</a>
{% if display_images %}
</div>
{% endif %}
<hr />
{% empty %}
<h4>{% trans "This journal currently has no news items to display" %}.</h4>
<h4>{% trans "This journal currently has no news items to display" %}.</h4>
{% endfor %}
</div>
</div>
</div>
</section>
13 changes: 13 additions & 0 deletions src/themes/material/templates/journal/homepage_elements/news.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
<span class="card-title">{% trans 'Latest' %} {{ journal_settings.news.news_title }} {% trans "Posts" %}</span>
</div>
{% for item in news_items %}
{% if display_images %}
{% if item.large_image_file or request.journal and request.journal.default_large_image or request.press.default_carousel_image %}
<div class="card-image">
<img src="{{ item.best_image_url }}"
alt="{{ item.title }}"/>
<span class="card-title">
<div class="carousel-text-wrapper">
{{ item.title }}
</div>
</span>
</div>
{% endif %}
{% endif %}
<div class="card-content">

<span class="card-title">{{ item.title }}</span>
Expand Down

0 comments on commit 9e80606

Please sign in to comment.