from boards.abstracts.settingsmanager import get_settings_manager, \ SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER from boards.models.user import Notification __author__ = 'neko259' import neboard from boards import settings from boards.models import Post, Tag, Thread CONTEXT_SITE_NAME = 'site_name' CONTEXT_VERSION = 'version' CONTEXT_THEME_CSS = 'theme_css' CONTEXT_THEME = 'theme' CONTEXT_PPD = 'posts_per_day' CONTEXT_TAGS = 'tags' CONTEXT_USER = 'user' CONTEXT_NEW_NOTIFICATIONS_COUNT = 'new_notifications_count' CONTEXT_USERNAMES = 'usernames' CONTEXT_TAGS_STR = 'tags_str' CONTEXT_IMAGE_VIEWER = 'image_viewer' CONTEXT_HAS_FAV_THREADS = 'has_fav_threads' CONTEXT_POW_DIFFICULTY = 'pow_difficulty' CONTEXT_NEW_POST_COUNT = 'new_post_count' CONTEXT_SEARCH_ENABLED = 'search_enabled' def get_notifications(context, request): settings_manager = get_settings_manager(request) usernames = settings_manager.get_notification_usernames() new_notifications_count = 0 if usernames is not None: last_notification_id = settings_manager.get_setting( SETTING_LAST_NOTIFICATION_ID) new_notifications_count = Notification.objects.get_notification_posts( usernames=usernames, last=last_notification_id).only('id').count() context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count context[CONTEXT_USERNAMES] = usernames def get_new_post_count(context, request): settings_manager = get_settings_manager(request) fav_threads = settings_manager.get_fav_threads() fav_thread_ops = Post.objects.filter(id__in=fav_threads.keys()) \ .order_by('-pub_time').only('thread_id', 'pub_time') ops = [{'op': op, 'last_id': fav_threads[str(op.id)]} for op in fav_thread_ops] count = Thread.objects.get_new_post_count(ops) if count > 0: context[CONTEXT_NEW_POST_COUNT] = '(+{})'.format(count) def user_and_ui_processor(request): context = dict() context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day()) settings_manager = get_settings_manager(request) fav_tags = settings_manager.get_fav_tags() context[CONTEXT_TAGS] = fav_tags context[CONTEXT_TAGS_STR] = Tag.objects.get_tag_url_list(fav_tags) theme = settings_manager.get_theme() context[CONTEXT_THEME] = theme context[CONTEXT_THEME_CSS] = 'css/' + theme + '/base_page.css' context[CONTEXT_VERSION] = settings.get('Version', 'Version') context[CONTEXT_SITE_NAME] = settings.get('Version', 'SiteName') if settings.get_bool('Forms', 'LimitPostingSpeed') and not settings_manager.get_setting('confirmed_user'): context[CONTEXT_POW_DIFFICULTY] = settings.get_int('Forms', 'PowDifficulty') context[CONTEXT_IMAGE_VIEWER] = settings_manager.get_setting( SETTING_IMAGE_VIEWER, default=settings.get('View', 'DefaultImageViewer')) context[CONTEXT_HAS_FAV_THREADS] =\ len(settings_manager.get_fav_threads()) > 0 context[CONTEXT_SEARCH_ENABLED] = 'haystack' in neboard.settings.INSTALLED_APPS get_notifications(context, request) get_new_post_count(context, request) return context