##// END OF EJS Templates
Order fav threads in list
neko259 -
r2050:218dce3d default
parent child Browse files
Show More
@@ -1,252 +1,252 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))
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
244
245 def get_settings_manager(request) -> SettingsManager:
245 def get_settings_manager(request) -> SettingsManager:
246 """
246 """
247 Get settings manager based on the request object. Currently database-based
247 Get settings manager based on the request object. Currently database-based
248 settings manager is implemented over the session-based one (settings that
248 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
249 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.
250 session-based manager is also supported but not used by default.
251 """
251 """
252 return DatabaseSettingsManager(request.session)
252 return DatabaseSettingsManager(request.session)
General Comments 0
You need to be logged in to leave comments. Login now