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