manage.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 collections import OrderedDict
  13. from flask import current_app
  14. from flask.ext.script import Manager, Shell, Server
  15. from flaskbb import create_app
  16. from flaskbb.extensions import db
  17. from flaskbb.user.models import User, Group
  18. from flaskbb.forum.models import Post, Topic, Forum
  19. # Use the development configuration if available
  20. try:
  21. from flaskbb.configs.development import DevelopmentConfig as Config
  22. except ImportError:
  23. from flaskbb.configs.default import DefaultConfig as Config
  24. app = create_app(Config)
  25. manager = Manager(app)
  26. # Run local server
  27. manager.add_command("runserver", Server("localhost", port=8080))
  28. # Add interactive project shell
  29. def make_shell_context():
  30. return dict(app=current_app, db=db)
  31. manager.add_command("shell", Shell(make_context=make_shell_context))
  32. @manager.command
  33. def initdb():
  34. """
  35. Creates the database.
  36. """
  37. db.create_all()
  38. @manager.command
  39. def createall():
  40. """
  41. Creates the database with some example content.
  42. """
  43. # Just for testing purposes
  44. dbfile = os.path.join(Config._basedir, "flaskbb.sqlite")
  45. if os.path.exists(dbfile):
  46. os.remove(dbfile)
  47. db.create_all()
  48. groups = OrderedDict((
  49. ('Administrator', {
  50. 'description': 'The Administrator Group',
  51. 'admin': True,
  52. 'super_mod': False,
  53. 'mod': False,
  54. 'banned': False,
  55. 'guest': False,
  56. 'editpost': True,
  57. 'deletepost': True,
  58. 'deletetopic': True,
  59. 'posttopic': True,
  60. 'postreply': True,
  61. 'viewtopic': True,
  62. 'viewprofile': True
  63. }),
  64. ('Super Moderator', {
  65. 'description': 'The Super Moderator Group',
  66. 'admin': False,
  67. 'super_mod': True,
  68. 'mod': False,
  69. 'banned': False,
  70. 'guest': False,
  71. 'editpost': True,
  72. 'deletepost': True,
  73. 'deletetopic': True,
  74. 'posttopic': True,
  75. 'postreply': True,
  76. 'viewtopic': True,
  77. 'viewprofiles': True
  78. }),
  79. ('Moderator', {
  80. 'description': 'The Moderator Group',
  81. 'admin': False,
  82. 'super_mod': False,
  83. 'mod': True,
  84. 'banned': False,
  85. 'guest': False,
  86. 'editpost': True,
  87. 'deletepost': True,
  88. 'deletetopic': True,
  89. 'posttopic': True,
  90. 'postreply': True,
  91. 'viewtopic': True,
  92. 'viewprofile': True
  93. }),
  94. ('Member', {
  95. 'description': 'The Member Group',
  96. 'admin': False,
  97. 'super_mod': False,
  98. 'mod': False,
  99. 'banned': False,
  100. 'guest': False,
  101. 'editpost': True,
  102. 'deletepost': False,
  103. 'deletetopic': False,
  104. 'posttopic': True,
  105. 'postreply': True,
  106. 'viewtopic': True,
  107. 'viewprofile': True
  108. }),
  109. ('Banned', {
  110. 'description': 'The Banned Group',
  111. 'admin': False,
  112. 'super_mod': False,
  113. 'mod': False,
  114. 'banned': True,
  115. 'guest': False,
  116. 'editpost': False,
  117. 'deletepost': False,
  118. 'deletetopic': False,
  119. 'posttopic': False,
  120. 'postreply': False,
  121. 'viewtopic': False,
  122. 'viewprofile': False
  123. }),
  124. ('Guest', {
  125. 'description': 'The Guest Group',
  126. 'admin': False,
  127. 'super_mod': False,
  128. 'mod': False,
  129. 'banned': False,
  130. 'guest': True,
  131. 'editpost': False,
  132. 'deletepost': False,
  133. 'deletetopic': False,
  134. 'posttopic': False,
  135. 'postreply': False,
  136. 'viewtopic': False,
  137. 'viewprofile': False
  138. })
  139. ))
  140. # create 5 groups
  141. for key, value in groups.items():
  142. group = Group(name=key)
  143. for k, v in value.items():
  144. setattr(group, k, v)
  145. db.session.add(group)
  146. db.session.commit()
  147. # create 5 users
  148. groups = Group.query.all()
  149. for u in range(1, 6):
  150. username = "test%s" % u
  151. email = "test%s@example.org" % u
  152. user = User(username=username, password="test", email=email)
  153. user.secondary_groups.append(groups[u-1])
  154. user.primary_group_id = u
  155. db.session.add(user)
  156. db.session.commit()
  157. # create 2 categories
  158. for i in range(1, 3):
  159. category_title = "Test Category %s" % i
  160. category = Forum(is_category=True, title=category_title,
  161. description="Test Description")
  162. db.session.add(category)
  163. # create 2 forums in each category
  164. for j in range(1, 3):
  165. if i == 2:
  166. j += 2
  167. forum_title = "Test Forum %s %s" % (j, i)
  168. forum = Forum(title=forum_title, description="Test Description",
  169. parent_id=i)
  170. db.session.add(forum)
  171. db.session.commit()
  172. # create 1 topic in each forum
  173. for k in [2, 3, 5, 6]: # Forum ids are not sequential because categories.
  174. topic = Topic()
  175. first_post = Post()
  176. topic.title = "Test Title %s" % k
  177. topic.user_id = 1
  178. topic.forum_id = k
  179. db.session.add(topic)
  180. db.session.commit()
  181. first_post.content = "Test Content"
  182. first_post.user_id = 1
  183. first_post.topic_id = topic.id
  184. db.session.add(first_post)
  185. db.session.commit()
  186. # Invalidate relevant caches
  187. topic.invalidate_cache()
  188. topic.forum.invalidate_cache()
  189. # create 2 additional posts for each topic
  190. for m in range(1, 3):
  191. post = Post(content="Test Post", user_id=2, topic_id=k)
  192. db.session.add(post)
  193. db.session.commit()
  194. # Update the post count
  195. post.user.invalidate_cache()
  196. topic.invalidate_cache()
  197. topic.forum.invalidate_cache()
  198. db.session.commit()
  199. db.session.commit()
  200. if __name__ == "__main__":
  201. manager.run()