Show More
@@ -1,252 +1,255 b'' | |||||
1 | import boards |
|
1 | import boards | |
2 | from boards.models import Tag, TagAlias, Attachment |
|
2 | from boards.models import Tag, TagAlias, Attachment | |
3 | from boards.models.attachment import AttachmentSticker |
|
3 | from boards.models.attachment import AttachmentSticker | |
4 | from boards.models.user import UserSettings |
|
4 | from boards.models.user import UserSettings | |
5 |
|
5 | |||
6 | MAX_TRIPCODE_COLLISIONS = 50 |
|
6 | MAX_TRIPCODE_COLLISIONS = 50 | |
7 |
|
7 | |||
8 | __author__ = 'neko259' |
|
8 | __author__ = 'neko259' | |
9 |
|
9 | |||
10 | SESSION_SETTING = 'setting' |
|
10 | SESSION_SETTING = 'setting' | |
11 |
|
11 | |||
12 | SETTING_THEME = 'theme' |
|
12 | SETTING_THEME = 'theme' | |
13 | SETTING_FAVORITE_TAGS = 'favorite_tags' |
|
13 | SETTING_FAVORITE_TAGS = 'favorite_tags' | |
14 | SETTING_FAVORITE_THREADS = 'favorite_threads' |
|
14 | SETTING_FAVORITE_THREADS = 'favorite_threads' | |
15 | SETTING_HIDDEN_TAGS = 'hidden_tags' |
|
15 | SETTING_HIDDEN_TAGS = 'hidden_tags' | |
16 | SETTING_USERNAME = 'username' |
|
16 | SETTING_USERNAME = 'username' | |
17 | SETTING_LAST_NOTIFICATION_ID = 'last_notification' |
|
17 | SETTING_LAST_NOTIFICATION_ID = 'last_notification' | |
18 | SETTING_IMAGE_VIEWER = 'image_viewer' |
|
18 | SETTING_IMAGE_VIEWER = 'image_viewer' | |
19 | SETTING_IMAGES = 'images_aliases' |
|
19 | SETTING_IMAGES = 'images_aliases' | |
20 | SETTING_ONLY_FAVORITES = 'only_favorites' |
|
20 | SETTING_ONLY_FAVORITES = 'only_favorites' | |
21 | SETTING_LAST_POSTS = 'last_posts' |
|
21 | SETTING_LAST_POSTS = 'last_posts' | |
22 |
|
22 | |||
23 | DEFAULT_THEME = 'md' |
|
23 | DEFAULT_THEME = 'md' | |
24 |
|
24 | |||
25 |
|
25 | |||
26 | class SettingsManager: |
|
26 | class SettingsManager: | |
27 | """ |
|
27 | """ | |
28 | Base settings manager class. get_setting and set_setting methods should |
|
28 | Base settings manager class. get_setting and set_setting methods should | |
29 | be overriden. |
|
29 | be overriden. | |
30 | """ |
|
30 | """ | |
31 | def __init__(self): |
|
31 | def __init__(self): | |
32 | pass |
|
32 | pass | |
33 |
|
33 | |||
34 | def get_theme(self) -> str: |
|
34 | def get_theme(self) -> str: | |
35 | theme = self.get_setting(SETTING_THEME) |
|
35 | theme = self.get_setting(SETTING_THEME) | |
36 | if not theme: |
|
36 | if not theme: | |
37 | theme = DEFAULT_THEME |
|
37 | theme = DEFAULT_THEME | |
38 | self.set_setting(SETTING_THEME, theme) |
|
38 | self.set_setting(SETTING_THEME, theme) | |
39 |
|
39 | |||
40 | return theme |
|
40 | return theme | |
41 |
|
41 | |||
42 | def set_theme(self, theme): |
|
42 | def set_theme(self, theme): | |
43 | self.set_setting(SETTING_THEME, theme) |
|
43 | self.set_setting(SETTING_THEME, theme) | |
44 |
|
44 | |||
45 | def get_setting(self, setting, default=None): |
|
45 | def get_setting(self, setting, default=None): | |
46 | pass |
|
46 | pass | |
47 |
|
47 | |||
48 | def set_setting(self, setting, value): |
|
48 | def set_setting(self, setting, value): | |
49 | pass |
|
49 | pass | |
50 |
|
50 | |||
51 | def get_fav_tags(self) -> list: |
|
51 | def get_fav_tags(self) -> list: | |
52 | tag_names = self.get_setting(SETTING_FAVORITE_TAGS) |
|
52 | tag_names = self.get_setting(SETTING_FAVORITE_TAGS) | |
53 | tags = [] |
|
53 | tags = [] | |
54 | if tag_names: |
|
54 | if tag_names: | |
55 | tags = list(Tag.objects.filter(aliases__in=TagAlias.objects |
|
55 | tags = list(Tag.objects.filter(aliases__in=TagAlias.objects | |
56 | .filter_localized(parent__aliases__name__in=tag_names)) |
|
56 | .filter_localized(parent__aliases__name__in=tag_names)) | |
57 | .order_by('aliases__name')) |
|
57 | .order_by('aliases__name')) | |
58 | return tags |
|
58 | return tags | |
59 |
|
59 | |||
60 | def add_fav_tag(self, tag): |
|
60 | def add_fav_tag(self, tag): | |
61 | tags = self.get_setting(SETTING_FAVORITE_TAGS) |
|
61 | tags = self.get_setting(SETTING_FAVORITE_TAGS) | |
62 | if not tags: |
|
62 | if not tags: | |
63 | tags = [tag.get_name()] |
|
63 | tags = [tag.get_name()] | |
64 | else: |
|
64 | else: | |
65 | if not tag.get_name() in tags: |
|
65 | if not tag.get_name() in tags: | |
66 | tags.append(tag.get_name()) |
|
66 | tags.append(tag.get_name()) | |
67 |
|
67 | |||
68 | tags.sort() |
|
68 | tags.sort() | |
69 | self.set_setting(SETTING_FAVORITE_TAGS, tags) |
|
69 | self.set_setting(SETTING_FAVORITE_TAGS, tags) | |
70 |
|
70 | |||
71 | def del_fav_tag(self, tag): |
|
71 | def del_fav_tag(self, tag): | |
72 | tags = self.get_setting(SETTING_FAVORITE_TAGS) |
|
72 | tags = self.get_setting(SETTING_FAVORITE_TAGS) | |
73 | if tag.get_name() in tags: |
|
73 | if tag.get_name() in tags: | |
74 | tags.remove(tag.get_name()) |
|
74 | tags.remove(tag.get_name()) | |
75 | self.set_setting(SETTING_FAVORITE_TAGS, tags) |
|
75 | self.set_setting(SETTING_FAVORITE_TAGS, tags) | |
76 |
|
76 | |||
77 | def get_hidden_tags(self) -> list: |
|
77 | def get_hidden_tags(self) -> list: | |
78 | tag_names = self.get_setting(SETTING_HIDDEN_TAGS) |
|
78 | tag_names = self.get_setting(SETTING_HIDDEN_TAGS) | |
79 | tags = [] |
|
79 | tags = [] | |
80 | if tag_names: |
|
80 | if tag_names: | |
81 | tags = list(Tag.objects.filter(aliases__in=TagAlias.objects |
|
81 | tags = list(Tag.objects.filter(aliases__in=TagAlias.objects | |
82 | .filter_localized(parent__aliases__name__in=tag_names)) |
|
82 | .filter_localized(parent__aliases__name__in=tag_names)) | |
83 | .order_by('aliases__name')) |
|
83 | .order_by('aliases__name')) | |
84 |
|
84 | |||
85 | return tags |
|
85 | return tags | |
86 |
|
86 | |||
87 | def add_hidden_tag(self, tag): |
|
87 | def add_hidden_tag(self, tag): | |
88 | tags = self.get_setting(SETTING_HIDDEN_TAGS) |
|
88 | tags = self.get_setting(SETTING_HIDDEN_TAGS) | |
89 | if not tags: |
|
89 | if not tags: | |
90 | tags = [tag.get_name()] |
|
90 | tags = [tag.get_name()] | |
91 | else: |
|
91 | else: | |
92 | if not tag.get_name() in tags: |
|
92 | if not tag.get_name() in tags: | |
93 | tags.append(tag.get_name()) |
|
93 | tags.append(tag.get_name()) | |
94 |
|
94 | |||
95 | tags.sort() |
|
95 | tags.sort() | |
96 | self.set_setting(SETTING_HIDDEN_TAGS, tags) |
|
96 | self.set_setting(SETTING_HIDDEN_TAGS, tags) | |
97 |
|
97 | |||
98 | def del_hidden_tag(self, tag): |
|
98 | def del_hidden_tag(self, tag): | |
99 | tags = self.get_setting(SETTING_HIDDEN_TAGS) |
|
99 | tags = self.get_setting(SETTING_HIDDEN_TAGS) | |
100 | if tag.get_name() in tags: |
|
100 | if tag.get_name() in tags: | |
101 | tags.remove(tag.get_name()) |
|
101 | tags.remove(tag.get_name()) | |
102 | self.set_setting(SETTING_HIDDEN_TAGS, tags) |
|
102 | self.set_setting(SETTING_HIDDEN_TAGS, tags) | |
103 |
|
103 | |||
104 | def add_or_read_fav_thread(self, opening_post): |
|
104 | def add_or_read_fav_thread(self, opening_post): | |
105 | last_post_ids = self.get_setting(SETTING_LAST_POSTS) |
|
105 | last_post_ids = self.get_setting(SETTING_LAST_POSTS) | |
106 | if not last_post_ids: |
|
106 | if not last_post_ids: | |
107 | last_post_ids = [] |
|
107 | last_post_ids = [] | |
108 |
|
108 | |||
109 | self.del_fav_thread(opening_post) |
|
109 | self.del_fav_thread(opening_post) | |
110 |
|
110 | |||
111 | last_post_id = opening_post.get_thread().get_replies().last().id |
|
111 | last_post_id = opening_post.get_thread().get_replies().last().id | |
112 | last_post_ids.append(last_post_id) |
|
112 | last_post_ids.append(last_post_id) | |
113 |
|
113 | |||
114 | self.set_setting(SETTING_LAST_POSTS, last_post_ids) |
|
114 | self.set_setting(SETTING_LAST_POSTS, last_post_ids) | |
115 |
|
115 | |||
116 | def del_fav_thread(self, opening_post): |
|
116 | def del_fav_thread(self, opening_post): | |
117 | last_posts_ids = self.get_setting(SETTING_LAST_POSTS) |
|
117 | last_posts_ids = self.get_setting(SETTING_LAST_POSTS) | |
118 |
|
118 | |||
119 | for post in self.get_last_posts(): |
|
119 | for post in self.get_last_posts(): | |
120 | if post.get_thread() == opening_post.get_thread(): |
|
120 | if post.get_thread() == opening_post.get_thread(): | |
121 | last_posts_ids.remove(post.id) |
|
121 | last_posts_ids.remove(post.id) | |
122 |
|
122 | |||
123 | self.set_setting(SETTING_LAST_POSTS, last_posts_ids) |
|
123 | self.set_setting(SETTING_LAST_POSTS, last_posts_ids) | |
124 |
|
124 | |||
125 | def thread_is_fav(self, opening_post): |
|
125 | def thread_is_fav(self, opening_post): | |
126 | for post in self.get_last_posts(): |
|
126 | for post in self.get_last_posts(): | |
127 | if post.get_thread() == opening_post.get_thread(): |
|
127 | if post.get_thread() == opening_post.get_thread(): | |
128 | return True |
|
128 | return True | |
129 | return False |
|
129 | return False | |
130 |
|
130 | |||
131 | def get_notification_usernames(self): |
|
131 | def get_notification_usernames(self): | |
132 | names = set() |
|
132 | names = set() | |
133 | name_list = self.get_setting(SETTING_USERNAME) |
|
133 | name_list = self.get_setting(SETTING_USERNAME) | |
134 | if name_list is not None: |
|
134 | if name_list is not None: | |
135 | name_list = name_list.strip() |
|
135 | name_list = name_list.strip() | |
136 | if len(name_list) > 0: |
|
136 | if len(name_list) > 0: | |
137 | names = name_list.lower().split(',') |
|
137 | names = name_list.lower().split(',') | |
138 | names = set(name.strip() for name in names) |
|
138 | names = set(name.strip() for name in names) | |
139 | return names |
|
139 | return names | |
140 |
|
140 | |||
141 | def get_attachment_by_alias(self, alias): |
|
141 | def get_attachment_by_alias(self, alias): | |
142 | images = self.get_setting(SETTING_IMAGES) |
|
142 | images = self.get_setting(SETTING_IMAGES) | |
143 | if images and alias in images: |
|
143 | if images and alias in images: | |
144 | try: |
|
144 | try: | |
145 | return Attachment.objects.get(id=images.get(alias)) |
|
145 | return Attachment.objects.get(id=images.get(alias)) | |
146 | except Attachment.DoesNotExist: |
|
146 | except Attachment.DoesNotExist: | |
147 | self.remove_attachment_alias(alias) |
|
147 | self.remove_attachment_alias(alias) | |
148 |
|
148 | |||
149 | def add_attachment_alias(self, alias, attachment): |
|
149 | def add_attachment_alias(self, alias, attachment): | |
150 | images = self.get_setting(SETTING_IMAGES) |
|
150 | images = self.get_setting(SETTING_IMAGES) | |
151 | if images is None: |
|
151 | if images is None: | |
152 | images = dict() |
|
152 | images = dict() | |
153 | images[alias] = attachment.id |
|
153 | images[alias] = attachment.id | |
154 | self.set_setting(SETTING_IMAGES, images) |
|
154 | self.set_setting(SETTING_IMAGES, images) | |
155 |
|
155 | |||
156 | def remove_attachment_alias(self, alias): |
|
156 | def remove_attachment_alias(self, alias): | |
157 | images = self.get_setting(SETTING_IMAGES) |
|
157 | images = self.get_setting(SETTING_IMAGES) | |
158 | del images[alias] |
|
158 | del images[alias] | |
159 | self.set_setting(SETTING_IMAGES, images) |
|
159 | self.set_setting(SETTING_IMAGES, images) | |
160 |
|
160 | |||
161 | def get_stickers(self): |
|
161 | def get_stickers(self): | |
162 | images = self.get_setting(SETTING_IMAGES) |
|
162 | images = self.get_setting(SETTING_IMAGES) | |
163 | stickers = [] |
|
163 | stickers = [] | |
164 | if images: |
|
164 | if images: | |
165 | for key, value in images.items(): |
|
165 | for key, value in images.items(): | |
166 | try: |
|
166 | try: | |
167 | attachment = Attachment.objects.get(id=value) |
|
167 | attachment = Attachment.objects.get(id=value) | |
168 | stickers.append(AttachmentSticker(name=key, attachment=attachment)) |
|
168 | stickers.append(AttachmentSticker(name=key, attachment=attachment)) | |
169 | except Attachment.DoesNotExist: |
|
169 | except Attachment.DoesNotExist: | |
170 | self.remove_attachment_alias(key) |
|
170 | self.remove_attachment_alias(key) | |
171 | return stickers |
|
171 | return stickers | |
172 |
|
172 | |||
173 | def tag_is_fav(self, tag): |
|
173 | def tag_is_fav(self, tag): | |
174 | fav_tag_names = self.get_setting(SETTING_FAVORITE_TAGS) |
|
174 | fav_tag_names = self.get_setting(SETTING_FAVORITE_TAGS) | |
175 | return fav_tag_names is not None and tag.get_name() in fav_tag_names |
|
175 | return fav_tag_names is not None and tag.get_name() in fav_tag_names | |
176 |
|
176 | |||
177 | def tag_is_hidden(self, tag): |
|
177 | def tag_is_hidden(self, tag): | |
178 | hidden_tag_names = self.get_setting(SETTING_HIDDEN_TAGS) |
|
178 | hidden_tag_names = self.get_setting(SETTING_HIDDEN_TAGS) | |
179 | return hidden_tag_names is not None and tag.get_name() in hidden_tag_names |
|
179 | return hidden_tag_names is not None and tag.get_name() in hidden_tag_names | |
180 |
|
180 | |||
181 | def get_last_posts(self): |
|
181 | def get_last_posts(self): | |
182 | post_ids = self.get_setting(SETTING_LAST_POSTS) or [] |
|
182 | post_ids = self.get_setting(SETTING_LAST_POSTS) or [] | |
183 | return list(boards.models.Post.objects.filter(id__in=post_ids).order_by('thread__id')) |
|
183 | return list(boards.models.Post.objects.filter(id__in=post_ids).order_by('thread__id')) | |
184 |
|
184 | |||
185 |
|
185 | |||
186 | class SessionSettingsManager(SettingsManager): |
|
186 | class SessionSettingsManager(SettingsManager): | |
187 | """ |
|
187 | """ | |
188 | Session-based settings manager. All settings are saved to the user's |
|
188 | Session-based settings manager. All settings are saved to the user's | |
189 | session. |
|
189 | session. | |
190 | """ |
|
190 | """ | |
191 | def __init__(self, session): |
|
191 | def __init__(self, session): | |
192 | SettingsManager.__init__(self) |
|
192 | SettingsManager.__init__(self) | |
193 | self.session = session |
|
193 | self.session = session | |
194 |
|
194 | |||
195 | def get_setting(self, setting, default=None): |
|
195 | def get_setting(self, setting, default=None): | |
196 | if setting in self.session: |
|
196 | if setting in self.session: | |
197 | return self.session[setting] |
|
197 | return self.session[setting] | |
198 | else: |
|
198 | else: | |
199 | self.set_setting(setting, default) |
|
199 | self.set_setting(setting, default) | |
200 | return default |
|
200 | return default | |
201 |
|
201 | |||
202 | def set_setting(self, setting, value): |
|
202 | def set_setting(self, setting, value): | |
203 | self.session[setting] = value |
|
203 | self.session[setting] = value | |
204 |
|
204 | |||
205 |
|
205 | |||
206 | class DatabaseSettingsManager(SessionSettingsManager): |
|
206 | class DatabaseSettingsManager(SessionSettingsManager): | |
207 | def __init__(self, session): |
|
207 | def __init__(self, session): | |
208 | super().__init__(session) |
|
208 | super().__init__(session) | |
209 |
|
209 | |||
210 | # First time a user accesses the server, his session is not saved |
|
210 | # First time a user accesses the server, his session is not saved | |
211 | # and does not have the key yet. In order to create the settings object |
|
211 | # and does not have the key yet. In order to create the settings object | |
212 | # we need to save it manually |
|
212 | # we need to save it manually | |
213 | if not session.session_key: |
|
213 | if not session.session_key: | |
214 | session.save() |
|
214 | session.save() | |
215 |
|
215 | |||
216 | self.settings, created = UserSettings.objects.get_or_create(session_key=session.session_key) |
|
216 | self.settings, created = UserSettings.objects.get_or_create(session_key=session.session_key) | |
217 |
|
217 | |||
218 | def add_fav_tag(self, tag): |
|
218 | def add_fav_tag(self, tag): | |
219 | self.settings.fav_tags.add(tag) |
|
219 | self.settings.fav_tags.add(tag) | |
220 |
|
220 | |||
221 | def del_fav_tag(self, tag): |
|
221 | def del_fav_tag(self, tag): | |
222 | self.settings.fav_tags.remove(tag) |
|
222 | self.settings.fav_tags.remove(tag) | |
223 |
|
223 | |||
224 | def get_fav_tags(self) -> list: |
|
224 | def get_fav_tags(self) -> list: | |
225 | return self.settings.fav_tags.filter( |
|
225 | return self.settings.fav_tags.filter( | |
226 | aliases__in=TagAlias.objects.filter_localized())\ |
|
226 | aliases__in=TagAlias.objects.filter_localized())\ | |
227 | .order_by('aliases__name') |
|
227 | .order_by('aliases__name') | |
228 |
|
228 | |||
229 | def get_hidden_tags(self) -> list: |
|
229 | def get_hidden_tags(self) -> list: | |
230 | return self.settings.hidden_tags.all() |
|
230 | return self.settings.hidden_tags.all() | |
231 |
|
231 | |||
232 | def add_hidden_tag(self, tag): |
|
232 | def add_hidden_tag(self, tag): | |
233 | self.settings.hidden_tags.add(tag) |
|
233 | self.settings.hidden_tags.add(tag) | |
234 |
|
234 | |||
235 | def del_hidden_tag(self, tag): |
|
235 | def del_hidden_tag(self, tag): | |
236 | self.settings.hidden_tags.remove(tag) |
|
236 | self.settings.hidden_tags.remove(tag) | |
237 |
|
237 | |||
238 | def tag_is_fav(self, tag): |
|
238 | def tag_is_fav(self, tag): | |
239 | return self.settings.fav_tags.filter(id=tag.id).exists() |
|
239 | return self.settings.fav_tags.filter(id=tag.id).exists() | |
240 |
|
240 | |||
241 | def tag_is_hidden(self, tag): |
|
241 | def tag_is_hidden(self, tag): | |
242 | return self.settings.hidden_tags.filter(id=tag.id).exists() |
|
242 | return self.settings.hidden_tags.filter(id=tag.id).exists() | |
243 |
|
243 | |||
|
244 | def get_user_settings(self): | |||
|
245 | return self.settings | |||
|
246 | ||||
244 |
|
247 | |||
245 | def get_settings_manager(request) -> SettingsManager: |
|
248 | def get_settings_manager(request) -> SettingsManager: | |
246 | """ |
|
249 | """ | |
247 | Get settings manager based on the request object. Currently database-based |
|
250 | Get settings manager based on the request object. Currently database-based | |
248 | settings manager is implemented over the session-based one (settings that |
|
251 | settings manager is implemented over the session-based one (settings that | |
249 | are not connected to the database in any way are stored in session). Pure |
|
252 | are not connected to the database in any way are stored in session). Pure | |
250 | session-based manager is also supported but not used by default. |
|
253 | session-based manager is also supported but not used by default. | |
251 | """ |
|
254 | """ | |
252 | return DatabaseSettingsManager(request.session) |
|
255 | return DatabaseSettingsManager(request.session) |
@@ -1,84 +1,84 b'' | |||||
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 | THEME_CSS = 'css/{}/base_page.css' |
|
9 | THEME_CSS = 'css/{}/base_page.css' | |
10 |
|
10 | |||
11 | CONTEXT_SITE_NAME = 'site_name' |
|
11 | CONTEXT_SITE_NAME = 'site_name' | |
12 | CONTEXT_VERSION = 'version' |
|
12 | CONTEXT_VERSION = 'version' | |
13 | CONTEXT_THEME_CSS = 'theme_css' |
|
13 | CONTEXT_THEME_CSS = 'theme_css' | |
14 | CONTEXT_THEME = 'theme' |
|
14 | CONTEXT_THEME = 'theme' | |
15 | CONTEXT_PPD = 'posts_per_day' |
|
15 | CONTEXT_PPD = 'posts_per_day' | |
16 | CONTEXT_USER = 'user' |
|
16 | CONTEXT_USER = 'user' | |
17 | CONTEXT_NEW_NOTIFICATIONS_COUNT = 'new_notifications_count' |
|
17 | CONTEXT_NEW_NOTIFICATIONS_COUNT = 'new_notifications_count' | |
18 | CONTEXT_USERNAMES = 'usernames' |
|
18 | CONTEXT_USERNAMES = 'usernames' | |
19 | CONTEXT_TAGS_STR = 'tags_str' |
|
19 | CONTEXT_TAGS_STR = 'tags_str' | |
20 | CONTEXT_IMAGE_VIEWER = 'image_viewer' |
|
20 | CONTEXT_IMAGE_VIEWER = 'image_viewer' | |
21 | CONTEXT_HAS_FAV_THREADS = 'has_fav_threads' |
|
21 | CONTEXT_HAS_FAV_THREADS = 'has_fav_threads' | |
22 | CONTEXT_POW_DIFFICULTY = 'pow_difficulty' |
|
22 | CONTEXT_POW_DIFFICULTY = 'pow_difficulty' | |
23 | CONTEXT_NEW_POST_COUNT = 'new_post_count' |
|
23 | CONTEXT_NEW_POST_COUNT = 'new_post_count' | |
24 | CONTEXT_BANNERS = 'banners' |
|
24 | CONTEXT_BANNERS = 'banners' | |
25 | CONTEXT_ONLY_FAVORITES = 'only_favorites' |
|
25 | CONTEXT_ONLY_FAVORITES = 'only_favorites' | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | def get_notifications(context, settings_manager): |
|
28 | def get_notifications(context, settings_manager): | |
29 | usernames = settings_manager.get_notification_usernames() |
|
29 | usernames = settings_manager.get_notification_usernames() | |
30 | fav_tags = settings_manager.get_fav_tags() |
|
30 | fav_tags = settings_manager.get_fav_tags() | |
31 | new_notifications_count = 0 |
|
31 | new_notifications_count = 0 | |
32 | if usernames or fav_tags: |
|
32 | if usernames or fav_tags: | |
33 | last_notification_id = settings_manager.get_setting( |
|
33 | last_notification_id = settings_manager.get_setting( | |
34 | SETTING_LAST_NOTIFICATION_ID) |
|
34 | SETTING_LAST_NOTIFICATION_ID) | |
35 |
|
35 | |||
36 | new_notifications_count = Notification.objects.get_notification_posts( |
|
36 | new_notifications_count = Notification.objects.get_notification_posts( | |
37 |
usernames=usernames, last=last_notification_id, |
|
37 | usernames=usernames, last=last_notification_id, user_settings=settings_manager.get_user_settings()).count() | |
38 | context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count |
|
38 | context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count | |
39 | context[CONTEXT_USERNAMES] = usernames |
|
39 | context[CONTEXT_USERNAMES] = usernames | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | def get_new_post_count(context, settings_manager): |
|
42 | def get_new_post_count(context, settings_manager): | |
43 | last_posts = settings_manager.get_last_posts() |
|
43 | last_posts = settings_manager.get_last_posts() | |
44 | count = Thread.objects.get_new_post_count(last_posts) |
|
44 | count = Thread.objects.get_new_post_count(last_posts) | |
45 | if count > 0: |
|
45 | if count > 0: | |
46 | context[CONTEXT_NEW_POST_COUNT] = '(+{})'.format(count) |
|
46 | context[CONTEXT_NEW_POST_COUNT] = '(+{})'.format(count) | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | def user_and_ui_processor(request): |
|
49 | def user_and_ui_processor(request): | |
50 | context = dict() |
|
50 | context = dict() | |
51 |
|
51 | |||
52 | context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day()) |
|
52 | context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day()) | |
53 |
|
53 | |||
54 | settings_manager = get_settings_manager(request) |
|
54 | settings_manager = get_settings_manager(request) | |
55 | fav_tags = settings_manager.get_fav_tags() |
|
55 | fav_tags = settings_manager.get_fav_tags() | |
56 |
|
56 | |||
57 | context[CONTEXT_TAGS_STR] = Tag.objects.get_tag_url_list(fav_tags) |
|
57 | context[CONTEXT_TAGS_STR] = Tag.objects.get_tag_url_list(fav_tags) | |
58 | theme = settings_manager.get_theme() |
|
58 | theme = settings_manager.get_theme() | |
59 | context[CONTEXT_THEME] = theme |
|
59 | context[CONTEXT_THEME] = theme | |
60 |
|
60 | |||
61 | # TODO Use static here |
|
61 | # TODO Use static here | |
62 | context[CONTEXT_THEME_CSS] = THEME_CSS.format(theme) |
|
62 | context[CONTEXT_THEME_CSS] = THEME_CSS.format(theme) | |
63 |
|
63 | |||
64 | context[CONTEXT_VERSION] = settings.get(SECTION_VERSION, 'Version') |
|
64 | context[CONTEXT_VERSION] = settings.get(SECTION_VERSION, 'Version') | |
65 | context[CONTEXT_SITE_NAME] = settings.get(SECTION_VERSION, 'SiteName') |
|
65 | context[CONTEXT_SITE_NAME] = settings.get(SECTION_VERSION, 'SiteName') | |
66 |
|
66 | |||
67 | if settings.get_bool(SECTION_FORMS, 'LimitFirstPosting'): |
|
67 | if settings.get_bool(SECTION_FORMS, 'LimitFirstPosting'): | |
68 | context[CONTEXT_POW_DIFFICULTY] = settings.get_int(SECTION_FORMS, 'PowDifficulty') |
|
68 | context[CONTEXT_POW_DIFFICULTY] = settings.get_int(SECTION_FORMS, 'PowDifficulty') | |
69 |
|
69 | |||
70 | context[CONTEXT_IMAGE_VIEWER] = settings_manager.get_setting( |
|
70 | context[CONTEXT_IMAGE_VIEWER] = settings_manager.get_setting( | |
71 | SETTING_IMAGE_VIEWER, |
|
71 | SETTING_IMAGE_VIEWER, | |
72 | default=settings.get(SECTION_VIEW, 'DefaultImageViewer')) |
|
72 | default=settings.get(SECTION_VIEW, 'DefaultImageViewer')) | |
73 |
|
73 | |||
74 | context[CONTEXT_HAS_FAV_THREADS] =\ |
|
74 | context[CONTEXT_HAS_FAV_THREADS] =\ | |
75 | len(settings_manager.get_last_posts()) > 0 |
|
75 | len(settings_manager.get_last_posts()) > 0 | |
76 |
|
76 | |||
77 | context[CONTEXT_BANNERS] = Banner.objects.order_by('-id') |
|
77 | context[CONTEXT_BANNERS] = Banner.objects.order_by('-id') | |
78 | context[CONTEXT_ONLY_FAVORITES] = settings_manager.get_setting( |
|
78 | context[CONTEXT_ONLY_FAVORITES] = settings_manager.get_setting( | |
79 | SETTING_ONLY_FAVORITES, default=False) |
|
79 | SETTING_ONLY_FAVORITES, default=False) | |
80 |
|
80 | |||
81 | get_notifications(context, settings_manager) |
|
81 | get_notifications(context, settings_manager) | |
82 | get_new_post_count(context, settings_manager) |
|
82 | get_new_post_count(context, settings_manager) | |
83 |
|
83 | |||
84 | return context |
|
84 | return context |
@@ -1,57 +1,57 b'' | |||||
1 | from django.db import models |
|
1 | from django.db import models | |
2 | from django.db.models import Q |
|
2 | from django.db.models import Q | |
3 | import boards |
|
3 | import boards | |
4 |
|
4 | |||
5 | __author__ = 'neko259' |
|
5 | __author__ = 'neko259' | |
6 |
|
6 | |||
7 | BAN_REASON_AUTO = 'Auto' |
|
7 | BAN_REASON_AUTO = 'Auto' | |
8 | BAN_REASON_MAX_LENGTH = 200 |
|
8 | BAN_REASON_MAX_LENGTH = 200 | |
9 |
|
9 | |||
10 | SESSION_KEY_MAX_LENGTH = 100 |
|
10 | SESSION_KEY_MAX_LENGTH = 100 | |
11 |
|
11 | |||
12 |
|
12 | |||
13 | class Ban(models.Model): |
|
13 | class Ban(models.Model): | |
14 |
|
14 | |||
15 | class Meta: |
|
15 | class Meta: | |
16 | app_label = 'boards' |
|
16 | app_label = 'boards' | |
17 |
|
17 | |||
18 | ip = models.GenericIPAddressField() |
|
18 | ip = models.GenericIPAddressField() | |
19 | reason = models.CharField(default=BAN_REASON_AUTO, |
|
19 | reason = models.CharField(default=BAN_REASON_AUTO, | |
20 | max_length=BAN_REASON_MAX_LENGTH) |
|
20 | max_length=BAN_REASON_MAX_LENGTH) | |
21 | can_read = models.BooleanField(default=True) |
|
21 | can_read = models.BooleanField(default=True) | |
22 |
|
22 | |||
23 | def __str__(self): |
|
23 | def __str__(self): | |
24 | return self.ip |
|
24 | return self.ip | |
25 |
|
25 | |||
26 |
|
26 | |||
27 | class NotificationManager(models.Manager): |
|
27 | class NotificationManager(models.Manager): | |
28 |
def get_notification_posts(self, usernames: list, last: int = None, |
|
28 | def get_notification_posts(self, usernames: list, last: int = None, user_settings=None): | |
29 | lower_names = [username.lower() for username in usernames] |
|
29 | lower_names = [username.lower() for username in usernames] | |
30 | posts = boards.models.post.Post.objects.filter( |
|
30 | posts = boards.models.post.Post.objects.filter( | |
31 | Q(notification__name__in=lower_names) | |
|
31 | Q(notification__name__in=lower_names) | | |
32 |
(Q(thread__tags__in= |
|
32 | (Q(thread__tags__settings_as_fav=user_settings) & Q(opening=True))).distinct() | |
33 | if last is not None: |
|
33 | if last is not None: | |
34 | posts = posts.filter(id__gt=last) |
|
34 | posts = posts.filter(id__gt=last) | |
35 | posts = posts.order_by('-id') |
|
35 | posts = posts.order_by('-id') | |
36 |
|
36 | |||
37 | return posts |
|
37 | return posts | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | class Notification(models.Model): |
|
40 | class Notification(models.Model): | |
41 |
|
41 | |||
42 | class Meta: |
|
42 | class Meta: | |
43 | app_label = 'boards' |
|
43 | app_label = 'boards' | |
44 |
|
44 | |||
45 | objects = NotificationManager() |
|
45 | objects = NotificationManager() | |
46 |
|
46 | |||
47 | post = models.ForeignKey('Post', on_delete=models.CASCADE) |
|
47 | post = models.ForeignKey('Post', on_delete=models.CASCADE) | |
48 | name = models.TextField() |
|
48 | name = models.TextField() | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | class UserSettings(models.Model): |
|
51 | class UserSettings(models.Model): | |
52 | class Meta: |
|
52 | class Meta: | |
53 | app_label = 'boards' |
|
53 | app_label = 'boards' | |
54 |
|
54 | |||
55 | session_key = models.CharField(max_length=SESSION_KEY_MAX_LENGTH, unique=True) |
|
55 | session_key = models.CharField(max_length=SESSION_KEY_MAX_LENGTH, unique=True) | |
56 | fav_tags = models.ManyToManyField('Tag', related_name='settings_as_fav') |
|
56 | fav_tags = models.ManyToManyField('Tag', related_name='settings_as_fav') | |
57 | hidden_tags = models.ManyToManyField('Tag', related_name='settings_as_hidden') |
|
57 | hidden_tags = models.ManyToManyField('Tag', related_name='settings_as_hidden') |
@@ -1,50 +1,48 b'' | |||||
1 | from django.shortcuts import render |
|
1 | from django.shortcuts import render | |
2 |
|
2 | |||
3 | from boards.abstracts.constants import PARAM_PAGE |
|
3 | from boards.abstracts.constants import PARAM_PAGE | |
4 | from boards.abstracts.paginator import get_paginator |
|
4 | from boards.abstracts.paginator import get_paginator | |
5 | from boards.abstracts.settingsmanager import get_settings_manager, \ |
|
5 | from boards.abstracts.settingsmanager import get_settings_manager, \ | |
6 | SETTING_LAST_NOTIFICATION_ID |
|
6 | SETTING_LAST_NOTIFICATION_ID | |
7 | from boards.models.user import Notification |
|
7 | from boards.models.user import Notification | |
8 | from boards.views.base import BaseBoardView |
|
8 | from boards.views.base import BaseBoardView | |
9 |
|
9 | |||
10 | DEFAULT_PAGE = '1' |
|
10 | DEFAULT_PAGE = '1' | |
11 |
|
11 | |||
12 | TEMPLATE = 'boards/notifications.html' |
|
12 | TEMPLATE = 'boards/notifications.html' | |
13 | PARAM_USERNAMES = 'notification_usernames' |
|
13 | PARAM_USERNAMES = 'notification_usernames' | |
14 | RESULTS_PER_PAGE = 10 |
|
14 | RESULTS_PER_PAGE = 10 | |
15 |
|
15 | |||
16 |
|
16 | |||
17 | class NotificationView(BaseBoardView): |
|
17 | class NotificationView(BaseBoardView): | |
18 |
|
18 | |||
19 | def get(self, request, username=None): |
|
19 | def get(self, request, username=None): | |
20 | params = self.get_context_data() |
|
20 | params = self.get_context_data() | |
21 |
|
21 | |||
22 | settings_manager = get_settings_manager(request) |
|
22 | settings_manager = get_settings_manager(request) | |
23 |
|
23 | |||
24 | # If we open our notifications, reset the "new" count |
|
24 | # If we open our notifications, reset the "new" count | |
25 | if username is None: |
|
25 | if username is None: | |
26 | notification_usernames = settings_manager.get_notification_usernames() |
|
26 | notification_usernames = settings_manager.get_notification_usernames() | |
27 | else: |
|
27 | else: | |
28 | notification_usernames = [username] |
|
28 | notification_usernames = [username] | |
29 |
|
29 | |||
30 | fav_tags = settings_manager.get_fav_tags() |
|
|||
31 |
|
||||
32 | posts = Notification.objects.get_notification_posts( |
|
30 | posts = Notification.objects.get_notification_posts( | |
33 |
usernames=notification_usernames, |
|
31 | usernames=notification_usernames, user_settings=settings_manager.get_user_settings()) | |
34 |
|
32 | |||
35 | if username is None: |
|
33 | if username is None: | |
36 | last = posts.first() |
|
34 | last = posts.first() | |
37 | if last is not None: |
|
35 | if last is not None: | |
38 | last_id = last.id |
|
36 | last_id = last.id | |
39 | settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID, |
|
37 | settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID, | |
40 | last_id) |
|
38 | last_id) | |
41 |
|
39 | |||
42 |
|
40 | |||
43 | paginator = get_paginator(posts, RESULTS_PER_PAGE) |
|
41 | paginator = get_paginator(posts, RESULTS_PER_PAGE) | |
44 |
|
42 | |||
45 | page = int(request.GET.get(PARAM_PAGE, DEFAULT_PAGE)) |
|
43 | page = int(request.GET.get(PARAM_PAGE, DEFAULT_PAGE)) | |
46 |
|
44 | |||
47 | params[PARAM_PAGE] = paginator.page(page) |
|
45 | params[PARAM_PAGE] = paginator.page(page) | |
48 | params[PARAM_USERNAMES] = notification_usernames |
|
46 | params[PARAM_USERNAMES] = notification_usernames | |
49 |
|
47 | |||
50 | return render(request, TEMPLATE, params) |
|
48 | return render(request, TEMPLATE, params) |
General Comments 0
You need to be logged in to leave comments.
Login now