manage.py 6.1 KB

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