"""Sirius models.
-Revision ID: 3be2a0e43b88
+Revision ID: d62b07987a2
Revises: None
-Create Date: 2015-01-15 15:33:10.629707
+Create Date: 2015-01-20 14:24:56.114106
"""
# revision identifiers, used by Alembic.
-revision = '3be2a0e43b88'
+revision = 'd62b07987a2'
down_revision = None
from alembic import op
def upgrade():
### commands auto generated by Alembic - please adjust! ###
- op.create_table('user',
- sa.Column('id', sa.Integer(), nullable=False),
- sa.Column('created', sa.DateTime(), nullable=True),
- sa.Column('username', sa.String(), nullable=True),
- sa.PrimaryKeyConstraint('id')
- )
op.create_table('bridge',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=True),
- sa.Column('bridge_address', sa.String(), nullable=False),
- sa.PrimaryKeyConstraint('id', 'bridge_address')
+ sa.Column('bridge_address', sa.String(), nullable=True),
+ sa.PrimaryKeyConstraint('id')
)
op.create_table('device_log',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('entry', sa.String(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
+ op.create_table('user',
+ sa.Column('id', sa.Integer(), nullable=False),
+ sa.Column('created', sa.DateTime(), nullable=True),
+ sa.Column('username', sa.String(), nullable=True),
+ sa.PrimaryKeyConstraint('id')
+ )
+ op.create_table('twitter_o_auth',
+ sa.Column('id', sa.Integer(), nullable=False),
+ sa.Column('created', sa.DateTime(), nullable=True),
+ sa.Column('user_id', sa.Integer(), nullable=True),
+ sa.Column('screen_name', sa.String(), nullable=True),
+ sa.Column('token', sa.String(), nullable=True),
+ sa.ForeignKeyConstraint(['user_id'], [u'user.id'], ),
+ sa.PrimaryKeyConstraint('id'),
+ sa.UniqueConstraint('screen_name'),
+ sa.UniqueConstraint('token')
+ )
op.create_table('printer',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['by_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
- op.create_table('twitter_o_auth',
+ op.create_table('message',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=True),
- sa.Column('user_id', sa.Integer(), nullable=True),
- sa.Column('screen_name', sa.String(), nullable=True),
- sa.Column('token', sa.String(), nullable=True),
- sa.ForeignKeyConstraint(['user_id'], [u'user.id'], ),
- sa.PrimaryKeyConstraint('id'),
- sa.UniqueConstraint('screen_name'),
- sa.UniqueConstraint('token')
+ sa.Column('pixels', sa.LargeBinary(), nullable=True),
+ sa.Column('sender_id', sa.Integer(), nullable=True),
+ sa.Column('target_printer_id', sa.Integer(), nullable=True),
+ sa.Column('response_timestamp', sa.DateTime(), nullable=True),
+ sa.Column('failure_message', sa.String(), nullable=True),
+ sa.ForeignKeyConstraint(['sender_id'], ['user.id'], ),
+ sa.ForeignKeyConstraint(['target_printer_id'], ['printer.id'], ),
+ sa.PrimaryKeyConstraint('id')
)
+ op.create_index(op.f('ix_message_created'), 'message', ['created'], unique=False)
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
- op.drop_table('twitter_o_auth')
+ op.drop_index(op.f('ix_message_created'), table_name='message')
+ op.drop_table('message')
op.drop_table('claim_code')
op.drop_table('printer')
+ op.drop_table('twitter_o_auth')
+ op.drop_table('user')
op.drop_table('device_log')
op.drop_table('bridge')
- op.drop_table('user')
### end Alembic commands ###
--- /dev/null
+import base64
+import datetime
+
+from sirius.models.db import db
+from sirius.models import hardware
+from sirius.models import user
+
+
+TIMEOUT_SECONDS = 60
+
+class Message(db.Model):
+ """Messages are printed by someone on ("sender") to a "target
+ printer".
+ """
+ id = db.Column(db.Integer, primary_key=True)
+ created = db.Column(db.DateTime, default=datetime.datetime.utcnow, index=True)
+ pixels = db.Column(db.LargeBinary)
+
+ sender_id = db.Column(db.Integer, db.ForeignKey('user.id'))
+ sender = db.relationship('User', backref=db.backref('messages', lazy='dynamic'))
+
+ target_printer_id = db.Column(db.Integer, db.ForeignKey('printer.id'))
+ target_printer = db.relationship('Printer', backref=db.backref('messages', lazy='dynamic'))
+
+ # The response is received by the protocol_loop. It's also
+ # possible to not receive a response (e.g. printer got
+ # disconnected) in which case we "time out" after TIMEOUT_SECONDS.
+ response_timestamp = db.Column(db.DateTime, nullable=True)
+ failure_message = db.Column(db.String, nullable=True)
+
+
+ @classmethod
+ def timeout_updates(cls, utcnow=None):
+ """Update all messages that are timed-out with an error message. This
+ is a destructive update. It should also be fairly cheap
+ because of the index on created.
+
+ We're doing these updates "eagerly" on every printer overview
+ page load instead of through a cron job, but there's no reason
+ why we couldn't do that.
+ """
+
+ if utcnow is None:
+ utcnow = datetime.datetime.utcnow()
+ cutoff = utcnow - datetime.timedelta(seconds=TIMEOUT_SECONDS)
+
+ cls.query.filter(cls.created <= cutoff).update(dict(
+ response_timestamp=utcnow,
+ failure_message="Timed out",
+ ))
+
+
+ def base64_pixels(self):
+ return base64.b64encode(self.pixels)