def check_and_set_expired_subscribers(): with app.app_context(): print('About to check and set for expired subscribers') try: subs_wrapper.set_expired_subscribers() except Exception as exp: print(f'something went wrong: {exp}')
def handle_emails(): with app.app_context(): print('About to handle emails') over_due_loans = loans_wrapper.get_overdue_loans() email_reminder(over_due_loans, overdue=True) upcoming_due_loans = loans_wrapper.get_upcoming_due_loans( Config.DUE_THRESHOLD) email_reminder(upcoming_due_loans, overdue=False)
def setUp(self): """ Creates a new database for the unit test to use """ app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['DEBUG'] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True self.client = app.test_client() self.ctx = app.app_context() self.app = app db.init_app(self.app) with self.app.app_context(): db.create_all()
def add_model_instance(model_class, amount=10, parent=None): # print(model_class.__name__) with app.app_context(): insp = inspect(model_class) insp = inspect(model_class) children = [] child = {} has_parent = False has_child = False insp = inspect(model_class) fk_column = "" parent_column = "" for prop in insp.iterate_properties: if isinstance(prop, RelationshipProperty): if prop.direction.__str__() == "symbol('ONETOMANY')": has_child = True child = prop.argument() children.append(child) if (parent and prop.direction.__str__() == "symbol('MANYTOONE')"): for e in meta.tables[ model_class.__tablename__].foreign_keys: has_parent = True fk_column = e.parent.__str__().split(".")[ 1] # foreign key column parent_column = e.target_fullname.split(".")[ 1] # parent column for i in range(amount): arglist = {} for attr in filter(lambda x: x.name != "id", insp.c): # if is_foreign_key(model_class.__tablename__, attr.name): # arglist[attr.name]=getattr(parent, ) if (attr.name == fk_column and has_parent): arglist[attr.name] = getattr(parent, parent_column) continue fakerMethod = attr.doc.split('-')[0] fakerLocale = attr.doc.split('-')[1] fake = Faker(fakerLocale) arglist[attr.name] = getattr(fake, fakerMethod)() new_object = model_class(**arglist) db.session.commit() if (has_child): add_model_instance(child, amount, new_object) processed.append(model_class.__name__)
from models import db, Models, app with app.app_context(): # Reload tables db.drop_all() db.create_all()
from models import app, db import views if __name__ == '__main__': with app.app_context(): db.create_all() print app.url_map app.run(debug=True)