##// END OF EJS Templates
Fixed page 404 test. Added a cat image to the banned page.
neko259 -
r181:fcbeb0b1 default
parent child Browse files
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
@@ -1,11 +1,12 b''
1 {% extends "boards/static_base.html" %}
1 {% extends "boards/static_base.html" %}
2
2
3 {% load i18n %}
3 {% load i18n %}
4
4
5 {% block head %}
5 {% block head %}
6 <title>{% trans "Banned" %}</title>
6 <title>{% trans "Banned" %}</title>
7 {% endblock %}
7 {% endblock %}
8
8
9 {% block staticcontent %}
9 {% block staticcontent %}
10 {% trans 'Your IP address has been banned. Contact the administrator' %}
10 <p><img src="{{ STATIC_URL }}images/banned.png" width="200" /></p>
11 <p>{% trans 'Your IP address has been banned. Contact the administrator' %}</p>
11 {% endblock %} No newline at end of file
12 {% endblock %}
@@ -1,173 +1,175 b''
1 # coding=utf-8
1 # coding=utf-8
2 from django.utils.unittest import TestCase
2 from django.utils.unittest import TestCase
3 from django.test.client import Client
3 from django.test.client import Client
4 import time
4 import time
5
5
6 import boards
6 import boards
7
7
8 from boards.models import Post, Tag
8 from boards.models import Post, Tag
9 from neboard import settings
9 from neboard import settings
10
10
11 TEST_TEXT = 'test text'
11 TEST_TEXT = 'test text'
12
12
13 NEW_THREAD_PAGE = '/'
13 NEW_THREAD_PAGE = '/'
14 THREAD_PAGE_ONE = '/thread/1/'
14 THREAD_PAGE_ONE = '/thread/1/'
15 THREAD_PAGE = '/thread/'
15 THREAD_PAGE = '/thread/'
16 TAG_PAGE = '/tag/'
16 TAG_PAGE = '/tag/'
17 HTTP_CODE_REDIRECT = 302
17 HTTP_CODE_REDIRECT = 302
18 HTTP_CODE_OK = 200
18 HTTP_CODE_OK = 200
19 HTTP_CODE_NOT_FOUND = 404
19 HTTP_CODE_NOT_FOUND = 404
20
20
21
21
22 class BoardTests(TestCase):
22 class BoardTests(TestCase):
23 def _create_post(self):
23 def _create_post(self):
24 return Post.objects.create_post(title='title',
24 return Post.objects.create_post(title='title',
25 text='text')
25 text='text')
26
26
27 def test_post_add(self):
27 def test_post_add(self):
28 post = self._create_post()
28 post = self._create_post()
29
29
30 self.assertIsNotNone(post)
30 self.assertIsNotNone(post)
31 self.assertEqual(boards.models.NO_PARENT, post.parent)
31 self.assertEqual(boards.models.NO_PARENT, post.parent)
32
32
33 def test_delete_post(self):
33 def test_delete_post(self):
34 post = self._create_post()
34 post = self._create_post()
35 post_id = post.id
35 post_id = post.id
36
36
37 Post.objects.delete_post(post)
37 Post.objects.delete_post(post)
38
38
39 self.assertFalse(Post.objects.exists(post_id))
39 self.assertFalse(Post.objects.exists(post_id))
40
40
41 def test_delete_posts_by_ip(self):
41 def test_delete_posts_by_ip(self):
42 post = self._create_post()
42 post = self._create_post()
43 post_id = post.id
43 post_id = post.id
44
44
45 Post.objects.delete_posts_by_ip('0.0.0.0')
45 Post.objects.delete_posts_by_ip('0.0.0.0')
46
46
47 self.assertFalse(Post.objects.exists(post_id))
47 self.assertFalse(Post.objects.exists(post_id))
48
48
49 # Authentication tests
49 # Authentication tests
50
50
51 def test_get_thread(self):
51 def test_get_thread(self):
52 opening_post = self._create_post()
52 opening_post = self._create_post()
53
53
54 for i in range(0, 2):
54 for i in range(0, 2):
55 Post.objects.create_post('title', 'text',thread=opening_post)
55 Post.objects.create_post('title', 'text',thread=opening_post)
56
56
57 thread = Post.objects.get_thread(opening_post.id)
57 thread = Post.objects.get_thread(opening_post.id)
58
58
59 self.assertEqual(3, len(thread))
59 self.assertEqual(3, len(thread))
60
60
61 def test_create_post_with_tag(self):
61 def test_create_post_with_tag(self):
62 tag = Tag.objects.create(name='test_tag')
62 tag = Tag.objects.create(name='test_tag')
63 post = Post.objects.create_post(title='title', text='text', tags=[tag])
63 post = Post.objects.create_post(title='title', text='text', tags=[tag])
64 self.assertIsNotNone(post)
64 self.assertIsNotNone(post)
65
65
66 def test_thread_max_count(self):
66 def test_thread_max_count(self):
67 for i in range(settings.MAX_THREAD_COUNT + 1):
67 for i in range(settings.MAX_THREAD_COUNT + 1):
68 self._create_post()
68 self._create_post()
69
69
70 self.assertEqual(settings.MAX_THREAD_COUNT,
70 self.assertEqual(settings.MAX_THREAD_COUNT,
71 len(Post.objects.get_threads()))
71 len(Post.objects.get_threads()))
72
72
73 def test_pages(self):
73 def test_pages(self):
74 """Test that the thread list is properly split into pages"""
74 """Test that the thread list is properly split into pages"""
75
75
76 for i in range(settings.MAX_THREAD_COUNT):
76 for i in range(settings.MAX_THREAD_COUNT):
77 self._create_post()
77 self._create_post()
78
78
79 all_threads = Post.objects.get_threads()
79 all_threads = Post.objects.get_threads()
80
80
81 posts_in_second_page = Post.objects.get_threads(page=1)
81 posts_in_second_page = Post.objects.get_threads(page=1)
82 first_post = posts_in_second_page[0]
82 first_post = posts_in_second_page[0]
83
83
84 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
84 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
85 first_post.id)
85 first_post.id)
86
86
87 def test_post_validation(self):
87 def test_post_validation(self):
88 """Test the validation of the post form"""
88 """Test the validation of the post form"""
89
89
90 # Disable captcha for the test
90 # Disable captcha for the test
91 captcha_enabled = settings.ENABLE_CAPTCHA
91 captcha_enabled = settings.ENABLE_CAPTCHA
92 settings.ENABLE_CAPTCHA = False
92 settings.ENABLE_CAPTCHA = False
93
93
94 Post.objects.all().delete()
94 Post.objects.all().delete()
95
95
96 client = Client()
96 client = Client()
97
97
98 valid_tags = u'tag1 tag_2 тег_3'
98 valid_tags = u'tag1 tag_2 тег_3'
99 invalid_tags = u'$%_356 ---'
99 invalid_tags = u'$%_356 ---'
100
100
101 response = client.post(NEW_THREAD_PAGE, {'title': 'test title',
101 response = client.post(NEW_THREAD_PAGE, {'title': 'test title',
102 'text': TEST_TEXT,
102 'text': TEST_TEXT,
103 'tags': valid_tags})
103 'tags': valid_tags})
104 self.assertEqual(response.status_code, HTTP_CODE_REDIRECT,
104 self.assertEqual(response.status_code, HTTP_CODE_REDIRECT,
105 msg='Posting new message failed: got code ' +
105 msg='Posting new message failed: got code ' +
106 str(response.status_code))
106 str(response.status_code))
107
107
108 self.assertEqual(1, Post.objects.count(),
108 self.assertEqual(1, Post.objects.count(),
109 msg='No posts were created')
109 msg='No posts were created')
110
110
111 client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT,
111 client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT,
112 'tags': invalid_tags})
112 'tags': invalid_tags})
113 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
113 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
114 'where it should fail')
114 'where it should fail')
115
115
116 # Change posting delay so we don't have to wait for 30 seconds or more
117 old_posting_delay = settings.POSTING_DELAY
116 # Wait fot the posting delay or we won't be able to post
118 # Wait fot the posting delay or we won't be able to post
119 settings.POSTING_DELAY = 1
117 time.sleep(settings.POSTING_DELAY + 1)
120 time.sleep(settings.POSTING_DELAY + 1)
118 response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT,
121 response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT,
119 'tags': valid_tags})
122 'tags': valid_tags})
120 self.assertEqual(HTTP_CODE_REDIRECT, response.status_code,
123 self.assertEqual(HTTP_CODE_REDIRECT, response.status_code,
121 msg=u'Posting new message failed: got code ' +
124 msg=u'Posting new message failed: got code ' +
122 str(response.status_code))
125 str(response.status_code))
126 # Restore posting delay
127 settings.POSTING_DELAY = old_posting_delay
123
128
124 self.assertEqual(2, Post.objects.count(),
129 self.assertEqual(2, Post.objects.count(),
125 msg=u'No posts were created')
130 msg=u'No posts were created')
126
131
127 # Restore captcha setting
132 # Restore captcha setting
128 settings.ENABLE_CAPTCHA = captcha_enabled
133 settings.ENABLE_CAPTCHA = captcha_enabled
129
134
130 # TODO This test fails for now. We must check for 404.html instead of
131 # code 404
132 def test_404(self):
135 def test_404(self):
133 """Test receiving error 404 when opening a non-existent page"""
136 """Test receiving error 404 when opening a non-existent page"""
134
137
135 Post.objects.all().delete()
138 Post.objects.all().delete()
136 Tag.objects.all().delete()
139 Tag.objects.all().delete()
137
140
138 tag_name = u'test_tag'
141 tag_name = u'test_tag'
139 tags, = [Tag.objects.get_or_create(name=tag_name)]
142 tags, = [Tag.objects.get_or_create(name=tag_name)]
140 client = Client()
143 client = Client()
141
144
142 Post.objects.create_post('title', TEST_TEXT, tags=tags)
145 Post.objects.create_post('title', TEST_TEXT, tags=tags)
143
146
144 existing_post_id = Post.objects.all()[0].id
147 existing_post_id = Post.objects.all()[0].id
145 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
148 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
146 '/')
149 '/')
147 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
150 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
148 u'Cannot open existing thread')
151 u'Cannot open existing thread')
149
152
150 response_not_existing = client.get(THREAD_PAGE + str(
153 response_not_existing = client.get(THREAD_PAGE + str(
151 existing_post_id + 1) + '/')
154 existing_post_id + 1) + '/')
152 response_not_existing.get_full_path()
155 self.assertEqual('boards/404.html',
153 self.assertEqual(HTTP_CODE_NOT_FOUND,
156 response_not_existing.templates[0].name,
154 response_not_existing.status_code,
155 u'Not existing thread is opened')
157 u'Not existing thread is opened')
156
158
157 response_existing = client.get(TAG_PAGE + tag_name + '/')
159 response_existing = client.get(TAG_PAGE + tag_name + '/')
158 self.assertEqual(HTTP_CODE_OK,
160 self.assertEqual(HTTP_CODE_OK,
159 response_existing.status_code,
161 response_existing.status_code,
160 u'Cannot open existing tag')
162 u'Cannot open existing tag')
161
163
162 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
164 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
163 self.assertEqual(HTTP_CODE_NOT_FOUND,
165 self.assertEqual('boards/404.html',
164 response_not_existing.status_code,
166 response_not_existing.templates[0].name,
165 u'Not existing tag is opened')
167 u'Not existing tag is opened')
166
168
167 reply_id = Post.objects.create_post('', TEST_TEXT,
169 reply_id = Post.objects.create_post('', TEST_TEXT,
168 parent_id=existing_post_id)
170 thread=Post.objects.all()[0])
169 response_not_existing = client.get(THREAD_PAGE + str(
171 response_not_existing = client.get(THREAD_PAGE + str(
170 reply_id) + '/')
172 reply_id) + '/')
171 self.assertEqual(HTTP_CODE_NOT_FOUND,
173 self.assertEqual('boards/404.html',
172 response_not_existing.status_code,
174 response_not_existing.templates[0].name,
173 u'Not existing thread is opened')
175 u'Reply is opened as a thread')
@@ -1,358 +1,355 b''
1 import hashlib
1 import hashlib
2 from django.core.urlresolvers import reverse
2 from django.core.urlresolvers import reverse
3 from django.http import HttpResponseRedirect
3 from django.http import HttpResponseRedirect
4 from django.template import RequestContext
4 from django.template import RequestContext
5 from django.shortcuts import render, redirect, get_object_or_404
5 from django.shortcuts import render, redirect, get_object_or_404
6 from django.utils import timezone
6 from django.utils import timezone
7
7
8 from boards import forms
8 from boards import forms
9 import boards
9 import boards
10 from boards import utils
10 from boards import utils
11 from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \
11 from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \
12 ThreadCaptchaForm, PostCaptchaForm, LoginForm
12 ThreadCaptchaForm, PostCaptchaForm, LoginForm
13
13
14 from boards.models import Post, Tag, Ban, User, RANK_USER, NO_PARENT
14 from boards.models import Post, Tag, Ban, User, RANK_USER, NO_PARENT
15 from boards import authors
15 from boards import authors
16 import neboard
16 import neboard
17
17
18
18
19 def index(request, page=0):
19 def index(request, page=0):
20 context = _init_default_context(request)
20 context = _init_default_context(request)
21
21
22 if utils.need_include_captcha(request):
22 if utils.need_include_captcha(request):
23 threadFormClass = ThreadCaptchaForm
23 threadFormClass = ThreadCaptchaForm
24 kwargs = {'request': request}
24 kwargs = {'request': request}
25 else:
25 else:
26 threadFormClass = ThreadForm
26 threadFormClass = ThreadForm
27 kwargs = {}
27 kwargs = {}
28
28
29 if request.method == 'POST':
29 if request.method == 'POST':
30 form = threadFormClass(request.POST, request.FILES,
30 form = threadFormClass(request.POST, request.FILES,
31 error_class=PlainErrorList, **kwargs)
31 error_class=PlainErrorList, **kwargs)
32 form.session = request.session
32 form.session = request.session
33
33
34 if form.is_valid():
34 if form.is_valid():
35 return _new_post(request, form)
35 return _new_post(request, form)
36 else:
36 else:
37 form = threadFormClass(error_class=PlainErrorList, **kwargs)
37 form = threadFormClass(error_class=PlainErrorList, **kwargs)
38
38
39 threads = []
39 threads = []
40 for thread in Post.objects.get_threads(page=int(page)):
40 for thread in Post.objects.get_threads(page=int(page)):
41 threads.append({'thread': thread,
41 threads.append({'thread': thread,
42 'bumpable': thread.can_bump()})
42 'bumpable': thread.can_bump()})
43
43
44 context['threads'] = None if len(threads) == 0 else threads
44 context['threads'] = None if len(threads) == 0 else threads
45 context['form'] = form
45 context['form'] = form
46 context['pages'] = range(Post.objects.get_thread_page_count())
46 context['pages'] = range(Post.objects.get_thread_page_count())
47
47
48 return render(request, 'boards/posting_general.html',
48 return render(request, 'boards/posting_general.html',
49 context)
49 context)
50
50
51
51
52 def _new_post(request, form, thread_id=boards.models.NO_PARENT):
52 def _new_post(request, form, thread_id=boards.models.NO_PARENT):
53 """Add a new post (in thread or as a reply)."""
53 """Add a new post (in thread or as a reply)."""
54
54
55 ip = _get_client_ip(request)
55 ip = _get_client_ip(request)
56 is_banned = Ban.objects.filter(ip=ip).count() > 0
56 is_banned = Ban.objects.filter(ip=ip).count() > 0
57
57
58 if is_banned:
58 if is_banned:
59 return redirect(you_are_banned)
59 return redirect(you_are_banned)
60
60
61 data = form.cleaned_data
61 data = form.cleaned_data
62
62
63 title = data['title']
63 title = data['title']
64 text = data['text']
64 text = data['text']
65
65
66 if 'image' in data.keys():
66 if 'image' in data.keys():
67 image = data['image']
67 image = data['image']
68 else:
68 else:
69 image = None
69 image = None
70
70
71 tags = []
71 tags = []
72
72
73 new_thread = thread_id == boards.models.NO_PARENT
73 new_thread = thread_id == boards.models.NO_PARENT
74 if new_thread:
74 if new_thread:
75 tag_strings = data['tags']
75 tag_strings = data['tags']
76
76
77 if tag_strings:
77 if tag_strings:
78 tag_strings = tag_strings.split(' ')
78 tag_strings = tag_strings.split(' ')
79 for tag_name in tag_strings:
79 for tag_name in tag_strings:
80 tag_name = tag_name.strip()
80 tag_name = tag_name.strip()
81 if len(tag_name) > 0:
81 if len(tag_name) > 0:
82 tag, created = Tag.objects.get_or_create(name=tag_name)
82 tag, created = Tag.objects.get_or_create(name=tag_name)
83 tags.append(tag)
83 tags.append(tag)
84
84
85 # TODO Add a possibility to define a link image instead of an image file.
86 # If a link is given, download the image automatically.
87
88 op = None if thread_id == boards.models.NO_PARENT else \
85 op = None if thread_id == boards.models.NO_PARENT else \
89 get_object_or_404(Post, id=thread_id)
86 get_object_or_404(Post, id=thread_id)
90 post = Post.objects.create_post(title=title, text=text, ip=ip,
87 post = Post.objects.create_post(title=title, text=text, ip=ip,
91 thread=op, image=image,
88 thread=op, image=image,
92 tags=tags)
89 tags=tags)
93
90
94 thread_to_show = (post.id if new_thread else thread_id)
91 thread_to_show = (post.id if new_thread else thread_id)
95
92
96 if new_thread:
93 if new_thread:
97 return redirect(thread, post_id=thread_to_show)
94 return redirect(thread, post_id=thread_to_show)
98 else:
95 else:
99 return redirect(reverse(thread, kwargs={'post_id': thread_to_show}) +
96 return redirect(reverse(thread, kwargs={'post_id': thread_to_show}) +
100 '#' + str(post.id))
97 '#' + str(post.id))
101
98
102
99
103 def tag(request, tag_name, page=0):
100 def tag(request, tag_name, page=0):
104 """Get all tag threads (posts without a parent)."""
101 """Get all tag threads (posts without a parent)."""
105
102
106 tag = get_object_or_404(Tag, name=tag_name)
103 tag = get_object_or_404(Tag, name=tag_name)
107 threads = []
104 threads = []
108 for thread in Post.objects.get_threads(tag=tag, page=int(page)):
105 for thread in Post.objects.get_threads(tag=tag, page=int(page)):
109 threads.append({'thread': thread,
106 threads.append({'thread': thread,
110 'bumpable': thread.can_bump()})
107 'bumpable': thread.can_bump()})
111
108
112 if request.method == 'POST':
109 if request.method == 'POST':
113 form = ThreadForm(request.POST, request.FILES,
110 form = ThreadForm(request.POST, request.FILES,
114 error_class=PlainErrorList)
111 error_class=PlainErrorList)
115 if form.is_valid():
112 if form.is_valid():
116 return _new_post(request, form)
113 return _new_post(request, form)
117 else:
114 else:
118 form = forms.ThreadForm(initial={'tags': tag_name},
115 form = forms.ThreadForm(initial={'tags': tag_name},
119 error_class=PlainErrorList)
116 error_class=PlainErrorList)
120
117
121 context = _init_default_context(request)
118 context = _init_default_context(request)
122 context['threads'] = None if len(threads) == 0 else threads
119 context['threads'] = None if len(threads) == 0 else threads
123 context['tag'] = tag
120 context['tag'] = tag
124 context['pages'] = range(Post.objects.get_thread_page_count(tag=tag))
121 context['pages'] = range(Post.objects.get_thread_page_count(tag=tag))
125
122
126 context['form'] = form
123 context['form'] = form
127
124
128 return render(request, 'boards/posting_general.html',
125 return render(request, 'boards/posting_general.html',
129 context)
126 context)
130
127
131
128
132 def thread(request, post_id):
129 def thread(request, post_id):
133 """Get all thread posts"""
130 """Get all thread posts"""
134
131
135 if utils.need_include_captcha(request):
132 if utils.need_include_captcha(request):
136 postFormClass = PostCaptchaForm
133 postFormClass = PostCaptchaForm
137 kwargs = {'request': request}
134 kwargs = {'request': request}
138 else:
135 else:
139 postFormClass = PostForm
136 postFormClass = PostForm
140 kwargs = {}
137 kwargs = {}
141
138
142 if request.method == 'POST':
139 if request.method == 'POST':
143 form = postFormClass(request.POST, request.FILES,
140 form = postFormClass(request.POST, request.FILES,
144 error_class=PlainErrorList, **kwargs)
141 error_class=PlainErrorList, **kwargs)
145 form.session = request.session
142 form.session = request.session
146
143
147 if form.is_valid():
144 if form.is_valid():
148 return _new_post(request, form, post_id)
145 return _new_post(request, form, post_id)
149 else:
146 else:
150 form = postFormClass(error_class=PlainErrorList, **kwargs)
147 form = postFormClass(error_class=PlainErrorList, **kwargs)
151
148
152 posts = Post.objects.get_thread(post_id)
149 posts = Post.objects.get_thread(post_id)
153
150
154 context = _init_default_context(request)
151 context = _init_default_context(request)
155
152
156 context['posts'] = posts
153 context['posts'] = posts
157 context['form'] = form
154 context['form'] = form
158 context['bumpable'] = posts[0].can_bump()
155 context['bumpable'] = posts[0].can_bump()
159
156
160 return render(request, 'boards/thread.html', context)
157 return render(request, 'boards/thread.html', context)
161
158
162
159
163 def login(request):
160 def login(request):
164 """Log in with user id"""
161 """Log in with user id"""
165
162
166 context = _init_default_context(request)
163 context = _init_default_context(request)
167
164
168 if request.method == 'POST':
165 if request.method == 'POST':
169 form = LoginForm(request.POST, request.FILES,
166 form = LoginForm(request.POST, request.FILES,
170 error_class=PlainErrorList)
167 error_class=PlainErrorList)
171 if form.is_valid():
168 if form.is_valid():
172 user = User.objects.get(user_id=form.cleaned_data['user_id'])
169 user = User.objects.get(user_id=form.cleaned_data['user_id'])
173 request.session['user_id'] = user.id
170 request.session['user_id'] = user.id
174 return redirect(index)
171 return redirect(index)
175
172
176 else:
173 else:
177 form = LoginForm()
174 form = LoginForm()
178
175
179 context['form'] = form
176 context['form'] = form
180
177
181 return render(request, 'boards/login.html', context)
178 return render(request, 'boards/login.html', context)
182
179
183
180
184 def settings(request):
181 def settings(request):
185 """User's settings"""
182 """User's settings"""
186
183
187 context = _init_default_context(request)
184 context = _init_default_context(request)
188
185
189 if request.method == 'POST':
186 if request.method == 'POST':
190 form = SettingsForm(request.POST)
187 form = SettingsForm(request.POST)
191 if form.is_valid():
188 if form.is_valid():
192 selected_theme = form.cleaned_data['theme']
189 selected_theme = form.cleaned_data['theme']
193
190
194 user = _get_user(request)
191 user = _get_user(request)
195 user.save_setting('theme', selected_theme)
192 user.save_setting('theme', selected_theme)
196
193
197 return redirect(settings)
194 return redirect(settings)
198 else:
195 else:
199 selected_theme = _get_theme(request)
196 selected_theme = _get_theme(request)
200 form = SettingsForm(initial={'theme': selected_theme})
197 form = SettingsForm(initial={'theme': selected_theme})
201 context['form'] = form
198 context['form'] = form
202
199
203 return render(request, 'boards/settings.html', context)
200 return render(request, 'boards/settings.html', context)
204
201
205
202
206 def all_tags(request):
203 def all_tags(request):
207 """All tags list"""
204 """All tags list"""
208
205
209 context = _init_default_context(request)
206 context = _init_default_context(request)
210 context['all_tags'] = Tag.objects.get_not_empty_tags()
207 context['all_tags'] = Tag.objects.get_not_empty_tags()
211
208
212 return render(request, 'boards/tags.html', context)
209 return render(request, 'boards/tags.html', context)
213
210
214
211
215 def jump_to_post(request, post_id):
212 def jump_to_post(request, post_id):
216 """Determine thread in which the requested post is and open it's page"""
213 """Determine thread in which the requested post is and open it's page"""
217
214
218 post = get_object_or_404(Post, id=post_id)
215 post = get_object_or_404(Post, id=post_id)
219
216
220 if not post.thread:
217 if not post.thread:
221 return redirect(thread, post_id=post.id)
218 return redirect(thread, post_id=post.id)
222 else:
219 else:
223 return redirect(reverse(thread, kwargs={'post_id': post.thread.id})
220 return redirect(reverse(thread, kwargs={'post_id': post.thread.id})
224 + '#' + str(post.id))
221 + '#' + str(post.id))
225
222
226
223
227 def authors(request):
224 def authors(request):
228 context = _init_default_context(request)
225 context = _init_default_context(request)
229 context['authors'] = boards.authors.authors
226 context['authors'] = boards.authors.authors
230
227
231 return render(request, 'boards/authors.html', context)
228 return render(request, 'boards/authors.html', context)
232
229
233
230
234 def delete(request, post_id):
231 def delete(request, post_id):
235 user = _get_user(request)
232 user = _get_user(request)
236 post = get_object_or_404(Post, id=post_id)
233 post = get_object_or_404(Post, id=post_id)
237
234
238 if user.is_moderator():
235 if user.is_moderator():
239 # TODO Show confirmation page before deletion
236 # TODO Show confirmation page before deletion
240 Post.objects.delete_post(post)
237 Post.objects.delete_post(post)
241
238
242 if not post.thread:
239 if not post.thread:
243 return _redirect_to_next(request)
240 return _redirect_to_next(request)
244 else:
241 else:
245 return redirect(thread, post_id=post.thread.id)
242 return redirect(thread, post_id=post.thread.id)
246
243
247
244
248 def ban(request, post_id):
245 def ban(request, post_id):
249 user = _get_user(request)
246 user = _get_user(request)
250 post = get_object_or_404(Post, id=post_id)
247 post = get_object_or_404(Post, id=post_id)
251
248
252 if user.is_moderator():
249 if user.is_moderator():
253 # TODO Show confirmation page before ban
250 # TODO Show confirmation page before ban
254 Ban.objects.get_or_create(ip=post.poster_ip)
251 Ban.objects.get_or_create(ip=post.poster_ip)
255
252
256 return _redirect_to_next(request)
253 return _redirect_to_next(request)
257
254
258
255
259 def you_are_banned(request):
256 def you_are_banned(request):
260 context = _init_default_context(request)
257 context = _init_default_context(request)
261 return render(request, 'boards/staticpages/banned.html', context)
258 return render(request, 'boards/staticpages/banned.html', context)
262
259
263
260
264 def page_404(request):
261 def page_404(request):
265 context = _init_default_context(request)
262 context = _init_default_context(request)
266 return render(request, 'boards/404.html', context)
263 return render(request, 'boards/404.html', context)
267
264
268
265
269 def tag_subscribe(request, tag_name):
266 def tag_subscribe(request, tag_name):
270 user = _get_user(request)
267 user = _get_user(request)
271 tag = get_object_or_404(Tag, name=tag_name)
268 tag = get_object_or_404(Tag, name=tag_name)
272
269
273 if not tag in user.fav_tags.all():
270 if not tag in user.fav_tags.all():
274 user.fav_tags.add(tag)
271 user.fav_tags.add(tag)
275
272
276 return _redirect_to_next(request)
273 return _redirect_to_next(request)
277
274
278
275
279 def tag_unsubscribe(request, tag_name):
276 def tag_unsubscribe(request, tag_name):
280 user = _get_user(request)
277 user = _get_user(request)
281 tag = get_object_or_404(Tag, name=tag_name)
278 tag = get_object_or_404(Tag, name=tag_name)
282
279
283 if tag in user.fav_tags.all():
280 if tag in user.fav_tags.all():
284 user.fav_tags.remove(tag)
281 user.fav_tags.remove(tag)
285
282
286 return _redirect_to_next(request)
283 return _redirect_to_next(request)
287
284
288
285
289 def static_page(request, name):
286 def static_page(request, name):
290 context = _init_default_context(request)
287 context = _init_default_context(request)
291 return render(request, 'boards/staticpages/' + name + '.html', context)
288 return render(request, 'boards/staticpages/' + name + '.html', context)
292
289
293
290
294 def _get_theme(request, user=None):
291 def _get_theme(request, user=None):
295 """Get user's CSS theme"""
292 """Get user's CSS theme"""
296
293
297 if not user:
294 if not user:
298 user = _get_user(request)
295 user = _get_user(request)
299 theme = user.get_setting('theme')
296 theme = user.get_setting('theme')
300 if not theme:
297 if not theme:
301 theme = neboard.settings.DEFAULT_THEME
298 theme = neboard.settings.DEFAULT_THEME
302
299
303 return theme
300 return theme
304
301
305
302
306 def _get_client_ip(request):
303 def _get_client_ip(request):
307 x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
304 x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
308 if x_forwarded_for:
305 if x_forwarded_for:
309 ip = x_forwarded_for.split(',')[-1].strip()
306 ip = x_forwarded_for.split(',')[-1].strip()
310 else:
307 else:
311 ip = request.META.get('REMOTE_ADDR')
308 ip = request.META.get('REMOTE_ADDR')
312 return ip
309 return ip
313
310
314
311
315 def _init_default_context(request):
312 def _init_default_context(request):
316 """Create context with default values that are used in most views"""
313 """Create context with default values that are used in most views"""
317
314
318 context = RequestContext(request)
315 context = RequestContext(request)
319
316
320 user = _get_user(request)
317 user = _get_user(request)
321 context['user'] = user
318 context['user'] = user
322 context['tags'] = user.get_sorted_fav_tags()
319 context['tags'] = user.get_sorted_fav_tags()
323 context['theme'] = _get_theme(request, user)
320 context['theme'] = _get_theme(request, user)
324
321
325 return context
322 return context
326
323
327
324
328 def _get_user(request):
325 def _get_user(request):
329 """Get current user from the session"""
326 """Get current user from the session"""
330
327
331 session = request.session
328 session = request.session
332 if not 'user_id' in session:
329 if not 'user_id' in session:
333 request.session.save()
330 request.session.save()
334
331
335 md5 = hashlib.md5()
332 md5 = hashlib.md5()
336 md5.update(session.session_key)
333 md5.update(session.session_key)
337 new_id = md5.hexdigest()
334 new_id = md5.hexdigest()
338
335
339 time_now = timezone.now()
336 time_now = timezone.now()
340 user = User.objects.create(user_id=new_id, rank=RANK_USER,
337 user = User.objects.create(user_id=new_id, rank=RANK_USER,
341 registration_time=time_now,
338 registration_time=time_now,
342 last_access_time=time_now)
339 last_access_time=time_now)
343
340
344 session['user_id'] = user.id
341 session['user_id'] = user.id
345 else:
342 else:
346 user = User.objects.get(id=session['user_id'])
343 user = User.objects.get(id=session['user_id'])
347 user.last_access_time = timezone.now()
344 user.last_access_time = timezone.now()
348 user.save()
345 user.save()
349
346
350 return user
347 return user
351
348
352
349
353 def _redirect_to_next(request):
350 def _redirect_to_next(request):
354 if 'next' in request.GET:
351 if 'next' in request.GET:
355 next_page = request.GET['next']
352 next_page = request.GET['next']
356 return HttpResponseRedirect(next_page)
353 return HttpResponseRedirect(next_page)
357 else:
354 else:
358 return redirect(index)
355 return redirect(index)
General Comments 0
You need to be logged in to leave comments. Login now