From: Gael Le Mignot Date: Fri, 17 Jun 2022 08:39:38 +0000 (+0200) Subject: Logging to external file provided in argument X-Git-Tag: 2.8.1-pro~113^2^2~1 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=74d73b3bcbc3619279b03a3274295b39f9b65a9d;p=teleforma.git Logging to external file provided in argument --- diff --git a/teleforma/management/commands/teleforma-message-all-seminar-users.py b/teleforma/management/commands/teleforma-message-all-seminar-users.py index 546b124b..ef83d227 100644 --- a/teleforma/management/commands/teleforma-message-all-seminar-users.py +++ b/teleforma/management/commands/teleforma-message-all-seminar-users.py @@ -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() diff --git a/teleforma/management/commands/teleforma-revisions-from-bbb.py b/teleforma/management/commands/teleforma-revisions-from-bbb.py index bf546b17..a1e2d5e8 100644 --- a/teleforma/management/commands/teleforma-revisions-from-bbb.py +++ b/teleforma/management/commands/teleforma-revisions-from-bbb.py @@ -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 " % (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()