[0] | 1 | # -*- coding: utf-8 -*-
|
---|
| 2 |
|
---|
| 3 | import os
|
---|
| 4 | from tools import validate_email_address
|
---|
[2] | 5 | from storage import JsonStorage as Storage
|
---|
[0] | 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 |
|
---|
[5] | 35 | def __init__(self, name, address, members={}, config={}):
|
---|
[0] | 36 | self.name = name
|
---|
| 37 | self.address = address
|
---|
| 38 | self.members = members
|
---|
| 39 | self.config = config
|
---|
[2] | 40 | self._validate_config() # validate the config parameters
|
---|
[4] | 41 |
|
---|
[0] | 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,
|
---|
[2] | 51 | 'mailings/')
|
---|
[0] | 52 | if not 'archive' in self.config.keys():
|
---|
[2] | 53 | self.config['archive'] = config.archive_path
|
---|
[0] | 54 | if not 'private' in self.config.keys():
|
---|
[2] | 55 | self.config['private'] = config.private_mailing
|
---|
[0] | 56 |
|
---|
[2] | 57 | def _validate_member_object(self, member=None):
|
---|
[0] | 58 | if not isinstance(member, Member):
|
---|
| 59 | raise TypeError(member, ' is not a valid Member instance')
|
---|
| 60 | return member
|
---|
| 61 |
|
---|
[2] | 62 | def _validate_member(self, member=None):
|
---|
[0] | 63 | member = self._validate_member_object(member)
|
---|
| 64 | return member.address in self.members_addresses()
|
---|
| 65 |
|
---|
[2] | 66 | def _validate_member_by_address(self, address=None):
|
---|
[0] | 67 | if not validate_email_address(address):
|
---|
| 68 | raise ValueError(address, ' is not a valid email address')
|
---|
| 69 | return address in self.members_addresses()
|
---|
[5] | 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 |
|
---|
[0] | 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 |
|
---|
[2] | 89 | def add_member_by_address(self, address=None):
|
---|
[0] | 90 | if self._validate_member_by_address(address):
|
---|
| 91 | return False
|
---|
| 92 | member = Member(address)
|
---|
| 93 | self.members[address] = member
|
---|
[1] | 94 | return True
|
---|
[0] | 95 |
|
---|
[2] | 96 | def delete_member(self, member=None):
|
---|
[0] | 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 |
|
---|
[5] | 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 |
|
---|
[0] | 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
|
---|
[2] | 119 | return True
|
---|
| 120 | return False
|
---|
[0] | 121 |
|
---|
| 122 | def save(self):
|
---|
| 123 | self.storage.write(self)
|
---|