##// END OF EJS Templates
Don't load fav tags twice in a context processor
neko259 -
r2057:34e04840 default
parent child Browse files
Show More
@@ -1,84 +1,90
1 from boards.abstracts.settingsmanager import get_settings_manager, \
1 from boards.abstracts.settingsmanager import get_settings_manager, \
2 SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER, SETTING_ONLY_FAVORITES
2 SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER, SETTING_ONLY_FAVORITES
3 from boards.models import Banner
3 from boards.models import Banner
4 from boards.models.user import Notification
4 from boards.models.user import Notification
5 from boards import settings
5 from boards import settings
6 from boards.models import Post, Tag, Thread
6 from boards.models import Post, Tag, Thread
7 from boards.settings import SECTION_FORMS, SECTION_VIEW, SECTION_VERSION
7 from boards.settings import SECTION_FORMS, SECTION_VIEW, SECTION_VERSION
8
8
9 PATTERN_NEW_POSTS_COUNT = '(+{})'
10
9 THEME_CSS = 'css/{}/base_page.css'
11 THEME_CSS = 'css/{}/base_page.css'
10
12
11 CONTEXT_SITE_NAME = 'site_name'
13 CONTEXT_SITE_NAME = 'site_name'
12 CONTEXT_VERSION = 'version'
14 CONTEXT_VERSION = 'version'
13 CONTEXT_THEME_CSS = 'theme_css'
15 CONTEXT_THEME_CSS = 'theme_css'
14 CONTEXT_THEME = 'theme'
16 CONTEXT_THEME = 'theme'
15 CONTEXT_PPD = 'posts_per_day'
17 CONTEXT_PPD = 'posts_per_day'
16 CONTEXT_USER = 'user'
18 CONTEXT_USER = 'user'
17 CONTEXT_NEW_NOTIFICATIONS_COUNT = 'new_notifications_count'
19 CONTEXT_NEW_NOTIFICATIONS_COUNT = 'new_notifications_count'
18 CONTEXT_USERNAMES = 'usernames'
20 CONTEXT_USERNAMES = 'usernames'
19 CONTEXT_TAGS_STR = 'tags_str'
21 CONTEXT_TAGS_STR = 'tags_str'
20 CONTEXT_IMAGE_VIEWER = 'image_viewer'
22 CONTEXT_IMAGE_VIEWER = 'image_viewer'
21 CONTEXT_HAS_FAV_THREADS = 'has_fav_threads'
23 CONTEXT_HAS_FAV_THREADS = 'has_fav_threads'
22 CONTEXT_POW_DIFFICULTY = 'pow_difficulty'
24 CONTEXT_POW_DIFFICULTY = 'pow_difficulty'
23 CONTEXT_NEW_POST_COUNT = 'new_post_count'
25 CONTEXT_NEW_POST_COUNT = 'new_post_count'
24 CONTEXT_BANNERS = 'banners'
26 CONTEXT_BANNERS = 'banners'
25 CONTEXT_ONLY_FAVORITES = 'only_favorites'
27 CONTEXT_ONLY_FAVORITES = 'only_favorites'
26
28
27
29
28 def get_notifications(context, settings_manager):
30 def get_notifications(context, settings_manager, fav_tags):
29 usernames = settings_manager.get_notification_usernames()
31 usernames = settings_manager.get_notification_usernames()
30 fav_tags = settings_manager.get_fav_tags()
32
31 new_notifications_count = 0
33 new_notifications_count = 0
34
32 if usernames or fav_tags:
35 if usernames or fav_tags:
33 last_notification_id = settings_manager.get_setting(
36 last_notification_id = settings_manager.get_setting(
34 SETTING_LAST_NOTIFICATION_ID)
37 SETTING_LAST_NOTIFICATION_ID)
35
38
36 new_notifications_count = Notification.objects.get_notification_posts(
39 new_notifications_count = Notification.objects.get_notification_posts(
37 usernames=usernames, last=last_notification_id, user_settings=settings_manager.get_user_settings()).count()
40 usernames=usernames,
41 last=last_notification_id,
42 user_settings=settings_manager.get_user_settings()).count()
43
38 context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count
44 context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count
39 context[CONTEXT_USERNAMES] = usernames
45 context[CONTEXT_USERNAMES] = usernames
40
46
41
47
42 def get_new_post_count(context, settings_manager):
48 def get_new_post_count(context, settings_manager):
43 last_posts = settings_manager.get_last_posts()
49 last_posts = settings_manager.get_last_posts()
44 count = Thread.objects.get_new_post_count(last_posts)
50 count = Thread.objects.get_new_post_count(last_posts)
45 if count > 0:
51 if count > 0:
46 context[CONTEXT_NEW_POST_COUNT] = '(+{})'.format(count)
52 context[CONTEXT_NEW_POST_COUNT] = PATTERN_NEW_POSTS_COUNT.format(count)
47
53
48
54
49 def user_and_ui_processor(request):
55 def user_and_ui_processor(request):
50 context = dict()
56 context = dict()
51
57
52 context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day())
58 context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day())
53
59
54 settings_manager = get_settings_manager(request)
60 settings_manager = get_settings_manager(request)
55 fav_tags = settings_manager.get_fav_tags()
61 fav_tags = settings_manager.get_fav_tags()
56
62
57 context[CONTEXT_TAGS_STR] = Tag.objects.get_tag_url_list(fav_tags)
63 context[CONTEXT_TAGS_STR] = Tag.objects.get_tag_url_list(fav_tags)
58 theme = settings_manager.get_theme()
64 theme = settings_manager.get_theme()
59 context[CONTEXT_THEME] = theme
65 context[CONTEXT_THEME] = theme
60
66
61 # TODO Use static here
67 # TODO Use static here
62 context[CONTEXT_THEME_CSS] = THEME_CSS.format(theme)
68 context[CONTEXT_THEME_CSS] = THEME_CSS.format(theme)
63
69
64 context[CONTEXT_VERSION] = settings.get(SECTION_VERSION, 'Version')
70 context[CONTEXT_VERSION] = settings.get(SECTION_VERSION, 'Version')
65 context[CONTEXT_SITE_NAME] = settings.get(SECTION_VERSION, 'SiteName')
71 context[CONTEXT_SITE_NAME] = settings.get(SECTION_VERSION, 'SiteName')
66
72
67 if settings.get_bool(SECTION_FORMS, 'LimitFirstPosting'):
73 if settings.get_bool(SECTION_FORMS, 'LimitFirstPosting'):
68 context[CONTEXT_POW_DIFFICULTY] = settings.get_int(SECTION_FORMS, 'PowDifficulty')
74 context[CONTEXT_POW_DIFFICULTY] = settings.get_int(SECTION_FORMS, 'PowDifficulty')
69
75
70 context[CONTEXT_IMAGE_VIEWER] = settings_manager.get_setting(
76 context[CONTEXT_IMAGE_VIEWER] = settings_manager.get_setting(
71 SETTING_IMAGE_VIEWER,
77 SETTING_IMAGE_VIEWER,
72 default=settings.get(SECTION_VIEW, 'DefaultImageViewer'))
78 default=settings.get(SECTION_VIEW, 'DefaultImageViewer'))
73
79
74 context[CONTEXT_HAS_FAV_THREADS] =\
80 context[CONTEXT_HAS_FAV_THREADS] =\
75 len(settings_manager.get_last_posts()) > 0
81 len(settings_manager.get_last_posts()) > 0
76
82
77 context[CONTEXT_BANNERS] = Banner.objects.order_by('-id')
83 context[CONTEXT_BANNERS] = Banner.objects.order_by('-id')
78 context[CONTEXT_ONLY_FAVORITES] = settings_manager.get_setting(
84 context[CONTEXT_ONLY_FAVORITES] = settings_manager.get_setting(
79 SETTING_ONLY_FAVORITES, default=False)
85 SETTING_ONLY_FAVORITES, default=False)
80
86
81 get_notifications(context, settings_manager)
87 get_notifications(context, settings_manager, fav_tags)
82 get_new_post_count(context, settings_manager)
88 get_new_post_count(context, settings_manager)
83
89
84 return context
90 return context
General Comments 0
You need to be logged in to leave comments. Login now