comment.poll truncate close_at

This commit is contained in:
nitely 2015-09-04 13:13:28 -03:00
parent 8f7a04acf9
commit 35b8c523fc
2 changed files with 55 additions and 4 deletions

View File

@ -459,3 +459,55 @@ class UtilsMarkdownTests(TestCase):
polls = md.get_polls()
self.assertEqual(len(polls['choices']), 0)
self.assertEqual(len(polls['polls']), 0)
def test_markdown_poll_choice_max(self):
"""
Should validate max is greater than 0
"""
comment = "[poll name=foo max=1]\n" \
"1. opt 1\n" \
"2. opt 2\n" \
"[/poll]"
md = Markdown(escape=True, hard_wrap=True)
comment_md = md.render(comment)
self.assertEqual(comment_md, '<poll name=foo>')
polls = md.get_polls()
self.assertEqual(polls['polls'][0]['choice_max'], 1)
def test_markdown_poll_choice_min(self):
"""
Should validate min is greater than 0
"""
comment = "[poll name=foo min=1]\n" \
"1. opt 1\n" \
"2. opt 2\n" \
"[/poll]"
md = Markdown(escape=True, hard_wrap=True)
comment_md = md.render(comment)
self.assertEqual(comment_md, '<poll name=foo>')
polls = md.get_polls()
self.assertEqual(polls['polls'][0]['choice_min'], 1)
def test_markdown_poll_truncates_close(self):
"""
Should truncates the close days
"""
def mock_now():
return now_fixed
org_now, timezone.now = timezone.now, mock_now
try:
comment = "[poll name=foo_1 close=100000000000d]\n" \
"# Foo or bar?\n" \
"1. opt 1\n" \
"2. opt 2\n" \
"[/poll]"
md = Markdown(escape=True, hard_wrap=True)
comment_md = md.render(comment)
self.assertEqual(comment_md, '<poll name=foo_1>')
self.assertEqual(
md.get_polls()['polls'][0]['close_at'],
now_fixed + timezone.timedelta(days=10000)
)
finally:
timezone.now = org_now

View File

@ -81,7 +81,7 @@ class BlockGrammar(mistune.BlockGrammar):
r'((?:\s+name=(?P<name>[\w\-_]+))'
r'(?:\s+min=(?P<min>\d+))?'
r'(?:\s+max=(?P<max>\d+))?'
r'(?:\s+close=(?P<close>\d+d))?'
r'(?:\s+close=(?P<close>\d+)d)?'
r'|(?P<invalid_params>[^\]]*))'
r'\])\n'
r'((?:#\s*(?P<title>[^\n]+\n))?'
@ -131,8 +131,6 @@ class BlockLexer(mistune.BlockLexer):
def parse_poll(self, m):
# todo: move to parsers/poll.py
# todo: test for numbers 1, 01, 001...
# todo: validate max > min and both > 0
token_raw = {'type': 'poll', 'raw': m.group(0)}
invalid_params = m.group('invalid_params')
invalid_body = m.group('invalid_body')
@ -147,6 +145,7 @@ class BlockLexer(mistune.BlockLexer):
name_max_len = 255
title_max_len = 255
description_max_len = 255
close_max_len = 5 # Fixed length
choices_limit = 20 # make a setting
# pre_validation()
@ -178,7 +177,7 @@ class BlockLexer(mistune.BlockLexer):
poll['choice_max'] = int(max_raw)
if close_at_raw:
days = int(close_at_raw[:-1]) # Remove 'd'
days = int(close_at_raw[:close_max_len])
poll['close_at'] = timezone.now() + timezone.timedelta(days=days)
# clean_choices()