Changeset 136:bfdb6e432cb8

Show
Ignore:
Timestamp:
07/05/07 15:23:03 (14 months ago)
Author:
Tarek Ziad?? <tarek@…>
Message:

finalized v1

Location:
mailer
Files:
3 added
6 modified

Legend:

Unmodified
Added
Removed
  • mailer/doc/mailer.txt

    r134 r136  
    1919    >>> from sender import send_mail 
    2020    >>> mail_id = send_mail(sender='tarek@ziade.org', 
    21     ...                     recipients=['tarek@ziade.org'], 
     21    ...                     recipients=['ziade.tarek@gmail.com'], 
    2222    ...                     subject='hello', 
    23     ...                     msg='hello') 
     23    ...                     msg='héllo') 
    2424 
    2525 
     
    3131    >>> from sender import mail_status 
    3232    >>> mail_status(mail_id) 
    33     'processing' 
     33    u'processing' 
     34 
     35When the id is not given, the whole mailed table is returned:: 
     36 
     37    >>> mail_status() 
     38    [] 
     39 
     40We can ask for the queue size as well:: 
     41 
     42    >>> from sender import mail_queue_size 
     43    >>> mail_queue_size() 
     44    1 
     45 
     46 
    3447 
    3548sending mails, for real 
     
    4659 
    4760    >>> import time 
    48     >>> while mail_status(mail_id) == 'processing': 
     61    >>> while mail_status(mail_id) == u'processing': 
    4962    ...     time.sleep(0.2) 
    5063    >>> mail_status(mail_id) 
     
    5568    >>> stop_server() 
    5669 
     70And see the status:: 
    5771 
     72    >>> status = mail_status() 
     73    >>> status[0]['subject'] 
     74    u'hello' 
     75    >>> status[0]['status'] 
     76    u'processed' 
    5877 
  • mailer/mailer.py

    r134 r136  
    2323import os 
    2424import sys 
     25import smtplib 
    2526import logging 
    2627from threading import Thread 
    2728import time 
     29from base64 import b64decode 
     30from email.MIMEText import MIMEText 
    2831 
    2932from model import mailed_data, mail_data 
     33import settings 
    3034 
    3135class MailWorker(Thread): 
     
    4145        return mail_data.select().execute().fetchall() 
    4246 
     47    def _get_message(self, mail): 
     48        """returns a Mime""" 
     49        msg = MIMEText(b64decode(mail.data)) 
     50        msg['From'] = mail.sender 
     51        msg['To'] = mail.recipients 
     52        msg['Subject'] = mail.subject 
     53 
     54        msg['Date'] = mail.date.isoformat() 
     55        return msg 
     56 
    4357    def _send_mail(self, mail): 
    4458        """sends the mail""" 
    45         pass 
     59        server = smtplib.SMTP(settings.SMTP_SERVER) 
     60        msg = self._get_message(mail) 
     61        try: 
     62            server.sendmail(msg['From'], msg['To'], msg.as_string()) 
     63        finally: 
     64            server.quit() 
    4665 
    4766    def _store_mail(self, mail, error=None): 
     
    5675 
    5776        # removes from original table 
    58         mailed_data.delete().execute(id=mail.id) 
     77        mail_data.delete().execute(id=mail.id) 
    5978 
    6079    def run(self): 
  • mailer/model.py

    r134 r136  
    3939                   Column('sender', String(300)), 
    4040                   Column('recipients', String(300)), 
    41                    Column('date', Date()), 
     41                   Column('date', DateTime()), 
    4242                   Column('data', TEXT())) 
    4343 
     
    5656                   Column('error', String(300)), 
    5757                   Column('data', TEXT()), 
    58                    Column('date', Date()), 
     58                   Column('date', DateTime()), 
    5959                   Column('status', String(10)),) 
    6060 
  • mailer/sender.py

    r134 r136  
    3434    return res.last_inserted_ids()[0] 
    3535 
    36 def mail_status(mail_id): 
     36def mail_status(mail_id=None): 
    3737    """getting mail status""" 
     38    def _small(mail): 
     39        return {'subject': mail.subject, 'date': mail.date, 
     40                'status': mail.status} 
     41 
     42    if mail_id is None: 
     43        return [_small(mail) for mail in 
     44                mailed_data.select().execute()] 
     45 
    3846    res = mailed_data.select().execute(original_id=mail_id).fetchall() 
    3947    if len(res) == 0: 
    4048        # still in queue 
    41         return 'processing' 
     49        return u'processing' 
    4250    return res[0].status 
    4351 
     52def mail_queue_size(): 
     53    """return the queue size""" 
     54    return len(mail_data.select().execute().fetchall()) 
     55 
  • mailer/settings.py

    r134 r136  
    11 
    22DATABASE = 'postgres://chainon:chainon@localhost/chainon' 
     3SMTP_SERVER = 'smtp.neuf.fr' 
    34 
  • mailer/tests/test_docs.py

    r134 r136  
    5555if current_dir == '': 
    5656    current_dir = '.' 
     57current_dir = os.path.realpath(current_dir) 
    5758 
    5859def test_suite(): 
    5960    globs = globals() 
    6061    test_db = os.path.join(current_dir, 'test.db') 
    61     globs['test_db'] = 'sqlite://%s' % test_db 
     62    if os.path.exists(test_db): 
     63        os.remove(test_db) 
     64 
     65    globs['test_db'] = 'sqlite:////%s' % test_db 
    6266    return doc_suite(current_dir, globs=globs) 
    6367