Browse Source

Merge branch 'master' of http://gitlab.jaquin.fr/clovis/bdlg2023-sms-back

tripeur 1 year ago
parent
commit
e0590d5339
3 changed files with 36 additions and 5 deletions
  1. 2 2
      app/create_sms_batch.py
  2. 30 0
      app/restore_project.py
  3. 4 3
      script/send_sms.py

+ 2 - 2
app/create_sms_batch.py

@@ -9,7 +9,7 @@ from app.core.session import session
 from app.models import User, Sms, Project
 
 TEST_SMS_PROJECT_NAME = "test_project pour sms"
-NUMBER_OF_SMS = 100
+NUMBER_OF_SMS = 80
 
 
 def main() -> None:
@@ -27,7 +27,7 @@ def main() -> None:
         now = datetime.now()
 
         for t in range(NUMBER_OF_SMS):
-            sending_time = now + timedelta(minutes=t * 5)
+            sending_time = now + timedelta(minutes=t * 45)
             sms = Sms(
                 project_id=project.id,
                 content=sending_time.strftime("%m/%d/%Y, %H:%M:%S"),

+ 30 - 0
app/restore_project.py

@@ -0,0 +1,30 @@
+"""
+
+"""
+
+from sqlalchemy import select
+from datetime import datetime, timedelta
+from app.core import config, security
+from app.core.session import session
+from app.models import User, Sms, Project
+
+TEST_SMS_PROJECT_NAME = "522e62c3-9620-47e1-bdd6-d856095533e7"
+NUMBER_OF_SMS = 80
+
+
+def main() -> None:
+    print("Create SMS ")
+    with session() as db:
+        # Get or create the project hosting the sms
+        result = db.execute(select(Project).where(Project.id == TEST_SMS_PROJECT_NAME))
+        project = result.scalars().first()
+        if project is None:
+            project = Project(id=TEST_SMS_PROJECT_NAME, name="BDLG 2023 V0", is_public=False)
+            db.add(project)
+            db.commit()
+            db.refresh(project)
+        db.commit()
+
+
+if __name__ == "__main__":
+    main()

+ 4 - 3
script/send_sms.py

@@ -24,13 +24,14 @@ def send_sms_from_api(url: str, login: str, pwd: str, device_name: str):
     # Init KDE Connect
     kde = KDEConnect(device_name=device_name)
 
-    # send sms & update api
+    # send sms & update database
     for sms in sms_list:
         try:
             kde.send_sms(sms.phone_number, sms.content)
             requests.post(url + "/sms/send-now/" + sms.id, headers=headers)
         except Exception as exc:
             logging.warning(f"Echec lors de l'envoie du sms {sms.id}\n{str(exc)}")
+        time.sleep(2)
 
 
 class InvalidEnvironnementVariable(Exception):
@@ -56,7 +57,7 @@ def main():
 if __name__ == "__main__":
     logging.basicConfig(
         format="%(asctime)s %(levelname)s - %(message)s",
-        level=logging.DEBUG,
+        level=logging.INFO,
         filemode="a",
         filename="log.txt",
     )
@@ -67,4 +68,4 @@ if __name__ == "__main__":
         except Exception as e:
             logging.exception("An error as occured : ")
         elapsed_time = time.time() - starting_time
-        time.sleep(300 - elapsed_time)
+        time.sleep(180 - elapsed_time)