notifications.py
49 lines
| 1.5 KiB
| text/x-python
|
PythonLexer
neko259
|
r990 | from django.shortcuts import render | ||
neko259
|
r1090 | |||
neko259
|
r990 | from boards.abstracts.paginator import get_paginator | ||
from boards.abstracts.settingsmanager import get_settings_manager, \ | ||||
SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID | ||||
from boards.models.user import Notification | ||||
from boards.views.base import BaseBoardView | ||||
neko259
|
r1090 | DEFAULT_PAGE = '1' | ||
neko259
|
r990 | TEMPLATE = 'boards/notifications.html' | ||
PARAM_PAGE = 'page' | ||||
neko259
|
r1429 | PARAM_USERNAMES = 'notification_usernames' | ||
neko259
|
r990 | REQUEST_PAGE = 'page' | ||
RESULTS_PER_PAGE = 10 | ||||
class NotificationView(BaseBoardView): | ||||
neko259
|
r1429 | def get(self, request, username=None): | ||
neko259
|
r990 | params = self.get_context_data() | ||
settings_manager = get_settings_manager(request) | ||||
# If we open our notifications, reset the "new" count | ||||
neko259
|
r1429 | if username is None: | ||
notification_usernames = settings_manager.get_notification_usernames() | ||||
else: | ||||
notification_usernames = [username] | ||||
neko259
|
r1008 | |||
posts = Notification.objects.get_notification_posts( | ||||
neko259
|
r1429 | usernames=notification_usernames) | ||
if username is None: | ||||
neko259
|
r994 | last = posts.first() | ||
neko259
|
r990 | if last is not None: | ||
last_id = last.id | ||||
settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID, | ||||
last_id) | ||||
neko259
|
r1429 | |||
neko259
|
r990 | paginator = get_paginator(posts, RESULTS_PER_PAGE) | ||
neko259
|
r1090 | page = int(request.GET.get(REQUEST_PAGE, DEFAULT_PAGE)) | ||
neko259
|
r990 | |||
params[PARAM_PAGE] = paginator.page(page) | ||||
neko259
|
r1429 | params[PARAM_USERNAMES] = notification_usernames | ||
neko259
|
r990 | |||
return render(request, TEMPLATE, params) | ||||