Добавлено заполнение базы данных новой моделью Company. Обновлены функции для создания аккаунтов и TgAgents с учетом связи с компанией. Изменены комментарии для соответствия новому порядку заполнения данных.

This commit is contained in:
Redsandyg 2025-06-03 14:03:56 +03:00
parent 1f11bd8012
commit f494f75020

View File

@ -1,7 +1,7 @@
import random import random
from uuid import uuid4 from uuid import uuid4
from sqlmodel import Session from sqlmodel import Session
from main import AUTH_DB_ENGINE, TgAgent, Ref, Sale, Transaction, Account from main import AUTH_DB_ENGINE, TgAgent, Ref, Sale, Transaction, Account, Company
from sqlalchemy import text from sqlalchemy import text
from datetime import datetime, timedelta from datetime import datetime, timedelta
from hashlib import sha256 from hashlib import sha256
@ -80,23 +80,34 @@ def fill_db():
session.execute(text("DELETE FROM ref")) session.execute(text("DELETE FROM ref"))
session.execute(text("DELETE FROM tgagent")) session.execute(text("DELETE FROM tgagent"))
session.execute(text("DELETE FROM account")) session.execute(text("DELETE FROM account"))
session.execute(text('DELETE FROM "transaction"'))
session.execute(text("DELETE FROM company"))
session.commit() session.commit()
# 0. Accounts # 0. Company
company = Company(
name="RE: Premium",
commission=10.0,
key="re-premium-key",
)
session.add(company)
session.commit()
session.refresh(company)
# 1. Accounts
accounts = [] accounts = []
for i in range(4): for i in range(4):
acc = Account( acc = Account(
login=f"user{i+1}", login=f"user{i+1}",
password_hash=get_password_hash("password123"), # теперь храним хеш password_hash=get_password_hash("password123"),
name=NAMES[i % len(NAMES)], name=NAMES[i % len(NAMES)],
email=f"user{i+1}@example.com", email=f"user{i+1}@example.com",
balance=round(random.uniform(1000, 10000), 2) company_id=company.id
) )
session.add(acc) session.add(acc)
accounts.append(acc) accounts.append(acc)
session.commit() session.commit()
for acc in accounts: for acc in accounts:
session.refresh(acc) session.refresh(acc)
# 1. TgAgents # 2. TgAgents
tg_agents = [] tg_agents = []
for i, tg_agent_id in enumerate(USER_IDS): for i, tg_agent_id in enumerate(USER_IDS):
dt = random.choice(date_list) dt = random.choice(date_list)
@ -107,6 +118,7 @@ def fill_db():
phone=PHONES[i % len(PHONES)], phone=PHONES[i % len(PHONES)],
name=NAMES[i % len(NAMES)], name=NAMES[i % len(NAMES)],
login=LOGINS[i % len(LOGINS)], login=LOGINS[i % len(LOGINS)],
company_id=company.id,
create_dttm=dt, create_dttm=dt,
update_dttm=dt, update_dttm=dt,
hash=hash_value hash=hash_value
@ -116,8 +128,7 @@ def fill_db():
session.commit() session.commit()
for tg_agent in tg_agents: for tg_agent in tg_agents:
session.refresh(tg_agent) session.refresh(tg_agent)
# 3. Refs (минимум 22 на агента)
# 2. Refs (минимум 22 на агента)
refs = [] refs = []
desc_count = len(ALL_DESCRIPTIONS) desc_count = len(ALL_DESCRIPTIONS)
for tg_agent in tg_agents: for tg_agent in tg_agents:
@ -138,8 +149,7 @@ def fill_db():
session.commit() session.commit()
for ref in refs: for ref in refs:
session.refresh(ref) session.refresh(ref)
# 4. Sales (минимум 20 на каждый ref)
# 3. Sales (минимум 20 на каждый ref)
for ref in refs: for ref in refs:
sale_count = random.randint(20, int(20 * 1.25)) # от 20 до 25 sale_count = random.randint(20, int(20 * 1.25)) # от 20 до 25
for _ in range(sale_count): for _ in range(sale_count):
@ -151,13 +161,13 @@ def fill_db():
crediting=crediting, crediting=crediting,
ref=ref.id, ref=ref.id,
sale_id=str(uuid4()), sale_id=str(uuid4()),
company_id=company.id,
create_dttm=dt, create_dttm=dt,
update_dttm=dt update_dttm=dt
) )
session.add(sale) session.add(sale)
session.commit() session.commit()
# 5. Transactions (только withdrawal на агента)
# 4. Transactions (только withdrawal на агента)
TRANSACTION_STATUSES = ['process', 'done', 'error', 'waiting'] TRANSACTION_STATUSES = ['process', 'done', 'error', 'waiting']
for tg_agent in tg_agents: for tg_agent in tg_agents:
withdrawal_count = random.randint(5, int(5 * 1.25)) # от 5 до 6 withdrawal_count = random.randint(5, int(5 * 1.25)) # от 5 до 6
@ -175,6 +185,7 @@ def fill_db():
sum=round(random.uniform(200, 3000), 2), sum=round(random.uniform(200, 3000), 2),
tg_agent_id=tg_agent.id, tg_agent_id=tg_agent.id,
status=status, status=status,
company_id=company.id,
create_dttm=dt, create_dttm=dt,
update_dttm=dt update_dttm=dt
) )