class SignupForm(RegistrationFormUniqueEmail): """ A user-friendly sign up form with dynamically configurable help texts and validation. """ # Have to redeclare the entire field from the super class in order # to add our own help text: email = forms.EmailField( help_text=lambda: markdown(config.EMAIL_HELP_TEXT), required=True, validators=[ validators.validate_confusables_email, ]) def clean_email(self): """Perform additional email validation if configured in the admin interface.""" pattern = config.EMAIL_PATTERN if pattern: # The pattern is always validated in the admin interface by testing if # re.compile() succeeds. If it fails to compile now, there must be some # serious error which we can't fix here and it's better to crash. regex = re.compile(pattern, re.VERBOSE) if not regex.search(self.cleaned_data["email"]): raise ValidationError(markdown(config.EMAIL_ERROR_MESSAGE)) return super().clean_email() def clean_username(self): """Ensure no duplicate user names exist, using case-insensitive comparison.""" username = self.cleaned_data.get("username") if User.objects.filter(username__iexact=username): raise forms.ValidationError( "A user with that username already exists.") return username
def test_markdown_tables(self): html = markdown(MARKDOWN_TABLE) signature_expressions = [ '<table>', '<thead>', '<tr>', '<th>A</th>', '<tbody>', '<td>Test 1</td>' ] for expression in signature_expressions: self.assertTrue(expression in html)
class ConfirmStudentDetailsForm(forms.ModelForm): ownwork_confirmed = forms.BooleanField( help_text=lambda: markdown(config.OWNWORK_DECLARATION), required=True, ) class Meta: model = StudentDetails fields = ['matnum', 'ownwork_confirmed']
def clean_email(self): """Perform additional email validation if configured in the admin interface.""" pattern = config.EMAIL_PATTERN if pattern: # The pattern is always validated in the admin interface by testing if # re.compile() succeeds. If it fails to compile now, there must be some # serious error which we can't fix here and it's better to crash. regex = re.compile(pattern, re.VERBOSE) if not regex.search(self.cleaned_data["email"]): raise ValidationError(markdown(config.EMAIL_ERROR_MESSAGE)) return super().clean_email()
def test_fenced_code(self): html = markdown(FENCED_CODE) self.assertTrue('<code class="language-python">' in html)
def test_autoids(self): html = markdown('# Heading') self.assertTrue('<h1 id="heading">Heading</h1>' in html)