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