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