context_processors.py
83 lines
| 3.0 KiB
| text/x-python
|
PythonLexer
/ boards / context_processors.py
neko259
|
r990 | from boards.abstracts.settingsmanager import get_settings_manager, \ | ||
neko259
|
r1691 | SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER, SETTING_ONLY_FAVORITES | ||
neko259
|
r1760 | from boards.models import Banner | ||
neko259
|
r990 | from boards.models.user import Notification | ||
neko259
|
r1429 | from boards import settings | ||
neko259
|
r1455 | from boards.models import Post, Tag, Thread | ||
neko259
|
r2011 | from boards.settings import SECTION_FORMS, SECTION_VIEW, SECTION_VERSION | ||
THEME_CSS = 'css/{}/base_page.css' | ||||
neko259
|
r690 | |||
neko259
|
r721 | CONTEXT_SITE_NAME = 'site_name' | ||
CONTEXT_VERSION = 'version' | ||||
CONTEXT_THEME_CSS = 'theme_css' | ||||
CONTEXT_THEME = 'theme' | ||||
CONTEXT_PPD = 'posts_per_day' | ||||
CONTEXT_USER = 'user' | ||||
neko259
|
r990 | CONTEXT_NEW_NOTIFICATIONS_COUNT = 'new_notifications_count' | ||
neko259
|
r1429 | CONTEXT_USERNAMES = 'usernames' | ||
neko259
|
r1027 | CONTEXT_TAGS_STR = 'tags_str' | ||
neko259
|
r1122 | CONTEXT_IMAGE_VIEWER = 'image_viewer' | ||
neko259
|
r1340 | CONTEXT_HAS_FAV_THREADS = 'has_fav_threads' | ||
neko259
|
r1428 | CONTEXT_POW_DIFFICULTY = 'pow_difficulty' | ||
neko259
|
r1455 | CONTEXT_NEW_POST_COUNT = 'new_post_count' | ||
neko259
|
r1646 | CONTEXT_BANNERS = 'banners' | ||
neko259
|
r1691 | CONTEXT_ONLY_FAVORITES = 'only_favorites' | ||
neko259
|
r811 | |||
neko259
|
r690 | |||
neko259
|
r1680 | def get_notifications(context, settings_manager): | ||
neko259
|
r1429 | usernames = settings_manager.get_notification_usernames() | ||
neko259
|
r990 | new_notifications_count = 0 | ||
neko259
|
r1680 | if usernames: | ||
neko259
|
r990 | last_notification_id = settings_manager.get_setting( | ||
SETTING_LAST_NOTIFICATION_ID) | ||||
neko259
|
r994 | |||
new_notifications_count = Notification.objects.get_notification_posts( | ||||
neko259
|
r1455 | usernames=usernames, last=last_notification_id).only('id').count() | ||
neko259
|
r990 | context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count | ||
neko259
|
r1429 | context[CONTEXT_USERNAMES] = usernames | ||
neko259
|
r990 | |||
neko259
|
r1680 | def get_new_post_count(context, settings_manager): | ||
neko259
|
r2044 | last_posts = settings_manager.get_last_posts() | ||
count = Thread.objects.get_new_post_count(last_posts) | ||||
if count > 0: | ||||
context[CONTEXT_NEW_POST_COUNT] = '(+{})'.format(count) | ||||
neko259
|
r1455 | |||
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() | ||
neko259
|
r1323 | |||
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 | ||
neko259
|
r1680 | |||
# TODO Use static here | ||||
neko259
|
r2011 | context[CONTEXT_THEME_CSS] = THEME_CSS.format(theme) | ||
neko259
|
r690 | |||
neko259
|
r2011 | context[CONTEXT_VERSION] = settings.get(SECTION_VERSION, 'Version') | ||
context[CONTEXT_SITE_NAME] = settings.get(SECTION_VERSION, 'SiteName') | ||||
neko259
|
r690 | |||
neko259
|
r2011 | if settings.get_bool(SECTION_FORMS, 'LimitFirstPosting'): | ||
neko259
|
r2004 | context[CONTEXT_POW_DIFFICULTY] = settings.get_int(SECTION_FORMS, 'PowDifficulty') | ||
neko259
|
r1428 | |||
neko259
|
r1122 | context[CONTEXT_IMAGE_VIEWER] = settings_manager.get_setting( | ||
neko259
|
r1153 | SETTING_IMAGE_VIEWER, | ||
neko259
|
r2011 | default=settings.get(SECTION_VIEW, 'DefaultImageViewer')) | ||
neko259
|
r1122 | |||
neko259
|
r1340 | context[CONTEXT_HAS_FAV_THREADS] =\ | ||
neko259
|
r2044 | len(settings_manager.get_last_posts()) > 0 | ||
neko259
|
r1340 | |||
neko259
|
r1646 | context[CONTEXT_BANNERS] = Banner.objects.order_by('-id') | ||
neko259
|
r1691 | context[CONTEXT_ONLY_FAVORITES] = settings_manager.get_setting( | ||
neko259
|
r2011 | SETTING_ONLY_FAVORITES, default=False) | ||
neko259
|
r1478 | |||
neko259
|
r1680 | get_notifications(context, settings_manager) | ||
get_new_post_count(context, settings_manager) | ||||
neko259
|
r990 | |||
neko259
|
r811 | return context | ||