# Path: deliver/db/models/message.py
# class Message(object):
# 
#     def __init__(self, id, content, received_at, sent_at=None):
#         self.id = id
#         self.content = content.as_string()
#         self.received_at = received_at
#         self.sent_at = sent_at
# 
#     @property
#     def parsed_content(self):
#         '''
#         Returns the email content as a UnicodeMessage object
#         '''
#         return UnicodeMessage(
#             email.message_from_string(self.content.encode('utf-8')))
# 
#     def __repr__(self):
#         return '<Message(id=%s, received_at=%s, sent_at=%s)>' \
#             % (self.id, self.received_at, self.sent_at)
# 
# Path: deliver/db/models/digest.py
# class Digest(object):
# 
#     def __init__(self, msg_id, send_to, scheduled_at, sent_at=None):
#         self.msg_id = msg_id
#         self.send_to = send_to
#         self.scheduled_at = scheduled_at
#         self.sent_at = sent_at
# 
#     def __repr__(self):
#         return '<Digest(msg_id=%s, send_to=%s, scheduled_at=%s, sent_at=%s)>' \
#             % (self.msg_id, self.send_to, self.scheduled_at, self.sent_at)


from sqlalchemy import *
from sqlalchemy.orm import mapper, clear_mappers, create_session, relationship, backref
from deliver.db.models.message import Message
from deliver.db.models.digest import Digest



class BaseDBWrapper(object):

    def _create_tables(self):

        metadata = MetaData()
        metadata.bind = self.engine
        self.messages = self._create_table('messages', metadata,
                                           Column('id', String(192), index=True, primary_key=True),
                                           Column('content', Text, nullable=False),
                                           Column('received_at', DateTime, nullable=False),
                                           Column('sent_at', DateTime))

        self.digests = self._create_table('digests', metadata,
                                          Column('msg_id', String(192), ForeignKey('messages.id', ondelete='cascade'),
                                                 primary_key=True),
                                          Column('send_to', String(192), index=True, primary_key=True),
                                          Column('scheduled_at', DateTime, nullable=False),
                                          Column('sent_at', DateTime))

        metadata.bind = self.engine
        metadata.create_all(self.engine)

        clear_mappers()
        mapper(Message, self.messages, properties={
                'digests': relationship(Digest, backref='msg')
                })

# Path: deliver/db/models/message.py
# class Message(object):
# 
#     def __init__(self, id, content, received_at, sent_at=None):
#         self.id = id
#         self.content = content.as_string()
#         self.received_at = received_at
#         self.sent_at = sent_at
# 
#     @property
#     def parsed_content(self):
#         '''
#         Returns the email content as a UnicodeMessage object
#         '''
#         return UnicodeMessage(
#             email.message_from_string(self.content.encode('utf-8')))
# 
#     def __repr__(self):
#         return '<Message(id=%s, received_at=%s, sent_at=%s)>' \
#             % (self.id, self.received_at, self.sent_at)
# 
# Path: deliver/db/models/digest.py
# class Digest(object):
# 
#     def __init__(self, msg_id, send_to, scheduled_at, sent_at=None):
#         self.msg_id = msg_id
#         self.send_to = send_to
#         self.scheduled_at = scheduled_at
#         self.sent_at = sent_at
# 
#     def __repr__(self):
#         return '<Digest(msg_id=%s, send_to=%s, scheduled_at=%s, sent_at=%s)>' \
#             % (self.msg_id, self.send_to, self.scheduled_at, self.sent_at)


# Path: deliver/db/base.py
from sqlalchemy import *
from sqlalchemy.orm import mapper, clear_mappers, create_session, relationship, backref
from deliver.db.models.message import Message
from deliver.db.models.digest import Digest




class BaseDBWrapper(object):

    def _create_tables(self):

        metadata = MetaData()
        metadata.bind = self.engine
        self.messages = self._create_table('messages', metadata,
                                           Column('id', String(192), index=True, primary_key=True),
                                           Column('content', Text, nullable=False),
                                           Column('received_at', DateTime, nullable=False),
                                           Column('sent_at', DateTime))

        self.digests = self._create_table('digests', metadata,
                                          Column('msg_id', String(192), ForeignKey('messages.id', ondelete='cascade'),
                                                 primary_key=True),
                                          Column('send_to', String(192), index=True, primary_key=True),
                                          Column('scheduled_at', DateTime, nullable=False),
                                          Column('sent_at', DateTime))

        metadata.bind = self.engine
        metadata.create_all(self.engine)

        clear_mappers()
        mapper(Message, self.messages, properties={
                'digests': relationship(Digest, backref='msg')
                })

        mapper(Digest, self.digests)