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