##// END OF EJS Templates
Added notification API
neko259 -
r994:e93bc5ac default
parent child Browse files
Show More
@@ -28,13 +28,9 def get_notifications(context, request):
28 28 if username is not None and len(username) > 0:
29 29 last_notification_id = settings_manager.get_setting(
30 30 SETTING_LAST_NOTIFICATION_ID)
31 if last_notification_id is not None:
32 new_notifications_count = Notification.objects.filter(
33 id__gt=last_notification_id).filter(
34 name=username).count()
35 else:
36 new_notifications_count = Notification.objects.filter(
37 name=username).count()
31
32 new_notifications_count = Notification.objects.get_notification_posts(
33 username=username, last=last_notification_id).count()
38 34 context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count
39 35 context[CONTEXT_USERNAME] = username
40 36
@@ -16,8 +16,8 from boards import settings
16 16 from boards.mdx_neboard import bbcode_extended
17 17 from boards.models import PostImage
18 18 from boards.models.base import Viewable
19 from boards.utils import datetime_to_epoch, cached_result
19 20 from boards.models.user import Notification
20 from boards.utils import datetime_to_epoch, cached_result
21 21 import boards.models.thread
22 22
23 23
@@ -1,5 +1,7
1 1 from django.db import models
2 2
3 import boards.models.post
4
3 5 __author__ = 'neko259'
4 6
5 7 BAN_REASON_AUTO = 'Auto'
@@ -20,11 +22,23 class Ban(models.Model):
20 22 return self.ip
21 23
22 24
25 class NotificationManager(models.Manager):
26 def get_notification_posts(self, username: str, last: int = None):
27 posts = boards.models.post.Post.objects.filter(notification__name=username)
28 if last is not None:
29 posts = posts.filter(id__gt=last)
30 posts = posts.order_by('-id')
31
32 return posts
33
34
23 35 class Notification(models.Model):
24 36
25 37 class Meta:
26 38 app_label = 'boards'
27 39
40 objects = NotificationManager()
41
28 42 post = models.ForeignKey('Post')
29 43 name = models.TextField()
30 44
@@ -66,6 +66,8 urlpatterns = patterns('',
66 66 name='get_thread'),
67 67 url(r'^api/add_post/(?P<opening_post_id>\w+)/$', api.api_add_post,
68 68 name='add_post'),
69 url(r'^api/notifications/(?P<username>\w+)/$', api.api_get_notifications,
70 name='api_notifications'),
69 71
70 72 # Search
71 73 url(r'^search/$', BoardSearchView.as_view(), name='search'),
@@ -12,6 +12,7 from boards.forms import PostForm, Plain
12 12 from boards.models import Post, Thread, Tag
13 13 from boards.utils import datetime_to_epoch
14 14 from boards.views.thread import ThreadView
15 from boards.models.user import Notification
15 16
16 17 __author__ = 'neko259'
17 18
@@ -201,6 +202,20 def api_get_thread_posts(request, openin
201 202 return HttpResponse(content=json.dumps(json_data))
202 203
203 204
205 def api_get_notifications(request, username):
206 last_notification_id_str = request.GET.get('last', None)
207 last_id = int(last_notification_id_str) if last_notification_id_str is not None else None
208
209 posts = Notification.objects.get_notification_posts(username=username,
210 last=last_id)
211
212 json_post_list = []
213 for post in posts:
214 json_post_list.append(get_post_data(post.id))
215 return HttpResponse(content=json.dumps(json_post_list))
216
217
218
204 219 def api_get_post(request, post_id):
205 220 """
206 221 Gets the JSON of a post. This can be
@@ -22,15 +22,15 class NotificationView(BaseBoardView):
22 22
23 23 # If we open our notifications, reset the "new" count
24 24 my_username = settings_manager.get_setting(SETTING_USERNAME)
25
26 posts = Notification.objects.get_notification_posts(username=username)
25 27 if username == my_username:
26 last = Notification.objects.filter(name=username).order_by(
27 'id').last()
28 last = posts.first()
28 29 if last is not None:
29 30 last_id = last.id
30 31 settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID,
31 32 last_id)
32 33
33 posts = Post.objects.filter(notification__name=username).order_by('-id')
34 34 paginator = get_paginator(posts, RESULTS_PER_PAGE)
35 35
36 36 page = int(request.GET.get(REQUEST_PAGE, '1'))
@@ -45,6 +45,15 format. 2 formats are available: ``html`
45 45 * ``updated``: list of updated posts
46 46 * ``last_update``: last update timestamp
47 47
48 ## Notifications ##
49
50 /api/notifications/<username>/[?last=<id>]
51
52 Get user notifications for user starting from the post ID.
53
54 * ``username``: name of the notified user
55 * ``id``: ID of a last notification post
56
48 57 ## General info ##
49 58
50 59 In case of incorrect request you can get http error 404.
General Comments 0
You need to be logged in to leave comments. Login now