manage.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. from flask import current_app
  12. from flask.ext.script import Manager, Shell, Server, prompt, prompt_pass
  13. from flask.ext.migrate import MigrateCommand
  14. from flaskbb import create_app
  15. from flaskbb.extensions import db
  16. from flaskbb.utils.populate import (create_test_data, create_welcome_forum,
  17. create_admin_user, create_default_groups)
  18. # Use the development configuration if available
  19. try:
  20. from flaskbb.configs.development import DevelopmentConfig as Config
  21. except ImportError:
  22. from flaskbb.configs.default import DefaultConfig as Config
  23. app = create_app(Config)
  24. manager = Manager(app)
  25. # Run local server
  26. manager.add_command("runserver", Server("localhost", port=8080))
  27. # Migration commands
  28. manager.add_command('db', MigrateCommand)
  29. # Add interactive project shell
  30. def make_shell_context():
  31. return dict(app=current_app, db=db)
  32. manager.add_command("shell", Shell(make_context=make_shell_context))
  33. @manager.command
  34. def initdb():
  35. """Creates the database."""
  36. db.create_all()
  37. @manager.command
  38. def dropdb():
  39. """Deletes the database"""
  40. db.drop_all()
  41. @manager.command
  42. def createall():
  43. """Creates the database with some testing content."""
  44. # Just for testing purposes
  45. db.drop_all()
  46. db.create_all()
  47. create_test_data()
  48. @manager.option('-u', '--username', dest='username')
  49. @manager.option('-p', '--password', dest='password')
  50. @manager.option('-e', '--email', dest='email')
  51. def create_admin(username, password, email):
  52. """Creates the admin user"""
  53. username = prompt("Username")
  54. email = prompt("A valid email address")
  55. password = prompt_pass("Password")
  56. create_admin_user(username, email, password)
  57. @manager.option('-u', '--username', dest='username')
  58. @manager.option('-p', '--password', dest='password')
  59. @manager.option('-e', '--email', dest='email')
  60. @manager.option('-d', '--dropdb', dest='dropdb', default=False)
  61. def initflaskbb(username, password, email, dropdb=False):
  62. """Initializes FlaskBB with all necessary data"""
  63. if dropdb:
  64. app.logger.info("Dropping previous database...")
  65. db.drop_all()
  66. app.logger.info("Creating tables...")
  67. db.create_all()
  68. app.logger.info("Creating default groups...")
  69. create_default_groups()
  70. app.logger.info("Creating admin user...")
  71. create_admin(username, password, email)
  72. app.logger.info("Creating welcome forum...")
  73. create_welcome_forum()
  74. app.logger.info("Congratulations! FlaskBB has been successfully installed")
  75. if __name__ == "__main__":
  76. manager.run()