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 20, 2024
1 parent a06d16e commit 6701bab
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 38 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
4 changes: 4 additions & 0 deletions locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -5374,6 +5374,10 @@ msgstr "news items"
msgid "ADD_NEWS_ITEM"
msgstr "Add news item"

#: news/templates/news/edit.html news/templates/news/list.html
msgid "REMOVE_SELECTED_NEWS_ITEMS"
msgstr "Remove selected news items"

#: news/templates/news/list.html
msgid "NEWS"
msgstr "News"
Expand Down
4 changes: 4 additions & 0 deletions locale/fi/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -5394,6 +5394,10 @@ msgstr "uutiset"
msgid "ADD_NEWS_ITEM"
msgstr "Lisää uutinen"

#: news/templates/news/edit.html news/templates/news/list.html
msgid "REMOVE_SELECTED_NEWS_ITEMS"
msgstr "Poista valitut uutiset"

#: news/templates/news/list.html
msgid "NEWS"
msgstr "Uutiset"
Expand Down
2 changes: 2 additions & 0 deletions news/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ 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,
}
for item in instance.news.all()
]

return {
'news': news,
}
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',
]
28 changes: 28 additions & 0 deletions news/migrations/0005_news_language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.11 on 2024-03-20 07:50

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("news", "0004_auto_20210812_1536"),
]

operations = [
migrations.AddField(
model_name="news",
name="language",
field=models.CharField(
choices=[
("-", "All"),
("en", "English"),
("fi", "Finnish"),
("sv", "Swedish"),
],
default="-",
max_length=30,
verbose_name="LANGUAGE",
),
),
]
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=([('-', 'All')] + settings.LANGUAGES), default='-',
)
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
30 changes: 23 additions & 7 deletions news/templates/news/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
<br />
<table class="table">
<tr>
<th>{% translate "SELECT" %}</th>
<th>{% translate "PUBLISH" %}</th>
<th>{% translate "AUDIENCE" %}</th>
<th>{% translate "TITLE" %}</th>
<th>{% translate "LANGUAGE" %}</th>
<th></th>
<th></th>
</tr>
<form method="post" action="{{ instance|url:'remove-selected-news' }}">
{% for item in news %}
<tr>
<td id={{item.id}}>
<input type="checkbox" name="selection" value="{{item.id}}">
</td>

<td>
{% if item.pin %}
<span class="glyphicon glyphicon-pushpin" aria-hidden="true"></span>
Expand All @@ -35,23 +42,32 @@
<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>
{% translate "EDIT" %}
</a>
</td>
<td>
<form method="post" action="{{ item|url:'news-remove' }}">
{% csrf_token %}
<button type="submit" class="aplus-button--danger aplus-button--xs">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
{% translate "REMOVE" %}
</button>
</form>
<a href="{{ item|url:'news-remove' }}" role="button" class="aplus-button--danger aplus-button--xs">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
{% translate "REMOVE" %}
</a>
</td>
</tr>
{% endfor %}
{% csrf_token %}
<button type="submit" class="aplus-button--danger aplus-button--sm">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
{% translate "REMOVE_SELECTED_NEWS_ITEMS" %}
</button>
</form>


</table>
<br />
<p>
Expand Down
56 changes: 28 additions & 28 deletions news/templates/news/user_news.html
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
{% load i18n %}
{% load news %}

{% load course %}

{% if news %}
<div class="panel panel-primary news-panel">
<div class="panel-heading">
<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 %}
{% for item in language_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 %}
</div>
</div>
<p class="list-group-item-text">
{{ item.body|safe }}
</p>
</div>
<p class="list-group-item-text">
{{ item.body|safe }}
</p>
</div>
{# 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 %}
{% if forloop.counter == more and language_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">
{% translate "SHOW_OLDER" %} <span class="caret"></span>
</a>
<div class="collapse" id="more-news">{# start of collapse.div #}
{% endif %}
{% if forloop.last and news|length > more and more > 0 %}
{% if forloop.last and language_news|length > more and more > 0 %}
</div>{# end of collapse.div #}
{% endif %}
{% endfor %}
Expand Down
13 changes: 12 additions & 1 deletion news/templatetags/news.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from collections import defaultdict

from django import template
from django.utils import timezone

from lib.errors import TagUsageError
from ..cache import CachedNews
from ..models import News

from django.utils.translation import get_language

register = template.Library()

Expand All @@ -28,10 +30,19 @@ def user_news(context, num, more=0): # pylint: disable=unused-argument
or user.userprofile.is_external
)

language_to_news = defaultdict(list)
for item in news:
language_to_news[item['language']].append(item)

# Get news of current chosen language AND news items with All language
language_dict = language_to_news['-'] + language_to_news[get_language()]

return {
'is_course_staff': context['is_course_staff'],
'now': context['now'],
'news': news,
'language_news': language_dict,
'instance_language': get_language(),
'more': more,
}

Expand Down
4 changes: 4 additions & 0 deletions news/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
re_path(EDIT_URL_PREFIX + r'news/(?P<news_id>\d+)/remove/$',
views.RemoveNewsView.as_view(),
name="news-remove"),
re_path(EDIT_URL_PREFIX + r'news/remove-selected-news/$',
views.RemoveSelectedNewsView.as_view(),
name="remove-selected-news"),

]
24 changes: 23 additions & 1 deletion news/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,33 @@ class ListNewsView(CourseInstanceBaseView):
access_mode = ACCESS.TEACHER
template_name = "news/list.html"

def get_common_objects(self):
super().get_common_objects()
self.news = self.instance.news.all()
print(self.news)

self.note("news")


class RemoveSelectedNewsView(CourseInstanceBaseView, BaseRedirectView):
access_mode = ACCESS.TEACHER
template_name = "news/list.html"

def get_common_objects(self):
super().get_common_objects()
self.news = self.instance.news.all()
self.note("news")

def post(self, request, *args, **kwargs):
selected_news_ids = request.POST.getlist("selection")

for new in self.news:
if str(new.id) in selected_news_ids:
print("Deleting ", new)
new.delete()

return self.redirect(self.instance.get_url("news-list"))


class EditNewsView(CourseInstanceMixin, BaseFormView):
access_mode = ACCESS.TEACHER
Expand Down Expand Up @@ -76,6 +98,6 @@ def get_resource_objects(self):
)
self.note("news_item")

def post(self, request, *args, **kwargs):
def get(self, request, *args, **kwargs):
self.news_item.delete()
return self.redirect(self.instance.get_url("news-list"))

0 comments on commit 6701bab

Please sign in to comment.