]> git.aero2k.de Git - sirius.git/commitdiff
Allow printers to have a name.
authorTom Hunger <tehunger@gmail.com>
Thu, 15 Jan 2015 11:38:24 +0000 (11:38 +0000)
committerTom Hunger <tehunger@gmail.com>
Thu, 15 Jan 2015 11:38:24 +0000 (11:38 +0000)
migrations/versions/4a0698610fcd_sirius_models.py [moved from migrations/versions/3b5c2e7b8185_sirius_models.py with 93% similarity]
notes/resetting_migration.md [new file with mode: 0644]
sirius/models/hardware.py
sirius/models/user.py
sirius/web/landing.py
sirius/web/templates/overview.html
sirius/web/templates/printer_overview.html
sirius/web/test_claiming.py

similarity index 93%
rename from migrations/versions/3b5c2e7b8185_sirius_models.py
rename to migrations/versions/4a0698610fcd_sirius_models.py
index fd0cd330067d3322156a92a6fb75780e2163724d..6f7087873a59928f333960cb8cd2442f849e9d6d 100644 (file)
@@ -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 (file)
index 0000000..b4e1d63
--- /dev/null
@@ -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
+```
index f4a17549f6c509a155673c3e9e695bd50133826a..600799cd06a9aa6258e24fd44a803c03ed214a9e 100644 (file)
@@ -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 '<ClaimCode xor: {} code: {}>'.format(self.hardware_xor, self.claim_code)
 
index 5d95baf9ebc71a9b7649b863f80652788ef4e10a..d74d4527f8c2e38c2fb380246e0c6348717e5be3 100644 (file)
@@ -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):
index ee1b4d0384afd6861e796e610075bf1bc46480c3..410b493a4467ca49320fa2a546795e229ff498dc 100644 (file)
@@ -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(
index faafa7fb76fde54a33b2b4b5e8f0d42b083caf7f..f9d789c89d9e120c8a9984c85fc2095b3b0b034b 100644 (file)
 {% for printer in my_printers %}
 <ul>
        <li>
-               <a href="{{ url_for('printer_print.printer_print',
+               <a href="{{ url_for('printer_overview.printer_overview',
                                user_id=current_user.id,
-                               username=current_user.username) }}">
-                       {{ printer.device_address }}</a>
+                               username=current_user.username,
+                               printer_id=printer.id) }}">
+                       {{ printer.name }}</a>
        </li>
 </ul>
 {% endfor %}
index 91489007a7b7a78b0f41e0ff7c953766b3740e8a..0c0042670a2ea87f836dbb9fa9c124548dc57f7a 100644 (file)
@@ -3,5 +3,5 @@
 {% block title %}Overview{% endblock %}
 
 {% block page_content %}
-<h1>Printer</h1>
+<h1>Printer {{ printer.name }}</h1>
 {% endblock %}
index 1d07d0b87b51b261ba3010844aea5d2e24c57856..6d14cbd6b9ff78104cc048d5281d4b4245ccd9fa 100644 (file)
@@ -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()