From: Tom Hunger Date: Thu, 15 Jan 2015 11:38:24 +0000 (+0000) Subject: Allow printers to have a name. X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=128cfbf0cf019adade62c38afabf4c176b498863;p=sirius.git Allow printers to have a name. --- diff --git a/migrations/versions/3b5c2e7b8185_sirius_models.py b/migrations/versions/4a0698610fcd_sirius_models.py similarity index 93% rename from migrations/versions/3b5c2e7b8185_sirius_models.py rename to migrations/versions/4a0698610fcd_sirius_models.py index fd0cd33..6f70878 100644 --- a/migrations/versions/3b5c2e7b8185_sirius_models.py +++ b/migrations/versions/4a0698610fcd_sirius_models.py @@ -1,13 +1,13 @@ """Sirius models. -Revision ID: 3b5c2e7b8185 +Revision ID: 4a0698610fcd Revises: None -Create Date: 2015-01-12 19:59:00.933933 +Create Date: 2015-01-15 11:27:22.373207 """ # revision identifiers, used by Alembic. -revision = '3b5c2e7b8185' +revision = '4a0698610fcd' down_revision = None from alembic import op @@ -35,12 +35,24 @@ def upgrade(): 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('claim_code', sa.Column('id', sa.Integer(), nullable=False), sa.Column('created', sa.DateTime(), nullable=True), sa.Column('by_id', sa.Integer(), nullable=True), sa.Column('hardware_xor', sa.Integer(), nullable=True), sa.Column('claim_code', sa.String(), nullable=True), + sa.Column('name', sa.String(), nullable=True), sa.ForeignKeyConstraint(['by_id'], ['user.id'], ), sa.PrimaryKeyConstraint('id') ) @@ -49,30 +61,20 @@ def upgrade(): sa.Column('created', sa.DateTime(), nullable=True), sa.Column('device_address', sa.String(), nullable=True), sa.Column('hardware_xor', sa.Integer(), nullable=True), + sa.Column('name', sa.String(), nullable=True), sa.Column('owner_id', sa.Integer(), nullable=True), sa.Column('used_claim_code', sa.String(), nullable=True), sa.ForeignKeyConstraint(['owner_id'], ['user.id'], ), 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') - ) ### end Alembic commands ### def downgrade(): ### commands auto generated by Alembic - please adjust! ### - op.drop_table('twitter_o_auth') op.drop_table('printer') op.drop_table('claim_code') + op.drop_table('twitter_o_auth') op.drop_table('user') op.drop_table('device_log') op.drop_table('bridge') diff --git a/notes/resetting_migration.md b/notes/resetting_migration.md new file mode 100644 index 0000000..b4e1d63 --- /dev/null +++ b/notes/resetting_migration.md @@ -0,0 +1,12 @@ +Migrations are meant for production systems. In order to start with a +clean migration layout run the following after a model change: + +```sh +rm -f sirius/data.sqlite +-git rm -f migrations/versions/*py +rm -f migrations/versions/*pyc +rm -f migrations/versions/*py +./manage.py db revision --autogenerate -m "Sirius models." +./manage.py db upgrade +git add migrations/versions/*py +``` diff --git a/sirius/models/hardware.py b/sirius/models/hardware.py index f4a1754..600799c 100644 --- a/sirius/models/hardware.py +++ b/sirius/models/hardware.py @@ -26,6 +26,9 @@ class Printer(db.Model): device_address = db.Column(db.String) hardware_xor = db.Column(db.Integer) + # Name of printer; used for display purposes. + name = db.Column(db.String) + # Update the following fields after we connected (i.e. joined over # hardware xor) a claim to a printer. The fields start out as # NULL. @@ -69,6 +72,7 @@ class Printer(db.Model): "claim code hardware xor collision: {}".format(hardware_xor) printer.owner_id = claim_code.by_id + printer.name = claim_code.name printer.used_claim_code = claim_code.claim_code db.session.add(printer) @@ -84,6 +88,9 @@ class ClaimCode(db.Model): hardware_xor = db.Column(db.Integer) claim_code = db.Column(db.String) + # Intended name of printer; used for display purposes. + name = db.Column(db.String) + def __repr__(self): return ''.format(self.hardware_xor, self.claim_code) diff --git a/sirius/models/user.py b/sirius/models/user.py index 5d95baf..d74d452 100644 --- a/sirius/models/user.py +++ b/sirius/models/user.py @@ -29,7 +29,7 @@ class User(db.Model): def is_authenticated(self): return True - def claim_printer(self, claim_code): + def claim_printer(self, claim_code, name): """Claiming can happen before the printer "calls home" for the first time so we need to be able to deal with that.""" @@ -38,6 +38,7 @@ class User(db.Model): by_id=self.id, hardware_xor=hardware_xor, claim_code=claim_code, + name=name, ) db.session.add(hcc) @@ -55,7 +56,9 @@ class User(db.Model): printer.claim_code = claim_code printer.hardware_xor = hardware_xor printer.owner_id = hcc.by_id + printer.name = name db.session.add(printer) + return printer class TwitterOAuth(db.Model): diff --git a/sirius/web/landing.py b/sirius/web/landing.py index ee1b4d0..410b493 100644 --- a/sirius/web/landing.py +++ b/sirius/web/landing.py @@ -54,8 +54,9 @@ def claim(user_id, username): form = ClaimForm() if form.validate_on_submit(): - login.current_user.claim_printer(form.claim_code.data) - db.session.commit() + login.current_user.claim_printer( + form.claim_code.data, + form.printer_name.data) return flask.redirect(flask.url_for('.landing')) return flask.render_template( diff --git a/sirius/web/templates/overview.html b/sirius/web/templates/overview.html index faafa7f..f9d789c 100644 --- a/sirius/web/templates/overview.html +++ b/sirius/web/templates/overview.html @@ -17,10 +17,11 @@ {% for printer in my_printers %} {% endfor %} diff --git a/sirius/web/templates/printer_overview.html b/sirius/web/templates/printer_overview.html index 9148900..0c00426 100644 --- a/sirius/web/templates/printer_overview.html +++ b/sirius/web/templates/printer_overview.html @@ -3,5 +3,5 @@ {% block title %}Overview{% endblock %} {% block page_content %} -

Printer

+

Printer {{ printer.name }}

{% endblock %} diff --git a/sirius/web/test_claiming.py b/sirius/web/test_claiming.py index 1d07d0b..6d14cbd 100644 --- a/sirius/web/test_claiming.py +++ b/sirius/web/test_claiming.py @@ -40,7 +40,7 @@ class Base(testing.TestCase): class TestClaiming(Base): def test_claim_first(self): - self.testuser.claim_printer('n5ry-p6x6-kth7-7hc4') + self.testuser.claim_printer('n5ry-p6x6-kth7-7hc4', 'my test printer') hardware.Printer.phone_home('000d6f000273ce0b') db.session.commit() printer = hardware.Printer.query.first() @@ -49,7 +49,7 @@ class TestClaiming(Base): def test_printer_phone_home_first(self): hardware.Printer.phone_home('000d6f000273ce0b') - self.testuser.claim_printer('n5ry-p6x6-kth7-7hc4') + self.testuser.claim_printer('n5ry-p6x6-kth7-7hc4', 'my test printer') db.session.commit() printer = hardware.Printer.query.first()