context_processors.py
88 lines
| 3.0 KiB
| text/x-python
|
PythonLexer
/ boards / context_processors.py
neko259
|
r990 | from boards.abstracts.settingsmanager import get_settings_manager, \ | ||
neko259
|
r1122 | SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER | ||
neko259
|
r990 | from boards.models.user import Notification | ||
neko259
|
r728 | |||
neko259
|
r721 | __author__ = 'neko259' | ||
neko259
|
r1109 | from boards import settings, utils | ||
neko259
|
r1027 | from boards.models import Post, Tag | ||
neko259
|
r690 | |||
neko259
|
r721 | CONTEXT_SITE_NAME = 'site_name' | ||
CONTEXT_VERSION = 'version' | ||||
CONTEXT_MODERATOR = 'moderator' | ||||
CONTEXT_THEME_CSS = 'theme_css' | ||||
CONTEXT_THEME = 'theme' | ||||
CONTEXT_PPD = 'posts_per_day' | ||||
CONTEXT_TAGS = 'tags' | ||||
CONTEXT_USER = 'user' | ||||
neko259
|
r990 | CONTEXT_NEW_NOTIFICATIONS_COUNT = 'new_notifications_count' | ||
CONTEXT_USERNAME = 'username' | ||||
neko259
|
r1027 | CONTEXT_TAGS_STR = 'tags_str' | ||
neko259
|
r1122 | CONTEXT_IMAGE_VIEWER = 'image_viewer' | ||
neko259
|
r1323 | CONTEXT_FAV_THREADS = 'fav_threads' | ||
neko259
|
r811 | |||
neko259
|
r690 | |||
neko259
|
r990 | def get_notifications(context, request): | ||
settings_manager = get_settings_manager(request) | ||||
username = settings_manager.get_setting(SETTING_USERNAME) | ||||
new_notifications_count = 0 | ||||
if username is not None and len(username) > 0: | ||||
last_notification_id = settings_manager.get_setting( | ||||
SETTING_LAST_NOTIFICATION_ID) | ||||
neko259
|
r994 | |||
new_notifications_count = Notification.objects.get_notification_posts( | ||||
username=username, last=last_notification_id).count() | ||||
neko259
|
r990 | context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count | ||
context[CONTEXT_USERNAME] = username | ||||
neko259
|
r690 | def user_and_ui_processor(request): | ||
neko259
|
r905 | context = dict() | ||
neko259
|
r690 | |||
neko259
|
r721 | context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day()) | ||
neko259
|
r690 | |||
neko259
|
r730 | settings_manager = get_settings_manager(request) | ||
neko259
|
r1027 | fav_tags = settings_manager.get_fav_tags() | ||
context[CONTEXT_TAGS] = fav_tags | ||||
neko259
|
r1323 | |||
_get_fav_threads(context, settings_manager) | ||||
neko259
|
r1027 | context[CONTEXT_TAGS_STR] = Tag.objects.get_tag_url_list(fav_tags) | ||
neko259
|
r728 | theme = settings_manager.get_theme() | ||
neko259
|
r721 | context[CONTEXT_THEME] = theme | ||
context[CONTEXT_THEME_CSS] = 'css/' + theme + '/base_page.css' | ||||
neko259
|
r690 | |||
# This shows the moderator panel | ||||
neko259
|
r1109 | context[CONTEXT_MODERATOR] = utils.is_moderator(request) | ||
neko259
|
r690 | |||
neko259
|
r1153 | context[CONTEXT_VERSION] = settings.get('Version', 'Version') | ||
context[CONTEXT_SITE_NAME] = settings.get('Version', 'SiteName') | ||||
neko259
|
r690 | |||
neko259
|
r1122 | context[CONTEXT_IMAGE_VIEWER] = settings_manager.get_setting( | ||
neko259
|
r1153 | SETTING_IMAGE_VIEWER, | ||
default=settings.get('View', 'DefaultImageViewer')) | ||||
neko259
|
r1122 | |||
neko259
|
r990 | get_notifications(context, request) | ||
neko259
|
r811 | return context | ||
neko259
|
r1323 | |||
def _get_fav_threads(context, settings_manager): | ||||
fav_threads_setting = settings_manager.get_fav_threads() | ||||
if fav_threads_setting: | ||||
fav_threads = Post.objects.filter( | ||||
id__in=fav_threads_setting.keys()).only('url', 'id', 'thread')\ | ||||
.select_related('thread') | ||||
neko259
|
r1327 | |||
context_thread_list = [] | ||||
for post in fav_threads: | ||||
new_replies = post.get_thread().get_replies_newer(fav_threads_setting[str(post.id)]) | ||||
element = dict() | ||||
element['post'] = post | ||||
element['count'] = new_replies.count() | ||||
if element['count'] > 0: | ||||
element['new_post'] = new_replies.first().get_absolute_url() | ||||
context_thread_list.append(element) | ||||
context[CONTEXT_FAV_THREADS] = context_thread_list | ||||