Python unittest TestCase objects have a lot of helper functions like
assert_(), failUnless(), assertEqual(), assertNotEqual() and so on[1]. If we
had a similar set of helper functions, possibly with more pythonic names,
newform validation functions could be written a little more succinctly.

    def clean_password2(self):
        if self.clean_data['password1'] != self.clean_data['password2']:
            raise ValidationError(u'Please make sure your passwords match.')
        return self.clean_data['password2']

can become

    def clean_password2(self):
        assert_equal(self.clean_data['password1'], self.clean_data['password2',
"Please make sure your passwords match."
        return self.clean_data['password2']

or
    def clean_code(self):
        try:
            ConfirmationCode(code=self.clean_data['code'])
        except ConfirmationCode.DoesNotExist:
            raise ValidationError("Code does not exist")
        return self.clean_data["code"]

becomes:

    def clean_code(self):
        assert_not_raises(ConfrimationCode.DoesNotExist, "Code does not
exist", ConfirmationCode.objects.get, code=self.clean_data['code'])
        return self.clean_data["code"]

or
    def clean_username(self):
        try: User.objects.get(username=self.clean_data["username"])
        except User.DoesNotExist: pass
        else: raise ValidationError("Username already taken")
        return self.clean_data["username"]

becomes

    def clean_username(self):
        assert_raises(User.DoesNotExist, "Username already taken",
User.objects.get, username=self.clean_data["username"])
        return self.clean_data["username"]

Message would be compulsory in these helper functions, and they will raise
ValidationError instead of AssertionError.

[1]: http://docs.python.org/lib/testcase-objects.html
-- 
Amit Upadhyay
Vakow! www.vakow.com
+91-9820-295-512

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to