source: mailjam/postman/models.py@ 6:2a0f6f8cbc52

Last change on this file since 6:2a0f6f8cbc52 was 5:573fdae8b1f6, checked in by Francisco de Borja Lopez Rio <borja@…>, 13 years ago

Added tests for postman.daemon.Postman

Added a method to postman.daemon.Postman to "clear" all the storage associated
to a given postman instance (removing all the files from disk)

Fixed some bugs, mostly caused by typos through the sources, detected while
writing more tests

Removed the storage and archive attributes from postman.models.MailingList,
now they are methods "marked" as properties using the @property decorator.
This keeps the storage objects from being serialized into json objects by
the storage backend (de-serializing them caused some trouble and it is not
necessary at all)

Added create() and delete() methods to postman.storage.JsonStorage. the first
one ensures the path to the storage file exists (creating all subdirs if
necessary) and the second one deletes the storage file from disk

File size: 3.7 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={}):
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
42 def __repr__(self):
43 return "<MailingList '%s'>" % self.address
44
45 def __str__(self):
46 return self.address
47
48 def _validate_config(self):
49 if not 'storage' in self.config.keys():
50 self.config['storage'] = os.path.join(config.storage_path,
51 'mailings/')
52 if not 'archive' in self.config.keys():
53 self.config['archive'] = config.archive_path
54 if not 'private' in self.config.keys():
55 self.config['private'] = config.private_mailing
56
57 def _validate_member_object(self, member=None):
58 if not isinstance(member, Member):
59 raise TypeError(member, ' is not a valid Member instance')
60 return member
61
62 def _validate_member(self, member=None):
63 member = self._validate_member_object(member)
64 return member.address in self.members_addresses()
65
66 def _validate_member_by_address(self, address=None):
67 if not validate_email_address(address):
68 raise ValueError(address, ' is not a valid email address')
69 return address in self.members_addresses()
70
71 @property
72 def storage(self):
73 return Storage(os.path.join(self.config['storage'], self.address))
74
75 @property
76 def archive(self):
77 return Storage(os.path.join(self.config['archive'], self.address))
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 info(self):
104 """
105 Returns a dict we can use to add this mailing list to a postman
106 instance
107 """
108 # FIXME: This code could be replaced with something that
109 # automagically generates the dict from the instance attributes
110 return {'name': self.name, 'address': self.address,
111 'members': self.members, 'config': self.config}
112
113 def load(self):
114 if self.storage.exists():
115 data = self.storage.read()
116 self.name = data.name
117 self.address = data.address
118 self.members = data.members
119 return True
120 return False
121
122 def save(self):
123 self.storage.write(self)
Note: See TracBrowser for help on using the repository browser.