Show More
@@ -1,145 +1,145 b'' | |||||
1 | from django.shortcuts import get_object_or_404 |
|
1 | from django.shortcuts import get_object_or_404 | |
2 | from boards.models import Tag |
|
2 | from boards.models import Tag | |
3 |
|
3 | |||
4 | __author__ = 'neko259' |
|
4 | __author__ = 'neko259' | |
5 |
|
5 | |||
6 | SESSION_SETTING = 'setting' |
|
6 | SESSION_SETTING = 'setting' | |
7 |
|
7 | |||
8 | # Remove this, it is not used any more cause there is a user's permission |
|
8 | # Remove this, it is not used any more cause there is a user's permission | |
9 | PERMISSION_MODERATE = 'moderator' |
|
9 | PERMISSION_MODERATE = 'moderator' | |
10 |
|
10 | |||
11 | SETTING_THEME = 'theme' |
|
11 | SETTING_THEME = 'theme' | |
12 | SETTING_FAVORITE_TAGS = 'favorite_tags' |
|
12 | SETTING_FAVORITE_TAGS = 'favorite_tags' | |
13 | SETTING_HIDDEN_TAGS = 'hidden_tags' |
|
13 | SETTING_HIDDEN_TAGS = 'hidden_tags' | |
14 | SETTING_PERMISSIONS = 'permissions' |
|
14 | SETTING_PERMISSIONS = 'permissions' | |
15 | SETTING_USERNAME = 'username' |
|
15 | SETTING_USERNAME = 'username' | |
16 | SETTING_LAST_NOTIFICATION_ID = 'last_notification' |
|
16 | SETTING_LAST_NOTIFICATION_ID = 'last_notification' | |
17 | SETTING_IMAGE_VIEWER = 'image_viewer' |
|
17 | SETTING_IMAGE_VIEWER = 'image_viewer' | |
18 |
|
18 | |||
19 | DEFAULT_THEME = 'md' |
|
19 | DEFAULT_THEME = 'md' | |
20 |
|
20 | |||
21 |
|
21 | |||
22 | class SettingsManager: |
|
22 | class SettingsManager: | |
23 | """ |
|
23 | """ | |
24 | Base settings manager class. get_setting and set_setting methods should |
|
24 | Base settings manager class. get_setting and set_setting methods should | |
25 | be overriden. |
|
25 | be overriden. | |
26 | """ |
|
26 | """ | |
27 | def __init__(self): |
|
27 | def __init__(self): | |
28 | pass |
|
28 | pass | |
29 |
|
29 | |||
30 | def get_theme(self) -> str: |
|
30 | def get_theme(self) -> str: | |
31 | theme = self.get_setting(SETTING_THEME) |
|
31 | theme = self.get_setting(SETTING_THEME) | |
32 | if not theme: |
|
32 | if not theme: | |
33 | theme = DEFAULT_THEME |
|
33 | theme = DEFAULT_THEME | |
34 | self.set_setting(SETTING_THEME, theme) |
|
34 | self.set_setting(SETTING_THEME, theme) | |
35 |
|
35 | |||
36 | return theme |
|
36 | return theme | |
37 |
|
37 | |||
38 | def set_theme(self, theme): |
|
38 | def set_theme(self, theme): | |
39 | self.set_setting(SETTING_THEME, theme) |
|
39 | self.set_setting(SETTING_THEME, theme) | |
40 |
|
40 | |||
41 | def has_permission(self, permission): |
|
41 | def has_permission(self, permission): | |
42 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
42 | permissions = self.get_setting(SETTING_PERMISSIONS) | |
43 | if permissions: |
|
43 | if permissions: | |
44 | return permission in permissions |
|
44 | return permission in permissions | |
45 | else: |
|
45 | else: | |
46 | return False |
|
46 | return False | |
47 |
|
47 | |||
48 | def get_setting(self, setting, default=None): |
|
48 | def get_setting(self, setting, default=None): | |
49 | pass |
|
49 | pass | |
50 |
|
50 | |||
51 | def set_setting(self, setting, value): |
|
51 | def set_setting(self, setting, value): | |
52 | pass |
|
52 | pass | |
53 |
|
53 | |||
54 | def add_permission(self, permission): |
|
54 | def add_permission(self, permission): | |
55 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
55 | permissions = self.get_setting(SETTING_PERMISSIONS) | |
56 | if not permissions: |
|
56 | if not permissions: | |
57 | permissions = [permission] |
|
57 | permissions = [permission] | |
58 | else: |
|
58 | else: | |
59 | permissions.append(permission) |
|
59 | permissions.append(permission) | |
60 | self.set_setting(SETTING_PERMISSIONS, permissions) |
|
60 | self.set_setting(SETTING_PERMISSIONS, permissions) | |
61 |
|
61 | |||
62 | def del_permission(self, permission): |
|
62 | def del_permission(self, permission): | |
63 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
63 | permissions = self.get_setting(SETTING_PERMISSIONS) | |
64 | if not permissions: |
|
64 | if not permissions: | |
65 | permissions = [] |
|
65 | permissions = [] | |
66 | else: |
|
66 | else: | |
67 | permissions.remove(permission) |
|
67 | permissions.remove(permission) | |
68 | self.set_setting(SETTING_PERMISSIONS, permissions) |
|
68 | self.set_setting(SETTING_PERMISSIONS, permissions) | |
69 |
|
69 | |||
70 | def get_fav_tags(self) -> list: |
|
70 | def get_fav_tags(self) -> list: | |
71 | tag_names = self.get_setting(SETTING_FAVORITE_TAGS) |
|
71 | tag_names = self.get_setting(SETTING_FAVORITE_TAGS) | |
72 | tags = [] |
|
72 | tags = [] | |
73 | if tag_names: |
|
73 | if tag_names: | |
74 | tags = Tag.objects.filter(name__in=tag_names) |
|
74 | tags = list(Tag.objects.filter(name__in=tag_names)) | |
75 | return tags |
|
75 | return tags | |
76 |
|
76 | |||
77 | def add_fav_tag(self, tag): |
|
77 | def add_fav_tag(self, tag): | |
78 | tags = self.get_setting(SETTING_FAVORITE_TAGS) |
|
78 | tags = self.get_setting(SETTING_FAVORITE_TAGS) | |
79 | if not tags: |
|
79 | if not tags: | |
80 | tags = [tag.name] |
|
80 | tags = [tag.name] | |
81 | else: |
|
81 | else: | |
82 | if not tag.name in tags: |
|
82 | if not tag.name in tags: | |
83 | tags.append(tag.name) |
|
83 | tags.append(tag.name) | |
84 |
|
84 | |||
85 | tags.sort() |
|
85 | tags.sort() | |
86 | self.set_setting(SETTING_FAVORITE_TAGS, tags) |
|
86 | self.set_setting(SETTING_FAVORITE_TAGS, tags) | |
87 |
|
87 | |||
88 | def del_fav_tag(self, tag): |
|
88 | def del_fav_tag(self, tag): | |
89 | tags = self.get_setting(SETTING_FAVORITE_TAGS) |
|
89 | tags = self.get_setting(SETTING_FAVORITE_TAGS) | |
90 | if tag.name in tags: |
|
90 | if tag.name in tags: | |
91 | tags.remove(tag.name) |
|
91 | tags.remove(tag.name) | |
92 | self.set_setting(SETTING_FAVORITE_TAGS, tags) |
|
92 | self.set_setting(SETTING_FAVORITE_TAGS, tags) | |
93 |
|
93 | |||
94 | def get_hidden_tags(self) -> list: |
|
94 | def get_hidden_tags(self) -> list: | |
95 | tag_names = self.get_setting(SETTING_HIDDEN_TAGS) |
|
95 | tag_names = self.get_setting(SETTING_HIDDEN_TAGS) | |
96 | tags = [] |
|
96 | tags = [] | |
97 | if tag_names: |
|
97 | if tag_names: | |
98 | tags = Tag.objects.filter(name__in=tag_names) |
|
98 | tags = list(Tag.objects.filter(name__in=tag_names)) | |
99 |
|
99 | |||
100 | return tags |
|
100 | return tags | |
101 |
|
101 | |||
102 | def add_hidden_tag(self, tag): |
|
102 | def add_hidden_tag(self, tag): | |
103 | tags = self.get_setting(SETTING_HIDDEN_TAGS) |
|
103 | tags = self.get_setting(SETTING_HIDDEN_TAGS) | |
104 | if not tags: |
|
104 | if not tags: | |
105 | tags = [tag.name] |
|
105 | tags = [tag.name] | |
106 | else: |
|
106 | else: | |
107 | if not tag.name in tags: |
|
107 | if not tag.name in tags: | |
108 | tags.append(tag.name) |
|
108 | tags.append(tag.name) | |
109 |
|
109 | |||
110 | tags.sort() |
|
110 | tags.sort() | |
111 | self.set_setting(SETTING_HIDDEN_TAGS, tags) |
|
111 | self.set_setting(SETTING_HIDDEN_TAGS, tags) | |
112 |
|
112 | |||
113 | def del_hidden_tag(self, tag): |
|
113 | def del_hidden_tag(self, tag): | |
114 | tags = self.get_setting(SETTING_HIDDEN_TAGS) |
|
114 | tags = self.get_setting(SETTING_HIDDEN_TAGS) | |
115 | if tag.name in tags: |
|
115 | if tag.name in tags: | |
116 | tags.remove(tag.name) |
|
116 | tags.remove(tag.name) | |
117 | self.set_setting(SETTING_HIDDEN_TAGS, tags) |
|
117 | self.set_setting(SETTING_HIDDEN_TAGS, tags) | |
118 |
|
118 | |||
119 |
|
119 | |||
120 | class SessionSettingsManager(SettingsManager): |
|
120 | class SessionSettingsManager(SettingsManager): | |
121 | """ |
|
121 | """ | |
122 | Session-based settings manager. All settings are saved to the user's |
|
122 | Session-based settings manager. All settings are saved to the user's | |
123 | session. |
|
123 | session. | |
124 | """ |
|
124 | """ | |
125 | def __init__(self, session): |
|
125 | def __init__(self, session): | |
126 | SettingsManager.__init__(self) |
|
126 | SettingsManager.__init__(self) | |
127 | self.session = session |
|
127 | self.session = session | |
128 |
|
128 | |||
129 | def get_setting(self, setting, default=None): |
|
129 | def get_setting(self, setting, default=None): | |
130 | if setting in self.session: |
|
130 | if setting in self.session: | |
131 | return self.session[setting] |
|
131 | return self.session[setting] | |
132 | else: |
|
132 | else: | |
133 | return default |
|
133 | return default | |
134 |
|
134 | |||
135 | def set_setting(self, setting, value): |
|
135 | def set_setting(self, setting, value): | |
136 | self.session[setting] = value |
|
136 | self.session[setting] = value | |
137 |
|
137 | |||
138 |
|
138 | |||
139 | def get_settings_manager(request) -> SettingsManager: |
|
139 | def get_settings_manager(request) -> SettingsManager: | |
140 | """ |
|
140 | """ | |
141 | Get settings manager based on the request object. Currently only |
|
141 | Get settings manager based on the request object. Currently only | |
142 | session-based manager is supported. In the future, cookie-based or |
|
142 | session-based manager is supported. In the future, cookie-based or | |
143 | database-based managers could be implemented. |
|
143 | database-based managers could be implemented. | |
144 | """ |
|
144 | """ | |
145 | return SessionSettingsManager(request.session) |
|
145 | return SessionSettingsManager(request.session) |
General Comments 0
You need to be logged in to leave comments.
Login now