create_sms_batch.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """ """
  2. from sqlalchemy import select
  3. from datetime import datetime, timedelta
  4. from app.core import config
  5. from app.core.session import session
  6. from app.models import Sms, Project
  7. TEST_SMS_PROJECT_NAME = "test_project pour sms"
  8. NUMBER_OF_SMS = 80
  9. def main() -> None:
  10. print("Create SMS ")
  11. with session() as db:
  12. # Get or create the project hosting the sms
  13. result = db.execute(select(Project).where(Project.name == TEST_SMS_PROJECT_NAME))
  14. project = result.scalars().first()
  15. if project is None:
  16. project = Project(name=TEST_SMS_PROJECT_NAME, is_public=False)
  17. db.add(project)
  18. db.commit()
  19. db.refresh(project)
  20. now = datetime.now()
  21. for t in range(NUMBER_OF_SMS):
  22. sending_time = now + timedelta(minutes=t * 45)
  23. sms = Sms(
  24. project_id=project.id,
  25. content=sending_time.strftime("%m/%d/%Y, %H:%M:%S"),
  26. phone_number=config.settings.BATCH_SMS_PHONE_NUMBER,
  27. sending_time=sending_time,
  28. )
  29. db.add(sms)
  30. db.commit()
  31. if __name__ == "__main__":
  32. main()