tartiflette/manage.py

83 lines
2.1 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
from flask_script import Manager, Shell, Command
from app import db, create_app
app = create_app()
2018-01-29 03:39:04 +01:00
def main():
manager = Manager(app)
manager.add_command('shell', Shell(make_context=lambda:{"app":app, "db":db}))
manager.add_command('nuke', Nuke(db))
manager.add_command('init', Init(db))
2018-01-29 03:39:04 +01:00
manager.add_command('update-appci', Update(db, "appci"))
manager.add_command('update-pr', Update(db, "pr"))
manager.run()
2018-01-29 03:39:04 +01:00
class Update(Command):
2018-01-29 03:39:04 +01:00
def __init__(self, db, what):
self.db = db
2018-01-29 03:39:04 +01:00
self.what = what
def run(self):
2018-01-29 03:39:04 +01:00
if self.what == "appci":
from app.models.appci import AppCI
AppCI.update()
elif self.what == "pr":
from app.models.pr import Repo
for repo in Repo.query.all():
repo.update()
else:
pass
class Nuke(Command):
"""Nuke the database (except the platform table)"""
def __init__(self, db):
self.db = db
def run(self):
2018-01-29 03:39:04 +01:00
import app.models.appci
import app.models.pr
print("> Droping tables...")
self.db.drop_all()
print("> Creating tables...")
self.db.create_all()
print("> Comitting sessions...")
self.db.session.commit()
2018-01-29 03:39:04 +01:00
class Init(Command):
def __init__(self, db):
self.db = db
def run(self):
import app.models
2018-01-29 03:39:04 +01:00
# Black magic to extract list of models from 'models' folder
submodules = [ app.models.__dict__.get(m) for m in dir(app.models) if not m.startswith('__') ]
stuff = []
for submodule in submodules:
stuff.extend([submodule.__dict__.get(s) for s in dir(submodule)])
models = [s for s in stuff if isinstance(s, type(db.Model))]
for model in models:
objs = model.init()
if objs is not None:
print("> Feeding model %s with init data" % str(model.__name__))
for obj in model.init():
db.session.add(obj)
db.session.commit()
2018-01-29 03:39:04 +01:00
if __name__ == '__main__':
main()