def test_italic(self): self.assertEquals(safe_html( '*text*'), '<em>text</em>') self.assertEquals(safe_html( '_text_'), '<em>text</em>')
def test_bold(self): self.assertEquals(safe_html( '**text**'), '<strong>text</strong>') self.assertEquals(safe_html( '__text__'), '<strong>text</strong>')
def test_combined(self): self.assertEqual( safe_html("`__*text*__`"), "<tt><strong><em>text</em></strong></tt>" ) self.assertEqual( safe_html("`**_text_**`"), "<tt><strong><em>text</em></strong></tt>" ) self.assertEqual(safe_html("_*text*_"), "<em><em>text</em></em>")
def test_combined(self): self.assertEquals(safe_html( '`__*text*__`'), '<tt><strong><em>text</em></strong></tt>') self.assertEquals(safe_html( '`**_text_**`'), '<tt><strong><em>text</em></strong></tt>') self.assertEquals(safe_html( '_*text*_'), '<em><em>text</em></em>')
def vote(self, person, votes): with self._lock: if len(self.votes) >= _MAX_VOTERS_PER_POLL: raise ValueError('Too many votes per poll') if len(votes) != len(self.options): raise ValueError('Malformed vote') self.votes.append((safe_html(person), votes))
def create(self, validated_data): poll_equal_width = validated_data['equal_width'] poll_title = safe_html(validated_data['title']) poll_option_names = [ safe_html(str(option_name)) for option_name in validated_data['options'] ] with transaction.atomic(): poll = Poll.objects.create(title=poll_title, equal_width=poll_equal_width) for i, option_name in enumerate(poll_option_names): PollOption.objects.create(poll=poll, position=i, name=option_name) return poll
def from_config(config): poll = _Poll() if _KEY_OPTIONS not in config \ or _KEY_TITLE not in config: raise ValueError('Malformed configuration: %s' % config) poll.config = { _KEY_EQUAL_WIDTH: bool(config.get(_KEY_EQUAL_WIDTH, False)), _KEY_TITLE: safe_html(config[_KEY_TITLE]), _KEY_DESCRIPTION: safe_html(config[_KEY_DESCRIPTION]), _KEY_LIMIT_DATE: config[_KEY_LIMIT_DATE], _KEY_OPTIONS: [], _KEY_PEOPLE: config[_KEY_PEOPLE], } poll.config[_KEY_OPTIONS][:] = (safe_html(x) for x in config[_KEY_OPTIONS]) return poll
def create(self, validated_data): poll_expires_at = timezone.now() + _PollLifetime.to_relativedelta( validated_data['lifetime']) poll_equal_width = validated_data['equal_width'] poll_title = safe_html(validated_data['title']) poll_option_names = [ safe_html(str(option_name)) for option_name in validated_data['options'] ] with transaction.atomic(): poll = Poll.objects.create(title=poll_title, expires_at=poll_expires_at, equal_width=poll_equal_width) for i, option_name in enumerate(poll_option_names): PollOption.objects.create(poll=poll, position=i, name=option_name) return poll
def from_config(config): poll = _Poll() if _KEY_OPTIONS not in config \ or _KEY_TITLE not in config: raise ValueError('Malformed configuration: %s' % config) poll.config = { _KEY_EQUAL_WIDTH: bool(config.get(_KEY_EQUAL_WIDTH, False)), _KEY_TITLE: safe_html(config[_KEY_TITLE]), _KEY_OPTIONS: map(safe_html, config[_KEY_OPTIONS]), } return poll
def poll_post_view(request): config = json.loads(request.POST.get('config', '{}')) poll_equal_width = bool(config.get('equal_width', False)) poll_title = safe_html(config.get('title', '')) poll_option_names = map(safe_html, config.get('options', [])) with transaction.atomic(): if Poll.objects.count() >= settings.JAWANNDENN_MAX_POLLS: return HttpResponseBadRequest( f'Maximum number of {settings.JAWANNDENN_MAX_POLLS} polls ' 'reached, please contact the administrator.') poll = Poll.objects.create(title=poll_title, equal_width=poll_equal_width) for i, option_name in enumerate(poll_option_names): PollOption.objects.create(poll=poll, position=i, name=option_name) return redirect(poll)
def vote_post_view(request, poll_id): with transaction.atomic(): poll = Poll.objects.get(slug=poll_id) if poll.ballots.count() >= settings.JAWANNDENN_MAX_VOTES_PER_POLL: return HttpResponseBadRequest( f'Maximum number of {settings.JAWANNDENN_MAX_VOTES_PER_POLL} ' 'votes reached for this poll' ', please contact the administrator.') voter_name = safe_html(request.POST.get('voterName')) votes = [ request.POST.get(f'option{i}', 'off') == 'on' for i in range(poll.options.count()) ] ballot = Ballot.objects.create(poll=poll, voter_name=voter_name) for option, vote in zip(poll.options.order_by('position'), votes): Vote.objects.create(ballot=ballot, option=option, yes=vote) return redirect(poll)
def test_bad_nesting(self): self.assertEqual( safe_html("*__text*__"), "<em><strong>text<em><strong></strong></em></strong></em>", )
def test_monospace(self): self.assertEquals(safe_html( '`text`'), '<tt>text</tt>')
def test_monospace(self): self.assertEqual(safe_html("`text`"), "<tt>text</tt>")
def test_italic(self): self.assertEqual(safe_html("*text*"), "<em>text</em>") self.assertEqual(safe_html("_text_"), "<em>text</em>")
def test_bold(self): self.assertEqual(safe_html("**text**"), "<strong>text</strong>") self.assertEqual(safe_html("__text__"), "<strong>text</strong>")
def test_html(self): self.assertEqual(safe_html("<b> </b>"), "<b>&nbsp;</b>")
def test_bad_nesting(self): self.assertEquals(safe_html( '*__text*__'), '<em><strong>text<em><strong></strong></em></strong></em>')
def test_vanilla(self): self.assertEquals(safe_html( 'text'), 'text')
def test_non_string(self): with self.assertRaises(ValueError): safe_html(123)
def test_vanilla(self): self.assertEqual(safe_html("text"), "text")
def test_html(self): self.assertEquals(safe_html( '<b> </b>'), '<b>&nbsp;</b>')