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