mailman.py
1.58 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import urlparse
import requests
from django.conf import settings
TIMEOUT = 1
def get_url(listname=None):
if listname:
return urlparse.urljoin(settings.MAILMAN_API_URL, '/' + listname)
return settings.MAILMAN_API_URL
def subscribe(listname, address):
url = get_url(listname)
try:
requests.put(url, timeout=TIMEOUT, data={'address': address})
except requests.exceptions.RequestException:
return False
return True
def unsubscribe(listname, address):
url = get_url(listname)
try:
requests.delete(url, timeout=TIMEOUT, data={'address': address})
except requests.exceptions.RequestException:
return False
return True
def update_subscription(address, lists):
current_lists = address_lists(address)
for maillist in current_lists:
if maillist not in lists:
unsubscribe(maillist, address)
for maillist in lists:
if maillist not in current_lists:
subscribe(maillist, address)
def address_lists(address, description=None):
url = get_url()
kwargs = {'address': address}
if description:
kwargs.update(description=description)
try:
lists = requests.get(url, timeout=TIMEOUT, params=kwargs)
except requests.exceptions.RequestException:
return []
return lists.json()
def all_lists(*args, **kwargs):
return address_lists('', *args, **kwargs)
def user_lists(user):
list_set = set()
for email in user.emails.values_list('address', flat=True):
list_set.update(address_lists(email))
return tuple(list_set)