##// 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
@@ -1,114 +1,143 b''
1 1 from django.shortcuts import get_object_or_404
2 2 from boards.models import Tag
3 3
4 4 __author__ = 'neko259'
5 5
6 6 SESSION_SETTING = 'setting'
7 7
8 8 PERMISSION_MODERATE = 'moderator'
9 9
10 10 SETTING_THEME = 'theme'
11 11 SETTING_FAVORITE_TAGS = 'favorite_tags'
12 12 SETTING_HIDDEN_TAGS = 'hidden_tags'
13 13 SETTING_PERMISSIONS = 'permissions'
14 14
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)
25 37 if not theme:
26 38 theme = DEFAULT_THEME
27 39 self.set_setting(SETTING_THEME, theme)
28 40
29 41 return theme
30 42
31 43 def set_theme(self, theme):
32 44 self.set_setting(SETTING_THEME, theme)
33 45
34 46 def has_permission(self, permission):
35 47 permissions = self.get_setting(SETTING_PERMISSIONS)
36 48 if permissions:
37 49 return permission in permissions
38 50 else:
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)
52 61 if not permissions:
53 62 permissions = [permission]
54 63 else:
55 64 permissions.append(permission)
56 65 self.set_setting(SETTING_PERMISSIONS, permissions)
57 66
58 67 def del_permission(self, permission):
59 68 permissions = self.get_setting(SETTING_PERMISSIONS)
60 69 if not permissions:
61 70 permissions = []
62 71 else:
63 72 permissions.remove(permission)
64 73 self.set_setting(SETTING_PERMISSIONS, permissions)
65 74
66 75 def get_fav_tags(self):
67 76 tag_names = self.get_setting(SETTING_FAVORITE_TAGS)
68 77 tags = []
69 78 if tag_names:
70 79 for tag_name in tag_names:
71 80 tag = get_object_or_404(Tag, name=tag_name)
72 81 tags.append(tag)
73 82
74 83 return tags
75 84
76 85 def add_fav_tag(self, tag):
77 86 tags = self.get_setting(SETTING_FAVORITE_TAGS)
78 87 if not tags:
79 88 tags = [tag.name]
80 89 else:
81 90 if not tag.name in tags:
82 91 tags.append(tag.name)
83 92 self.set_setting(SETTING_FAVORITE_TAGS, tags)
84 93
85 94 def del_fav_tag(self, tag):
86 95 tags = self.get_setting(SETTING_FAVORITE_TAGS)
87 96 if tag.name in tags:
88 97 tags.remove(tag.name)
89 98 self.set_setting(SETTING_FAVORITE_TAGS, tags)
90 99
91 100 def get_hidden_tags(self):
92 101 tag_names = self.get_setting(SETTING_HIDDEN_TAGS)
93 102 tags = []
94 103 if tag_names:
95 104 for tag_name in tag_names:
96 105 tag = get_object_or_404(Tag, name=tag_name)
97 106 tags.append(tag)
98 107
99 108 return tags
100 109
101 110 def add_hidden_tag(self, tag):
102 111 tags = self.get_setting(SETTING_HIDDEN_TAGS)
103 112 if not tags:
104 113 tags = [tag.name]
105 114 else:
106 115 if not tag.name in tags:
107 116 tags.append(tag.name)
108 117 self.set_setting(SETTING_HIDDEN_TAGS, tags)
109 118
110 119 def del_hidden_tag(self, tag):
111 120 tags = self.get_setting(SETTING_HIDDEN_TAGS)
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,37 +1,37 b''
1 from boards.abstracts.settingsmanager import SettingsManager, \
2 PERMISSION_MODERATE
1 from boards.abstracts.settingsmanager import PERMISSION_MODERATE, \
2 get_settings_manager
3 3
4 4 __author__ = 'neko259'
5 5
6 6 from boards import settings
7 7 from boards.models import Post
8 8
9 9 CONTEXT_SITE_NAME = 'site_name'
10 10 CONTEXT_VERSION = 'version'
11 11 CONTEXT_MODERATOR = 'moderator'
12 12 CONTEXT_THEME_CSS = 'theme_css'
13 13 CONTEXT_THEME = 'theme'
14 14 CONTEXT_PPD = 'posts_per_day'
15 15 CONTEXT_TAGS = 'tags'
16 16 CONTEXT_USER = 'user'
17 17
18 18
19 19 def user_and_ui_processor(request):
20 20 context = {}
21 21
22 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 25 context[CONTEXT_TAGS] = settings_manager.get_fav_tags()
26 26 theme = settings_manager.get_theme()
27 27 context[CONTEXT_THEME] = theme
28 28 context[CONTEXT_THEME_CSS] = 'css/' + theme + '/base_page.css'
29 29
30 30 # This shows the moderator panel
31 31 moderate = settings_manager.has_permission(PERMISSION_MODERATE)
32 32 context[CONTEXT_MODERATOR] = moderate
33 33
34 34 context[CONTEXT_VERSION] = settings.VERSION
35 35 context[CONTEXT_SITE_NAME] = settings.SITE_NAME
36 36
37 37 return context No newline at end of file
@@ -1,259 +1,275 b''
1 1 # coding=utf-8
2 2 import time
3 3 import logging
4 4 from django.core.paginator import Paginator
5 5
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
12 13 from boards import settings
13 14 import neboard
14 15
15 16 PAGE_404 = 'boards/404.html'
16 17
17 18 TEST_TEXT = 'test text'
18 19
19 20 NEW_THREAD_PAGE = '/'
20 21 THREAD_PAGE_ONE = '/thread/1/'
21 22 THREAD_PAGE = '/thread/'
22 23 TAG_PAGE = '/tag/'
23 24 HTTP_CODE_REDIRECT = 302
24 25 HTTP_CODE_OK = 200
25 26 HTTP_CODE_NOT_FOUND = 404
26 27
27 28 logger = logging.getLogger(__name__)
28 29
29 30
30 31 class PostTests(TestCase):
31 32
32 33 def _create_post(self):
33 34 return Post.objects.create_post(title='title', text='text')
34 35
35 36 def test_post_add(self):
36 37 """Test adding post"""
37 38
38 39 post = self._create_post()
39 40
40 41 self.assertIsNotNone(post, 'No post was created')
41 42
42 43 def test_delete_post(self):
43 44 """Test post deletion"""
44 45
45 46 post = self._create_post()
46 47 post_id = post.id
47 48
48 49 Post.objects.delete_post(post)
49 50
50 51 self.assertFalse(Post.objects.filter(id=post_id).exists())
51 52
52 53 def test_delete_thread(self):
53 54 """Test thread deletion"""
54 55
55 56 opening_post = self._create_post()
56 57 thread = opening_post.get_thread()
57 58 reply = Post.objects.create_post("", "", thread=thread)
58 59
59 60 thread.delete()
60 61
61 62 self.assertFalse(Post.objects.filter(id=reply.id).exists())
62 63
63 64 def test_post_to_thread(self):
64 65 """Test adding post to a thread"""
65 66
66 67 op = self._create_post()
67 68 post = Post.objects.create_post("", "", thread=op.get_thread())
68 69
69 70 self.assertIsNotNone(post, 'Reply to thread wasn\'t created')
70 71 self.assertEqual(op.get_thread().last_edit_time, post.pub_time,
71 72 'Post\'s create time doesn\'t match thread last edit'
72 73 ' time')
73 74
74 75 def test_delete_posts_by_ip(self):
75 76 """Test deleting posts with the given ip"""
76 77
77 78 post = self._create_post()
78 79 post_id = post.id
79 80
80 81 Post.objects.delete_posts_by_ip('0.0.0.0')
81 82
82 83 self.assertFalse(Post.objects.filter(id=post_id).exists())
83 84
84 85 def test_get_thread(self):
85 86 """Test getting all posts of a thread"""
86 87
87 88 opening_post = self._create_post()
88 89
89 90 for i in range(0, 2):
90 91 Post.objects.create_post('title', 'text',
91 92 thread=opening_post.get_thread())
92 93
93 94 thread = opening_post.get_thread()
94 95
95 96 self.assertEqual(3, thread.replies.count())
96 97
97 98 def test_create_post_with_tag(self):
98 99 """Test adding tag to post"""
99 100
100 101 tag = Tag.objects.create(name='test_tag')
101 102 post = Post.objects.create_post(title='title', text='text', tags=[tag])
102 103
103 104 thread = post.get_thread()
104 105 self.assertIsNotNone(post, 'Post not created')
105 106 self.assertTrue(tag in thread.tags.all(), 'Tag not added to thread')
106 107 self.assertTrue(thread in tag.threads.all(), 'Thread not added to tag')
107 108
108 109 def test_thread_max_count(self):
109 110 """Test deletion of old posts when the max thread count is reached"""
110 111
111 112 for i in range(settings.MAX_THREAD_COUNT + 1):
112 113 self._create_post()
113 114
114 115 self.assertEqual(settings.MAX_THREAD_COUNT,
115 116 len(Thread.objects.filter(archived=False)))
116 117
117 118 def test_pages(self):
118 119 """Test that the thread list is properly split into pages"""
119 120
120 121 for i in range(settings.MAX_THREAD_COUNT):
121 122 self._create_post()
122 123
123 124 all_threads = Thread.objects.filter(archived=False)
124 125
125 126 paginator = Paginator(Thread.objects.filter(archived=False),
126 127 settings.THREADS_PER_PAGE)
127 128 posts_in_second_page = paginator.page(2).object_list
128 129 first_post = posts_in_second_page[0]
129 130
130 131 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
131 132 first_post.id)
132 133
133 134 def test_linked_tag(self):
134 135 """Test adding a linked tag"""
135 136
136 137 linked_tag = Tag.objects.create(name=u'tag1')
137 138 tag = Tag.objects.create(name=u'tag2', linked=linked_tag)
138 139
139 140 post = Post.objects.create_post("", "", tags=[tag])
140 141
141 142 self.assertTrue(linked_tag in post.get_thread().tags.all(),
142 143 'Linked tag was not added')
143 144
144 145
145 146 class PagesTest(TestCase):
146 147
147 148 def test_404(self):
148 149 """Test receiving error 404 when opening a non-existent page"""
149 150
150 151 tag_name = u'test_tag'
151 152 tag = Tag.objects.create(name=tag_name)
152 153 client = Client()
153 154
154 155 Post.objects.create_post('title', TEST_TEXT, tags=[tag])
155 156
156 157 existing_post_id = Post.objects.all()[0].id
157 158 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
158 159 '/')
159 160 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
160 161 u'Cannot open existing thread')
161 162
162 163 response_not_existing = client.get(THREAD_PAGE + str(
163 164 existing_post_id + 1) + '/')
164 165 self.assertEqual(PAGE_404, response_not_existing.templates[0].name,
165 166 u'Not existing thread is opened')
166 167
167 168 response_existing = client.get(TAG_PAGE + tag_name + '/')
168 169 self.assertEqual(HTTP_CODE_OK,
169 170 response_existing.status_code,
170 171 u'Cannot open existing tag')
171 172
172 173 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
173 174 self.assertEqual(PAGE_404,
174 175 response_not_existing.templates[0].name,
175 176 u'Not existing tag is opened')
176 177
177 178 reply_id = Post.objects.create_post('', TEST_TEXT,
178 179 thread=Post.objects.all()[0]
179 180 .get_thread())
180 181 response_not_existing = client.get(THREAD_PAGE + str(
181 182 reply_id) + '/')
182 183 self.assertEqual(PAGE_404,
183 184 response_not_existing.templates[0].name,
184 185 u'Reply is opened as a thread')
185 186
186 187
187 188 class FormTest(TestCase):
188 189 def test_post_validation(self):
189 190 # Disable captcha for the test
190 191 captcha_enabled = neboard.settings.ENABLE_CAPTCHA
191 192 neboard.settings.ENABLE_CAPTCHA = False
192 193
193 194 client = Client()
194 195
195 196 valid_tags = u'tag1 tag_2 Ρ‚Π΅Π³_3'
196 197 invalid_tags = u'$%_356 ---'
197 198
198 199 response = client.post(NEW_THREAD_PAGE, {'title': 'test title',
199 200 'text': TEST_TEXT,
200 201 'tags': valid_tags})
201 202 self.assertEqual(response.status_code, HTTP_CODE_REDIRECT,
202 203 msg='Posting new message failed: got code ' +
203 204 str(response.status_code))
204 205
205 206 self.assertEqual(1, Post.objects.count(),
206 207 msg='No posts were created')
207 208
208 209 client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT,
209 210 'tags': invalid_tags})
210 211 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
211 212 'where it should fail')
212 213
213 214 # Change posting delay so we don't have to wait for 30 seconds or more
214 215 old_posting_delay = neboard.settings.POSTING_DELAY
215 216 # Wait fot the posting delay or we won't be able to post
216 217 settings.POSTING_DELAY = 1
217 218 time.sleep(neboard.settings.POSTING_DELAY + 1)
218 219 response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT,
219 220 'tags': valid_tags})
220 221 self.assertEqual(HTTP_CODE_REDIRECT, response.status_code,
221 222 msg=u'Posting new message failed: got code ' +
222 223 str(response.status_code))
223 224 # Restore posting delay
224 225 settings.POSTING_DELAY = old_posting_delay
225 226
226 227 self.assertEqual(2, Post.objects.count(),
227 228 msg=u'No posts were created')
228 229
229 230 # Restore captcha setting
230 231 settings.ENABLE_CAPTCHA = captcha_enabled
231 232
232 233
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:
243 244 try:
244 245 view_name = url.name
245 246 logger.debug('Testing view %s' % view_name)
246 247
247 248 try:
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
255 256 except Exception, e:
256 257 self.fail('Got exception %s at %s view' % (e, view_name))
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
@@ -1,139 +1,139 b''
1 1 import string
2 2
3 3 from django.db import transaction
4 4 from django.shortcuts import render, redirect
5 5
6 6 from boards import utils, settings
7 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 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
12 12 from boards.views.base import BaseBoardView, CONTEXT_FORM
13 13 from boards.views.posting_mixin import PostMixin
14 14
15 15 FORM_TAGS = 'tags'
16 16 FORM_TEXT = 'text'
17 17 FORM_TITLE = 'title'
18 18 FORM_IMAGE = 'image'
19 19
20 20 TAG_DELIMITER = ' '
21 21
22 22 PARAMETER_CURRENT_PAGE = 'current_page'
23 23 PARAMETER_PAGINATOR = 'paginator'
24 24 PARAMETER_THREADS = 'threads'
25 25
26 26 TEMPLATE = 'boards/posting_general.html'
27 27 DEFAULT_PAGE = 1
28 28
29 29
30 30 class AllThreadsView(PostMixin, BaseBoardView):
31 31
32 32 def __init__(self):
33 33 self.settings_manager = None
34 34 super(AllThreadsView, self).__init__()
35 35
36 36 def get(self, request, page=DEFAULT_PAGE, form=None):
37 37 context = self.get_context_data(request=request)
38 38
39 39 if not form:
40 40 form = ThreadForm(error_class=PlainErrorList)
41 41
42 self.settings_manager = SettingsManager(request.session)
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)
46 46
47 47 threads = paginator.page(page).object_list
48 48
49 49 context[PARAMETER_THREADS] = threads
50 50 context[CONTEXT_FORM] = form
51 51
52 52 self._get_page_context(paginator, context, page)
53 53
54 54 return render(request, TEMPLATE, context)
55 55
56 56 def post(self, request, page=DEFAULT_PAGE):
57 57 form = ThreadForm(request.POST, request.FILES,
58 58 error_class=PlainErrorList)
59 59 form.session = request.session
60 60
61 61 if form.is_valid():
62 62 return self.create_thread(request, form)
63 63 if form.need_to_ban:
64 64 # Ban user because he is suspected to be a bot
65 65 self._ban_current_user(request)
66 66
67 67 return self.get(request, page, form)
68 68
69 69 @staticmethod
70 70 def _get_page_context(paginator, context, page):
71 71 """
72 72 Get pagination context variables
73 73 """
74 74
75 75 context[PARAMETER_PAGINATOR] = paginator
76 76 context[PARAMETER_CURRENT_PAGE] = paginator.page(int(page))
77 77
78 78 @staticmethod
79 79 def parse_tags_string(tag_strings):
80 80 """
81 81 Parses tag list string and returns tag object list.
82 82 """
83 83
84 84 tags = []
85 85
86 86 if tag_strings:
87 87 tag_strings = tag_strings.split(TAG_DELIMITER)
88 88 for tag_name in tag_strings:
89 89 tag_name = string.lower(tag_name.strip())
90 90 if len(tag_name) > 0:
91 91 tag, created = Tag.objects.get_or_create(name=tag_name)
92 92 tags.append(tag)
93 93
94 94 return tags
95 95
96 96 @transaction.atomic
97 97 def create_thread(self, request, form, html_response=True):
98 98 """
99 99 Creates a new thread with an opening post.
100 100 """
101 101
102 102 ip = utils.get_client_ip(request)
103 103 is_banned = Ban.objects.filter(ip=ip).exists()
104 104
105 105 if is_banned:
106 106 if html_response:
107 107 return redirect(BannedView().as_view())
108 108 else:
109 109 return
110 110
111 111 data = form.cleaned_data
112 112
113 113 title = data[FORM_TITLE]
114 114 text = data[FORM_TEXT]
115 115
116 116 text = self._remove_invalid_links(text)
117 117
118 118 if FORM_IMAGE in data.keys():
119 119 image = data[FORM_IMAGE]
120 120 else:
121 121 image = None
122 122
123 123 tag_strings = data[FORM_TAGS]
124 124
125 125 tags = self.parse_tags_string(tag_strings)
126 126
127 127 post = Post.objects.create_post(title=title, text=text, image=image,
128 128 ip=ip, tags=tags)
129 129
130 130 if html_response:
131 131 return redirect(post.get_url())
132 132
133 133 def get_threads(self):
134 134 """
135 135 Gets list of threads that will be shown on a page.
136 136 """
137 137
138 138 return Thread.objects.all().order_by('-bump_time')\
139 139 .exclude(tags__in=self.settings_manager.get_hidden_tags())
@@ -1,26 +1,26 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 SettingsManager, \
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
9 9
10 10
11 11 class BanUserView(BaseBoardView, RedirectNextMixin):
12 12
13 13 @transaction.atomic
14 14 def get(self, request, post_id):
15 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 19 if settings_manager.has_permission(PERMISSION_MODERATE):
20 20 # TODO Show confirmation page before ban
21 21 ban, created = Ban.objects.get_or_create(ip=post.poster_ip)
22 22 if created:
23 23 ban.reason = 'Banned for post ' + str(post_id)
24 24 ban.save()
25 25
26 26 return self.redirect_to_next(request)
@@ -1,28 +1,28 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 SettingsManager, \
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
9 9
10 10
11 11 class DeletePostView(BaseBoardView, RedirectNextMixin):
12 12
13 13 @transaction.atomic
14 14 def get(self, request, post_id):
15 15 post = get_object_or_404(Post, id=post_id)
16 16
17 17 opening_post = post.is_opening()
18 18
19 settings_manager = SettingsManager(request.session)
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)
23 23
24 24 if not opening_post:
25 25 thread = post.thread_new
26 26 return redirect('thread', post_id=thread.get_opening_post().id)
27 27 else:
28 28 return self.redirect_to_next(request)
@@ -1,33 +1,33 b''
1 1 from django.shortcuts import render, redirect
2 2
3 from boards.abstracts.settingsmanager import SettingsManager, \
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
8 8
9 9 __author__ = 'neko259'
10 10
11 11
12 12 class LoginView(BaseBoardView):
13 13
14 14 def get(self, request, form=None):
15 15 context = self.get_context_data(request=request)
16 16
17 17 if not form:
18 18 form = LoginForm()
19 19 context[CONTEXT_FORM] = form
20 20
21 21 return render(request, 'boards/login.html', context)
22 22
23 23 def post(self, request):
24 24 form = LoginForm(request.POST, request.FILES,
25 25 error_class=PlainErrorList)
26 26 form.session = request.session
27 27
28 28 if form.is_valid():
29 settings_manager = SettingsManager(request.session)
29 settings_manager = get_settings_manager(request)
30 30 settings_manager.add_permission(PERMISSION_MODERATE)
31 31 return redirect('index')
32 32 else:
33 33 return self.get(request, form)
@@ -1,24 +1,24 b''
1 1 from django.shortcuts import render
2 2
3 from boards.abstracts.settingsmanager import SettingsManager, \
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
8 8
9 9 __author__ = 'neko259'
10 10
11 11
12 12 class LogoutView(BaseBoardView):
13 13
14 14 def get(self, request, form=None):
15 settings_manager = SettingsManager(request.session)
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)
19 19
20 20 if not form:
21 21 form = LoginForm()
22 22 context[CONTEXT_FORM] = form
23 23
24 24 return render(request, 'boards/login.html', context) No newline at end of file
@@ -1,60 +1,60 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
8 8 from boards.models.tag import Tag
9 9 from boards.forms import AddTagForm, PlainErrorList
10 10
11 11
12 12 class PostAdminView(BaseBoardView, DispatcherMixin):
13 13
14 14 def get(self, request, post_id, form=None):
15 settings_manager = SettingsManager(request.session)
15 settings_manager = get_settings_manager(request)
16 16 if not settings_manager.has_permission(PERMISSION_MODERATE):
17 17 redirect('index')
18 18
19 19 post = get_object_or_404(Post, id=post_id)
20 20
21 21 if not form:
22 22 dispatch_result = self.dispatch_method(request, post)
23 23 if dispatch_result:
24 24 return dispatch_result
25 25 form = AddTagForm()
26 26
27 27 context = self.get_context_data(request=request)
28 28
29 29 context['post'] = post
30 30
31 31 context['tag_form'] = form
32 32
33 33 return render(request, 'boards/post_admin.html', context)
34 34
35 35 def post(self, request, post_id):
36 settings_manager = SettingsManager(request.session)
36 settings_manager = get_settings_manager(request)
37 37 if not settings_manager.has_permission(PERMISSION_MODERATE):
38 38 redirect('index')
39 39
40 40 post = get_object_or_404(Post, id=post_id)
41 41 return self.dispatch_method(request, post)
42 42
43 43 def delete_tag(self, request, post):
44 44 tag_name = request.GET['tag']
45 45 tag = get_object_or_404(Tag, name=tag_name)
46 46
47 47 post.remove_tag(tag)
48 48
49 49 return redirect('post_admin', post.id)
50 50
51 51 def add_tag(self, request, post):
52 52 form = AddTagForm(request.POST, error_class=PlainErrorList)
53 53 if form.is_valid():
54 54 tag_name = form.cleaned_data['tag']
55 55 tag, created = Tag.objects.get_or_create(name=tag_name)
56 56
57 57 post.add_tag(tag)
58 58 return redirect('post_admin', post.id)
59 59 else:
60 60 return self.get(request, post.id, form)
@@ -1,35 +1,35 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
8 8
9 9 class SettingsView(BaseBoardView):
10 10
11 11 def get(self, request):
12 12 context = self.get_context_data(request=request)
13 settings_manager = SettingsManager(request.session)
13 settings_manager = get_settings_manager(request)
14 14
15 15 selected_theme = settings_manager.get_theme()
16 16
17 17 form = SettingsForm(initial={'theme': selected_theme},
18 18 error_class=PlainErrorList)
19 19
20 20 context[CONTEXT_FORM] = form
21 21
22 22 return render(request, 'boards/settings.html', context)
23 23
24 24 def post(self, request):
25 settings_manager = SettingsManager(request.session)
25 settings_manager = get_settings_manager(request)
26 26
27 27 with transaction.atomic():
28 28 form = SettingsForm(request.POST, error_class=PlainErrorList)
29 29
30 30 if form.is_valid():
31 31 selected_theme = form.cleaned_data['theme']
32 32
33 33 settings_manager.set_theme(selected_theme)
34 34
35 35 return redirect('settings')
@@ -1,91 +1,92 b''
1 1 from django.shortcuts import get_object_or_404
2 from boards import utils
3 from boards.abstracts.settingsmanager import SettingsManager
4 from boards.models import Tag, Post
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
12 13 class TagView(AllThreadsView, DispatcherMixin, RedirectNextMixin):
13 14
14 15 tag_name = None
15 16
16 17 def get_threads(self):
17 18 tag = get_object_or_404(Tag, name=self.tag_name)
18 19
19 20 return tag.threads.all().order_by('-bump_time')
20 21
21 22 def get_context_data(self, **kwargs):
22 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 27 tag = get_object_or_404(Tag, name=self.tag_name)
27 28 context['tag'] = tag
28 29
29 30 context['fav_tags'] = settings_manager.get_fav_tags()
30 31 context['hidden_tags'] = settings_manager.get_hidden_tags()
31 32
32 33 return context
33 34
34 35 def get(self, request, tag_name, page=DEFAULT_PAGE, form=None):
35 36 self.tag_name = tag_name
36 37
37 38 dispatch_result = self.dispatch_method(request)
38 39 if dispatch_result:
39 40 return dispatch_result
40 41 else:
41 42 return super(TagView, self).get(request, page, form)
42 43
43 44 def post(self, request, tag_name, page=DEFAULT_PAGE):
44 45 form = ThreadForm(request.POST, request.FILES,
45 46 error_class=PlainErrorList)
46 47 form.session = request.session
47 48
48 49 if form.is_valid():
49 50 return self.create_thread(request, form)
50 51 if form.need_to_ban:
51 52 # Ban user because he is suspected to be a bot
52 53 self._ban_current_user(request)
53 54
54 55 return self.get(request, tag_name, page, form)
55 56
56 57 def subscribe(self, request):
57 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 61 settings_manager.add_fav_tag(tag)
61 62
62 63 return self.redirect_to_next(request)
63 64
64 65 def unsubscribe(self, request):
65 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 69 settings_manager.del_fav_tag(tag)
69 70
70 71 return self.redirect_to_next(request)
71 72
72 73 def hide(self, request):
73 74 """
74 75 Adds tag to user's hidden tags. Threads with this tag will not be
75 76 shown.
76 77 """
77 78
78 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 82 settings_manager.add_hidden_tag(tag)
82 83
83 84 def unhide(self, request):
84 85 """
85 86 Removed tag from user's hidden tags.
86 87 """
87 88
88 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 92 settings_manager.del_hidden_tag(tag)
General Comments 0
You need to be logged in to leave comments. Login now