populate.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.utils.populate
  4. ~~~~~~~~~~~~~~~~~~~~
  5. A module that makes creating data more easily
  6. :copyright: (c) 2014 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from flaskbb.management.models import Setting, SettingsGroup
  10. from flaskbb.user.models import User, Group
  11. from flaskbb.forum.models import Post, Topic, Forum, Category
  12. def delete_settings_from_fixture(fixture):
  13. """Deletes the settings from a fixture from the database.
  14. Returns the deleted groups and settings.
  15. :param fixture: The fixture that should be deleted.
  16. """
  17. deleted_settings = {}
  18. for settingsgroup in fixture:
  19. group = SettingsGroup.query.filter_by(key=settingsgroup[0]).first()
  20. deleted_settings[group] = []
  21. for settings in settingsgroup[1]["settings"]:
  22. setting = Setting.query.filter_by(key=settings[0]).first()
  23. if setting:
  24. deleted_settings[group].append(setting)
  25. setting.delete()
  26. group.delete()
  27. return deleted_settings
  28. def create_settings_from_fixture(fixture):
  29. """Inserts the settings from a fixture into the database.
  30. Returns the created groups and settings.
  31. :param fixture: The fixture which should inserted.
  32. """
  33. created_settings = {}
  34. for settingsgroup in fixture:
  35. group = SettingsGroup(
  36. key=settingsgroup[0],
  37. name=settingsgroup[1]["name"],
  38. description=settingsgroup[1]["description"]
  39. )
  40. group.save()
  41. created_settings[group] = []
  42. for settings in settingsgroup[1]["settings"]:
  43. setting = Setting(
  44. key=settings[0],
  45. value=settings[1]["value"],
  46. value_type=settings[1]["value_type"],
  47. name=settings[1]["name"],
  48. description=settings[1]["description"],
  49. extra=settings[1].get("extra", ""), # Optional field
  50. settingsgroup=group.key
  51. )
  52. if setting:
  53. setting.save()
  54. created_settings[group].append(setting)
  55. return created_settings
  56. def update_settings_from_fixture(fixture, overwrite_group=False,
  57. overwrite_setting=False):
  58. """Updates the database settings from a fixture.
  59. Returns the updated groups and settings.
  60. :param fixture: The fixture which should be inserted/updated.
  61. :param overwrite_group: Set this to ``True`` if you want to overwrite
  62. the group if it already exists.
  63. Defaults to ``False``.
  64. :param overwrite_setting: Set this to ``True`` if you want to overwrite the
  65. setting if it already exists.
  66. Defaults to ``False``.
  67. """
  68. updated_settings = {}
  69. for settingsgroup in fixture:
  70. group = SettingsGroup.query.filter_by(key=settingsgroup[0]).first()
  71. if (group is not None and overwrite_group) or group is None:
  72. if group is not None:
  73. group.name = settingsgroup[1]["name"]
  74. group.description = settingsgroup[1]["description"]
  75. else:
  76. group = SettingsGroup(
  77. key=settingsgroup[0],
  78. name=settingsgroup[1]["name"],
  79. description=settingsgroup[1]["description"]
  80. )
  81. group.save()
  82. updated_settings[group] = []
  83. for settings in settingsgroup[1]["settings"]:
  84. setting = Setting.query.filter_by(key=settings[0]).first()
  85. if (setting is not None and overwrite_setting) or setting is None:
  86. if setting is not None:
  87. setting.value = settings[1]["value"]
  88. setting.value_type = settings[1]["value_type"]
  89. setting.name = settings[1]["name"]
  90. setting.description = settings[1]["description"]
  91. setting.extra = settings[1].get("extra", "")
  92. setting.settingsgroup = group.key
  93. else:
  94. setting = Setting(
  95. key=settings[0],
  96. value=settings[1]["value"],
  97. value_type=settings[1]["value_type"],
  98. name=settings[1]["name"],
  99. description=settings[1]["description"],
  100. extra=settings[1].get("extra", ""),
  101. settingsgroup=group.key
  102. )
  103. setting.save()
  104. updated_settings[group].append(setting)
  105. return updated_settings
  106. def create_default_settings():
  107. """Creates the default settings."""
  108. from flaskbb.fixtures.settings import fixture
  109. create_settings_from_fixture(fixture)
  110. def create_default_groups():
  111. """This will create the 5 default groups."""
  112. from flaskbb.fixtures.groups import fixture
  113. result = []
  114. for key, value in fixture.items():
  115. group = Group(name=key)
  116. for k, v in value.items():
  117. setattr(group, k, v)
  118. group.save()
  119. result.append(group)
  120. return result
  121. def create_admin_user(username, password, email):
  122. """Creates the administrator user.
  123. Returns the created admin user.
  124. :param username: The username of the user.
  125. :param password: The password of the user.
  126. :param email: The email address of the user.
  127. """
  128. admin_group = Group.query.filter_by(admin=True).first()
  129. user = User()
  130. user.username = username
  131. user.password = password
  132. user.email = email
  133. user.primary_group_id = admin_group.id
  134. user.save()
  135. return user
  136. def create_welcome_forum():
  137. """This will create the `welcome forum` with a welcome topic.
  138. Returns True if it's created successfully.
  139. """
  140. if User.query.count() < 1:
  141. return False
  142. user = User.query.filter_by(id=1).first()
  143. category = Category(title="My Category", position=1)
  144. category.save()
  145. forum = Forum(title="Welcome", description="Your first forum",
  146. category_id=category.id)
  147. forum.save()
  148. topic = Topic(title="Welcome!")
  149. post = Post(content="Have fun with your new FlaskBB Forum!")
  150. topic.save(user=user, forum=forum, post=post)
  151. return True
  152. def create_test_data(users=5, categories=2, forums=2, topics=1, posts=1):
  153. """Creates 5 users, 2 categories and 2 forums in each category.
  154. It also creates a new topic topic in each forum with a post.
  155. Returns the amount of created users, categories, forums, topics and posts
  156. as a dict.
  157. :param users: The number of users.
  158. :param categories: The number of categories.
  159. :param forums: The number of forums which are created in each category.
  160. :param topics: The number of topics which are created in each forum.
  161. :param posts: The number of posts which are created in each topic.
  162. """
  163. create_default_groups()
  164. create_default_settings()
  165. data_created = {'users': 0, 'categories': 0, 'forums': 0,
  166. 'topics': 0, 'posts': 0}
  167. # create 5 users
  168. for u in range(1, users + 1):
  169. username = "test%s" % u
  170. email = "test%s@example.org" % u
  171. user = User(username=username, password="test", email=email)
  172. user.primary_group_id = u
  173. user.save()
  174. data_created['users'] += 1
  175. user1 = User.query.filter_by(id=1).first()
  176. user2 = User.query.filter_by(id=2).first()
  177. # lets send them a few private messages
  178. for i in range(1, 3):
  179. # TODO
  180. pass
  181. # create 2 categories
  182. for i in range(1, categories + 1):
  183. category_title = "Test Category %s" % i
  184. category = Category(title=category_title,
  185. description="Test Description")
  186. category.save()
  187. data_created['categories'] += 1
  188. # create 2 forums in each category
  189. for j in range(1, forums + 1):
  190. if i == 2:
  191. j += 2
  192. forum_title = "Test Forum %s %s" % (j, i)
  193. forum = Forum(title=forum_title, description="Test Description",
  194. category_id=i)
  195. forum.save()
  196. data_created['forums'] += 1
  197. for t in range(1, topics + 1):
  198. # create a topic
  199. topic = Topic()
  200. post = Post()
  201. topic.title = "Test Title %s" % j
  202. post.content = "Test Content"
  203. topic.save(post=post, user=user1, forum=forum)
  204. data_created['topics'] += 1
  205. for p in range(1, posts + 1):
  206. # create a second post in the forum
  207. post = Post()
  208. post.content = "Test Post"
  209. post.save(user=user2, topic=topic)
  210. data_created['posts'] += 1
  211. return data_created
  212. def insert_mass_data(topics=100, posts=100):
  213. """Creates a few topics in the first forum and each topic has
  214. a few posts. WARNING: This might take very long!
  215. Returns the count of created topics and posts.
  216. :param topics: The amount of topics in the forum.
  217. :param posts: The number of posts in each topic.
  218. """
  219. user1 = User.query.filter_by(id=1).first()
  220. user2 = User.query.filter_by(id=2).first()
  221. forum = Forum.query.filter_by(id=1).first()
  222. created_posts = 0
  223. created_topics = 0
  224. if not (user1 or user2 or forum):
  225. return False
  226. # create 1000 topics
  227. for i in range(1, topics + 1):
  228. # create a topic
  229. topic = Topic()
  230. post = Post()
  231. topic.title = "Test Title %s" % i
  232. post.content = "Test Content"
  233. topic.save(post=post, user=user1, forum=forum)
  234. created_topics += 1
  235. # create 100 posts in each topic
  236. for j in range(1, posts + 1):
  237. post = Post()
  238. post.content = "Test Post"
  239. post.save(user=user2, topic=topic)
  240. created_posts += 1
  241. return created_topics, created_posts