manage.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """
  2. flaskbb.manage
  3. ~~~~~~~~~~~~~~~~~~~~
  4. This script provides some easy to use commands for
  5. creating the database with or without some sample content.
  6. You can also run the development server with it.
  7. Just type `python manage.py` to see the full list of commands.
  8. :copyright: (c) 2014 by the FlaskBB Team.
  9. :license: BSD, see LICENSE for more details.
  10. """
  11. import sys
  12. from flask import current_app
  13. from sqlalchemy.exc import IntegrityError, OperationalError
  14. from flask.ext.script import (Manager, Shell, Server, prompt, prompt_pass,
  15. prompt_bool)
  16. from flask.ext.migrate import MigrateCommand
  17. from flaskbb import create_app
  18. from flaskbb.extensions import db
  19. from flaskbb.utils.populate import (create_test_data, create_welcome_forum,
  20. create_admin_user, create_default_groups,
  21. create_default_settings)
  22. # Use the development configuration if available
  23. try:
  24. from flaskbb.configs.development import DevelopmentConfig as Config
  25. except ImportError:
  26. from flaskbb.configs.default import DefaultConfig as Config
  27. app = create_app(Config)
  28. manager = Manager(app)
  29. # Run local server
  30. manager.add_command("runserver", Server("localhost", port=8080))
  31. # Migration commands
  32. manager.add_command('db', MigrateCommand)
  33. # Add interactive project shell
  34. def make_shell_context():
  35. return dict(app=current_app, db=db)
  36. manager.add_command("shell", Shell(make_context=make_shell_context))
  37. @manager.command
  38. def initdb():
  39. """Creates the database."""
  40. db.create_all()
  41. @manager.command
  42. def dropdb():
  43. """Deletes the database"""
  44. db.drop_all()
  45. @manager.command
  46. def createall(dropdb=False, createdb=False):
  47. """Creates the database with some testing content.
  48. If you do not want to drop or create the db add
  49. '-c' (to not create the db) and '-d' (to not drop the db)
  50. """
  51. if not dropdb:
  52. app.logger.info("Dropping database...")
  53. db.drop_all()
  54. if not createdb:
  55. app.logger.info("Creating database...")
  56. db.create_all()
  57. app.logger.info("Creating test data...")
  58. create_test_data()
  59. @manager.option('-u', '--username', dest='username')
  60. @manager.option('-p', '--password', dest='password')
  61. @manager.option('-e', '--email', dest='email')
  62. def create_admin(username=None, password=None, email=None):
  63. """Creates the admin user"""
  64. if not (username and password and email):
  65. username = prompt("Username")
  66. email = prompt("A valid email address")
  67. password = prompt_pass("Password")
  68. create_admin_user(username=username, password=password, email=email)
  69. @manager.option('-u', '--username', dest='username')
  70. @manager.option('-p', '--password', dest='password')
  71. @manager.option('-e', '--email', dest='email')
  72. def initflaskbb(username=None, password=None, email=None):
  73. """Initializes FlaskBB with all necessary data"""
  74. app.logger.info("Creating default data...")
  75. try:
  76. create_default_groups()
  77. create_default_settings()
  78. except IntegrityError:
  79. app.logger.error("Couldn't create the default data because it already "
  80. "exist!")
  81. if prompt_bool("Do you want to recreate the database? (y/n)"):
  82. db.session.rollback()
  83. db.drop_all()
  84. db.create_all()
  85. create_default_groups()
  86. create_default_settings()
  87. else:
  88. sys.exit(0)
  89. except OperationalError:
  90. app.logger.error("No database found.")
  91. if prompt_bool("Do you want to create the database now? (y/n)"):
  92. db.session.rollback()
  93. db.create_all()
  94. create_default_groups()
  95. create_default_settings()
  96. else:
  97. sys.exit(0)
  98. app.logger.info("Creating admin user...")
  99. if username and password and email:
  100. create_admin_user(username=username, password=password, email=email)
  101. else:
  102. create_admin()
  103. app.logger.info("Creating welcome forum...")
  104. create_welcome_forum()
  105. app.logger.info("Congratulations! FlaskBB has been successfully installed")
  106. if __name__ == "__main__":
  107. manager.run()