]> git.parisson.com Git - teleforma.git/commitdiff
Logging to external file provided in argument
authorGael Le Mignot <gael@pilotsystems.net>
Fri, 17 Jun 2022 08:39:38 +0000 (10:39 +0200)
committerGael Le Mignot <gael@pilotsystems.net>
Fri, 17 Jun 2022 08:39:38 +0000 (10:39 +0200)
teleforma/management/commands/teleforma-message-all-seminar-users.py
teleforma/management/commands/teleforma-revisions-from-bbb.py

index 546b124bbd9b7aa4acad54d748539097c3081048..ef83d227cec7f2dd5984395652fe9fe17bdb1ea9 100644 (file)
@@ -11,24 +11,39 @@ from teleforma.views.pro import get_seminar_delta
 import datetime
 from postman.utils import notify_user
 from teleforma.context_processors import seminar_validated
-
+import sys
 
 class Command(BaseCommand):
     help = """Send a reminder to all users when their subscripted seminars 
             expire before a given number of days"""
-    args = ['days',]
     message_template = 'teleforma/messages/seminar_remind.txt'
     subject_template = 'teleforma/messages/seminar_remind_subject.txt'
     language_code = 'fr_FR'
 
+    def add_arguments(self, parser):
+        parser.add_argument('days', type=int,
+                            help='number of days')
+        parser.add_argument('logfile', type=str, 
+                            help='log file to use')
+
+    def log(self, msg):
+        today = datetime.datetime.now()
+        print(today.strftime('%Y-%m-%d %H:%M'), msg)
+        
     def handle(self, *args, **kwargs):
-        days = int(args[-1])
+        days = kwargs['days']
+        logpath = kwargs['logfile']
+        sys.stdout = open(logpath, 'a', encoding='utf-8')
+
+        self.log("Starting run")
+        
         users = User.objects.all()
         translation.activate(self.language_code)
         sender_email = settings.DEFAULT_FROM_EMAIL
         sender = User.objects.get(email=sender_email)
         today = datetime.datetime.now()
 
+        
         for user in users:
             auditor = user.auditor.all()
             professor = user.professor.all()
@@ -47,6 +62,7 @@ class Command(BaseCommand):
                         enough_time = get_seminar_delta(user, seminar)
                         validated = completed and enough_time > 0
                         if delta.days < days and delta.days >= 0 and not validated:
+                            self.log("Seminar %s is about to expire" % seminar)
                             seminars.append(seminar)
 
 
@@ -62,6 +78,8 @@ class Command(BaseCommand):
                     subject = render_to_string(self.subject_template, context)
                     # subject = '%s : %s' % (seminar.title, subject)
                     mess = Message(sender=sender, recipient=user, subject=subject[:119], body=text)
+                    self.log("Sending mail to %s about %d seminars" % (user, len(seminars)))
+                    
                     # mess = Message(sender=sender, recipient='yoanl@pilotsystems.net', subject=subject[:119], body=text)
                     mess.moderation_status = 'a'
                     mess.save()
index bf546b17becffdabcafaf5f5897fe84cb7f20752..a1e2d5e8e9028314519c1b609ba131d06de316ee 100644 (file)
@@ -17,15 +17,19 @@ def as_list(what):
 
 class Command(BaseCommand):
     help = "Credit time to students connected to a Big Blue Button webclass"
-    args = "duration"
 
+    def add_arguments(self, parser):
+        parser.add_argument('duration', type=int,
+                            help='duration in seconds')
+        parser.add_argument('logfile', type=str, 
+                            help='log file to use')
+        
+    
     def handle(self, *args, **options):
-        if len(args) != 1 or not args[0].isdigit():            
-            print(("Syntax: %s %s <duration in seconds>" % (sys.argv[0],
-                                                           sys.argv[1])))
-            sys.exit(1)
+        duration = options['duration']
+        logpath = options['logfile']
+        sys.stdout = open(logpath, 'a', encoding='utf-8')
 
-        duration = int(args[0])
         end = datetime.datetime.now()
         start = end - datetime.timedelta(seconds = duration)
         print(("=== Starting at %s" % end.strftime('%Y-%m-%d %H:%M:%S')))
@@ -37,7 +41,7 @@ class Command(BaseCommand):
             return
         meetings = as_list(meetings["meeting"])
 
-        print(("=== Starting at %s" % end.strftime('%Y-%m-%d %H:%M:%S')))
+        print(("=== Starting at %s (with duration %d)" % (end.strftime('%Y-%m-%d %H:%M:%S'), duration)))
 
         done = set()