aboutsummaryrefslogtreecommitdiffstats
path: root/pygithub3/requests/users/emails.py
blob: ff89b6255e7cb37ccb7c419d88b87d05c61b465d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import re

from . import Request, ValidationError

# Src: http://code.djangoproject.com/svn/django/trunk/django/core/validators.py
email_re = re.compile(
    r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"  # dot-atom
    # quoted-string, see also http://tools.ietf.org/html/rfc2822#section-3.2.5
    r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"'
    r')@((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$)'  # domain
    r'|\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$', re.IGNORECASE)  # literal form, ipv4 address (SMTP 4.1.3)


class List(Request):

    uri = 'user/emails'


class Add(Request):

    uri = 'user/emails'

    def clean_body(self):
        def is_email(email):
            return bool(email_re.match(email))
        if not self.body:
            raise ValidationError("'%s' request needs emails"
                                  % (self.__class__.__name__))

        return filter(is_email, self.body)


class Delete(Request):

    uri = 'user/emails'

    def clean_body(self):
        if not self.body:
            raise ValidationError("'%s' request needs emails"
                                  % (self.__class__.__name__))
        return self.body