# Path: deliver/distribute.py
# class OfflineDistributor(Distributor):
#     '''
#     Distributes deferred mails (digests)
# 
#     There is one public method, update. When it is called, the list of
#     users with pending emails is retrieved from the Store. For each of
#     these users, the pending mails are retrieved. A digest mail is
#     built from them and sent to the user.
#     '''
# 
#     def __init__(self, config):
#         super(OfflineDistributor,self).__init__(config)
# 
#     def update(self):
#         '''
#         Update the pending digests. For each user with pending
#         digests, a mail is built and sent to them.
#         '''
#         logger.debug('update is called')
#         self._store.discard_old_digests(self._cfg['digest_age_limit'])
#         users = self._store.users_with_pending_digests()
#         for user in users:
#             messages = self._store.messages_for_user(user)
#             msg = DigestMessage(messages)
#             self._sender.send(msg, user)
#             self._store.mark_digest_as_sent(user)
#         logger.debug('update is finished')
#         return len(users) != 0
# 
# Path: deliver/send.py
# class Sender:
#     '''
#     Class that is responsible of sending emails. It uses a specified SMTP server.
#     '''
# 
#     def __init__(self, config):
#         self._cfg = config
# 
#     def send(self, msg, *recipients):
#         '''
#         Sends the given UnicodeMessage to each of the specified recipients.
#         '''
#         assert isinstance(msg, UnicodeMessage)
#         for recipient in recipients:
#             assert isinstance(recipient, unicode)
#         self._send(msg, *recipients)
# 
#     def _send(self, msg, *recipients):
#         '''
#         Sends the given UnicodeMessage to each of
#         the specified recipients.
# 
#         The emails are sent from the specified server, using the specified address. The subject
#         is modified to include a subject prefix if it is not already there.
#         '''
#         msg.replace_header('Subject', self._prepare_subject(msg['Subject']))
#         msg.replace_header('From', self.get_address())
#         msg.replace_header('Reply-To', self.get_address())
#         logging.debug('email ready to be sent: %s', msg['Message-Id'])
#         for recipient in recipients:
#             logger.debug('Sending message to %s', recipient)
#             s = smtplib.SMTP(self._cfg['smtp_server'])
#             msg.replace_header('To', recipient)
#             s.sendmail(self.get_address(), recipient, msg.as_string())
#             s.quit()
# 
#     def get_address(self):
#         '''Gets the address used by this sender'''
#         return self._cfg['sender']
# 
#     def _prepare_subject(self, subject):
#         '''Modifies the given subject to include a prefix if it is not already there'''
#         if self._cfg['subject_prefix'] in subject:
#             return subject
#         return self._cfg['subject_prefix'] + ' ' + subject


from test_base import BaseTest, load_msg, get_msg, archive_msg, get_digests
from mock import Mock
from datetime import datetime, timedelta
from deliver.distribute import OfflineDistributor
from deliver.send import Sender
# -*- coding: utf-8 -*-


class OfflineDistributeTest(BaseTest):

    def setUp(self):
        super(OfflineDistributeTest,self).setUp()

# Path: deliver/distribute.py
# class OfflineDistributor(Distributor):
#     '''
#     Distributes deferred mails (digests)
# 
#     There is one public method, update. When it is called, the list of
#     users with pending emails is retrieved from the Store. For each of
#     these users, the pending mails are retrieved. A digest mail is
#     built from them and sent to the user.
#     '''
# 
#     def __init__(self, config):
#         super(OfflineDistributor,self).__init__(config)
# 
#     def update(self):
#         '''
#         Update the pending digests. For each user with pending
#         digests, a mail is built and sent to them.
#         '''
#         logger.debug('update is called')
#         self._store.discard_old_digests(self._cfg['digest_age_limit'])
#         users = self._store.users_with_pending_digests()
#         for user in users:
#             messages = self._store.messages_for_user(user)
#             msg = DigestMessage(messages)
#             self._sender.send(msg, user)
#             self._store.mark_digest_as_sent(user)
#         logger.debug('update is finished')
#         return len(users) != 0
# 
# Path: deliver/send.py
# class Sender:
#     '''
#     Class that is responsible of sending emails. It uses a specified SMTP server.
#     '''
# 
#     def __init__(self, config):
#         self._cfg = config
# 
#     def send(self, msg, *recipients):
#         '''
#         Sends the given UnicodeMessage to each of the specified recipients.
#         '''
#         assert isinstance(msg, UnicodeMessage)
#         for recipient in recipients:
#             assert isinstance(recipient, unicode)
#         self._send(msg, *recipients)
# 
#     def _send(self, msg, *recipients):
#         '''
#         Sends the given UnicodeMessage to each of
#         the specified recipients.
# 
#         The emails are sent from the specified server, using the specified address. The subject
#         is modified to include a subject prefix if it is not already there.
#         '''
#         msg.replace_header('Subject', self._prepare_subject(msg['Subject']))
#         msg.replace_header('From', self.get_address())
#         msg.replace_header('Reply-To', self.get_address())
#         logging.debug('email ready to be sent: %s', msg['Message-Id'])
#         for recipient in recipients:
#             logger.debug('Sending message to %s', recipient)
#             s = smtplib.SMTP(self._cfg['smtp_server'])
#             msg.replace_header('To', recipient)
#             s.sendmail(self.get_address(), recipient, msg.as_string())
#             s.quit()
# 
#     def get_address(self):
#         '''Gets the address used by this sender'''
#         return self._cfg['sender']
# 
#     def _prepare_subject(self, subject):
#         '''Modifies the given subject to include a prefix if it is not already there'''
#         if self._cfg['subject_prefix'] in subject:
#             return subject
#         return self._cfg['subject_prefix'] + ' ' + subject


# Path: deliver/tests/test_distribute_digest.py
from test_base import BaseTest, load_msg, get_msg, archive_msg, get_digests
from mock import Mock
from datetime import datetime, timedelta
from deliver.distribute import OfflineDistributor
from deliver.send import Sender

# -*- coding: utf-8 -*-


class OfflineDistributeTest(BaseTest):

    def setUp(self):
        super(OfflineDistributeTest,self).setUp()

        self.distributor = OfflineDistributor(self.config)