The validator checks a comma separated list of email addresses. It also cleans up the string and removes the duplicates. This class inherits from validator.Email.
The method _to_python cleans up the string, and removes the empty email addresses and the duplicates.
Then validate_python calls validators.Email.validate_python for each email address to verify it.
Usage:
>>> v=MailListValidator() >>> v.to_python('mike@domain.net, marc@exemple.com') 'marc@exemple.com,mike@domain.net' # removes duplicates. >>> v.to_python('mike@domain.net, marc@exemple.com, mike@domain.net') 'marc@exemple.com, mike@domain.net' >>> v.to_python('mike@domain.net, marc') Traceback (most recent call last): ...
The code:
class MailListValidator(validators.Email): r""" Validate a list of email addresses. If you pass ``max_emails=<nb>``, then it will limit the length of the list to <nb> addresses. By default there is no limit to the number of email addresses in the list. """ max_emails = None __unpackargs__ = ('max_emails',) messages = { 'max': _("You can only enter %(max)i email addresses (%(nb)i given)"), 'err_s': _("The email address '%(email)s' is not valid"), 'err_p': _("The email addresses '%(email)s' are not valid") } def __init__(self, *args, **kw): self.max_emails = kw.get('max_emails') validators.Email.__init__(self, *args, **kw) def __unique(self, data): S = {} for d in data: S[d] = 1 return S.keys() def _to_python(self, value, state): value = value.lower() recipients = [r.strip() for r in value.split(',') if r.strip()] recipients = self.__unique(recipients) return ','.join(recipients) def validate_python(self, value, state): email_list = value.split(',') nb = len(email_list) if self.max_emails and nb > self.max_emails: raise validators.Invalid( self.message("max", state, max=self.max_emails, nb=nb), value, state) wrong_emails = [] for v in email_list: try: validators.Email.validate_python(self, v, state) except: wrong_emails.append(v) if len(wrong_emails) == 1: raise validators.Invalid( self.message("err_s", state, email=', '.join(wrong_emails)), value, state) if len(wrong_emails) > 1: raise validators.Invalid( self.message("err_p", state, email=', '.join(wrong_emails)), value, state)
Comments
Posted by: gmt master ii Dec 07, 2009 @ 23:19
Great article. Excellent reproduciton Rolex gmt master ii,welcome to check our web. Thanks!
Posted by: Rc helicopters Dec 16, 2009 @ 20:13
i like it rcmodelhobby.com/
Posted by: rolex replica Dec 28, 2009 @ 21:47
our site sells the excellent reproduction... pls check our site,you will find the watch you like,some of them are really great.such as:BvlgariReplica Watches, Cartier Replica watches, Cartier Replica watches, Chanel Replica Watches………
Posted by: replica watches Dec 29, 2009 @ 21:52
Our watches are all beautifully decorated stainless steel or 18k gold designer replica watches,perfectly fitted to you wirst ,once you wear our watch you will never look back ,Now I will show you some thing wonderful: Rolex Replica Watches, Corum Replica Watches, Dewitt Replica Watches, Rolex Replica Watches, Ferrari Replica Watches…………
Posted by: laptop battery Dec 30, 2009 @ 22:45
Everything will be all right,I am behind you.
Posted by: replica watches Jan 03, 2010 @ 21:25
We are Rolex watch experts selling only the finest, top level condition, and preowned men's and ladies' Rolex watches, used Rolex watches, unused Rolex watches and new Rolex watches on the market. Our top priority is to provide every customer with the highest quality Rolex watch at the lowest price available. Browse our wide selection of Rolexes for sale.
Posted by: evening dresses Jan 09, 2010 @ 04:13
Great work every buddy can get lots of interesting information, keep on posting this type of brilliant articles. I really like this keep it up again!
Posted by: Ed Hardy Jan 11, 2010 @ 18:05
Exquisite design, superior material, exquisite craft. [HTML_REMOVED]Ed Hardy[HTML_REMOVED] Close to her heart, the most perfect expression of your love.
Posted by: replica watches Jan 11, 2010 @ 18:43
As the business grows, replica rolex paypal has also jumped into an international brand. It is worth mentioning that, rolex replica is the ancestor of today's brand-oriented, in order to protect the quality and brand name will be printed on their products, the history of fashion in the world, is the first one first. replica watch paypal,replica watches,fake rolex.
Posted by: film izle Jan 17, 2010 @ 04:53
Thank you for sharing your friends. Hope to see you another day.
Posted by: divx film izle Jan 17, 2010 @ 04:55
Thanks for your good website and for sharing your experiences.
Posted by: Wholesale Laptops Jan 17, 2010 @ 22:06
As the business grows, replica rolex paypal has also jumped into an international brand
Posted by: evening dresses Feb 19, 2010 @ 06:07
Thanks for your good website
Posted by: Nike Air Max Mar 04, 2010 @ 01:02
So beautiful sharing!Thank you very much.
Posted by: Nike Air Max Mar 11, 2010 @ 19:16
Best wishes!Your blog is very good!
Posted by: Nike Air Max Mar 18, 2010 @ 02:52
It is so nice.
Posted by: payday loans in canada Apr 09, 2010 @ 12:38
This is some very valueable information, thank you very much.