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