shell.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import flask_migrate
  2. from IPython import embed
  3. from sqlalchemy_utils import create_database, database_exists, drop_database
  4. from app.config import DB_URI
  5. from app.email_utils import send_email, render
  6. from app.models import *
  7. from server import create_app
  8. def create_db():
  9. if not database_exists(DB_URI):
  10. LOG.debug("db not exist, create database")
  11. create_database(DB_URI)
  12. # Create all tables
  13. # Use flask-migrate instead of db.create_all()
  14. flask_migrate.upgrade()
  15. def change_password(user_id, new_password):
  16. user = User.get(user_id)
  17. user.set_password(new_password)
  18. db.session.commit()
  19. def reset_db():
  20. if database_exists(DB_URI):
  21. drop_database(DB_URI)
  22. create_db()
  23. def send_safari_extension_newsletter():
  24. for user in User.query.all():
  25. send_email(
  26. user.email,
  27. "Quickly create alias with our Safari extension",
  28. render("com/safari-extension.txt", user=user),
  29. render("com/safari-extension.html", user=user),
  30. )
  31. app = create_app()
  32. with app.app_context():
  33. # to test email template
  34. # with open("/tmp/email.html", "w") as f:
  35. # user = User.get(1)
  36. # f.write(
  37. # render(
  38. # "transactional/reset-password.html",
  39. # email=user.email,
  40. # user=user,
  41. # name=user.name,
  42. # activation_link="https://ab.cd",
  43. # alias="alias@ab.cd",
  44. # directory="dir",
  45. # domain="domain",
  46. # new_email="new@email.com",
  47. # current_email="current@email.com",
  48. # link="https://link.com",
  49. # mailbox_email="mailbox_email@email.com",
  50. # sender="sender@example.com",
  51. # reset_password_link="http://reset_password_link",
  52. # )
  53. # )
  54. embed()