manage.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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) 2013 by the FlaskBB Team.
  9. :license: BSD, see LICENSE for more details.
  10. """
  11. import os
  12. from flask import current_app
  13. from flask.ext.script import Manager, Shell, Server
  14. from flaskbb import create_app
  15. from flaskbb.configs.development import DevelopmentConfig, BaseConfig
  16. from flaskbb.extensions import db
  17. from flaskbb.user.models import User
  18. from flaskbb.forum.models import Post, Topic, Forum, Category
  19. app = create_app(DevelopmentConfig)
  20. manager = Manager(app)
  21. # Run local server
  22. manager.add_command("runserver", Server("localhost", port=8080))
  23. # Add interactive project shell
  24. def make_shell_context():
  25. return dict(app=current_app, db=db)
  26. manager.add_command("shell", Shell(make_context=make_shell_context))
  27. @manager.command
  28. def initdb():
  29. """
  30. Creates the database.
  31. """
  32. db.create_all()
  33. @manager.command
  34. def createall():
  35. """
  36. Creates the database with some example content.
  37. """
  38. # Just for testing purposes
  39. dbfile = os.path.join(BaseConfig._basedir, "flaskbb.sqlite")
  40. if os.path.exists(dbfile):
  41. print "Removing old database file..."
  42. os.remove(dbfile)
  43. db.create_all()
  44. # create 5 users
  45. for u in range(1, 6):
  46. username = "test%s" % u
  47. email = "test%s@example.org" % u
  48. user = User(username=username, password="test", email=email)
  49. db.session.add(user)
  50. # create 2 categories
  51. for i in range(1, 3):
  52. category_title = "Test Category %s" % i
  53. category = Category(title=category_title, description="Test Description")
  54. db.session.add(category)
  55. # create 2 forums in each category
  56. for j in range(1, 3):
  57. if i == 2:
  58. j += 2
  59. forum_title = "Test Forum %s %s" % (j, i)
  60. forum = Forum(title=forum_title, description="Test Description",
  61. category_id=i)
  62. db.session.add(forum)
  63. # create 1 topic in each forum
  64. for k in range(1, 5):
  65. topic = Topic()
  66. topic.first_post = Post()
  67. topic.title = "Test Title %s" % k
  68. topic.user_id = 1
  69. topic.forum_id = k
  70. topic.first_post.content = "Test Content"
  71. topic.first_post.user_id = 1
  72. topic.first_post.topic_id = k
  73. db.session.add(topic)
  74. db.session.commit()
  75. # Update the post and topic count
  76. topic.forum.topic_count += 1
  77. topic.forum.post_count += 1
  78. topic.post_count += 1
  79. topic.first_post.user.post_count += 1
  80. # create 2 additional posts for each topic
  81. for m in range(1, 3):
  82. post = Post(content="Test Post", user_id=2, topic_id=k)
  83. db.session.add(post)
  84. db.session.commit()
  85. # Update the post count
  86. post.user.post_count += 1
  87. topic.post_count += 1
  88. topic.forum.post_count += 1
  89. topic.last_post_id = post.id
  90. topic.forum.last_post_id = post.id
  91. db.session.commit()
  92. db.session.commit()
  93. if __name__ == "__main__":
  94. manager.run()