Changeset - 0312941b045c
[Not reviewed]
0 4 2
Lance Edgar - 9 years ago 2016-01-13 02:34:15
ledgar@sacfoodcoop.com
Add initial 'messages' support in schema/import.
6 files changed with 236 insertions and 0 deletions:
0 comments (0 inline, 0 general)
rattail/db/alembic/versions/40326eb83e18_add_messages.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8 -*-
 
"""add messages
 

	
 
Revision ID: 40326eb83e18
 
Revises: d042f5e34b77
 
Create Date: 2016-01-11 14:08:29.612184
 

	
 
"""
 

	
 
from __future__ import unicode_literals
 

	
 
# revision identifiers, used by Alembic.
 
revision = '40326eb83e18'
 
down_revision = u'd042f5e34b77'
 
branch_labels = None
 
depends_on = None
 

	
 
from alembic import op
 
import sqlalchemy as sa
 
import rattail.db.types
 
from sqlalchemy.dialects import postgresql
 

	
 

	
 
def upgrade():
 

	
 
    # message
 
    op.create_table('message',
 
                    sa.Column('uuid', sa.String(length=32), nullable=False),
 
                    sa.Column('sender_uuid', sa.String(length=32), nullable=False),
 
                    sa.Column('subject', sa.String(length=255), nullable=True),
 
                    sa.Column('body', sa.Text(), nullable=True),
 
                    sa.Column('sent', sa.DateTime(), nullable=False),
 
                    sa.ForeignKeyConstraint(['sender_uuid'], [u'user.uuid'], name=u'message_fk_sender'),
 
                    sa.PrimaryKeyConstraint('uuid')
 
    )
 

	
 
    # message_recip
 
    op.create_table('message_recip',
 
                    sa.Column('uuid', sa.String(length=32), nullable=False),
 
                    sa.Column('message_uuid', sa.String(length=32), nullable=False),
 
                    sa.Column('recipient_uuid', sa.String(length=32), nullable=False),
 
                    sa.Column('status', sa.Integer(), nullable=False),
 
                    sa.ForeignKeyConstraint(['message_uuid'], [u'message.uuid'], name=u'message_recip_fk_message'),
 
                    sa.ForeignKeyConstraint(['recipient_uuid'], [u'user.uuid'], name=u'message_recip_fk_recipient'),
 
                    sa.PrimaryKeyConstraint('uuid')
 
    )
 

	
 

	
 
def downgrade():
 

	
 
    # message_recip
 
    op.drop_table('message_recip')
 

	
 
    # message
 
    op.drop_table('message')
rattail/db/importing/models.py
Show inline comments
 
@@ -220,6 +220,35 @@ class UserImporter(Importer):
 
                    user.roles.remove(self.admin)
 

	
 

	
 
class MessageImporter(Importer):
 
    """
 
    User message data importer.
 
    """
 
    model_class = model.Message
 
    normalizer_class = normal.MessageNormalizer
 
    simple_fields = [
 
        'uuid',
 
        'sender_uuid',
 
        'subject',
 
        'body',
 
        'sent',
 
    ]
 

	
 

	
 
class MessageRecipientImporter(Importer):
 
    """
 
    User message recipient data importer.
 
    """
 
    model_class = model.MessageRecipient
 
    normalizer_class = normal.MessageRecipientNormalizer
 
    simple_fields = [
 
        'uuid',
 
        'message_uuid',
 
        'recipient_uuid',
 
        'status',
 
    ]
 

	
 

	
 
class StoreImporter(Importer):
 
    """
 
    Store data importer.
rattail/db/importing/normal.py
Show inline comments
 
@@ -53,3 +53,32 @@ class UserNormalizer(Normalizer):
 
            'active': user.active,
 
            'admin': self.admin in user.roles,
 
        }
 

	
 

	
 
class MessageNormalizer(Normalizer):
 
    """
 
    Normalizer for message data.
 
    """
 

	
 
    def normalize(self, message):
 
        return {
 
            'uuid': message.uuid,
 
            'sender_uuid': message.sender_uuid,
 
            'subject': message.subject,
 
            'body': message.body,
 
            'sent': message.sent,
 
        }
 

	
 

	
 
class MessageRecipientNormalizer(Normalizer):
 
    """
 
    Normalizer for message recipient data.
 
    """
 

	
 
    def normalize(self, recip):
 
        return {
 
            'uuid': recip.uuid,
 
            'message_uuid': recip.message_uuid,
 
            'recipient_uuid': recip.recipient_uuid,
 
            'status': recip.status,
 
        }
rattail/db/model/__init__.py
Show inline comments
 
@@ -37,6 +37,7 @@ from .customers import (Customer, CustomerPhoneNumber, CustomerEmailAddress,
 
from .vendors import Vendor, VendorPhoneNumber, VendorEmailAddress, VendorContact
 
from .org import Department, Subdepartment, Category, Family, ReportCode, DepositLink
 
from .products import Brand, Tax, Product, ProductCode, ProductCost, ProductPrice
 
from .messages import Message, MessageRecipient
 

	
 
from .datasync import DataSyncChange
 
from .batches import Batch, BatchColumn, BatchRow
rattail/db/model/messages.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8 -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2016 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
#  Rattail is free software: you can redistribute it and/or modify it under the
 
#  terms of the GNU Affero General Public License as published by the Free
 
#  Software Foundation, either version 3 of the License, or (at your option)
 
#  any later version.
 
#
 
#  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
 
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
#  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
 
#  more details.
 
#
 
#  You should have received a copy of the GNU Affero General Public License
 
#  along with Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 
"""
 
