1 | # -*- coding: utf-8 -*-
|
---|
2 |
|
---|
3 | import os
|
---|
4 | from tools import validate_email_address
|
---|
5 | from storage import JsonStorage as Storage
|
---|
6 | import config
|
---|
7 |
|
---|
8 | class Member():
|
---|
9 |
|
---|
10 | """
|
---|
11 | Class that defines a mailing list member
|
---|
12 | """
|
---|
13 |
|
---|
14 | def __init__(self, address):
|
---|
15 | self.address = self._validate_address(address)
|
---|
16 |
|
---|
17 | def __repr__(self):
|
---|
18 | return "<Member '%s'>" % self.address
|
---|
19 |
|
---|
20 | def __str__(self):
|
---|
21 | return self.address
|
---|
22 |
|
---|
23 | def _validate_address(self, address):
|
---|
24 | if not validate_email_address(address):
|
---|
25 | raise ValueError(address, ' is not a valid email address')
|
---|
26 | return address
|
---|
27 |
|
---|
28 |
|
---|
29 | class MailingList():
|
---|
30 |
|
---|
31 | """
|
---|
32 | Class that defines a mailing list
|
---|
33 | """
|
---|
34 |
|
---|
35 | def __init__(self, name, address, members={}, config={}, storage=None):
|
---|
36 | self.name = name
|
---|
37 | self.address = address
|
---|
38 | self.members = members
|
---|
39 | self.config = config
|
---|
40 | self._validate_config() # validate the config parameters
|
---|
41 | self.storage = Storage(os.path.join(self.config['storage'],
|
---|
42 | self.address))
|
---|
43 | self.archive = Storage(os.path.join(self.config['archive'],
|
---|
44 | self.address))
|
---|
45 | # try to load data from the storage
|
---|
46 | loaded = self.load()
|
---|
47 | # FIXME: if loaded is False, the storage does not exist, perhaps
|
---|
48 | # this would be a good place to create it for the first time
|
---|
49 |
|
---|
50 | def __repr__(self):
|
---|
51 | return "<MailingList '%s'>" % self.address
|
---|
52 |
|
---|
53 | def __str__(self):
|
---|
54 | return self.address
|
---|
55 |
|
---|
56 | def _validate_config(self):
|
---|
57 | if not 'storage' in self.config.keys():
|
---|
58 | self.config['storage'] = os.path.join(config.storage_path,
|
---|
59 | 'mailings/')
|
---|
60 | if not 'archive' in self.config.keys():
|
---|
61 | self.config['archive'] = config.archive_path
|
---|
62 | if not 'private' in self.config.keys():
|
---|
63 | self.config['private'] = config.private_mailing
|
---|
64 |
|
---|
65 | def _validate_member_object(self, member=None):
|
---|
66 | if not isinstance(member, Member):
|
---|
67 | raise TypeError(member, ' is not a valid Member instance')
|
---|
68 | return member
|
---|
69 |
|
---|
70 | def _validate_member(self, member=None):
|
---|
71 | member = self._validate_member_object(member)
|
---|
72 | return member.address in self.members_addresses()
|
---|
73 |
|
---|
74 | def _validate_member_by_address(self, address=None):
|
---|
75 | if not validate_email_address(address):
|
---|
76 | raise ValueError(address, ' is not a valid email address')
|
---|
77 | return address in self.members_addresses()
|
---|
78 |
|
---|
79 | def members_addresses(self):
|
---|
80 | return self.members.keys()
|
---|
81 |
|
---|
82 | def add_member(self, member):
|
---|
83 | member = self._validate_member_object(member)
|
---|
84 | if self._validate_member(member):
|
---|
85 | return False
|
---|
86 | self.members[member.address] = member
|
---|
87 | return True
|
---|
88 |
|
---|
89 | def add_member_by_address(self, address=None):
|
---|
90 | if self._validate_member_by_address(address):
|
---|
91 | return False
|
---|
92 | member = Member(address)
|
---|
93 | self.members[address] = member
|
---|
94 | return True
|
---|
95 |
|
---|
96 | def delete_member(self, member=None):
|
---|
97 | member = self._validate_member_object(member)
|
---|
98 | if not self._validate_member(member):
|
---|
99 | return False
|
---|
100 | del self.members[member.address]
|
---|
101 | return True
|
---|
102 |
|
---|
103 | def load(self):
|
---|
104 | if self.storage.exists():
|
---|
105 | data = self.storage.read()
|
---|
106 | self.name = data.name
|
---|
107 | self.address = data.address
|
---|
108 | self.members = data.members
|
---|
109 | return True
|
---|
110 | return False
|
---|
111 |
|
---|
112 | def save(self):
|
---|
113 | self.storage.write(self)
|
---|