def user(email, password, roles): """Create a new user to access the app with""" with app.app_context(): user = User.query.filter_by(email=email).count() if user: print("A user already exists with email: {}".format(email)) exit(1) user = current_app.user_datastore.create_user( email=email, password=encrypt_password(password) ) current_app.db.session.add(user) if roles: for role in roles.split(','): db_role = Role.query.filter_by(name=role).first() if not db_role: db_role = Role(name=role, description=None) current_app.db.session.add(db_role) user.roles.append(db_role) current_app.db.session.commit()
def drop_all(): """Drop all data from the current DB Context.""" if prompt_bool("Are you sure you want to lose all your data?"): with app.app_context(): current_app.db.drop_all() current_app.db.session.commit() else: exit(1)
def send_email_text(title, recipients, text): try: with app.app_context(): message = Message( subject=title, recipients=recipients, html=text ) mail.send(message) except Exception as e: logger.warning(f"sending email error: {e}")
def send_email(title, recipients, template, context): try: with app.app_context(): message = Message( subject=title, recipients=recipients, html=render_template(template, **context) ) mail.send(message) except Exception as e: logger.warning(f"Sending email error: {e}")
def user(email, password, roles): """Create a new user to access the app with""" with app.app_context(): user = User.query.filter_by(email=email).count() if user: print("A user already exists with email: {}".format(email)) exit(1) user = current_app.user_datastore.create_user( email=email, password=encrypt_password(password)) current_app.db.session.add(user) if roles: for role in roles.split(','): db_role = Role.query.filter_by(name=role).first() if not db_role: db_role = Role(name=role, description=None) current_app.db.session.add(db_role) user.roles.append(db_role) current_app.db.session.commit()
def structure(): """Call db.create_all() on the current DB context.""" with app.app_context(): current_app.db.create_all() current_app.db.session.commit()