[Twisted-web] various formless types

Andrea Arcangeli andrea at cpushare.com
Tue Feb 15 18:12:36 MST 2005


On Mon, Jan 31, 2005 at 06:35:00PM +0100, andrea at cpushare.com wrote:
> class Email(MaxlengthString):
> 	def coerce(self, val, configurable):
> 		r = super(Email, self).coerce(val, configurable)
> 		if invalid_email(val):
> 			raise annotate.InputError("%r is not a valid email address." % val)
> 		return r

Just in case somebody uses the above, I noticed one little bug with the case,
see now I return r.lower().

def invalid_email(email):
	return not re.match(r'^[\w\-~@.+]+\Z', email) or \
	       re.search(r'@[^@]*@', email) or \
	       not re.match(r'^[\w\-~.+]+ at .*[\w\-~+]+\.[\w]{2,}\Z', email)
class Email(MaxlengthString):
	def coerce(self, val, configurable):
		r = super(Email, self).coerce(val, configurable)
		if invalid_email(r):
			raise annotate.InputError("%r is not a valid email address." % val)
		return r.lower()

Now I'm doing checking on the domain with twisted dns capabilities too before
trying to send an email address (this helps against typos). But I do that 
in the handlers, not in the coerce method (it apparently doesn't like if 
I return a deferred from a coerce method). No problem of course (though
it'd be a bit cleaner if I could handle deferreds in the coerce methods too).

I'd need ctx passed to coerce too, I had to implement my ProcessTypedContext in
one case due the lack of ctx being passed to coerce, as described in the
previous email.

thanks!



More information about the Twisted-web mailing list