# HG changeset patch # User neko259 # Date 2013-11-27 19:37:52 # Node ID cc6ec42b16f5ded06928ad360e70adb1be8669b1 # Parent b00a7edbb97de0a3af2551262e567ae3e310cebd Small refactoring. Added index view test diff --git a/boards/tests.py b/boards/tests.py --- a/boards/tests.py +++ b/boards/tests.py @@ -178,8 +178,6 @@ class PagesTest(TestCase): class FormTest(TestCase): def test_post_validation(self): - """Test the validation of the post form""" - # Disable captcha for the test captcha_enabled = settings.ENABLE_CAPTCHA settings.ENABLE_CAPTCHA = False @@ -222,3 +220,14 @@ class FormTest(TestCase): # Restore captcha setting settings.ENABLE_CAPTCHA = captcha_enabled + + +class ViewTest(TestCase): + def test_index(self): + client = Client() + + response = client.get('/') + self.assertEqual(HTTP_CODE_OK, response.status_code, 'Index page not ' + 'opened') + self.assertEqual('boards/posting_general.html', response.templates[0] + .name, 'Index page should open posting_general template') \ No newline at end of file diff --git a/boards/views.py b/boards/views.py --- a/boards/views.py +++ b/boards/views.py @@ -55,16 +55,7 @@ def index(request, page=0): threads = [] for thread_to_show in Post.objects.get_threads(page=int(page)): - last_replies = thread_to_show.get_last_replies() - skipped_replies_count = thread_to_show.get_replies().count() \ - - len(last_replies) - 1 - threads.append({ - 'thread': thread_to_show, - 'op': thread_to_show.get_replies()[0], - 'bumpable': thread_to_show.can_bump(), - 'last_replies': last_replies, - 'skipped_replies': skipped_replies_count, - }) + threads.append(_get_template_thread(thread_to_show)) # TODO Make this generic for tag and threads list pages context['threads'] = None if len(threads) == 0 else threads @@ -142,16 +133,7 @@ def tag(request, tag_name, page=0): tag = get_object_or_404(Tag, name=tag_name) threads = [] for thread_to_show in Post.objects.get_threads(page=int(page), tag=tag): - last_replies = thread_to_show.get_last_replies() - skipped_replies_count = thread_to_show.get_replies().count() \ - - len(last_replies) - 1 - threads.append({ - 'thread': thread_to_show, - 'op': thread_to_show.get_replies()[0], - 'bumpable': thread_to_show.can_bump(), - 'last_replies': last_replies, - 'skipped_replies': skipped_replies_count, - }) + threads.append(_get_template_thread(thread_to_show)) if request.method == 'POST': form = ThreadForm(request.POST, request.FILES, @@ -572,3 +554,18 @@ def _datetime_to_epoch(datetime): return int(time.mktime(timezone.localtime( datetime,timezone.get_current_timezone()).timetuple()) * 1000000 + datetime.microsecond) + + +def _get_template_thread(thread_to_show): + """Get template values for thread""" + + last_replies = thread_to_show.get_last_replies() + skipped_replies_count = thread_to_show.get_replies().count() \ + - len(last_replies) - 1 + return { + 'thread': thread_to_show, + 'op': thread_to_show.get_replies()[0], + 'bumpable': thread_to_show.can_bump(), + 'last_replies': last_replies, + 'skipped_replies': skipped_replies_count, + }