Browse Source

feat: 构建假数据

SongZihuan 2 years ago
parent
commit
10b3011cc0
2 changed files with 51 additions and 0 deletions
  1. 48 0
      app/db.py
  2. 3 0
      requirements.txt

+ 48 - 0
app/db.py

@@ -163,3 +163,51 @@ def create_all():
 
     db.session.add_all([admin, coordinator, default])
     db.session.commit()
+
+
+def create_faker_user():
+    from faker import Faker
+    from sqlalchemy.exc import IntegrityError
+    fake = Faker("zh_CN")
+
+    count_user = 0
+    while count_user < 100:
+        user = User(email=fake.email(), passwd_hash=User.get_passwd_hash("passwd"), role_id=3)
+        db.session.add(user)
+
+        try:
+            db.session.commit()
+        except IntegrityError:
+            db.session.rollback()
+        else:
+            count_user += 1
+
+
+def create_faker_comment(auth_max=100):
+    from random import randint, random
+    from faker import Faker
+    from sqlalchemy.exc import IntegrityError
+    fake = Faker("zh_CN")
+
+    count_comment = 0
+    while count_comment < 100:
+        title = None
+        if random() < 0.5:
+            title = "加人" + fake.company()
+
+        father = None
+        if count_comment > 10 and random() < 0.5:
+            father = randint(1, count_comment)
+
+        time = fake.past_datetime()
+
+        comment = Comment(title=title, content=fake.text(), update_time=time, create_time=time, father_id=father,
+                          auth_id=randint(1, auth_max))
+        db.session.add(comment)
+
+        try:
+            db.session.commit()
+        except IntegrityError:
+            db.session.rollback()
+        else:
+            count_comment += 1

+ 3 - 0
requirements.txt

@@ -4,6 +4,7 @@ cffi==1.15.1
 click==8.1.3
 colorama==0.4.5
 cryptography==38.0.1
+Faker==15.1.1
 Flask==2.2.2
 Flask-Login==0.6.2
 Flask-Mail==0.9.1
@@ -20,6 +21,8 @@ packaging==21.3
 pycparser==2.21
 PyMySQL==1.0.2
 pyparsing==3.0.9
+python-dateutil==2.8.2
+six==1.16.0
 SQLAlchemy==1.4.41
 Werkzeug==2.2.2
 WTForms==3.0.1