Changeset 2:108a82defd3e in mailjam


Ignore:
Timestamp:
May 15, 2012, 2:29:03 AM (10 years ago)
Author:
Francisco de Borja Lopez Rio <borja@…>
Branch:
default
Phase:
public
Message:

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.

Location:
postman
Files:
6 added
4 edited

Legend:

Unmodified
Added
Removed
  • postman/config.py

    r1 r2  
    44
    55# The default path for storage files
    6 storage_path = os.path.join(os.path.dirname(__file__), 'db')
     6storage_path = os.path.join(os.path.dirname(__file__), 'storage')
     7
     8# The default path for the archives
     9archive_path = os.path.join(os.path.dirname(__file__), 'archives')
    710
    811# Set to True to set that, by default, only emails from members
  • postman/models.py

    r1 r2  
    33import os
    44from tools import validate_email_address
    5 from storage import JsonStorage
     5from storage import JsonStorage as Storage
    66import config
    77
     
    3838        self.members = members
    3939        self.config = config
    40         self.storage = JsonStorage(os.path.join(config['storage'],
    41                                                 self.address))
     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
    4249
    4350    def __repr__(self):
     
    5057        if not 'storage' in self.config.keys():
    5158            self.config['storage'] = os.path.join(config.storage_path,
    52                                                   self.address)
     59                                                  'mailings/')
    5360        if not 'archive' in self.config.keys():
    54             self.config['archive'] = os.path.join(self.config['storage'],
    55                                                   'archive')
     61            self.config['archive'] = config.archive_path
    5662        if not 'private' in self.config.keys():
    57             self.config['private'] = config.private
     63            self.config['private'] = config.private_mailing
    5864       
    59     def _validate_member_object(self, member):
     65    def _validate_member_object(self, member=None):
    6066        if not isinstance(member, Member):
    6167            raise TypeError(member, ' is not a valid Member instance')
    6268        return member
    6369
    64     def _validate_member(self, member):
     70    def _validate_member(self, member=None):
    6571        member = self._validate_member_object(member)
    6672        return member.address in self.members_addresses()
    6773
    68     def _validate_member_by_address(self, address):
     74    def _validate_member_by_address(self, address=None):
    6975        if not validate_email_address(address):
    7076            raise ValueError(address, ' is not a valid email address')
     
    8187        return True
    8288
    83     def add_member_by_address(self, address):
     89    def add_member_by_address(self, address=None):
    8490        if self._validate_member_by_address(address):
    8591            return False
     
    8894        return True
    8995               
    90     def delete_member(self, member):
     96    def delete_member(self, member=None):
    9197        member = self._validate_member_object(member)
    9298        if not self._validate_member(member):
     
    101107            self.address = data.address
    102108            self.members = data.members
     109            return True
     110        return False
    103111   
    104112    def save(self):
  • postman/mta.py

    r1 r2  
    88class Sendmail():
    99   
    10     def __init__(self, mailing_list):
     10    def __init__(self, mailing_list=None):
    1111        if not isinstance(mailing_list, MailingList):
    1212            raise ValueError(mailing_list, ' is not a valid mailing list')
    1313        self.mailing_list = mailing_list
    1414        self.suscriptors = self.mailing_list.members_addresses
    15         self.reply_address = self.mailing_list.address
     15        self.reply_to = self.mailing_list.address
     16        self.raw_email = None
    1617        self.queue = []
    1718        self.archive = self.mailing_list.config.get('archive',
    18                                                     config.storage_path)
     19                                                    config.archive_path)
    1920       
    20     def get_raw_email():
     21    def get_raw_email(self):
    2122        try:       
    22             raw_email = sys.stdin.read()
     23            self.raw_email = sys.stdin.read()
    2324        except:
    2425            raise IOError('Can not get a valid email from stdin')
    25         return raw_email
     26        return self.raw_email
    2627
    27     def save_raw_email():
     28    def save_raw_email(self):
     29        if not self.raw_email:
     30            # FIXME: perhaps a while loop here, with some maximum recursion
     31            # check, would be nice here
     32            self.get_raw_email
    2833        filename = os.path.join(self.archive,
    2934                                datetime.today().strftime('%Y%d%m%H%M%S%f'))
    3035        tmpfile = file(filename, 'w')
    31         tmpfile.write(raw_email)
     36        tmpfile.write(self.raw_email)
    3237        tmpfile.close()
    3338        self.queue.append(filename)
    3439
    35     def send_email():
    36         if not self.queue:
    37             raise ValueError('The emails queue is empty')
    38         next_email = self.queue.pop()
    39         email_file = file(next_email, 'r')
    40         email_data = email.message_from_file(email_file)
    41         email_file.close()
     40    def send_email(self):
     41        if self.queue:
     42            next_email = self.queue.pop()
     43            email_file = file(next_email, 'r')
     44            email_data = email.message_from_file(email_file)
     45            email_file.close()
    4246
    43         email_data['Reply-to'] = self.reply_address
     47            email_data['Reply-to'] = self.reply_to
    4448
    45         smtp_conn = smtplib.SMTP()
    46         smtp_conn.connect()
    47         smtp_conn.sendmail(email_data['From'], self.suscriptors,
    48                            email_data.as_string())
    49         smtp_conn.close()       
     49            smtp_conn = smtplib.SMTP()
     50            smtp_conn.connect()
     51            smtp_conn.sendmail(email_data['From'], self.suscriptors,
     52                               email_data.as_string())
     53            smtp_conn.close()
     54
     55    def run(self):
     56        self.get_raw_email()
     57        self.save_raw_email()
     58        self.send_email()
  • postman/tests/__init__.py

    r1 r2  
    22
    33from models import *
     4from mta import *
    45
    5 
Note: See TracChangeset for help on using the changeset viewer.