manage.py 6.1 KB

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