def setup_database(_app): # {{{ """Add some sample data to database""" with _app.app_context(): _app.logger.info('Creating databases') DB.drop_all() # TODO do not drop all data on restart DB.create_all() DB.session.commit() _app.logger.info('Created databases') example_game = Game.query.filter_by(game_ext_id='BASEMENT').first() if example_game is None: _app.logger.info('Creating example game') example_game = Game(game_ext_id='BASEMENT') DB.session.add(example_game) DB.session.commit() for i in range(5): test_playersession = PlayerSession( username='******'.format(num=i), session_key=gen_session_key(), game_id=example_game.id) DB.session.add(test_playersession) DB.session.commit() _app.logger.info('Created test game and playersessions')
def create_app(): # {{{ """ Get configuration and create the flask instance. :return: The flask app instance. :rtype: Flask """ _app = Flask(__name__, template_folder='../templates') _app.secret_key = 'yeetyeetskeetskeet' # This seems like a TODO, but I don't know what to do about it _app.logger = create_logger(_app) _app.logger.setLevel(logging.DEBUG) db_host = os.environ.get('DBHOST', '127.0.0.1') db_port = int(os.environ.get('DBPORT', 5433)) db_password = os.environ.get('DBPASS', 'notwaterloo') db_database = 'buzzdb' db_string = 'postgresql://*****:*****@{host}:{port}/{database}'.format( password=db_password, host=db_host, port=db_port, database=db_database) _app.config['SQLALCHEMY_DATABASE_URI'] = db_string _app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False DB.init_app(_app) IP_LIMITER.init_app(_app) return _app
def setup_database(_app): # {{{ """Add some sample data to database""" with _app.app_context(): _app.logger.info('Creating databases') DB.drop_all() # TODO DB.create_all() DB.session.commit() _app.logger.info('Created databases') example_user = User.query.filter_by(username='******').first() if example_user is None: _app.logger.info('Creating test user') example_user = User(username='******', email='*****@*****.**', password_hash=hash_password('asdf')) DB.session.add(example_user) DB.session.commit() example_item = Item.query.filter_by(user_id=example_user.id).first() if example_item is None: _app.logger.info('Creating test item') example_item = Item(user_id=example_user.id, name='Test', purchase_price=123, sell_price=2345) DB.session.add(example_item) example_item_two = Item(user_id=example_user.id, name='Test2', purchase_price=23, sell_price=5678) DB.session.add(example_item_two) example_item_three = Item(user_id=example_user.id, name='Test3', purchase_price=1235, sell_price=778) DB.session.add(example_item_three) DB.session.commit() example_picture = Picture.query.filter_by( item_id=example_item.id).first() if example_picture is None: _app.logger.info('Creating test picture') example_picture = Picture(item_id=example_item.id, path='http://reddit.com') DB.session.add(example_picture) DB.session.commit() example_location = Location.query.filter_by( name='Freelton Market').first() if example_location is None: _app.logger.info('Creating test location') example_location = Location(name='Freelton Market', user_id=example_user.id) DB.session.add(example_location) DB.session.commit() _app.logger.info('Created test user, item, picture and location')
class Game(DB.Model): """Game database model""" __tablename__ = 'game' id = DB.Column(DB.Integer, nullable=False, autoincrement=True, primary_key=True) game_ext_id = DB.Column(DB.String(8), nullable=False) q_num = DB.Column(DB.Integer, nullable=False, default=0)
class Account(DB.Model): """Account database model""" __tablename__ = 'accounts' id = DB.Column(DB.Integer, nullable=False, autoincrement=True, primary_key=True) device_key = DB.Column(DB.String(350), nullable=False) create_date = DB.Column(DB.DateTime, nullable=False, default=datetime.utcnow)
class Location(DB.Model): """Location database model""" __tablename__ = 'locations' id = DB.Column(DB.Integer, nullable=False, autoincrement=True, primary_key=True) name = DB.Column(DB.String(120), nullable=False) user_id = DB.Column(DB.Integer, DB.ForeignKey(User.__tablename__ + '.id'), nullable=False)
class Picture(DB.Model): """Picture database model""" __tablename__ = 'pictures' id = DB.Column(DB.Integer, nullable=False, autoincrement=True, primary_key=True) create_date = DB.Column(DB.DateTime, nullable=False, default=datetime.utcnow) item_id = DB.Column(DB.Integer, DB.ForeignKey(Item.__tablename__ + '.id'), nullable=False) path = DB.Column(DB.Text, nullable=False)
class User(DB.Model): """User database model""" __tablename__ = 'users' id = DB.Column(DB.Integer, nullable=False, autoincrement=True, primary_key=True) username = DB.Column(DB.String(80), unique=True, nullable=False) email = DB.Column(DB.String(120), nullable=False) password_hash = DB.Column(DB.String(64), nullable=False) create_date = DB.Column(DB.DateTime, nullable=False, default=datetime.utcnow) items = DB.relationship('Item', backref='user', lazy=True)
class PlayerSession(DB.Model): """Player->Game session database model""" __tablename__ = 'playersession' id = DB.Column(DB.Integer, nullable=False, autoincrement=True, primary_key=True) username = DB.Column(DB.String(64), nullable=False) session_key = DB.Column(DB.String(8), nullable=False) buzz_time = DB.Column(DB.DateTime, nullable=True) game_id = DB.Column(DB.Integer, DB.ForeignKey(Game.__tablename__ + '.id', ondelete='CASCADE'), nullable=False) is_creator = DB.Column(DB.Boolean, nullable=False, default=False)
class Hype(DB.Model): """Hype database model""" __tablename__ = 'hypes' id = DB.Column(DB.Integer, nullable=False, autoincrement=True, primary_key=True) event_id = DB.Column(DB.Integer, DB.ForeignKey(Event.__tablename__ + '.id', ondelete='CASCADE'), nullable=False) account_id = DB.Column(DB.Integer, DB.ForeignKey(Account.__tablename__ + '.id', ondelete='CASCADE'), nullable=False) create_date = DB.Column(DB.DateTime, nullable=False, default=datetime.utcnow) __table_args__ = (DB.UniqueConstraint( 'event_id', 'account_id', name='_hype_event_account_unique_constraint'), )
class Item(DB.Model): """Item database model""" __tablename__ = 'items' id = DB.Column(DB.Integer, nullable=False, autoincrement=True, primary_key=True) create_date = DB.Column(DB.DateTime, nullable=False, default=datetime.utcnow) user_id = DB.Column(DB.Integer, DB.ForeignKey(User.__tablename__ + '.id'), nullable=False) location_id = DB.Column(DB.Integer, DB.ForeignKey(Location.__tablename__ + '.id'), nullable=True) description = DB.Column(DB.Text, nullable=True) name = DB.Column(DB.Text, nullable=False) purchase_date = DB.Column(DB.DateTime, nullable=True) purchase_price = DB.Column(DB.Numeric(precision=19, scale=4, asdecimal=True), nullable=True) sell_date = DB.Column(DB.DateTime, nullable=True) sell_price = DB.Column(DB.Numeric(precision=19, scale=4, asdecimal=True), nullable=True) listed_price = DB.Column(DB.Numeric(precision=19, scale=4, asdecimal=True), nullable=True) pictures = DB.relationship("Picture", cascade="all,delete", backref="item")
def setup_database(_app): # {{{ """Add some sample data to database""" with _app.app_context(): _app.logger.info('Creating databases') DB.drop_all() # TODO do not drop all data on restart DB.create_all() DB.session.commit() _app.logger.info('Created databases') example_account = Account.query.filter_by(device_key='exampleaccount').first() if example_account is None: _app.logger.info('Creating example account') example_account = Account(device_key='exampleaccount') DB.session.add(example_account) DB.session.commit() test_accounts = [Account()] * 5 # five test accounts for i, _ in enumerate(test_accounts): test_accounts[i] = Account.query.filter_by(device_key='testaccount{i}'.format(i=i)).first() if test_accounts[i] is None: test_accounts[i] = Account(device_key='testaccount{i}'.format(i=i)) DB.session.add(test_accounts[i]) DB.session.commit() example_event = Event.query.filter_by(account_id=example_account.id).first() if example_event is None: _app.logger.info('Creating test event') example_event = Event(account_id=example_account.id, latitude=32.079663, longitude=34.775528, group_size_max=3, group_size_min=1, title="Isreal is real", category="Social", time=datetime.datetime.utcnow(), description="Let us come together in peace. 3 hypes") DB.session.add(example_event) DB.session.commit() for i in range(3): DB.session.add(Hype(account_id=test_accounts[i].id, event_id=example_event.id)) DB.session.add(Checkin(account_id=test_accounts[i].id, event_id=example_event.id)) example_event_two = Event(account_id=example_account.id, latitude=43.545199, longitude=-80.246926, group_size_max=5, group_size_min=3, title="Trappers halloween costume party", category="Social drinking", time=datetime.datetime.utcnow(), description="BYOB costume party. Hawaiian theme. 5 hypes") DB.session.add(example_event_two) DB.session.commit() for i in range(5): DB.session.add(Hype(account_id=test_accounts[i].id, event_id=example_event_two.id)) DB.session.add(Checkin(account_id=test_accounts[i].id, event_id=example_event_two.id)) example_event_three = Event(account_id=example_account.id, latitude=43.530793, longitude=-80.229077, group_size_max=1, group_size_min=1, title="LAN party in Reynolds!", category="Sports", time=datetime.datetime.utcnow(), description="Bring a laptop and Halo CE for the LAN party. 3 hypes") DB.session.add(example_event_three) DB.session.commit() for i in range(3): DB.session.add(Hype(account_id=test_accounts[i].id, event_id=example_event_three.id)) DB.session.add(Checkin(account_id=test_accounts[i].id, event_id=example_event_three.id)) example_event_four = Event(account_id=example_account.id, latitude=43.531793, longitude=-80.228077, group_size_max=1, group_size_min=1, title="Vapers anonymous", category="Social", time=datetime.datetime.utcnow(), description="0 hypes") DB.session.add(example_event_four) DB.session.commit() _app.logger.info('Created test account and events')
class Event(DB.Model): """Event database model""" __tablename__ = 'events' id = DB.Column(DB.Integer, nullable=False, autoincrement=True, primary_key=True) latitude = DB.Column(DB.Float(precision=32, asdecimal=True), nullable=False) longitude = DB.Column(DB.Float(precision=32, asdecimal=True), nullable=False) account_id = DB.Column(DB.Integer, DB.ForeignKey(Account.__tablename__ + '.id', ondelete='CASCADE'), nullable=False) time = DB.Column(DB.DateTime, nullable=False) group_size_max = DB.Column(DB.Integer, nullable=False) group_size_min = DB.Column(DB.Integer, nullable=False) title = DB.Column(DB.String(50), nullable=False) category = DB.Column(DB.String(50), nullable=False) description = DB.Column(DB.String(140), nullable=False) create_date = DB.Column(DB.DateTime, nullable=False, default=datetime.utcnow)