tartiflette/manage.py

87 lines
2.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
from flask_script import Manager, Shell, Command, Server
from app import db, create_app
app = create_app()
2018-05-30 02:32:31 +02:00
manager = Manager(app)
def main():
manager.add_command('shell', Shell(make_context=lambda:{"app":app, "db":db}))
manager.add_command("runserver", Server())
manager.run()
2018-05-30 02:32:31 +02:00
@manager.add_command
class Update(Command):
2018-05-30 02:32:31 +02:00
name = "update"
capture_all_args = True
2018-05-30 02:32:31 +02:00
def run(self, args=None):
valid_what = ["applists", "appci", "pr", "appobservatory"]
2018-05-30 02:32:31 +02:00
what = args[0] if args else None
assert what in valid_what, "Please specify what to update among %s" % ', '.join(valid_what)
if what == "applists":
from app.models.applists import AppList
AppList.update()
elif what == "appci":
2018-01-29 03:39:04 +01:00
from app.models.appci import AppCI
AppCI.update()
2018-05-30 02:32:31 +02:00
elif what == "pr":
2018-01-29 03:39:04 +01:00
from app.models.pr import Repo
for repo in Repo.query.all():
repo.update()
2018-05-30 02:32:31 +02:00
elif what == "appobservatory":
from app.models.unlistedapps import UnlistedApp
UnlistedApp.update()
2018-01-29 03:39:04 +01:00
else:
pass
2018-05-30 02:32:31 +02:00
@manager.add_command
class Nuke(Command):
"""Nuke the database (except the platform table)"""
2018-05-30 02:32:31 +02:00
name = "nuke"
def run(self):
import app.models.applists
2018-01-29 03:39:04 +01:00
import app.models.appci
import app.models.pr
2018-05-30 02:32:31 +02:00
import app.models.unlistedapps
2018-01-29 03:39:04 +01:00
print("> Droping tables...")
2018-05-30 02:32:31 +02:00
db.drop_all()
print("> Creating tables...")
2018-05-30 02:32:31 +02:00
db.create_all()
print("> Comitting sessions...")
2018-05-30 02:32:31 +02:00
db.session.commit()
2018-01-29 03:39:04 +01:00
2018-05-30 02:32:31 +02:00
@manager.add_command
class Init(Command):
2018-05-30 02:32:31 +02:00
name = "init"
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))]
models = set(models)
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()