##// END OF EJS Templates
Divided settings manager into base settings manager class and session-based settings manager. This allowes to add other backends to the settings manager
neko259 -
r730:447bb8d7 2.0-dev
parent child Browse files
Show More
@@ -15,10 +15,22 b" SETTING_PERMISSIONS = 'permissions'"
15 DEFAULT_THEME = 'md'
15 DEFAULT_THEME = 'md'
16
16
17
17
18 class SettingsManager:
18 def get_settings_manager(request):
19 """
20 Get settings manager based on the request object. Currently only
21 session-based manager is supported. In the future, cookie-based or
22 database-based managers could be implemented.
23 """
24 return SessionSettingsManager(request.session)
19
25
20 def __init__(self, session):
26
21 self.session = session
27 class SettingsManager:
28 """
29 Base settings manager class. get_setting and set_setting methods should
30 be overriden.
31 """
32 def __init__(self):
33 pass
22
34
23 def get_theme(self):
35 def get_theme(self):
24 theme = self.get_setting(SETTING_THEME)
36 theme = self.get_setting(SETTING_THEME)
@@ -39,13 +51,10 b' class SettingsManager:'
39 return False
51 return False
40
52
41 def get_setting(self, setting):
53 def get_setting(self, setting):
42 if setting in self.session:
54 pass
43 return self.session[setting]
44 else:
45 return None
46
55
47 def set_setting(self, setting, value):
56 def set_setting(self, setting, value):
48 self.session[setting] = value
57 pass
49
58
50 def add_permission(self, permission):
59 def add_permission(self, permission):
51 permissions = self.get_setting(SETTING_PERMISSIONS)
60 permissions = self.get_setting(SETTING_PERMISSIONS)
@@ -112,3 +121,23 b' class SettingsManager:'
112 if tag.name in tags:
121 if tag.name in tags:
113 tags.remove(tag.name)
122 tags.remove(tag.name)
114 self.set_setting(SETTING_HIDDEN_TAGS, tags)
123 self.set_setting(SETTING_HIDDEN_TAGS, tags)
124
125
126 class SessionSettingsManager(SettingsManager):
127 """
128 Session-based settings manager. All settings are saved to the user's
129 session.
130 """
131 def __init__(self, session):
132 SettingsManager.__init__(self)
133 self.session = session
134
135 def get_setting(self, setting):
136 if setting in self.session:
137 return self.session[setting]
138 else:
139 return None
140
141 def set_setting(self, setting, value):
142 self.session[setting] = value
143
@@ -1,5 +1,5 b''
1 from boards.abstracts.settingsmanager import SettingsManager, \
1 from boards.abstracts.settingsmanager import PERMISSION_MODERATE, \
2 PERMISSION_MODERATE
2 get_settings_manager
3
3
4 __author__ = 'neko259'
4 __author__ = 'neko259'
5
5
@@ -21,7 +21,7 b' def user_and_ui_processor(request):'
21
21
22 context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day())
22 context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day())
23
23
24 settings_manager = SettingsManager(request.session)
24 settings_manager = get_settings_manager(request)
25 context[CONTEXT_TAGS] = settings_manager.get_fav_tags()
25 context[CONTEXT_TAGS] = settings_manager.get_fav_tags()
26 theme = settings_manager.get_theme()
26 theme = settings_manager.get_theme()
27 context[CONTEXT_THEME] = theme
27 context[CONTEXT_THEME] = theme
@@ -6,6 +6,7 b' from django.core.paginator import Pagina'
6 from django.test import TestCase
6 from django.test import TestCase
7 from django.test.client import Client
7 from django.test.client import Client
8 from django.core.urlresolvers import reverse, NoReverseMatch
8 from django.core.urlresolvers import reverse, NoReverseMatch
9 from boards.abstracts.settingsmanager import get_settings_manager
9
10
10 from boards.models import Post, Tag, Thread
11 from boards.models import Post, Tag, Thread
11 from boards import urls
12 from boards import urls
@@ -233,10 +234,10 b' class FormTest(TestCase):'
233 class ViewTest(TestCase):
234 class ViewTest(TestCase):
234
235
235 def test_all_views(self):
236 def test_all_views(self):
236 '''
237 """
237 Try opening all views defined in ulrs.py that don't need additional
238 Try opening all views defined in ulrs.py that don't need additional
238 parameters
239 parameters
239 '''
240 """
240
241
241 client = Client()
242 client = Client()
242 for url in urls.urlpatterns:
243 for url in urls.urlpatterns:
@@ -248,7 +249,7 b' class ViewTest(TestCase):'
248 response = client.get(reverse(view_name))
249 response = client.get(reverse(view_name))
249
250
250 self.assertEqual(HTTP_CODE_OK, response.status_code,
251 self.assertEqual(HTTP_CODE_OK, response.status_code,
251 '%s view not opened' % view_name)
252 '%s view not opened' % view_name)
252 except NoReverseMatch:
253 except NoReverseMatch:
253 # This view just needs additional arguments
254 # This view just needs additional arguments
254 pass
255 pass
@@ -257,3 +258,18 b' class ViewTest(TestCase):'
257 except AttributeError:
258 except AttributeError:
258 # This is normal, some views do not have names
259 # This is normal, some views do not have names
259 pass
260 pass
261
262
263 class AbstractTest(TestCase):
264 def test_settings_manager(self):
265 request = MockRequest()
266 settings_manager = get_settings_manager(request)
267
268 settings_manager.set_setting('test_setting', 'test_value')
269 self.assertEqual('test_value', settings_manager.get_setting(
270 'test_setting'), u'Setting update failed.')
271
272
273 class MockRequest:
274 def __init__(self):
275 self.session = dict() No newline at end of file
@@ -5,7 +5,7 b' from django.shortcuts import render, red'
5
5
6 from boards import utils, settings
6 from boards import utils, settings
7 from boards.abstracts.paginator import get_paginator
7 from boards.abstracts.paginator import get_paginator
8 from boards.abstracts.settingsmanager import SettingsManager
8 from boards.abstracts.settingsmanager import get_settings_manager
9 from boards.forms import ThreadForm, PlainErrorList
9 from boards.forms import ThreadForm, PlainErrorList
10 from boards.models import Post, Thread, Ban, Tag
10 from boards.models import Post, Thread, Ban, Tag
11 from boards.views.banned import BannedView
11 from boards.views.banned import BannedView
@@ -39,7 +39,7 b' class AllThreadsView(PostMixin, BaseBoar'
39 if not form:
39 if not form:
40 form = ThreadForm(error_class=PlainErrorList)
40 form = ThreadForm(error_class=PlainErrorList)
41
41
42 self.settings_manager = SettingsManager(request.session)
42 self.settings_manager = get_settings_manager(request)
43 paginator = get_paginator(self.get_threads(),
43 paginator = get_paginator(self.get_threads(),
44 settings.THREADS_PER_PAGE)
44 settings.THREADS_PER_PAGE)
45 paginator.current_page = int(page)
45 paginator.current_page = int(page)
@@ -1,8 +1,8 b''
1 from django.db import transaction
1 from django.db import transaction
2 from django.shortcuts import get_object_or_404
2 from django.shortcuts import get_object_or_404
3
3
4 from boards.abstracts.settingsmanager import SettingsManager, \
4 from boards.abstracts.settingsmanager import PERMISSION_MODERATE, \
5 PERMISSION_MODERATE
5 get_settings_manager
6 from boards.views.base import BaseBoardView
6 from boards.views.base import BaseBoardView
7 from boards.models import Post, Ban
7 from boards.models import Post, Ban
8 from boards.views.mixins import RedirectNextMixin
8 from boards.views.mixins import RedirectNextMixin
@@ -14,7 +14,7 b' class BanUserView(BaseBoardView, Redirec'
14 def get(self, request, post_id):
14 def get(self, request, post_id):
15 post = get_object_or_404(Post, id=post_id)
15 post = get_object_or_404(Post, id=post_id)
16
16
17 settings_manager = SettingsManager(request.session)
17 settings_manager = get_settings_manager(request)
18
18
19 if settings_manager.has_permission(PERMISSION_MODERATE):
19 if settings_manager.has_permission(PERMISSION_MODERATE):
20 # TODO Show confirmation page before ban
20 # TODO Show confirmation page before ban
@@ -1,8 +1,8 b''
1 from django.shortcuts import redirect, get_object_or_404
1 from django.shortcuts import redirect, get_object_or_404
2 from django.db import transaction
2 from django.db import transaction
3
3
4 from boards.abstracts.settingsmanager import SettingsManager, \
4 from boards.abstracts.settingsmanager import PERMISSION_MODERATE,\
5 PERMISSION_MODERATE
5 get_settings_manager
6 from boards.views.base import BaseBoardView
6 from boards.views.base import BaseBoardView
7 from boards.views.mixins import RedirectNextMixin
7 from boards.views.mixins import RedirectNextMixin
8 from boards.models import Post
8 from boards.models import Post
@@ -16,7 +16,7 b' class DeletePostView(BaseBoardView, Redi'
16
16
17 opening_post = post.is_opening()
17 opening_post = post.is_opening()
18
18
19 settings_manager = SettingsManager(request.session)
19 settings_manager = get_settings_manager(request)
20 if settings_manager.has_permission(PERMISSION_MODERATE):
20 if settings_manager.has_permission(PERMISSION_MODERATE):
21 # TODO Show confirmation page before deletion
21 # TODO Show confirmation page before deletion
22 Post.objects.delete_post(post)
22 Post.objects.delete_post(post)
@@ -1,7 +1,7 b''
1 from django.shortcuts import render, redirect
1 from django.shortcuts import render, redirect
2
2
3 from boards.abstracts.settingsmanager import SettingsManager, \
3 from boards.abstracts.settingsmanager import PERMISSION_MODERATE, \
4 PERMISSION_MODERATE
4 get_settings_manager
5 from boards.forms import LoginForm, PlainErrorList
5 from boards.forms import LoginForm, PlainErrorList
6 from boards.views.base import BaseBoardView, CONTEXT_FORM
6 from boards.views.base import BaseBoardView, CONTEXT_FORM
7
7
@@ -26,7 +26,7 b' class LoginView(BaseBoardView):'
26 form.session = request.session
26 form.session = request.session
27
27
28 if form.is_valid():
28 if form.is_valid():
29 settings_manager = SettingsManager(request.session)
29 settings_manager = get_settings_manager(request)
30 settings_manager.add_permission(PERMISSION_MODERATE)
30 settings_manager.add_permission(PERMISSION_MODERATE)
31 return redirect('index')
31 return redirect('index')
32 else:
32 else:
@@ -1,7 +1,7 b''
1 from django.shortcuts import render
1 from django.shortcuts import render
2
2
3 from boards.abstracts.settingsmanager import SettingsManager, \
3 from boards.abstracts.settingsmanager import PERMISSION_MODERATE,\
4 PERMISSION_MODERATE
4 get_settings_manager
5 from boards.forms import LoginForm
5 from boards.forms import LoginForm
6 from boards.views.base import BaseBoardView, CONTEXT_FORM
6 from boards.views.base import BaseBoardView, CONTEXT_FORM
7
7
@@ -12,7 +12,7 b' from boards.views.base import BaseBoardV'
12 class LogoutView(BaseBoardView):
12 class LogoutView(BaseBoardView):
13
13
14 def get(self, request, form=None):
14 def get(self, request, form=None):
15 settings_manager = SettingsManager(request.session)
15 settings_manager = get_settings_manager(request)
16 settings_manager.del_permission(PERMISSION_MODERATE)
16 settings_manager.del_permission(PERMISSION_MODERATE)
17
17
18 context = self.get_context_data(request=request)
18 context = self.get_context_data(request=request)
@@ -1,7 +1,7 b''
1 from django.shortcuts import render, get_object_or_404, redirect
1 from django.shortcuts import render, get_object_or_404, redirect
2 from boards.abstracts.settingsmanager import SettingsManager, \
3 PERMISSION_MODERATE
4
2
3 from boards.abstracts.settingsmanager import PERMISSION_MODERATE,\
4 get_settings_manager
5 from boards.views.base import BaseBoardView
5 from boards.views.base import BaseBoardView
6 from boards.views.mixins import DispatcherMixin
6 from boards.views.mixins import DispatcherMixin
7 from boards.models.post import Post
7 from boards.models.post import Post
@@ -12,7 +12,7 b' from boards.forms import AddTagForm, Pla'
12 class PostAdminView(BaseBoardView, DispatcherMixin):
12 class PostAdminView(BaseBoardView, DispatcherMixin):
13
13
14 def get(self, request, post_id, form=None):
14 def get(self, request, post_id, form=None):
15 settings_manager = SettingsManager(request.session)
15 settings_manager = get_settings_manager(request)
16 if not settings_manager.has_permission(PERMISSION_MODERATE):
16 if not settings_manager.has_permission(PERMISSION_MODERATE):
17 redirect('index')
17 redirect('index')
18
18
@@ -33,7 +33,7 b' class PostAdminView(BaseBoardView, Dispa'
33 return render(request, 'boards/post_admin.html', context)
33 return render(request, 'boards/post_admin.html', context)
34
34
35 def post(self, request, post_id):
35 def post(self, request, post_id):
36 settings_manager = SettingsManager(request.session)
36 settings_manager = get_settings_manager(request)
37 if not settings_manager.has_permission(PERMISSION_MODERATE):
37 if not settings_manager.has_permission(PERMISSION_MODERATE):
38 redirect('index')
38 redirect('index')
39
39
@@ -1,7 +1,7 b''
1 from django.db import transaction
1 from django.db import transaction
2 from django.shortcuts import render, redirect
2 from django.shortcuts import render, redirect
3 from boards.abstracts.settingsmanager import SettingsManager
4
3
4 from boards.abstracts.settingsmanager import get_settings_manager
5 from boards.views.base import BaseBoardView, CONTEXT_FORM
5 from boards.views.base import BaseBoardView, CONTEXT_FORM
6 from boards.forms import SettingsForm, PlainErrorList
6 from boards.forms import SettingsForm, PlainErrorList
7
7
@@ -10,7 +10,7 b' class SettingsView(BaseBoardView):'
10
10
11 def get(self, request):
11 def get(self, request):
12 context = self.get_context_data(request=request)
12 context = self.get_context_data(request=request)
13 settings_manager = SettingsManager(request.session)
13 settings_manager = get_settings_manager(request)
14
14
15 selected_theme = settings_manager.get_theme()
15 selected_theme = settings_manager.get_theme()
16
16
@@ -22,7 +22,7 b' class SettingsView(BaseBoardView):'
22 return render(request, 'boards/settings.html', context)
22 return render(request, 'boards/settings.html', context)
23
23
24 def post(self, request):
24 def post(self, request):
25 settings_manager = SettingsManager(request.session)
25 settings_manager = get_settings_manager(request)
26
26
27 with transaction.atomic():
27 with transaction.atomic():
28 form = SettingsForm(request.POST, error_class=PlainErrorList)
28 form = SettingsForm(request.POST, error_class=PlainErrorList)
@@ -1,11 +1,12 b''
1 from django.shortcuts import get_object_or_404
1 from django.shortcuts import get_object_or_404
2 from boards import utils
2
3 from boards.abstracts.settingsmanager import SettingsManager
3 from boards.abstracts.settingsmanager import get_settings_manager
4 from boards.models import Tag, Post
4 from boards.models import Tag
5 from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE
5 from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE
6 from boards.views.mixins import DispatcherMixin, RedirectNextMixin
6 from boards.views.mixins import DispatcherMixin, RedirectNextMixin
7 from boards.forms import ThreadForm, PlainErrorList
7 from boards.forms import ThreadForm, PlainErrorList
8
8
9
9 __author__ = 'neko259'
10 __author__ = 'neko259'
10
11
11
12
@@ -21,7 +22,7 b' class TagView(AllThreadsView, Dispatcher'
21 def get_context_data(self, **kwargs):
22 def get_context_data(self, **kwargs):
22 context = super(TagView, self).get_context_data(**kwargs)
23 context = super(TagView, self).get_context_data(**kwargs)
23
24
24 settings_manager = SettingsManager(kwargs['request'].session)
25 settings_manager = get_settings_manager(kwargs['request'])
25
26
26 tag = get_object_or_404(Tag, name=self.tag_name)
27 tag = get_object_or_404(Tag, name=self.tag_name)
27 context['tag'] = tag
28 context['tag'] = tag
@@ -56,7 +57,7 b' class TagView(AllThreadsView, Dispatcher'
56 def subscribe(self, request):
57 def subscribe(self, request):
57 tag = get_object_or_404(Tag, name=self.tag_name)
58 tag = get_object_or_404(Tag, name=self.tag_name)
58
59
59 settings_manager = SettingsManager(request.session)
60 settings_manager = get_settings_manager(request)
60 settings_manager.add_fav_tag(tag)
61 settings_manager.add_fav_tag(tag)
61
62
62 return self.redirect_to_next(request)
63 return self.redirect_to_next(request)
@@ -64,7 +65,7 b' class TagView(AllThreadsView, Dispatcher'
64 def unsubscribe(self, request):
65 def unsubscribe(self, request):
65 tag = get_object_or_404(Tag, name=self.tag_name)
66 tag = get_object_or_404(Tag, name=self.tag_name)
66
67
67 settings_manager = SettingsManager(request.session)
68 settings_manager = get_settings_manager(request)
68 settings_manager.del_fav_tag(tag)
69 settings_manager.del_fav_tag(tag)
69
70
70 return self.redirect_to_next(request)
71 return self.redirect_to_next(request)
@@ -77,7 +78,7 b' class TagView(AllThreadsView, Dispatcher'
77
78
78 tag = get_object_or_404(Tag, name=self.tag_name)
79 tag = get_object_or_404(Tag, name=self.tag_name)
79
80
80 settings_manager = SettingsManager(request.session)
81 settings_manager = get_settings_manager(request)
81 settings_manager.add_hidden_tag(tag)
82 settings_manager.add_hidden_tag(tag)
82
83
83 def unhide(self, request):
84 def unhide(self, request):
@@ -87,5 +88,5 b' class TagView(AllThreadsView, Dispatcher'
87
88
88 tag = get_object_or_404(Tag, name=self.tag_name)
89 tag = get_object_or_404(Tag, name=self.tag_name)
89
90
90 settings_manager = SettingsManager(request.session)
91 settings_manager = get_settings_manager(request)
91 settings_manager.del_hidden_tag(tag)
92 settings_manager.del_hidden_tag(tag)
General Comments 0
You need to be logged in to leave comments. Login now