Просмотр исходного кода

Refactored the populate code a bit

sh4nks 11 лет назад
Родитель
Сommit
8b0cd7dcb7
2 измененных файлов с 48 добавлено и 41 удалено
  1. 27 39
      flaskbb/utils/populate.py
  2. 21 2
      manage.py

+ 27 - 39
flaskbb/utils/populate.py

@@ -166,47 +166,35 @@ def create_test_data():
         email = "test%s@example.org" % u
         user = User(username=username, password="test", email=email)
         user.primary_group_id = u
-        db.session.add(user)
-        db.session.commit()
+        user.save()
 
-    # create 2 categories
+    # create a category
+    category = Forum(is_category=True, title="Test Category",
+                     description="Test Description")
+    category.save()
+
+    # create 2 forums in the category
     for i in range(1, 3):
-        category_title = "Test Category %s" % i
-        category = Forum(is_category=True, title=category_title,
-                         description="Test Description")
-        db.session.add(category)
-
-        # create 2 forums in each category
-        for j in range(1, 3):
-            if i == 2:
-                j += 2
-
-            forum_title = "Test Forum %s %s" % (j, i)
-            forum = Forum(title=forum_title, description="Test Description",
-                          parent_id=i)
-            db.session.add(forum)
-        db.session.commit()
+        forum_title = "Test Forum %s " % i
+        forum = Forum(title=forum_title, description="Test Description",
+                      parent_id=1)
+        forum.save()
+
+        # Create a subforum
+        subforum_title = "Test Subforum %s " % i
+        subforum = Forum(title=subforum_title,
+                         description="Test Description", parent_id=forum.id)
+        subforum.save()
+
+    user = User.query.filter_by(id=1).first()
 
     # create 1 topic in each forum
-    for k in [2, 3, 5, 6]:  # Forum ids are not sequential because categories.
+    for i in range(2, 6):  # Forum ids are not sequential because categories.
+        forum = Forum.query.filter_by(id=i).first()
+
         topic = Topic()
-        topic.first_post = Post()
-
-        topic.title = "Test Title %s" % k
-        topic.user_id = 1
-        topic.forum_id = k
-        topic.first_post.content = "Test Content"
-        topic.first_post.user_id = 1
-        topic.first_post.topic_id = topic.id
-
-        db.session.add(topic)
-        db.session.commit()
-
-        # Update the post and topic count
-        topic.forum.topic_count += 1
-        topic.forum.post_count += 1
-        topic.post_count += 1
-        topic.first_post.user.post_count += 1
-        db.session.commit()
-
-    db.session.commit()
+        post = Post()
+
+        topic.title = "Test Title %s" % (i-1)
+        post.content = "Test Content"
+        topic.save(user=user, forum=forum, post=post)

+ 21 - 2
manage.py

@@ -17,7 +17,8 @@ from flask.ext.script import Manager, Shell, Server
 
 from flaskbb import create_app
 from flaskbb.extensions import db
-from flaskbb.utils import create_test_data
+from flaskbb.utils.populate import (create_test_data, create_admin_user,
+                                    create_welcome_forum, create_default_groups)
 
 # Use the development configuration if available
 try:
@@ -59,9 +60,27 @@ def createall():
         os.remove(dbfile)
 
     db.create_all()
-
     create_test_data()
 
 
+@manager.command
+def create_admin():
+    """
+    Creates the admin user
+    """
+    db.create_all()
+    create_admin_user()
+
+
+@manager.command
+def create_default_data():
+    """
+    This should be created by every flaskbb installation
+    """
+    db.create_all()
+    create_default_groups()
+    create_welcome_forum()
+
+
 if __name__ == "__main__":
     manager.run()