Skip to content

Commit

Permalink
fixes #237
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Sep 1, 2018
1 parent d02b5d2 commit c504d07
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 20 deletions.
5 changes: 5 additions & 0 deletions spirit/comment/utils.py
Expand Up @@ -26,3 +26,8 @@ def post_comment_update(comment):


comment.comment_html = post_render_static_polls(comment) comment.comment_html = post_render_static_polls(comment)
CommentHistory.create(comment) CommentHistory.create(comment)


# XXX add tests
def post_comment_move(comment, topic):
TopicNotification.sync(comment=comment, topic=topic)
5 changes: 3 additions & 2 deletions spirit/comment/views.py
Expand Up @@ -16,7 +16,7 @@
from ..topic.models import Topic from ..topic.models import Topic
from .models import Comment from .models import Comment
from .forms import CommentForm, CommentMoveForm, CommentImageForm, CommentFileForm from .forms import CommentForm, CommentMoveForm, CommentImageForm, CommentFileForm
from .utils import comment_posted, post_comment_update, pre_comment_update from .utils import comment_posted, post_comment_update, pre_comment_update, post_comment_move




@login_required @login_required
Expand Down Expand Up @@ -109,14 +109,15 @@ def move(request, topic_id):
for comment in comments: for comment in comments:
comment_posted(comment=comment, mentions=None) comment_posted(comment=comment, mentions=None)
topic.decrease_comment_count() topic.decrease_comment_count()
post_comment_move(comment=comment, topic=topic)
else: else:
messages.error(request, render_form_errors(form)) messages.error(request, render_form_errors(form))


return redirect(request.POST.get('next', topic.get_absolute_url())) return redirect(request.POST.get('next', topic.get_absolute_url()))




def find(request, pk): def find(request, pk):
comment = get_object_or_404(Comment, pk=pk) comment = get_object_or_404(Comment.objects.select_related('topic'), pk=pk)
comment_number = Comment.objects.filter(topic=comment.topic, date__lte=comment.date).count() comment_number = Comment.objects.filter(topic=comment.topic, date__lte=comment.date).count()
url = paginator.get_url(comment.topic.get_absolute_url(), url = paginator.get_url(comment.topic.get_absolute_url(),
comment_number, comment_number,
Expand Down
3 changes: 3 additions & 0 deletions spirit/topic/notification/managers.py
Expand Up @@ -29,3 +29,6 @@ def read(self, user):
# returns updated rows count (int) # returns updated rows count (int)
return self.filter(user=user)\ return self.filter(user=user)\
.update(is_read=True) .update(is_read=True)

def with_related_data(self):
return self.select_related('comment__user__st', 'topic')
29 changes: 29 additions & 0 deletions spirit/topic/notification/models.py
Expand Up @@ -40,6 +40,9 @@ class Meta:
verbose_name_plural = _("topics notification") verbose_name_plural = _("topics notification")


def get_absolute_url(self): def get_absolute_url(self):
if self.topic_id != self.comment.topic_id:
# Out of sync
return self.topic.get_absolute_url()
return self.comment.get_absolute_url() return self.comment.get_absolute_url()


@property @property
Expand Down Expand Up @@ -117,3 +120,29 @@ def bulk_create(cls, users, comment):
is_active=True) is_active=True)
for user in users for user in users
]) ])

# XXX add tests
# XXX fix with migration (see issue #237)
@classmethod
def sync(cls, comment, topic):
# Notifications can go out of sync
# when the comment is no longer
# within the topic (i.e moved).
# User is subscribed to the topic,
# not the comment, so we either update
# it to a newer comment or set it as undefined
if comment.topic_id == topic.pk:
return
next_comment = (
topic.comment_set
.filter(date__gt=comment.date)
.order_by('date')
.first())
if next_comment is None:
(cls.objects
.filter(comment=comment, topic=topic)
.update(is_read=True, action=UNDEFINED))
return
(cls.objects
.filter(comment=comment, topic=topic)
.update(is_read=True, comment=next_comment, action=COMMENT))
38 changes: 20 additions & 18 deletions spirit/topic/notification/views.py
Expand Up @@ -55,61 +55,63 @@ def index_ajax(request):
if not request.is_ajax(): if not request.is_ajax():
return Http404() return Http404()


notifications = TopicNotification.objects\ notifications = (
.for_access(request.user)\ TopicNotification.objects
.order_by("is_read", "-date")\ .for_access(request.user)
.select_related('comment__user__st', 'comment__topic') .order_by("is_read", "-date")
.with_related_data())


notifications = notifications[:settings.ST_NOTIFICATIONS_PER_PAGE] notifications = notifications[:settings.ST_NOTIFICATIONS_PER_PAGE]


notifications = [ notifications = [
{ {
'user': escape(n.comment.user.username), 'user': escape(n.comment.user.username),
'action': n.action, 'action': n.action,
'title': escape(n.comment.topic.title), 'title': escape(n.topic.title),
'url': n.get_absolute_url(), 'url': n.get_absolute_url(),
'is_read': n.is_read 'is_read': n.is_read
} }
for n in notifications for n in notifications
] ]


return HttpResponse(json.dumps({'n': notifications, }), content_type="application/json") return HttpResponse(json.dumps({'n': notifications}), content_type="application/json")




@login_required @login_required
def index_unread(request): def index_unread(request):
notifications = TopicNotification.objects\ notifications = (
.for_access(request.user)\ TopicNotification.objects
.filter(is_read=False) .for_access(request.user)
.filter(is_read=False)
.with_related_data())


page = paginate( page = paginate(
request, request,
query_set=notifications, query_set=notifications,
lookup_field='date', lookup_field='date',
page_var='notif', page_var='notif',
per_page=settings.ST_NOTIFICATIONS_PER_PAGE per_page=settings.ST_NOTIFICATIONS_PER_PAGE)
)
next_page_pk = None


next_page_pk = None
if page: if page:
next_page_pk = page[-1].pk next_page_pk = page[-1].pk


context = { context = {
'page': page, 'page': page,
'next_page_pk': next_page_pk 'next_page_pk': next_page_pk}
}


return render(request, 'spirit/topic/notification/index_unread.html', context) return render(request, 'spirit/topic/notification/index_unread.html', context)




@login_required @login_required
def index(request): def index(request):
notifications = yt_paginate( notifications = yt_paginate(
TopicNotification.objects.for_access(request.user), TopicNotification.objects
.for_access(request.user)
.with_related_data(),
per_page=config.topics_per_page, per_page=config.topics_per_page,
page_number=request.GET.get('page', 1) page_number=request.GET.get('page', 1))
)


context = {'notifications': notifications, } context = {'notifications': notifications}


return render(request, 'spirit/topic/notification/index.html', context) return render(request, 'spirit/topic/notification/index.html', context)

0 comments on commit c504d07

Please sign in to comment.