source: mailjam/postman/models.py@ 1:421645ee4cd9

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

Moved the source code from src/ to postman/

Added tests (unittests) splitted into one testing module per app source
file (tests/models.py for testing models.py, tests/storage.py for testing
storage.py and so on). Currently only tests for models have been added.

Added a script to run the tests

File size: 3.1 KB
Line 
1# -*- coding: utf-8 -*-
2
3import os
4from tools import validate_email_address
5from storage import JsonStorage
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.storage = JsonStorage(os.path.join(config['storage'],
41 self.address))
42
43 def __repr__(self):
44 return "<MailingList '%s'>" % self.address
45
46 def __str__(self):
47 return self.address
48
49 def _validate_config(self):
50 if not 'storage' in self.config.keys():
51 self.config['storage'] = os.path.join(config.storage_path,
52 self.address)
53 if not 'archive' in self.config.keys():
54 self.config['archive'] = os.path.join(self.config['storage'],
55 'archive')
56 if not 'private' in self.config.keys():
57 self.config['private'] = config.private
58
59 def _validate_member_object(self, member):
60 if not isinstance(member, Member):
61 raise TypeError(member, ' is not a valid Member instance')
62 return member
63
64 def _validate_member(self, member):
65 member = self._validate_member_object(member)
66 return member.address in self.members_addresses()
67
68 def _validate_member_by_address(self, address):
69 if not validate_email_address(address):
70 raise ValueError(address, ' is not a valid email address')
71 return address in self.members_addresses()
72
73 def members_addresses(self):
74 return self.members.keys()
75
76 def add_member(self, member):
77 member = self._validate_member_object(member)
78 if self._validate_member(member):
79 return False
80 self.members[member.address] = member
81 return True
82
83 def add_member_by_address(self, address):
84 if self._validate_member_by_address(address):
85 return False
86 member = Member(address)
87 self.members[address] = member
88 return True
89
90 def delete_member(self, member):
91 member = self._validate_member_object(member)
92 if not self._validate_member(member):
93 return False
94 del self.members[member.address]
95 return True
96
97 def load(self):
98 if self.storage.exists():
99 data = self.storage.read()
100 self.name = data.name
101 self.address = data.address
102 self.members = data.members
103
104 def save(self):
105 self.storage.write(self)
Note: See TracBrowser for help on using the repository browser.