Spaces:
Runtime error
Runtime error
Greg Claude Opus 4.7 (1M context) commited on
Commit ·
95bfceb
1
Parent(s): 5b2025a
feat(scripts): backfill users.sex for is_demo accounts
Browse filesCo-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
backend/scripts/__init__.py
ADDED
|
File without changes
|
backend/scripts/backfill_sex_demo.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""One-off: populate users.sex for users in is_demo accounts whose sex IS NULL.
|
| 2 |
+
|
| 3 |
+
Idempotent: re-running does nothing once everyone has sex. Skips non-demo accounts.
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import random
|
| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 10 |
+
|
| 11 |
+
from app.core.database import SessionLocal
|
| 12 |
+
from app.models.account import Account
|
| 13 |
+
from app.models.user import User
|
| 14 |
+
from app.utils.sex_counts import sex_for
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def main() -> int:
|
| 18 |
+
rng = random.Random(2026)
|
| 19 |
+
db = SessionLocal()
|
| 20 |
+
try:
|
| 21 |
+
demo_ids = [a.id for a in db.query(Account).filter(Account.is_demo.is_(True)).all()]
|
| 22 |
+
if not demo_ids:
|
| 23 |
+
print("No hay cuentas is_demo. Nada que backfillear.")
|
| 24 |
+
return 0
|
| 25 |
+
total = 0
|
| 26 |
+
for aid in demo_ids:
|
| 27 |
+
users = db.query(User).filter(
|
| 28 |
+
User.account_id == aid,
|
| 29 |
+
(User.sex.is_(None)) | (User.sex == ""),
|
| 30 |
+
).all()
|
| 31 |
+
for u in users:
|
| 32 |
+
u.sex = sex_for(u.name or "", rng)
|
| 33 |
+
db.commit()
|
| 34 |
+
print(f" account_id={aid}: {len(users)} usuarios actualizados.")
|
| 35 |
+
total += len(users)
|
| 36 |
+
print(f"Total: {total} usuarios.")
|
| 37 |
+
return 0
|
| 38 |
+
finally:
|
| 39 |
+
db.close()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
sys.exit(main())
|