74 lines
1.7 KiB
Python
74 lines
1.7 KiB
Python
import random
|
|
import string
|
|
import time
|
|
import uuid
|
|
from typing import Type
|
|
|
|
from tortoise import Tortoise, run_async, Model
|
|
|
|
from service.config import TORTOISE_ORM
|
|
from service.database.models import User
|
|
|
|
CHARS = string.ascii_letters + string.digits
|
|
user_count = 205
|
|
journal_count = 27
|
|
form_count = 2
|
|
form_input_count = 5
|
|
option_count = 8
|
|
|
|
|
|
def rand_str(k: int = 10) -> str:
|
|
return "".join(random.choices(CHARS, k=k))
|
|
|
|
|
|
async def get_model_next_id(model: Type[Model]) -> int:
|
|
max_uid = await model.all().order_by("-id").first()
|
|
if max_uid is None:
|
|
max_uid = 0
|
|
else:
|
|
max_uid = max_uid.id
|
|
return max_uid + 1
|
|
|
|
|
|
async def do_import():
|
|
await Tortoise.init(TORTOISE_ORM)
|
|
_p = "$2b$12$3k.eYVcZxKRbSpRaz/R5luVxI0QI.CRiANGE8LINDGU6El9jYQxgC"
|
|
usernames = [uuid.uuid4().hex for _ in range(user_count)]
|
|
u_id = await get_model_next_id(User)
|
|
users = [
|
|
User(
|
|
id=u_id + i,
|
|
username=un,
|
|
email=f"{un}@{{ cookiecutter.project_slug }}.com",
|
|
password=_p,
|
|
)
|
|
for i, un in enumerate(usernames)
|
|
]
|
|
ts = time.time()
|
|
await User.bulk_create(users)
|
|
print(f"User: {len(users)} rows in {time.time() - ts:.5f}s)")
|
|
|
|
uc = len(users)
|
|
print(
|
|
"Data generation finished!\n"
|
|
f"Total of {sum((uc, ))} objects created!\n"
|
|
f"Generated {uc} Users"
|
|
)
|
|
toc = await User.all().count()
|
|
print(
|
|
"Total elements in tables:\n"
|
|
f"Users: {toc}\n"
|
|
f"Total: {toc}"
|
|
)
|
|
|
|
|
|
def main():
|
|
start_ts = time.time()
|
|
run_async(do_import())
|
|
end_ts = time.time()
|
|
print(f"Data generated in {end_ts - start_ts:.3f}s")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|