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