manage.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, upgrade as db_upgrade
  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. # Use the development configuration if available
  22. try:
  23. from flaskbb.configs.development import DevelopmentConfig as Config
  24. except ImportError:
  25. from flaskbb.configs.default import DefaultConfig as Config
  26. app = create_app(Config)
  27. manager = Manager(app)
  28. # Run local server
  29. manager.add_command("runserver", Server("localhost", port=8080))
  30. # Migration commands
  31. manager.add_command('db', MigrateCommand)
  32. # Add interactive project shell
  33. def make_shell_context():
  34. return dict(app=current_app, db=db)
  35. manager.add_command("shell", Shell(make_context=make_shell_context))
  36. @manager.command
  37. def initdb():
  38. """Creates the database."""
  39. db.create_all()
  40. @manager.command
  41. def dropdb():
  42. """Deletes the database"""
  43. db.drop_all()
  44. @manager.command
  45. def createall(dropdb=False, createdb=False):
  46. """Creates the database with some testing content.
  47. If you do not want to drop or create the db add
  48. '-c' (to not create the db) and '-d' (to not drop the db)
  49. """
  50. if not dropdb:
  51. app.logger.info("Dropping database...")
  52. db.drop_all()
  53. if not createdb:
  54. app.logger.info("Creating database...")
  55. db.create_all()
  56. app.logger.info("Creating test data...")
  57. create_test_data()
  58. @manager.option('-u', '--username', dest='username')
  59. @manager.option('-p', '--password', dest='password')
  60. @manager.option('-e', '--email', dest='email')
  61. def create_admin(username, password, email):
  62. """Creates the admin user"""
  63. if not (username and password and email):
  64. username = prompt("Username")
  65. email = prompt("A valid email address")
  66. password = prompt_pass("Password")
  67. create_admin_user(username, email, password)
  68. @manager.option('-u', '--username', dest='username')
  69. @manager.option('-p', '--password', dest='password')
  70. @manager.option('-e', '--email', dest='email')
  71. def initflaskbb(username, password, email):
  72. """Initializes FlaskBB with all necessary data"""
  73. app.logger.info("Creating default groups...")
  74. try:
  75. create_default_groups()
  76. except IntegrityError:
  77. app.logger.error("Couldn't create the default groups because they are already exist!")
  78. if prompt_bool("Do you want to recreate the database? (y/n)"):
  79. db.session.rollback()
  80. db.drop_all()
  81. db.create_all()
  82. create_default_groups()
  83. else:
  84. sys.exit(0)
  85. except OperationalError:
  86. app.logger.error("No database found.")
  87. if prompt_bool("Do you want to create the database? (y/n)"):
  88. db.session.rollback()
  89. db.create_all()
  90. create_default_groups()
  91. else:
  92. sys.exit(0)
  93. app.logger.info("Creating admin user...")
  94. if username and password and email:
  95. create_admin_user(username, password, email)
  96. else:
  97. create_admin(username, password, email)
  98. app.logger.info("Creating welcome forum...")
  99. create_welcome_forum()
  100. app.logger.info("Congratulations! FlaskBB has been successfully installed")
  101. if __name__ == "__main__":
  102. manager.run()