source: mailjam/postman/models.py@ 2:108a82defd3e

Last change on this file since 2:108a82defd3e was 2:108a82defd3e, checked in by Francisco de Borja Lopez Rio <borja@…>, 12 years ago

Fixed some bugs in the models and mta modules

Added tests for the mta module, including a dummy email that is used for
those tests.

Added an initial version of the Postman class (inside the daemon module). This
class will handle the main execution of the postman daemon, the manager
that will take care of mailing lists management.

Added the structure of folders needed to store json files associated with a given
postman instance.

File size: 3.5 KB
Line 
1# -*- coding: utf-8 -*-
2
3import os
4from tools import validate_email_address
5from storage import JsonStorage as Storage
6import config
7
8class 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
29class 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)
Note: See TracBrowser for help on using the repository browser.