Data Models for User Messages
 
"""
 

	
 
from __future__ import unicode_literals, absolute_import
 

	
 
import datetime
 

	
 
import sqlalchemy as sa
 
from sqlalchemy import orm
 

	
 
from .core import Base, uuid_column
 
from .users import User
 

	
 

	
 
class Message(Base):
 
    """
 
    Represents a message, sent from one user to other user(s).
 
    """
 
    __tablename__ = 'message'
 
    __table_args__ = (
 
        sa.ForeignKeyConstraint(['sender_uuid'], ['user.uuid'], name='message_fk_sender'),
 
    )
 

	
 
    uuid = uuid_column()
 
    sender_uuid = sa.Column(sa.String(length=32), nullable=False)
 

	
 
    sender = orm.relationship(
 
        User, doc="""
 
        Reference to the user who sent the message.
 
        """,
 
        backref=orm.backref('sent_messages', cascade='all, delete-orphan', doc="""
 
        List of all messages which have ever been sent by the user.
 
        """))
 

	
 
    subject = sa.Column(sa.String(length=255), nullable=True, doc="""
 
    Subject for the message.
 
    """)
 

	
 
    body = sa.Column(sa.Text(), nullable=True, doc="""
 
    Body for the message.  This is assumed to be of type 'text/html'.
 
    """)
 

	
 
    sent = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow, doc="""
 
    UTC timestamp when the message was sent.
 
    """)
 

	
 
    def __unicode__(self):
 
        return unicode(self.subject or '')
 

	
 
    def add_recipient(self, user, **kwargs):
 
        kwargs['recipient'] = user
 
        self.recipients.append(MessageRecipient(**kwargs))
 

	
 

	
 
class MessageRecipient(Base):
 
    """
 
    Represents the combination of a single message and a single recipient.
 
    Also tracks status of the message for that recipient, i.e. whether it shows
 
    in their "inbox".
 
    """
 
    __tablename__ = 'message_recip'
 
    __table_args__ = (
 
        sa.ForeignKeyConstraint(['message_uuid'], ['message.uuid'], name='message_recip_fk_message'),
 
        sa.ForeignKeyConstraint(['recipient_uuid'], ['user.uuid'], name='message_recip_fk_recipient'),
 
    )
 

	
 
    uuid = uuid_column()
 
    message_uuid = sa.Column(sa.String(length=32), nullable=False)
 
    recipient_uuid = sa.Column(sa.String(length=32), nullable=False)
 

	
 
    status = sa.Column(sa.Integer(), nullable=False, doc="""
 
    Status code for the message; used to indicate inbox vs. archive.
 
    """)
 

	
 
    message = orm.relationship(
 
        Message, doc="""
 
        Reference to the message which has been "received".
 
        """,
 
        backref=orm.backref('recipients', cascade='all, delete-orphan', doc="""
 
        List of recipients for the message.
 
        """,
 
        ))
 

	
 
    recipient = orm.relationship(
 
        User, doc="""
 
        Reference to the user who "received" the message.
 
        """)
 

	
 
    def __unicode__(self):
 
        return unicode(self.recipient)
rattail/enum.py
Show inline comments
 
@@ -85,6 +85,15 @@ EMAIL_PREFERENCE = {
 
    }
 

	
 

	
 
MESSAGE_STATUS_INBOX            = 1
 
MESSAGE_STATUS_ARCHIVE          = 2
 

	
 
MESSAGE_STATUS = {
 
    MESSAGE_STATUS_INBOX        : "Inbox",
 
    MESSAGE_STATUS_ARCHIVE      : "Archive",
 
}
 

	
 

	
 
PHONE_TYPE_HOME                 = 'home'
 
PHONE_TYPE_MOBILE               = 'mobile'
 
PHONE_TYPE_OTHER                = 'other'
0 comments (0 inline, 0 general)