Unverified Commit ae28fea1 authored by Dylan Jones's avatar Dylan Jones
Browse files

no slug

parent 16f647d6
"""inital database models
Revision ID: 24dfd0eb9da6
Revises:
Create Date: 2020-02-16 19:22:25.106964
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '24dfd0eb9da6'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('category',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('field',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('type', sa.Enum('STRING', 'FLOAT', 'INTEGER', 'BOOLEAN', 'RATING', name='fieldtype'), nullable=False),
sa.Column('unit', sa.String(), nullable=True),
sa.Column('category_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['category_id'], ['category.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('point',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(), nullable=True),
sa.Column('lat', sa.Float(), nullable=False),
sa.Column('lon', sa.Float(), nullable=False),
sa.Column('attributes', sa.JSON(), nullable=False),
sa.Column('category_id', sa.Integer(), nullable=False),
sa.Column('parent_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['category_id'], ['category.id'], ),
sa.ForeignKeyConstraint(['parent_id'], ['point.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('point')
op.drop_table('field')
op.drop_table('category')
# ### end Alembic commands ###
"""inital database models
Revision ID: 4af5fa1a7676
Revises:
Create Date: 2020-02-16 19:07:30.646828
"""
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision = '4af5fa1a7676'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
op.create_table('category',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('slug', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name'),
sa.UniqueConstraint('slug')
)
op.create_table('field',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('type', sa.Enum('STRING', 'FLOAT', 'INTEGER', 'BOOLEAN', 'RATING', name='fieldtype'), nullable=False),
sa.Column('unit', sa.String(), nullable=True),
sa.Column('category_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(('category_id',), ['category.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('point',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(), nullable=True),
sa.Column('lat', sa.Float(), nullable=False),
sa.Column('lon', sa.Float(), nullable=False),
sa.Column('attributes', sa.JSON(), nullable=False),
sa.Column('category_id', sa.Integer(), nullable=False),
sa.Column('parent_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(('category_id',), ['category.id'], ),
sa.ForeignKeyConstraint(('parent_id',), ['point.id'], ),
sa.PrimaryKeyConstraint('id')
)
def downgrade():
op.drop_table('point')
op.drop_table('field')
op.drop_table('category')
from flask import Flask
import sa
from sqlalchemy.orm import Session
from . import sa
app = Flask(__name__)
@app.route('/')
def index():
return "<h1>Hello</h1>"
return """
<head>
</head>
<body>
<h1>W H E R E</h1>
<p>This is the WHERE project.</p>
<a href='/test_data'>Click here to nuke the database and make it all be test data.</a>
</body>
"""
@app.route('/test_data')
def test_data():
with sa.session_context() as session:
session = Session()
session.query(sa.Point).delete()
session.query(sa.Field).delete()
session.query(sa.Category).delete()
wf = sa.Category()
wf.name = "Water Fountain"
wf.slug = "water_fountain"
if __name__ == '__main__':
......
from contextlib import contextmanager
from sqlalchemy import String, ForeignKey, Enum, Integer, Float, JSON
from sqlalchemy.ext.declarative import as_declarative
from sqlalchemy.orm import relationship, validates
from sqlalchemy.orm import relationship, validates, Session
from sqlalchemy.schema import Column
from .field_types import FieldType
@contextmanager
def session_context() -> Session:
session = Session()
try:
yield session
session.commit()
except BaseException:
session.rollback()
raise
finally:
session.close()
@as_declarative()
class Base(object):
id = Column(Integer, primary_key=True, autoincrement=True)
......@@ -50,7 +65,6 @@ class Category(Base):
"""
__tablename__ = 'category'
slug = Column(String, nullable=False, unique=True)
name = Column(String, nullable=False, unique=True)
fields = relationship("Field")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment