manage.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 werkzeug.utils import import_string
  14. from sqlalchemy.exc import IntegrityError, OperationalError
  15. from flask.ext.script import (Manager, Shell, Server, prompt, prompt_pass,
  16. prompt_bool)
  17. from flask.ext.migrate import MigrateCommand
  18. from flaskbb import create_app
  19. from flaskbb.extensions import db
  20. from flaskbb.utils.populate import (create_test_data, create_welcome_forum,
  21. create_admin_user, create_default_groups,
  22. create_default_settings, insert_mass_data,
  23. update_settings_from_fixture)
  24. # Use the development configuration if available
  25. try:
  26. from flaskbb.configs.development import DevelopmentConfig as Config
  27. except ImportError:
  28. from flaskbb.configs.default import DefaultConfig as Config
  29. app = create_app(Config)
  30. manager = Manager(app)
  31. # Run local server
  32. manager.add_command("runserver", Server("localhost", port=8080))
  33. # Migration commands
  34. manager.add_command('db', MigrateCommand)
  35. # Add interactive project shell
  36. def make_shell_context():
  37. return dict(app=current_app, db=db)
  38. manager.add_command("shell", Shell(make_context=make_shell_context))
  39. @manager.command
  40. def initdb():
  41. """Creates the database."""
  42. db.create_all()
  43. @manager.command
  44. def dropdb():
  45. """Deletes the database"""
  46. db.drop_all()
  47. @manager.option('-s', '--settings', dest="settings")
  48. @manager.option('-f', '--force', dest="force")
  49. def update(settings=None, force=False):
  50. """Updates the settings via a fixture. All fixtures have to be placed
  51. in the `fixture`.
  52. Usage: python manage.py update -s your_fixture
  53. """
  54. try:
  55. fixture = import_string(
  56. "flaskbb.fixtures.{}".format(settings)
  57. )
  58. fixture = fixture.fixture
  59. except ImportError:
  60. raise "{} fixture is not available".format(settings)
  61. if force:
  62. count = update_settings_from_fixture(fixture, overwrite_group=True,
  63. overwrite_setting=True)
  64. app.logger.info(
  65. "{} groups and {} settings forcefully updated."
  66. .format(count[0], count[1])
  67. )
  68. else:
  69. count = update_settings_from_fixture(fixture)
  70. app.logger.info(
  71. "{} groups and {} settings updated.".format(count[0], count[1])
  72. )
  73. @manager.command
  74. def createall(dropdb=False, createdb=False):
  75. """Creates the database with some testing content.
  76. If you do not want to drop or create the db add
  77. '-c' (to not create the db) and '-d' (to not drop the db)
  78. """
  79. if not dropdb:
  80. app.logger.info("Dropping database...")
  81. db.drop_all()
  82. if not createdb:
  83. app.logger.info("Creating database...")
  84. db.create_all()
  85. app.logger.info("Creating test data...")
  86. create_test_data()
  87. @manager.option('-u', '--username', dest='username')
  88. @manager.option('-p', '--password', dest='password')
  89. @manager.option('-e', '--email', dest='email')
  90. def create_admin(username=None, password=None, email=None):
  91. """Creates the admin user"""
  92. if not (username and password and email):
  93. username = prompt("Username")
  94. email = prompt("A valid email address")
  95. password = prompt_pass("Password")
  96. create_admin_user(username=username, password=password, email=email)
  97. @manager.option('-u', '--username', dest='username')
  98. @manager.option('-p', '--password', dest='password')
  99. @manager.option('-e', '--email', dest='email')
  100. def initflaskbb(username=None, password=None, email=None):
  101. """Initializes FlaskBB with all necessary data"""
  102. app.logger.info("Creating default data...")
  103. try:
  104. create_default_groups()
  105. create_default_settings()
  106. except IntegrityError:
  107. app.logger.error("Couldn't create the default data because it already "
  108. "exist!")
  109. if prompt_bool("Do you want to recreate the database? (y/n)"):
  110. db.session.rollback()
  111. db.drop_all()
  112. db.create_all()
  113. create_default_groups()
  114. create_default_settings()
  115. else:
  116. sys.exit(0)
  117. except OperationalError:
  118. app.logger.error("No database found.")
  119. if prompt_bool("Do you want to create the database now? (y/n)"):
  120. db.session.rollback()
  121. db.create_all()
  122. create_default_groups()
  123. create_default_settings()
  124. else:
  125. sys.exit(0)
  126. app.logger.info("Creating admin user...")
  127. if username and password and email:
  128. create_admin_user(username=username, password=password, email=email)
  129. else:
  130. create_admin()
  131. app.logger.info("Creating welcome forum...")
  132. create_welcome_forum()
  133. app.logger.info("Congratulations! FlaskBB has been successfully installed")
  134. @manager.command
  135. def insertmassdata():
  136. """Warning: This can take a long time!.
  137. Creates 100 topics and each topic contains 100 posts.
  138. """
  139. insert_mass_data()
  140. if __name__ == "__main__":
  141. manager.run()