##// 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 if username is not None and len(username) > 0:
28 if username is not None and len(username) > 0:
29 last_notification_id = settings_manager.get_setting(
29 last_notification_id = settings_manager.get_setting(
30 SETTING_LAST_NOTIFICATION_ID)
30 SETTING_LAST_NOTIFICATION_ID)
31 if last_notification_id is not None:
31
32 new_notifications_count = Notification.objects.filter(
32 new_notifications_count = Notification.objects.get_notification_posts(
33 id__gt=last_notification_id).filter(
33 username=username, last=last_notification_id).count()
34 name=username).count()
35 else:
36 new_notifications_count = Notification.objects.filter(
37 name=username).count()
38 context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count
34 context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count
39 context[CONTEXT_USERNAME] = username
35 context[CONTEXT_USERNAME] = username
40
36
@@ -16,8 +16,8 from boards import settings
16 from boards.mdx_neboard import bbcode_extended
16 from boards.mdx_neboard import bbcode_extended
17 from boards.models import PostImage
17 from boards.models import PostImage
18 from boards.models.base import Viewable
18 from boards.models.base import Viewable
19 from boards.utils import datetime_to_epoch, cached_result
19 from boards.models.user import Notification
20 from boards.models.user import Notification
20 from boards.utils import datetime_to_epoch, cached_result
21 import boards.models.thread
21 import boards.models.thread
22
22
23
23
@@ -1,5 +1,7
1 from django.db import models
1 from django.db import models
2
2
3 import boards.models.post
4
3 __author__ = 'neko259'
5 __author__ = 'neko259'
4
6
5 BAN_REASON_AUTO = 'Auto'
7 BAN_REASON_AUTO = 'Auto'
@@ -20,11 +22,23 class Ban(models.Model):
20 return self.ip
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 class Notification(models.Model):
35 class Notification(models.Model):
24
36
25 class Meta:
37 class Meta:
26 app_label = 'boards'
38 app_label = 'boards'
27
39
40 objects = NotificationManager()
41
28 post = models.ForeignKey('Post')
42 post = models.ForeignKey('Post')
29 name = models.TextField()
43 name = models.TextField()
30
44
@@ -66,6 +66,8 urlpatterns = patterns('',
66 name='get_thread'),
66 name='get_thread'),
67 url(r'^api/add_post/(?P<opening_post_id>\w+)/$', api.api_add_post,
67 url(r'^api/add_post/(?P<opening_post_id>\w+)/$', api.api_add_post,
68 name='add_post'),
68 name='add_post'),
69 url(r'^api/notifications/(?P<username>\w+)/$', api.api_get_notifications,
70 name='api_notifications'),
69
71
70 # Search
72 # Search
71 url(r'^search/$', BoardSearchView.as_view(), name='search'),
73 url(r'^search/$', BoardSearchView.as_view(), name='search'),
@@ -12,6 +12,7 from boards.forms import PostForm, Plain
12 from boards.models import Post, Thread, Tag
12 from boards.models import Post, Thread, Tag
13 from boards.utils import datetime_to_epoch
13 from boards.utils import datetime_to_epoch
14 from boards.views.thread import ThreadView
14 from boards.views.thread import ThreadView
15 from boards.models.user import Notification
15
16
16 __author__ = 'neko259'
17 __author__ = 'neko259'
17
18
@@ -201,6 +202,20 def api_get_thread_posts(request, openin
201 return HttpResponse(content=json.dumps(json_data))
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 def api_get_post(request, post_id):
219 def api_get_post(request, post_id):
205 """
220 """
206 Gets the JSON of a post. This can be
221 Gets the JSON of a post. This can be
@@ -22,15 +22,15 class NotificationView(BaseBoardView):
22
22
23 # If we open our notifications, reset the "new" count
23 # If we open our notifications, reset the "new" count
24 my_username = settings_manager.get_setting(SETTING_USERNAME)
24 my_username = settings_manager.get_setting(SETTING_USERNAME)
25
26 posts = Notification.objects.get_notification_posts(username=username)
25 if username == my_username:
27 if username == my_username:
26 last = Notification.objects.filter(name=username).order_by(
28 last = posts.first()
27 'id').last()
28 if last is not None:
29 if last is not None:
29 last_id = last.id
30 last_id = last.id
30 settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID,
31 settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID,
31 last_id)
32 last_id)
32
33
33 posts = Post.objects.filter(notification__name=username).order_by('-id')
34 paginator = get_paginator(posts, RESULTS_PER_PAGE)
34 paginator = get_paginator(posts, RESULTS_PER_PAGE)
35
35
36 page = int(request.GET.get(REQUEST_PAGE, '1'))
36 page = int(request.GET.get(REQUEST_PAGE, '1'))
@@ -45,6 +45,15 format. 2 formats are available: ``html`
45 * ``updated``: list of updated posts
45 * ``updated``: list of updated posts
46 * ``last_update``: last update timestamp
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 ## General info ##
57 ## General info ##
49
58
50 In case of incorrect request you can get http error 404.
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