Show More
@@ -0,0 +1,22 b'' | |||||
|
1 | # Generated by Django 2.0 on 2017-12-17 09:30 | |||
|
2 | ||||
|
3 | from django.db import migrations, models | |||
|
4 | ||||
|
5 | ||||
|
6 | class Migration(migrations.Migration): | |||
|
7 | ||||
|
8 | dependencies = [ | |||
|
9 | ('boards', '0068_auto_20171214_0833'), | |||
|
10 | ] | |||
|
11 | ||||
|
12 | operations = [ | |||
|
13 | migrations.CreateModel( | |||
|
14 | name='UserSettings', | |||
|
15 | fields=[ | |||
|
16 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | |||
|
17 | ('session_key', models.CharField(max_length=100, unique=True)), | |||
|
18 | ('fav_tags', models.ManyToManyField(related_name='settings_as_fav', to='boards.Tag')), | |||
|
19 | ('hidden_tags', models.ManyToManyField(related_name='settings_as_hidden', to='boards.Tag')), | |||
|
20 | ], | |||
|
21 | ), | |||
|
22 | ] |
@@ -2,7 +2,7 b' from boards import settings' | |||||
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.thread import FAV_THREAD_NO_UPDATES |
|
4 | from boards.models.thread import FAV_THREAD_NO_UPDATES | |
5 |
from boards.models. |
|
5 | from boards.models.user import UserSettings | |
6 | from boards.settings import SECTION_VIEW |
|
6 | from boards.settings import SECTION_VIEW | |
7 |
|
7 | |||
8 | MAX_TRIPCODE_COLLISIONS = 50 |
|
8 | MAX_TRIPCODE_COLLISIONS = 50 | |
@@ -11,18 +11,13 b' MAX_TRIPCODE_COLLISIONS = 50' | |||||
11 |
|
11 | |||
12 | SESSION_SETTING = 'setting' |
|
12 | SESSION_SETTING = 'setting' | |
13 |
|
13 | |||
14 | # Remove this, it is not used any more cause there is a user's permission |
|
|||
15 | PERMISSION_MODERATE = 'moderator' |
|
|||
16 |
|
||||
17 | SETTING_THEME = 'theme' |
|
14 | SETTING_THEME = 'theme' | |
18 | SETTING_FAVORITE_TAGS = 'favorite_tags' |
|
15 | SETTING_FAVORITE_TAGS = 'favorite_tags' | |
19 | SETTING_FAVORITE_THREADS = 'favorite_threads' |
|
16 | SETTING_FAVORITE_THREADS = 'favorite_threads' | |
20 | SETTING_HIDDEN_TAGS = 'hidden_tags' |
|
17 | SETTING_HIDDEN_TAGS = 'hidden_tags' | |
21 | SETTING_PERMISSIONS = 'permissions' |
|
|||
22 | SETTING_USERNAME = 'username' |
|
18 | SETTING_USERNAME = 'username' | |
23 | SETTING_LAST_NOTIFICATION_ID = 'last_notification' |
|
19 | SETTING_LAST_NOTIFICATION_ID = 'last_notification' | |
24 | SETTING_IMAGE_VIEWER = 'image_viewer' |
|
20 | SETTING_IMAGE_VIEWER = 'image_viewer' | |
25 | SETTING_TRIPCODE = 'tripcode' |
|
|||
26 | SETTING_IMAGES = 'images_aliases' |
|
21 | SETTING_IMAGES = 'images_aliases' | |
27 | SETTING_ONLY_FAVORITES = 'only_favorites' |
|
22 | SETTING_ONLY_FAVORITES = 'only_favorites' | |
28 |
|
23 | |||
@@ -48,35 +43,12 b' class SettingsManager:' | |||||
48 | def set_theme(self, theme): |
|
43 | def set_theme(self, theme): | |
49 | self.set_setting(SETTING_THEME, theme) |
|
44 | self.set_setting(SETTING_THEME, theme) | |
50 |
|
45 | |||
51 | def has_permission(self, permission): |
|
|||
52 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
|||
53 | if permissions: |
|
|||
54 | return permission in permissions |
|
|||
55 | else: |
|
|||
56 | return False |
|
|||
57 |
|
||||
58 | def get_setting(self, setting, default=None): |
|
46 | def get_setting(self, setting, default=None): | |
59 | pass |
|
47 | pass | |
60 |
|
48 | |||
61 | def set_setting(self, setting, value): |
|
49 | def set_setting(self, setting, value): | |
62 | pass |
|
50 | pass | |
63 |
|
51 | |||
64 | def add_permission(self, permission): |
|
|||
65 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
|||
66 | if not permissions: |
|
|||
67 | permissions = [permission] |
|
|||
68 | else: |
|
|||
69 | permissions.append(permission) |
|
|||
70 | self.set_setting(SETTING_PERMISSIONS, permissions) |
|
|||
71 |
|
||||
72 | def del_permission(self, permission): |
|
|||
73 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
|||
74 | if not permissions: |
|
|||
75 | permissions = [] |
|
|||
76 | else: |
|
|||
77 | permissions.remove(permission) |
|
|||
78 | self.set_setting(SETTING_PERMISSIONS, permissions) |
|
|||
79 |
|
||||
80 | def get_fav_tags(self) -> list: |
|
52 | def get_fav_tags(self) -> list: | |
81 | tag_names = self.get_setting(SETTING_FAVORITE_TAGS) |
|
53 | tag_names = self.get_setting(SETTING_FAVORITE_TAGS) | |
82 | tags = [] |
|
54 | tags = [] | |
@@ -198,6 +170,14 b' class SettingsManager:' | |||||
198 | self.remove_attachment_alias(key) |
|
170 | self.remove_attachment_alias(key) | |
199 | return stickers |
|
171 | return stickers | |
200 |
|
172 | |||
|
173 | def tag_is_fav(self, tag): | |||
|
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 | |||
|
176 | ||||
|
177 | def tag_is_hidden(self, tag): | |||
|
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 | |||
|
180 | ||||
201 |
|
181 | |||
202 | class SessionSettingsManager(SettingsManager): |
|
182 | class SessionSettingsManager(SettingsManager): | |
203 | """ |
|
183 | """ | |
@@ -219,10 +199,42 b' class SessionSettingsManager(SettingsMan' | |||||
219 | self.session[setting] = value |
|
199 | self.session[setting] = value | |
220 |
|
200 | |||
221 |
|
201 | |||
|
202 | class DatabaseSettingsManager(SessionSettingsManager): | |||
|
203 | def __init__(self, session): | |||
|
204 | super().__init__(session) | |||
|
205 | self.settings, created = UserSettings.objects.get_or_create(session_key=session.session_key) | |||
|
206 | ||||
|
207 | def add_fav_tag(self, tag): | |||
|
208 | self.settings.fav_tags.add(tag) | |||
|
209 | ||||
|
210 | def del_fav_tag(self, tag): | |||
|
211 | self.settings.fav_tags.remove(tag) | |||
|
212 | ||||
|
213 | def get_fav_tags(self) -> list: | |||
|
214 | return self.settings.fav_tags.filter( | |||
|
215 | aliases__in=TagAlias.objects.filter_localized())\ | |||
|
216 | .order_by('aliases__name') | |||
|
217 | ||||
|
218 | def get_hidden_tags(self) -> list: | |||
|
219 | return self.settings.hidden_tags.all() | |||
|
220 | ||||
|
221 | def add_hidden_tag(self, tag): | |||
|
222 | self.settings.hidden_tags.add(tag) | |||
|
223 | ||||
|
224 | def del_hidden_tag(self, tag): | |||
|
225 | self.settings.hidden_tags.remove(tag) | |||
|
226 | ||||
|
227 | def tag_is_fav(self, tag): | |||
|
228 | return self.settings.fav_tags.filter(id=tag.id).exists() | |||
|
229 | ||||
|
230 | def tag_is_hidden(self, tag): | |||
|
231 | return self.settings.hidden_tags.filter(id=tag.id).exists() | |||
|
232 | ||||
|
233 | ||||
222 | def get_settings_manager(request) -> SettingsManager: |
|
234 | def get_settings_manager(request) -> SettingsManager: | |
223 | """ |
|
235 | """ | |
224 | Get settings manager based on the request object. Currently only |
|
236 | Get settings manager based on the request object. Currently only | |
225 | session-based manager is supported. In the future, cookie-based or |
|
237 | session-based manager is supported. In the future, cookie-based or | |
226 | database-based managers could be implemented. |
|
238 | database-based managers could be implemented. | |
227 | """ |
|
239 | """ | |
228 |
return |
|
240 | return DatabaseSettingsManager(request.session) |
@@ -6,6 +6,8 b' import boards' | |||||
6 | BAN_REASON_AUTO = 'Auto' |
|
6 | BAN_REASON_AUTO = 'Auto' | |
7 | BAN_REASON_MAX_LENGTH = 200 |
|
7 | BAN_REASON_MAX_LENGTH = 200 | |
8 |
|
8 | |||
|
9 | SESSION_KEY_MAX_LENGTH = 100 | |||
|
10 | ||||
9 |
|
11 | |||
10 | class Ban(models.Model): |
|
12 | class Ban(models.Model): | |
11 |
|
13 | |||
@@ -43,3 +45,11 b' class Notification(models.Model):' | |||||
43 | post = models.ForeignKey('Post', on_delete=models.CASCADE) |
|
45 | post = models.ForeignKey('Post', on_delete=models.CASCADE) | |
44 | name = models.TextField() |
|
46 | name = models.TextField() | |
45 |
|
47 | |||
|
48 | ||||
|
49 | class UserSettings(models.Model): | |||
|
50 | class Meta: | |||
|
51 | app_label = 'boards' | |||
|
52 | ||||
|
53 | session_key = models.CharField(max_length=SESSION_KEY_MAX_LENGTH, unique=True) | |||
|
54 | fav_tags = models.ManyToManyField('Tag', related_name='settings_as_fav') | |||
|
55 | hidden_tags = models.ManyToManyField('Tag', related_name='settings_as_hidden') |
@@ -27,7 +27,7 b' class TagView(AllThreadsView, Dispatcher' | |||||
27 | tag_alias = get_object_or_404(TagAlias, name=self.tag_name) |
|
27 | tag_alias = get_object_or_404(TagAlias, name=self.tag_name) | |
28 | tag = tag_alias.parent |
|
28 | tag = tag_alias.parent | |
29 |
|
29 | |||
30 | hidden_tags = self.settings_manager.get_hidden_tags() |
|
30 | hidden_tags = list(self.settings_manager.get_hidden_tags()) | |
31 |
|
31 | |||
32 | try: |
|
32 | try: | |
33 | hidden_tags.remove(tag) |
|
33 | hidden_tags.remove(tag) | |
@@ -46,11 +46,8 b' class TagView(AllThreadsView, Dispatcher' | |||||
46 | tag = tag_alias.parent |
|
46 | tag = tag_alias.parent | |
47 | params[PARAM_TAG] = tag |
|
47 | params[PARAM_TAG] = tag | |
48 |
|
48 | |||
49 | fav_tag_names = settings_manager.get_setting(SETTING_FAVORITE_TAGS) |
|
49 | params[PARAM_IS_FAVORITE] = settings_manager.tag_is_fav(tag) | |
50 | hidden_tag_names = settings_manager.get_setting(SETTING_HIDDEN_TAGS) |
|
50 | params[PARAM_IS_HIDDEN] = settings_manager.tag_is_hidden(tag) | |
51 |
|
||||
52 | params[PARAM_IS_FAVORITE] = fav_tag_names is not None and tag.get_name() in fav_tag_names |
|
|||
53 | params[PARAM_IS_HIDDEN] = hidden_tag_names is not None and tag.get_name() in hidden_tag_names |
|
|||
54 |
|
51 | |||
55 | params[PARAM_RANDOM_IMAGE_POST] = tag.get_random_image_post() |
|
52 | params[PARAM_RANDOM_IMAGE_POST] = tag.get_random_image_post() | |
56 | params[PARAM_RELATED_TAGS] = tag.get_related_tags() |
|
53 | params[PARAM_RELATED_TAGS] = tag.get_related_tags() |
General Comments 0
You need to be logged in to leave comments.
Login now