Skip to content

Commit

Permalink
Merge 2b92af5 into e989dea
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Oct 3, 2020
2 parents e989dea + 2b92af5 commit 1ce2570
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 58 deletions.
2 changes: 1 addition & 1 deletion spirit/core/templates/spirit/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{% if user.is_authenticated %}
stModules.notification(document.querySelectorAll('.js-tab-notification'), {
notificationUrl: "{% url "spirit:topic:notification:index-ajax" %}",
notificationListUrl: "{% url "spirit:topic:notification:index" %}",
notificationListUrl: "{% url "spirit:topic:notification:index-unread" %}",
mentionTxt: "{% trans "{user} has mention you on {topic}" %}",
commentTxt: "{% trans "{user} has commented on {topic}" %}",
showAll: "{% trans "Show all" %}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
{% load i18n %}

<div class="layout__menu">
<div class="menu__dropdown">
<div class="menu__dropdown js-tabs-container">
<div class="menu__dropdown_button">
<span class="dropdown_button">{% trans "Notifications" %}</span>
{% if active == "notifications" %}
<a
class="dropdown_button js-tab"
href="#"
data-related=".js-categories-content"
>{% trans "Notifications" %} <i class="fa fa-chevron-down"></i></a>
{% else %}
<a
class="dropdown_button js-tab"
href="#"
data-related=".js-categories-content"
>{% trans "Unread notifications" %} <i class="fa fa-chevron-down"></i></a>
{% endif %}
</div>
<div class="menu_list_wrapper js-tab-content js-categories-content" style="display: none;">
{% spaceless %}
<ul class="menu_list">
<li><a
class="menu_list__link"
href="{% url "spirit:topic:notification:index" %}"
>{% trans "Notifications" %}</a></li>
<li><a
class="menu_list__link"
href="{% url "spirit:topic:notification:index-unread" %}"
>{% trans "Unread notifications" %}</a></li>
</ul>
{% endspaceless %}
</div>
</div>
<div class="menu__button">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{% block content %}
<div class="layout__article">
{% include "spirit/topic/notification/_top_bar.html" %}
{% include "spirit/topic/notification/_top_bar.html" with active="notifications" %}

{% include "spirit/topic/notification/_render_list.html" %}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% extends "spirit/_base.html" %}

{% load spirit_tags i18n %}

{% block title %}{% trans "Unread notifications" %}{% endblock %}

{% block content %}
<div class="layout__article">
{% include "spirit/topic/notification/_top_bar.html" with active="unread_notifications" %}

{% if page %}
{% include "spirit/topic/notification/_render_list.html" with notifications=page %}
{% else %}
<p>{% trans "There are no new notifications" %}.</p>
{% endif %}

{# TODO: make this a template tag #}
{% if page.has_next %}
{% spaceless %}
<ul class="paginator">
<li><a
class="paginator__button"
href="?p={{ next_page }}"
>{% trans "Next" %} <i class="fa fa-chevron-right"></i></a></li>
</ul>
{% endspaceless %}
{% endif %}
</div>
{% endblock %}
94 changes: 42 additions & 52 deletions spirit/topic/notification/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.utils import timezone

from djconfig.utils import override_djconfig
from infinite_scroll_pagination.serializers import to_page_key

from spirit.core.tests import utils
from .models import TopicNotification
Expand Down Expand Up @@ -58,58 +59,6 @@ def test_topic_notification_list(self):
list(response.context['notifications']),
[self.topic_notification])

@override_settings(ST_NOTIFICATIONS_PER_PAGE=20)
def test_topic_notification_list_order(self):
TopicNotification.objects.all().delete()
old_date = timezone.now() - datetime.timedelta(days=10)
topic0 = utils.create_topic(self.category)
comment0 = utils.create_comment(topic=topic0)
tn_comment0 = TopicNotification.objects.create(
user=self.user, topic=topic0,
comment=comment0, is_active=True,
action=COMMENT)
topic1 = utils.create_topic(self.category)
comment1 = utils.create_comment(topic=topic1)
tn_comment1 = TopicNotification.objects.create(
user=self.user, topic=topic1,
comment=comment1, is_active=True,
action=COMMENT,
date=old_date)
topic2 = utils.create_topic(self.category)
comment2 = utils.create_comment(topic=topic2)
tn_mention1 = TopicNotification.objects.create(
user=self.user, topic=topic2,
comment=comment2, is_active=True,
action=MENTION,
date=old_date)
topic3 = utils.create_topic(self.category)
comment3 = utils.create_comment(topic=topic3)
tn_mention2_read = TopicNotification.objects.create(
user=self.user, topic=topic3,
comment=comment3, is_active=True,
is_read=True,
action=MENTION)
topic4 = utils.create_topic(self.category)
comment4 = utils.create_comment(topic=topic4)
tn_comment2 = TopicNotification.objects.create(
user=self.user, topic=topic4,
comment=comment4, is_active=True,
action=COMMENT,
date=old_date)
topic5 = utils.create_topic(self.category)
comment5 = utils.create_comment(topic=topic5)
tn_comment3_read = TopicNotification.objects.create(
user=self.user, topic=topic5,
comment=comment5, is_active=True,
is_read=True,
action=COMMENT)
utils.login(self)
response = self.client.get(reverse('spirit:topic:notification:index'))
self.assertEqual(
list(response.context['notifications']),
[tn_mention1, tn_comment0, tn_comment2,
tn_comment1, tn_mention2_read, tn_comment3_read])

@override_djconfig(topics_per_page=1)
def test_topic_notification_list_paginate(self):
"""
Expand Down Expand Up @@ -146,6 +95,11 @@ def test_topic_notification_list_show_private_topic(self):
list(response.context['notifications']),
[topic_notif, ])

# list unread should behave the same
response = self.client.get(
reverse('spirit:topic:notification:index-unread'))
self.assertEqual(list(response.context['page']), [topic_notif, ])

# ajax list should behave the same
response = self.client.get(
reverse('spirit:topic:notification:index-ajax'),
Expand Down Expand Up @@ -193,13 +147,49 @@ def test_topic_notification_list_dont_show_topic_removed_or_no_access(self):
response = self.client.get(reverse('spirit:topic:notification:index'))
self.assertEqual(list(response.context['notifications']), [])

# list unread should behave the same
response = self.client.get(
reverse('spirit:topic:notification:index-unread'))
self.assertEqual(list(response.context['page']), [])

# ajax list should behave the same
response = self.client.get(
reverse('spirit:topic:notification:index-ajax'),
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
res = json.loads(response.content.decode('utf-8'))
self.assertEqual(len(res['n']), 0)

@override_settings(ST_NOTIFICATIONS_PER_PAGE=10)
def test_topic_notification_list_unread(self):
"""
topic notification list
"""
topic = utils.create_topic(self.category, user=self.user2)
comment = utils.create_comment(topic=topic, user=self.user2)
topic_notification = TopicNotification.objects.create(
user=self.user,
topic=topic,
comment=comment,
is_active=True,
action=COMMENT)

utils.login(self)
response = self.client.get(
reverse('spirit:topic:notification:index-unread'))
self.assertEqual(
list(response.context['page']),
[topic_notification, self.topic_notification])

# fake next page
response = self.client.get(
reverse('spirit:topic:notification:index-unread'),
{'p': to_page_key(
value=topic_notification.date,
pk=topic_notification.pk)})
self.assertEqual(
list(response.context['page']),
[self.topic_notification, ])

def test_topic_notification_ajax(self):
"""
get notifications
Expand Down
1 change: 1 addition & 0 deletions spirit/topic/notification/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
app_name = 'notification'
urlpatterns = [
re_path(r'^$', views.index, name='index'),
re_path(r'^unread/$', views.index_unread, name='index-unread'),
re_path(r'^ajax/$', views.index_ajax, name='index-ajax'),
re_path(r'^(?P<topic_id>[0-9]+)/create/$', views.create, name='create'),
re_path(r'^(?P<pk>[0-9]+)/update/$', views.update, name='update'),
Expand Down
26 changes: 24 additions & 2 deletions spirit/topic/notification/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
from django.urls import reverse

from djconfig import config
from infinite_scroll_pagination.serializers import to_page_key

from spirit.core.conf import settings
from spirit.core import utils
from spirit.core.utils.paginator import yt_paginate
from spirit.core.utils.paginator.infinite_paginator import paginate
from spirit.core.utils.views import is_ajax
from spirit.topic.models import Topic
from .models import TopicNotification
Expand Down Expand Up @@ -63,7 +65,7 @@ def index_ajax(request):
notifications = (
TopicNotification.objects
.for_access(request.user)
.order_by("is_read", "action", "-date", "-pk")
.order_by("is_read", "-date")
.with_related_data())
notifications = notifications[:settings.ST_NOTIFICATIONS_PER_PAGE]
notifications = [
Expand All @@ -79,12 +81,32 @@ def index_ajax(request):
content_type="application/json")


@login_required
def index_unread(request):
notifications = (
TopicNotification.objects
.for_access(request.user)
.filter(is_read=False)
.with_related_data())
page = paginate(
request,
query_set=notifications,
lookup_field='date',
page_var='p',
per_page=settings.ST_NOTIFICATIONS_PER_PAGE)
return render(
request=request,
template_name='spirit/topic/notification/index_unread.html',
context={
'page': page,
'next_page': to_page_key(**page.next_page())})


@login_required
def index(request):
notifications = yt_paginate(
TopicNotification.objects
.for_access(request.user)
.order_by("is_read", "action", "-date", "-pk")
.with_related_data(),
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1))
Expand Down

0 comments on commit 1ce2570

Please sign in to comment.