diff --git a/boards/context_processors.py b/boards/context_processors.py --- a/boards/context_processors.py +++ b/boards/context_processors.py @@ -28,13 +28,9 @@ def get_notifications(context, request): if username is not None and len(username) > 0: last_notification_id = settings_manager.get_setting( SETTING_LAST_NOTIFICATION_ID) - if last_notification_id is not None: - new_notifications_count = Notification.objects.filter( - id__gt=last_notification_id).filter( - name=username).count() - else: - new_notifications_count = Notification.objects.filter( - name=username).count() + + new_notifications_count = Notification.objects.get_notification_posts( + username=username, last=last_notification_id).count() context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count context[CONTEXT_USERNAME] = username diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -16,8 +16,8 @@ from boards import settings from boards.mdx_neboard import bbcode_extended from boards.models import PostImage from boards.models.base import Viewable +from boards.utils import datetime_to_epoch, cached_result from boards.models.user import Notification -from boards.utils import datetime_to_epoch, cached_result import boards.models.thread diff --git a/boards/models/user.py b/boards/models/user.py --- a/boards/models/user.py +++ b/boards/models/user.py @@ -1,5 +1,7 @@ from django.db import models +import boards.models.post + __author__ = 'neko259' BAN_REASON_AUTO = 'Auto' @@ -20,11 +22,23 @@ class Ban(models.Model): return self.ip +class NotificationManager(models.Manager): + def get_notification_posts(self, username: str, last: int = None): + posts = boards.models.post.Post.objects.filter(notification__name=username) + if last is not None: + posts = posts.filter(id__gt=last) + posts = posts.order_by('-id') + + return posts + + class Notification(models.Model): class Meta: app_label = 'boards' + objects = NotificationManager() + post = models.ForeignKey('Post') name = models.TextField() diff --git a/boards/urls.py b/boards/urls.py --- a/boards/urls.py +++ b/boards/urls.py @@ -66,6 +66,8 @@ urlpatterns = patterns('', name='get_thread'), url(r'^api/add_post/(?P\w+)/$', api.api_add_post, name='add_post'), + url(r'^api/notifications/(?P\w+)/$', api.api_get_notifications, + name='api_notifications'), # Search url(r'^search/$', BoardSearchView.as_view(), name='search'), diff --git a/boards/views/api.py b/boards/views/api.py --- a/boards/views/api.py +++ b/boards/views/api.py @@ -12,6 +12,7 @@ from boards.forms import PostForm, Plain from boards.models import Post, Thread, Tag from boards.utils import datetime_to_epoch from boards.views.thread import ThreadView +from boards.models.user import Notification __author__ = 'neko259' @@ -201,6 +202,20 @@ def api_get_thread_posts(request, openin return HttpResponse(content=json.dumps(json_data)) +def api_get_notifications(request, username): + last_notification_id_str = request.GET.get('last', None) + last_id = int(last_notification_id_str) if last_notification_id_str is not None else None + + posts = Notification.objects.get_notification_posts(username=username, + last=last_id) + + json_post_list = [] + for post in posts: + json_post_list.append(get_post_data(post.id)) + return HttpResponse(content=json.dumps(json_post_list)) + + + def api_get_post(request, post_id): """ Gets the JSON of a post. This can be diff --git a/boards/views/notifications.py b/boards/views/notifications.py --- a/boards/views/notifications.py +++ b/boards/views/notifications.py @@ -22,15 +22,15 @@ class NotificationView(BaseBoardView): # If we open our notifications, reset the "new" count my_username = settings_manager.get_setting(SETTING_USERNAME) + + posts = Notification.objects.get_notification_posts(username=username) if username == my_username: - last = Notification.objects.filter(name=username).order_by( - 'id').last() + last = posts.first() if last is not None: last_id = last.id settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID, last_id) - posts = Post.objects.filter(notification__name=username).order_by('-id') paginator = get_paginator(posts, RESULTS_PER_PAGE) page = int(request.GET.get(REQUEST_PAGE, '1')) diff --git a/docs/api.markdown b/docs/api.markdown --- a/docs/api.markdown +++ b/docs/api.markdown @@ -45,6 +45,15 @@ format. 2 formats are available: ``html` * ``updated``: list of updated posts * ``last_update``: last update timestamp +## Notifications ## + + /api/notifications//[?last=] + +Get user notifications for user starting from the post ID. + +* ``username``: name of the notified user +* ``id``: ID of a last notification post + ## General info ## In case of incorrect request you can get http error 404.