Skip to content

Instantly share code, notes, and snippets.

@boolbag
Created September 3, 2015 04:35
Show Gist options
  • Save boolbag/2ee6e0ab53094b556790 to your computer and use it in GitHub Desktop.
Save boolbag/2ee6e0ab53094b556790 to your computer and use it in GitHub Desktop.
from flask import Flask
from flask_admin import Admin
from flask_admin.contrib import sqla
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
# Create application
app = Flask(__name__)
# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = '123456790'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///a_sample_database.sqlite'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
# Create admin app
admin = Admin(app, name="Cascade Accessories", template_mode='bootstrap3')
# Flask views
@app.route('/')
def index():
return '<a href="/admin/">Click me to get to Admin!</a>'
class Activity(db.Model):
__tablename__ = 'activity'
actid = Column(Integer, primary_key=True)
activity = Column(String(20), nullable=False, unique=True)
accessory = relationship('Accessory', backref='activity')
event = relationship('Event', backref='activity')
def __str__(self):
return self.activity
class Accessory(db.Model):
__tablename__ = 'accessory'
accid = Column(Integer, primary_key=True)
actid = Column(ForeignKey('activity.actid', ondelete='CASCADE', onupdate='CASCADE'), nullable=False)
accessory = Column(String(20), nullable=False, unique=True)
eventaccessory = relationship('EventAccessory', backref='accessory')
inline_models = (Activity,)
def __str__(self):
return self.accessory
class Event(db.Model):
__tablename__ = 'event'
eid = Column(Integer, primary_key=True)
actid = Column(ForeignKey('activity.actid', ondelete='CASCADE', onupdate='CASCADE'), nullable=False)
event = Column(String(20), nullable=False, index=True)
eventaccessory = relationship('EventAccessory', backref='event')
inline_models = (Activity,)
def __str__(self):
return self.event
class EventAccessory(db.Model):
__tablename__ = 'eventaccessory'
evaccid = Column(Integer, primary_key=True)
eid = Column(ForeignKey('event.eid', ondelete='CASCADE', onupdate='CASCADE'), nullable=False)
accid = Column(ForeignKey('accessory.accid', ondelete='CASCADE', onupdate='CASCADE'), nullable=False)
notes = Column(String(80), nullable=False)
inline_models = (Event, Accessory)
def __str__(self):
return self.notes
class AccessoryAdmin(sqla.ModelView):
column_list = ('activity', 'accessory')
column_editable_list = column_list
class EventAdmin(sqla.ModelView):
column_list = ('activity', 'event')
column_editable_list = column_list
class EventAccessoryAdmin(sqla.ModelView):
column_list = ('event', 'accessory', 'notes')
column_editable_list = column_list
pass
admin.add_view(EventAccessoryAdmin(EventAccessory, db.session))
# More submenu
admin.add_view(sqla.ModelView(Activity, db.session, category='See Other Tables'))
admin.add_view(AccessoryAdmin(Accessory, db.session, category='See Other Tables'))
admin.add_view(EventAdmin(Event, db.session, category='See Other Tables'))
if __name__ == '__main__':
db.drop_all()
db.create_all()
db.session.add(Activity(actid=1, activity='Swimming'))
db.session.add(Activity(actid=2, activity='Tennis'))
db.session.commit()
db.session.add(Accessory(accid=1, actid=1, accessory='swimming suit'))
db.session.add(Accessory(accid=2, actid=1, accessory='goggles'))
db.session.add(Accessory(accid=3, actid=2, accessory='racket'))
db.session.add(Accessory(accid=4, actid=2, accessory='shorts'))
db.session.add(Event(eid=1, actid=1, event='The Big Swim'))
db.session.add(Event(eid=2, actid=2, event='Wimbledon'))
db.session.commit()
db.session.add(EventAccessory(evaccid=1, eid=1, accid=1, notes='Get all sizes' ))
db.session.add(EventAccessory(evaccid=2, eid=2, accid=3, notes='Large rackets only' ))
db.session.commit()
# Start app
app.run(debug=True)
@boolbag
Copy link
Author

boolbag commented Sep 3, 2015

Accompanies this request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment