Skip to content

Commit

Permalink
Add multilanguage news items
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelGusse committed Mar 14, 2024
1 parent a06d16e commit 42fa3bc
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 27 deletions.
1 change: 1 addition & 0 deletions course/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class Meta(AplusModelSerializer.Meta):
'title',
'audience',
'publish',
'language',
'body',
'pin',
)
Expand Down
1 change: 1 addition & 0 deletions news/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def _generate_data(self, instance, data=None): # pylint: disable=arguments-diffe
'id': item.id,
'audience': item.audience,
'publish': item.publish,
'language': item.language,
'title': item.title,
'body': item.body,
'pin': item.pin,
Expand Down
1 change: 1 addition & 0 deletions news/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Meta:
'pin',
'email_students',
'email_staff',
'language',
'title',
'body',
]
8 changes: 7 additions & 1 deletion news/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.utils import timezone
from aplus import settings

from course.models import CourseInstance
from lib.models import UrlMixin
Expand All @@ -21,6 +22,11 @@ class News(models.Model, UrlMixin):
verbose_name=_('LABEL_PUBLISH'),
default=timezone.now,
)
language = models.CharField(
verbose_name=_('LANGUAGE'),
max_length=30,
choices=settings.LANGUAGES, default=settings.LANGUAGES[0],
)
title = models.CharField(
verbose_name=_('LABEL_TITLE'),
max_length=255,
Expand All @@ -39,7 +45,7 @@ class Meta:
ordering = ['course_instance', '-pin', '-publish']

def __str__(self):
return "{} {}".format(str(self.publish), self.title)
return "{} {} {}".format(str(self.publish), self.title, str(self.language))

def get_url_kwargs(self):
return dict(news_id=self.id, **self.course_instance.get_url_kwargs()) # pylint: disable=use-dict-literal
5 changes: 5 additions & 0 deletions news/templates/news/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<th>{% translate "PUBLISH" %}</th>
<th>{% translate "AUDIENCE" %}</th>
<th>{% translate "TITLE" %}</th>
<th>{% translate "LANGUAGE" %}</th>
<th></th>
<th></th>
</tr>
Expand All @@ -35,6 +36,10 @@
<td>
{{ item.title|safe }}
</td>
<td>
{{ item.language|safe }}
</td>

<td>
<a href="{{ item|url:'news-edit' }}" role="button" class="aplus-button--secondary aplus-button--xs">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
Expand Down
52 changes: 27 additions & 25 deletions news/templates/news/user_news.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% load i18n %}
{% load news %}

{% load course %}

{% if news %}
<div class="panel panel-primary news-panel">
Expand All @@ -9,32 +9,34 @@ <h3 class="panel-title">{% translate "COURSE_NEWS" %}</h3>
</div>
<div class="list-group">
{% for item in news %}
<div class="{% if item.pin %}pinned-{% endif %}list-group-item">
{% if item.pin %}
<span class="sr-only">{% translate "NEWS_ITEM_PINNED_SR_TEXT" %}</span>
{% endif %}
<div class="list-group-item-heading">
<h4 class="list-group-item-title">
{{ item.title|safe }}
</h4>
<div class="list-group-item-details">
{% if not item|is_published:now %}
<span class="future-instance">{% translate "PUBLISHED_ON" %}: <time>{{ item.publish }}</time></span>
{% else %}
<time class="current-instance">{{ item.publish }}</time>
{% endif %}
{% if is_course_staff %}
<span class="label label-primary">{{ item.audience|news_audience }}</span>
{% endif %}
{% if item.pin %}
<span class="glyphicon glyphicon-pushpin" aria-hidden="true"></span>
{% endif %}
{% if item.language == instance_language or instance.language|first != "|" %}
<div class="{% if item.pin %}pinned-{% endif %}list-group-item">
{% if item.pin %}
<span class="sr-only">{% translate "NEWS_ITEM_PINNED_SR_TEXT" %}</span>
{% endif %}
<div class="list-group-item-heading">
<h4 class="list-group-item-title">
{{ item.title|safe }}
</h4>
<div class="list-group-item-details">
{% if not item|is_published:now %}
<span class="future-instance">{% translate "PUBLISHED_ON" %}: <time>{{ item.publish }}</time></span>
{% else %}
<time class="current-instance">{{ item.publish }}</time>
{% endif %}
{% if is_course_staff %}
<span class="label label-primary">{{ item.audience|news_audience }}</span>
{% endif %}
{% if item.pin %}
<span class="glyphicon glyphicon-pushpin" aria-hidden="true"></span>
{% endif %}
</div>
</div>
<p class="list-group-item-text">
{{ item.body|safe }}
</p>
</div>
<p class="list-group-item-text">
{{ item.body|safe }}
</p>
</div>
{% endif %}
{# If a limit for folding news (more) is set, this creates the folding section #}
{% if forloop.counter == more and news|length > more and more > 0 %}
<a class="folding-list-group-item collapsed" href="#more-news" data-toggle="collapse" role="button" aria-controls="more-news" aria-expanded="false">
Expand Down
7 changes: 6 additions & 1 deletion news/templatetags/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
from lib.errors import TagUsageError
from ..cache import CachedNews
from ..models import News

from ..models import CourseInstance
from django.utils.translation import get_language

register = template.Library()


@register.inclusion_tag("news/user_news.html", takes_context=True)
def user_news(context, num, more=0): # pylint: disable=unused-argument
if 'instance' not in context:
context['instance'] = CourseInstance(context['instance'])
raise TagUsageError()
if 'now' not in context:
context['now'] = timezone.now()
if 'course_news' not in context:
context['course_news'] = CachedNews(context['instance'])
news = context['course_news']
instance = context['instance']

if context['is_course_staff']:
news = news.for_staff()
Expand All @@ -32,6 +35,8 @@ def user_news(context, num, more=0): # pylint: disable=unused-argument
'is_course_staff': context['is_course_staff'],
'now': context['now'],
'news': news,
'instance': instance,
'instance_language': get_language(),
'more': more,
}

Expand Down

0 comments on commit 42fa3bc

Please sign in to comment.