create_sms_batch.py 1.1 KB

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