Show More
@@ -15,10 +15,22 b" SETTING_PERMISSIONS = 'permissions'" | |||
|
15 | 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): | |
|
21 | self.session = session | |
|
26 | ||
|
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 | 35 | def get_theme(self): |
|
24 | 36 | theme = self.get_setting(SETTING_THEME) |
@@ -39,13 +51,10 b' class SettingsManager:' | |||
|
39 | 51 | return False |
|
40 | 52 | |
|
41 | 53 | def get_setting(self, setting): |
|
42 | if setting in self.session: | |
|
43 | return self.session[setting] | |
|
44 | else: | |
|
45 | return None | |
|
54 | pass | |
|
46 | 55 | |
|
47 | 56 | def set_setting(self, setting, value): |
|
48 | self.session[setting] = value | |
|
57 | pass | |
|
49 | 58 | |
|
50 | 59 | def add_permission(self, permission): |
|
51 | 60 | permissions = self.get_setting(SETTING_PERMISSIONS) |
@@ -112,3 +121,23 b' class SettingsManager:' | |||
|
112 | 121 | if tag.name in tags: |
|
113 | 122 | tags.remove(tag.name) |
|
114 | 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 |
|
|
2 | PERMISSION_MODERATE | |
|
1 | from boards.abstracts.settingsmanager import PERMISSION_MODERATE, \ | |
|
2 | get_settings_manager | |
|
3 | 3 | |
|
4 | 4 | __author__ = 'neko259' |
|
5 | 5 | |
@@ -21,7 +21,7 b' def user_and_ui_processor(request):' | |||
|
21 | 21 | |
|
22 | 22 | context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day()) |
|
23 | 23 | |
|
24 |
settings_manager = |
|
|
24 | settings_manager = get_settings_manager(request) | |
|
25 | 25 | context[CONTEXT_TAGS] = settings_manager.get_fav_tags() |
|
26 | 26 | theme = settings_manager.get_theme() |
|
27 | 27 | context[CONTEXT_THEME] = theme |
@@ -6,6 +6,7 b' from django.core.paginator import Pagina' | |||
|
6 | 6 | from django.test import TestCase |
|
7 | 7 | from django.test.client import Client |
|
8 | 8 | from django.core.urlresolvers import reverse, NoReverseMatch |
|
9 | from boards.abstracts.settingsmanager import get_settings_manager | |
|
9 | 10 | |
|
10 | 11 | from boards.models import Post, Tag, Thread |
|
11 | 12 | from boards import urls |
@@ -233,10 +234,10 b' class FormTest(TestCase):' | |||
|
233 | 234 | class ViewTest(TestCase): |
|
234 | 235 | |
|
235 | 236 | def test_all_views(self): |
|
236 |
|
|
|
237 | """ | |
|
237 | 238 | Try opening all views defined in ulrs.py that don't need additional |
|
238 | 239 | parameters |
|
239 |
|
|
|
240 | """ | |
|
240 | 241 | |
|
241 | 242 | client = Client() |
|
242 | 243 | for url in urls.urlpatterns: |
@@ -248,7 +249,7 b' class ViewTest(TestCase):' | |||
|
248 | 249 | response = client.get(reverse(view_name)) |
|
249 | 250 | |
|
250 | 251 | self.assertEqual(HTTP_CODE_OK, response.status_code, |
|
251 | '%s view not opened' % view_name) | |
|
252 | '%s view not opened' % view_name) | |
|
252 | 253 | except NoReverseMatch: |
|
253 | 254 | # This view just needs additional arguments |
|
254 | 255 | pass |
@@ -257,3 +258,18 b' class ViewTest(TestCase):' | |||
|
257 | 258 | except AttributeError: |
|
258 | 259 | # This is normal, some views do not have names |
|
259 | 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 | 6 | from boards import utils, settings |
|
7 | 7 | from boards.abstracts.paginator import get_paginator |
|
8 |
from boards.abstracts.settingsmanager import |
|
|
8 | from boards.abstracts.settingsmanager import get_settings_manager | |
|
9 | 9 | from boards.forms import ThreadForm, PlainErrorList |
|
10 | 10 | from boards.models import Post, Thread, Ban, Tag |
|
11 | 11 | from boards.views.banned import BannedView |
@@ -39,7 +39,7 b' class AllThreadsView(PostMixin, BaseBoar' | |||
|
39 | 39 | if not form: |
|
40 | 40 | form = ThreadForm(error_class=PlainErrorList) |
|
41 | 41 | |
|
42 |
self.settings_manager = |
|
|
42 | self.settings_manager = get_settings_manager(request) | |
|
43 | 43 | paginator = get_paginator(self.get_threads(), |
|
44 | 44 | settings.THREADS_PER_PAGE) |
|
45 | 45 | paginator.current_page = int(page) |
@@ -1,8 +1,8 b'' | |||
|
1 | 1 | from django.db import transaction |
|
2 | 2 | from django.shortcuts import get_object_or_404 |
|
3 | 3 | |
|
4 |
from boards.abstracts.settingsmanager import |
|
|
5 | PERMISSION_MODERATE | |
|
4 | from boards.abstracts.settingsmanager import PERMISSION_MODERATE, \ | |
|
5 | get_settings_manager | |
|
6 | 6 | from boards.views.base import BaseBoardView |
|
7 | 7 | from boards.models import Post, Ban |
|
8 | 8 | from boards.views.mixins import RedirectNextMixin |
@@ -14,7 +14,7 b' class BanUserView(BaseBoardView, Redirec' | |||
|
14 | 14 | def get(self, request, post_id): |
|
15 | 15 | post = get_object_or_404(Post, id=post_id) |
|
16 | 16 | |
|
17 |
settings_manager = |
|
|
17 | settings_manager = get_settings_manager(request) | |
|
18 | 18 | |
|
19 | 19 | if settings_manager.has_permission(PERMISSION_MODERATE): |
|
20 | 20 | # TODO Show confirmation page before ban |
@@ -1,8 +1,8 b'' | |||
|
1 | 1 | from django.shortcuts import redirect, get_object_or_404 |
|
2 | 2 | from django.db import transaction |
|
3 | 3 | |
|
4 |
from boards.abstracts.settingsmanager import |
|
|
5 | PERMISSION_MODERATE | |
|
4 | from boards.abstracts.settingsmanager import PERMISSION_MODERATE,\ | |
|
5 | get_settings_manager | |
|
6 | 6 | from boards.views.base import BaseBoardView |
|
7 | 7 | from boards.views.mixins import RedirectNextMixin |
|
8 | 8 | from boards.models import Post |
@@ -16,7 +16,7 b' class DeletePostView(BaseBoardView, Redi' | |||
|
16 | 16 | |
|
17 | 17 | opening_post = post.is_opening() |
|
18 | 18 | |
|
19 |
settings_manager = |
|
|
19 | settings_manager = get_settings_manager(request) | |
|
20 | 20 | if settings_manager.has_permission(PERMISSION_MODERATE): |
|
21 | 21 | # TODO Show confirmation page before deletion |
|
22 | 22 | Post.objects.delete_post(post) |
@@ -1,7 +1,7 b'' | |||
|
1 | 1 | from django.shortcuts import render, redirect |
|
2 | 2 | |
|
3 |
from boards.abstracts.settingsmanager import |
|
|
4 | PERMISSION_MODERATE | |
|
3 | from boards.abstracts.settingsmanager import PERMISSION_MODERATE, \ | |
|
4 | get_settings_manager | |
|
5 | 5 | from boards.forms import LoginForm, PlainErrorList |
|
6 | 6 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
7 | 7 | |
@@ -26,7 +26,7 b' class LoginView(BaseBoardView):' | |||
|
26 | 26 | form.session = request.session |
|
27 | 27 | |
|
28 | 28 | if form.is_valid(): |
|
29 |
settings_manager = |
|
|
29 | settings_manager = get_settings_manager(request) | |
|
30 | 30 | settings_manager.add_permission(PERMISSION_MODERATE) |
|
31 | 31 | return redirect('index') |
|
32 | 32 | else: |
@@ -1,7 +1,7 b'' | |||
|
1 | 1 | from django.shortcuts import render |
|
2 | 2 | |
|
3 |
from boards.abstracts.settingsmanager import |
|
|
4 | PERMISSION_MODERATE | |
|
3 | from boards.abstracts.settingsmanager import PERMISSION_MODERATE,\ | |
|
4 | get_settings_manager | |
|
5 | 5 | from boards.forms import LoginForm |
|
6 | 6 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
7 | 7 | |
@@ -12,7 +12,7 b' from boards.views.base import BaseBoardV' | |||
|
12 | 12 | class LogoutView(BaseBoardView): |
|
13 | 13 | |
|
14 | 14 | def get(self, request, form=None): |
|
15 |
settings_manager = |
|
|
15 | settings_manager = get_settings_manager(request) | |
|
16 | 16 | settings_manager.del_permission(PERMISSION_MODERATE) |
|
17 | 17 | |
|
18 | 18 | context = self.get_context_data(request=request) |
@@ -1,7 +1,7 b'' | |||
|
1 | 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 | 5 | from boards.views.base import BaseBoardView |
|
6 | 6 | from boards.views.mixins import DispatcherMixin |
|
7 | 7 | from boards.models.post import Post |
@@ -12,7 +12,7 b' from boards.forms import AddTagForm, Pla' | |||
|
12 | 12 | class PostAdminView(BaseBoardView, DispatcherMixin): |
|
13 | 13 | |
|
14 | 14 | def get(self, request, post_id, form=None): |
|
15 |
settings_manager = |
|
|
15 | settings_manager = get_settings_manager(request) | |
|
16 | 16 | if not settings_manager.has_permission(PERMISSION_MODERATE): |
|
17 | 17 | redirect('index') |
|
18 | 18 | |
@@ -33,7 +33,7 b' class PostAdminView(BaseBoardView, Dispa' | |||
|
33 | 33 | return render(request, 'boards/post_admin.html', context) |
|
34 | 34 | |
|
35 | 35 | def post(self, request, post_id): |
|
36 |
settings_manager = |
|
|
36 | settings_manager = get_settings_manager(request) | |
|
37 | 37 | if not settings_manager.has_permission(PERMISSION_MODERATE): |
|
38 | 38 | redirect('index') |
|
39 | 39 |
@@ -1,7 +1,7 b'' | |||
|
1 | 1 | from django.db import transaction |
|
2 | 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 | 5 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
6 | 6 | from boards.forms import SettingsForm, PlainErrorList |
|
7 | 7 | |
@@ -10,7 +10,7 b' class SettingsView(BaseBoardView):' | |||
|
10 | 10 | |
|
11 | 11 | def get(self, request): |
|
12 | 12 | context = self.get_context_data(request=request) |
|
13 |
settings_manager = |
|
|
13 | settings_manager = get_settings_manager(request) | |
|
14 | 14 | |
|
15 | 15 | selected_theme = settings_manager.get_theme() |
|
16 | 16 | |
@@ -22,7 +22,7 b' class SettingsView(BaseBoardView):' | |||
|
22 | 22 | return render(request, 'boards/settings.html', context) |
|
23 | 23 | |
|
24 | 24 | def post(self, request): |
|
25 |
settings_manager = |
|
|
25 | settings_manager = get_settings_manager(request) | |
|
26 | 26 | |
|
27 | 27 | with transaction.atomic(): |
|
28 | 28 | form = SettingsForm(request.POST, error_class=PlainErrorList) |
@@ -1,11 +1,12 b'' | |||
|
1 | 1 | from django.shortcuts import get_object_or_404 |
|
2 | from boards import utils | |
|
3 |
from boards.abstracts.settingsmanager import |
|
|
4 |
from boards.models import Tag |
|
|
2 | ||
|
3 | from boards.abstracts.settingsmanager import get_settings_manager | |
|
4 | from boards.models import Tag | |
|
5 | 5 | from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE |
|
6 | 6 | from boards.views.mixins import DispatcherMixin, RedirectNextMixin |
|
7 | 7 | from boards.forms import ThreadForm, PlainErrorList |
|
8 | 8 | |
|
9 | ||
|
9 | 10 | __author__ = 'neko259' |
|
10 | 11 | |
|
11 | 12 | |
@@ -21,7 +22,7 b' class TagView(AllThreadsView, Dispatcher' | |||
|
21 | 22 | def get_context_data(self, **kwargs): |
|
22 | 23 | context = super(TagView, self).get_context_data(**kwargs) |
|
23 | 24 | |
|
24 |
settings_manager = |
|
|
25 | settings_manager = get_settings_manager(kwargs['request']) | |
|
25 | 26 | |
|
26 | 27 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
27 | 28 | context['tag'] = tag |
@@ -56,7 +57,7 b' class TagView(AllThreadsView, Dispatcher' | |||
|
56 | 57 | def subscribe(self, request): |
|
57 | 58 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
58 | 59 | |
|
59 |
settings_manager = |
|
|
60 | settings_manager = get_settings_manager(request) | |
|
60 | 61 | settings_manager.add_fav_tag(tag) |
|
61 | 62 | |
|
62 | 63 | return self.redirect_to_next(request) |
@@ -64,7 +65,7 b' class TagView(AllThreadsView, Dispatcher' | |||
|
64 | 65 | def unsubscribe(self, request): |
|
65 | 66 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
66 | 67 | |
|
67 |
settings_manager = |
|
|
68 | settings_manager = get_settings_manager(request) | |
|
68 | 69 | settings_manager.del_fav_tag(tag) |
|
69 | 70 | |
|
70 | 71 | return self.redirect_to_next(request) |
@@ -77,7 +78,7 b' class TagView(AllThreadsView, Dispatcher' | |||
|
77 | 78 | |
|
78 | 79 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
79 | 80 | |
|
80 |
settings_manager = |
|
|
81 | settings_manager = get_settings_manager(request) | |
|
81 | 82 | settings_manager.add_hidden_tag(tag) |
|
82 | 83 | |
|
83 | 84 | def unhide(self, request): |
@@ -87,5 +88,5 b' class TagView(AllThreadsView, Dispatcher' | |||
|
87 | 88 | |
|
88 | 89 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
89 | 90 | |
|
90 |
settings_manager = |
|
|
91 | settings_manager = get_settings_manager(request) | |
|
91 | 92 | settings_manager.del_hidden_tag(tag) |
General Comments 0
You need to be logged in to leave comments.
Login now