Show More
@@ -1,200 +1,203 b'' | |||||
1 | from boards import settings |
|
1 | from boards import settings | |
2 | from boards.models import Tag |
|
2 | from boards.models import Tag | |
3 | from boards.models.thread import FAV_THREAD_NO_UPDATES |
|
3 | from boards.models.thread import FAV_THREAD_NO_UPDATES | |
|
4 | from boards.models.tag import DEFAULT_LOCALE | |||
4 |
|
5 | |||
5 | MAX_TRIPCODE_COLLISIONS = 50 |
|
6 | MAX_TRIPCODE_COLLISIONS = 50 | |
6 |
|
7 | |||
7 | __author__ = 'neko259' |
|
8 | __author__ = 'neko259' | |
8 |
|
9 | |||
9 | SESSION_SETTING = 'setting' |
|
10 | SESSION_SETTING = 'setting' | |
10 |
|
11 | |||
11 | # Remove this, it is not used any more cause there is a user's permission |
|
12 | # Remove this, it is not used any more cause there is a user's permission | |
12 | PERMISSION_MODERATE = 'moderator' |
|
13 | PERMISSION_MODERATE = 'moderator' | |
13 |
|
14 | |||
14 | SETTING_THEME = 'theme' |
|
15 | SETTING_THEME = 'theme' | |
15 | SETTING_FAVORITE_TAGS = 'favorite_tags' |
|
16 | SETTING_FAVORITE_TAGS = 'favorite_tags' | |
16 | SETTING_FAVORITE_THREADS = 'favorite_threads' |
|
17 | SETTING_FAVORITE_THREADS = 'favorite_threads' | |
17 | SETTING_HIDDEN_TAGS = 'hidden_tags' |
|
18 | SETTING_HIDDEN_TAGS = 'hidden_tags' | |
18 | SETTING_PERMISSIONS = 'permissions' |
|
19 | SETTING_PERMISSIONS = 'permissions' | |
19 | SETTING_USERNAME = 'username' |
|
20 | SETTING_USERNAME = 'username' | |
20 | SETTING_LAST_NOTIFICATION_ID = 'last_notification' |
|
21 | SETTING_LAST_NOTIFICATION_ID = 'last_notification' | |
21 | SETTING_IMAGE_VIEWER = 'image_viewer' |
|
22 | SETTING_IMAGE_VIEWER = 'image_viewer' | |
22 | SETTING_TRIPCODE = 'tripcode' |
|
23 | SETTING_TRIPCODE = 'tripcode' | |
23 | SETTING_IMAGES = 'images_aliases' |
|
24 | SETTING_IMAGES = 'images_aliases' | |
24 | SETTING_ONLY_FAVORITES = 'only_favorites' |
|
25 | SETTING_ONLY_FAVORITES = 'only_favorites' | |
25 |
|
26 | |||
26 | DEFAULT_THEME = 'md' |
|
27 | DEFAULT_THEME = 'md' | |
27 |
|
28 | |||
28 |
|
29 | |||
29 | class SettingsManager: |
|
30 | class SettingsManager: | |
30 | """ |
|
31 | """ | |
31 | Base settings manager class. get_setting and set_setting methods should |
|
32 | Base settings manager class. get_setting and set_setting methods should | |
32 | be overriden. |
|
33 | be overriden. | |
33 | """ |
|
34 | """ | |
34 | def __init__(self): |
|
35 | def __init__(self): | |
35 | pass |
|
36 | pass | |
36 |
|
37 | |||
37 | def get_theme(self) -> str: |
|
38 | def get_theme(self) -> str: | |
38 | theme = self.get_setting(SETTING_THEME) |
|
39 | theme = self.get_setting(SETTING_THEME) | |
39 | if not theme: |
|
40 | if not theme: | |
40 | theme = DEFAULT_THEME |
|
41 | theme = DEFAULT_THEME | |
41 | self.set_setting(SETTING_THEME, theme) |
|
42 | self.set_setting(SETTING_THEME, theme) | |
42 |
|
43 | |||
43 | return theme |
|
44 | return theme | |
44 |
|
45 | |||
45 | def set_theme(self, theme): |
|
46 | def set_theme(self, theme): | |
46 | self.set_setting(SETTING_THEME, theme) |
|
47 | self.set_setting(SETTING_THEME, theme) | |
47 |
|
48 | |||
48 | def has_permission(self, permission): |
|
49 | def has_permission(self, permission): | |
49 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
50 | permissions = self.get_setting(SETTING_PERMISSIONS) | |
50 | if permissions: |
|
51 | if permissions: | |
51 | return permission in permissions |
|
52 | return permission in permissions | |
52 | else: |
|
53 | else: | |
53 | return False |
|
54 | return False | |
54 |
|
55 | |||
55 | def get_setting(self, setting, default=None): |
|
56 | def get_setting(self, setting, default=None): | |
56 | pass |
|
57 | pass | |
57 |
|
58 | |||
58 | def set_setting(self, setting, value): |
|
59 | def set_setting(self, setting, value): | |
59 | pass |
|
60 | pass | |
60 |
|
61 | |||
61 | def add_permission(self, permission): |
|
62 | def add_permission(self, permission): | |
62 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
63 | permissions = self.get_setting(SETTING_PERMISSIONS) | |
63 | if not permissions: |
|
64 | if not permissions: | |
64 | permissions = [permission] |
|
65 | permissions = [permission] | |
65 | else: |
|
66 | else: | |
66 | permissions.append(permission) |
|
67 | permissions.append(permission) | |
67 | self.set_setting(SETTING_PERMISSIONS, permissions) |
|
68 | self.set_setting(SETTING_PERMISSIONS, permissions) | |
68 |
|
69 | |||
69 | def del_permission(self, permission): |
|
70 | def del_permission(self, permission): | |
70 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
71 | permissions = self.get_setting(SETTING_PERMISSIONS) | |
71 | if not permissions: |
|
72 | if not permissions: | |
72 | permissions = [] |
|
73 | permissions = [] | |
73 | else: |
|
74 | else: | |
74 | permissions.remove(permission) |
|
75 | permissions.remove(permission) | |
75 | self.set_setting(SETTING_PERMISSIONS, permissions) |
|
76 | self.set_setting(SETTING_PERMISSIONS, permissions) | |
76 |
|
77 | |||
77 | def get_fav_tags(self) -> list: |
|
78 | def get_fav_tags(self) -> list: | |
78 | tag_names = self.get_setting(SETTING_FAVORITE_TAGS) |
|
79 | tag_names = self.get_setting(SETTING_FAVORITE_TAGS) | |
79 | tags = [] |
|
80 | tags = [] | |
80 | if tag_names: |
|
81 | if tag_names: | |
81 |
tags = list(Tag.objects.filter(aliases__name__in=tag_names |
|
82 | tags = list(Tag.objects.filter(aliases__name__in=tag_names, | |
|
83 | aliases__locale=DEFAULT_LOCALE)) | |||
82 | return tags |
|
84 | return tags | |
83 |
|
85 | |||
84 | def add_fav_tag(self, tag): |
|
86 | def add_fav_tag(self, tag): | |
85 | tags = self.get_setting(SETTING_FAVORITE_TAGS) |
|
87 | tags = self.get_setting(SETTING_FAVORITE_TAGS) | |
86 | if not tags: |
|
88 | if not tags: | |
87 | tags = [tag.get_name()] |
|
89 | tags = [tag.get_name()] | |
88 | else: |
|
90 | else: | |
89 | if not tag.get_name() in tags: |
|
91 | if not tag.get_name() in tags: | |
90 | tags.append(tag.get_name()) |
|
92 | tags.append(tag.get_name()) | |
91 |
|
93 | |||
92 | tags.sort() |
|
94 | tags.sort() | |
93 | self.set_setting(SETTING_FAVORITE_TAGS, tags) |
|
95 | self.set_setting(SETTING_FAVORITE_TAGS, tags) | |
94 |
|
96 | |||
95 | def del_fav_tag(self, tag): |
|
97 | def del_fav_tag(self, tag): | |
96 | tags = self.get_setting(SETTING_FAVORITE_TAGS) |
|
98 | tags = self.get_setting(SETTING_FAVORITE_TAGS) | |
97 | if tag.get_name() in tags: |
|
99 | if tag.get_name() in tags: | |
98 | tags.remove(tag.get_name()) |
|
100 | tags.remove(tag.get_name()) | |
99 | self.set_setting(SETTING_FAVORITE_TAGS, tags) |
|
101 | self.set_setting(SETTING_FAVORITE_TAGS, tags) | |
100 |
|
102 | |||
101 | def get_hidden_tags(self) -> list: |
|
103 | def get_hidden_tags(self) -> list: | |
102 | tag_names = self.get_setting(SETTING_HIDDEN_TAGS) |
|
104 | tag_names = self.get_setting(SETTING_HIDDEN_TAGS) | |
103 | tags = [] |
|
105 | tags = [] | |
104 | if tag_names: |
|
106 | if tag_names: | |
105 |
tags = list(Tag.objects.filter(aliases__name__in=tag_names |
|
107 | tags = list(Tag.objects.filter(aliases__name__in=tag_names, | |
|
108 | aliases__locale=DEFAULT_LOCALE)) | |||
106 |
|
109 | |||
107 | return tags |
|
110 | return tags | |
108 |
|
111 | |||
109 | def add_hidden_tag(self, tag): |
|
112 | def add_hidden_tag(self, tag): | |
110 | tags = self.get_setting(SETTING_HIDDEN_TAGS) |
|
113 | tags = self.get_setting(SETTING_HIDDEN_TAGS) | |
111 | if not tags: |
|
114 | if not tags: | |
112 | tags = [tag.get_name()] |
|
115 | tags = [tag.get_name()] | |
113 | else: |
|
116 | else: | |
114 | if not tag.get_name() in tags: |
|
117 | if not tag.get_name() in tags: | |
115 | tags.append(tag.get_name()) |
|
118 | tags.append(tag.get_name()) | |
116 |
|
119 | |||
117 | tags.sort() |
|
120 | tags.sort() | |
118 | self.set_setting(SETTING_HIDDEN_TAGS, tags) |
|
121 | self.set_setting(SETTING_HIDDEN_TAGS, tags) | |
119 |
|
122 | |||
120 | def del_hidden_tag(self, tag): |
|
123 | def del_hidden_tag(self, tag): | |
121 | tags = self.get_setting(SETTING_HIDDEN_TAGS) |
|
124 | tags = self.get_setting(SETTING_HIDDEN_TAGS) | |
122 | if tag.get_name() in tags: |
|
125 | if tag.get_name() in tags: | |
123 | tags.remove(tag.get_name()) |
|
126 | tags.remove(tag.get_name()) | |
124 | self.set_setting(SETTING_HIDDEN_TAGS, tags) |
|
127 | self.set_setting(SETTING_HIDDEN_TAGS, tags) | |
125 |
|
128 | |||
126 | def get_fav_threads(self) -> dict: |
|
129 | def get_fav_threads(self) -> dict: | |
127 | return self.get_setting(SETTING_FAVORITE_THREADS, default=dict()) |
|
130 | return self.get_setting(SETTING_FAVORITE_THREADS, default=dict()) | |
128 |
|
131 | |||
129 | def add_or_read_fav_thread(self, opening_post): |
|
132 | def add_or_read_fav_thread(self, opening_post): | |
130 | threads = self.get_fav_threads() |
|
133 | threads = self.get_fav_threads() | |
131 |
|
134 | |||
132 | max_fav_threads = settings.get_int('View', 'MaxFavoriteThreads') |
|
135 | max_fav_threads = settings.get_int('View', 'MaxFavoriteThreads') | |
133 | if (str(opening_post.id) in threads) or (len(threads) < max_fav_threads): |
|
136 | if (str(opening_post.id) in threads) or (len(threads) < max_fav_threads): | |
134 | thread = opening_post.get_thread() |
|
137 | thread = opening_post.get_thread() | |
135 | # Don't check for new posts if the thread is archived already |
|
138 | # Don't check for new posts if the thread is archived already | |
136 | if thread.is_archived(): |
|
139 | if thread.is_archived(): | |
137 | last_id = FAV_THREAD_NO_UPDATES |
|
140 | last_id = FAV_THREAD_NO_UPDATES | |
138 | else: |
|
141 | else: | |
139 | last_id = thread.get_replies().last().id |
|
142 | last_id = thread.get_replies().last().id | |
140 | threads[str(opening_post.id)] = last_id |
|
143 | threads[str(opening_post.id)] = last_id | |
141 | self.set_setting(SETTING_FAVORITE_THREADS, threads) |
|
144 | self.set_setting(SETTING_FAVORITE_THREADS, threads) | |
142 |
|
145 | |||
143 | def del_fav_thread(self, opening_post): |
|
146 | def del_fav_thread(self, opening_post): | |
144 | threads = self.get_fav_threads() |
|
147 | threads = self.get_fav_threads() | |
145 | if self.thread_is_fav(opening_post): |
|
148 | if self.thread_is_fav(opening_post): | |
146 | del threads[str(opening_post.id)] |
|
149 | del threads[str(opening_post.id)] | |
147 | self.set_setting(SETTING_FAVORITE_THREADS, threads) |
|
150 | self.set_setting(SETTING_FAVORITE_THREADS, threads) | |
148 |
|
151 | |||
149 | def thread_is_fav(self, opening_post): |
|
152 | def thread_is_fav(self, opening_post): | |
150 | return str(opening_post.id) in self.get_fav_threads() |
|
153 | return str(opening_post.id) in self.get_fav_threads() | |
151 |
|
154 | |||
152 | def get_notification_usernames(self): |
|
155 | def get_notification_usernames(self): | |
153 | names = set() |
|
156 | names = set() | |
154 | name_list = self.get_setting(SETTING_USERNAME) |
|
157 | name_list = self.get_setting(SETTING_USERNAME) | |
155 | if name_list is not None: |
|
158 | if name_list is not None: | |
156 | name_list = name_list.strip() |
|
159 | name_list = name_list.strip() | |
157 | if len(name_list) > 0: |
|
160 | if len(name_list) > 0: | |
158 | names = name_list.lower().split(',') |
|
161 | names = name_list.lower().split(',') | |
159 | names = set(name.strip() for name in names) |
|
162 | names = set(name.strip() for name in names) | |
160 | return names |
|
163 | return names | |
161 |
|
164 | |||
162 | def get_image_by_alias(self, alias): |
|
165 | def get_image_by_alias(self, alias): | |
163 | images = self.get_setting(SETTING_IMAGES) |
|
166 | images = self.get_setting(SETTING_IMAGES) | |
164 | if images is not None and len(images) > 0: |
|
167 | if images is not None and len(images) > 0: | |
165 | return images.get(alias) |
|
168 | return images.get(alias) | |
166 |
|
169 | |||
167 | def add_image_alias(self, alias, image): |
|
170 | def add_image_alias(self, alias, image): | |
168 | images = self.get_setting(SETTING_IMAGES) |
|
171 | images = self.get_setting(SETTING_IMAGES) | |
169 | if images is None: |
|
172 | if images is None: | |
170 | images = dict() |
|
173 | images = dict() | |
171 | images.put(alias, image) |
|
174 | images.put(alias, image) | |
172 |
|
175 | |||
173 |
|
176 | |||
174 | class SessionSettingsManager(SettingsManager): |
|
177 | class SessionSettingsManager(SettingsManager): | |
175 | """ |
|
178 | """ | |
176 | Session-based settings manager. All settings are saved to the user's |
|
179 | Session-based settings manager. All settings are saved to the user's | |
177 | session. |
|
180 | session. | |
178 | """ |
|
181 | """ | |
179 | def __init__(self, session): |
|
182 | def __init__(self, session): | |
180 | SettingsManager.__init__(self) |
|
183 | SettingsManager.__init__(self) | |
181 | self.session = session |
|
184 | self.session = session | |
182 |
|
185 | |||
183 | def get_setting(self, setting, default=None): |
|
186 | def get_setting(self, setting, default=None): | |
184 | if setting in self.session: |
|
187 | if setting in self.session: | |
185 | return self.session[setting] |
|
188 | return self.session[setting] | |
186 | else: |
|
189 | else: | |
187 | self.set_setting(setting, default) |
|
190 | self.set_setting(setting, default) | |
188 | return default |
|
191 | return default | |
189 |
|
192 | |||
190 | def set_setting(self, setting, value): |
|
193 | def set_setting(self, setting, value): | |
191 | self.session[setting] = value |
|
194 | self.session[setting] = value | |
192 |
|
195 | |||
193 |
|
196 | |||
194 | def get_settings_manager(request) -> SettingsManager: |
|
197 | def get_settings_manager(request) -> SettingsManager: | |
195 | """ |
|
198 | """ | |
196 | Get settings manager based on the request object. Currently only |
|
199 | Get settings manager based on the request object. Currently only | |
197 | session-based manager is supported. In the future, cookie-based or |
|
200 | session-based manager is supported. In the future, cookie-based or | |
198 | database-based managers could be implemented. |
|
201 | database-based managers could be implemented. | |
199 | """ |
|
202 | """ | |
200 | return SessionSettingsManager(request.session) |
|
203 | return SessionSettingsManager(request.session) |
General Comments 0
You need to be logged in to leave comments.
Login now