from boards.abstracts.settingsmanager import get_settings_manager, \ SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER, SETTING_ONLY_FAVORITES from boards.models import Banner from boards.models.user import Notification from boards import settings from boards.models import Post, Tag, Thread from boards.settings import SECTION_FORMS, SECTION_VIEW, SECTION_VERSION THEME_CSS = 'css/{}/base_page.css' CONTEXT_SITE_NAME = 'site_name' CONTEXT_VERSION = 'version' CONTEXT_THEME_CSS = 'theme_css' CONTEXT_THEME = 'theme' CONTEXT_PPD = 'posts_per_day' 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_BANNERS = 'banners' CONTEXT_ONLY_FAVORITES = 'only_favorites' def get_notifications(context, settings_manager): usernames = settings_manager.get_notification_usernames() new_notifications_count = 0 if usernames: 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, settings_manager): fav_threads = settings_manager.get_fav_threads() if 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_STR] = Tag.objects.get_tag_url_list(fav_tags) theme = settings_manager.get_theme() context[CONTEXT_THEME] = theme # TODO Use static here context[CONTEXT_THEME_CSS] = THEME_CSS.format(theme) context[CONTEXT_VERSION] = settings.get(SECTION_VERSION, 'Version') context[CONTEXT_SITE_NAME] = settings.get(SECTION_VERSION, 'SiteName') if settings.get_bool(SECTION_FORMS, 'LimitFirstPosting'): context[CONTEXT_POW_DIFFICULTY] = settings.get_int(SECTION_FORMS, 'PowDifficulty') context[CONTEXT_IMAGE_VIEWER] = settings_manager.get_setting( SETTING_IMAGE_VIEWER, default=settings.get(SECTION_VIEW, 'DefaultImageViewer')) context[CONTEXT_HAS_FAV_THREADS] =\ len(settings_manager.get_fav_threads()) > 0 context[CONTEXT_BANNERS] = Banner.objects.order_by('-id') context[CONTEXT_ONLY_FAVORITES] = settings_manager.get_setting( SETTING_ONLY_FAVORITES, default=False) get_notifications(context, settings_manager) get_new_post_count(context, settings_manager) return context