]> git.parisson.com Git - teleforma.git/commitdiff
add deactivate command
authorGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Tue, 16 Mar 2021 14:35:04 +0000 (15:35 +0100)
committerGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Tue, 16 Mar 2021 14:35:04 +0000 (15:35 +0100)
teleforma/management/commands/teleforma-deactivate-students.py [new file with mode: 0644]
teleforma/management/commands/teleforma-send-payment-emails.py

diff --git a/teleforma/management/commands/teleforma-deactivate-students.py b/teleforma/management/commands/teleforma-deactivate-students.py
new file mode 100644 (file)
index 0000000..3aca50e
--- /dev/null
@@ -0,0 +1,69 @@
+from optparse import make_option
+from django.conf import settings
+from django.core.management.base import BaseCommand, CommandError
+from django.contrib.auth.models import User
+from django.contrib.auth.forms import PasswordResetForm
+from django.contrib.auth.tokens import default_token_generator
+from django.template.defaultfilters import slugify
+from django.template.loader import render_to_string
+from django.core.mail import send_mail, mail_admins
+from django.utils import translation
+from telemeta.models import *
+from telemeta.util.unaccent import unaccent
+from teleforma.models import *
+import logging
+from postman import *
+
+
+class Logger:
+    """A logging object"""
+
+    def __init__(self, file):
+        self.logger = logging.getLogger('teleforma')
+        self.hdlr = logging.FileHandler(file)
+        self.formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
+        self.hdlr.setFormatter(self.formatter)
+        self.logger.addHandler(self.hdlr)
+        self.logger.setLevel(logging.INFO)
+
+
+class Command(BaseCommand):
+    help = "Deactivate student user and send an email"
+    language_code = 'fr_FR'
+
+    def email(self, student):
+        site = Site.objects.get_current()
+        if student.platform_only:
+            mode = 'E-learning'
+            message = student.period.message_platform
+        else:
+            mode = 'Presentielle'
+            message = student.period.message_local
+
+        ctx_dict = {'site': site, 'organization': settings.TELEMETA_ORGANIZATION, 'student': student, 'mode': mode}
+        subject_template = 'teleforma/messages/email_deactivate_subject.txt'
+        subject = render_to_string(subject_template, ctx_dict)
+        subject = ''.join(subject.splitlines())
+        message_template = 'teleforma/messages/email_deactivate_body.txt'
+        message = render_to_string(message_template, ctx_dict)
+        send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [student.user.email], fail_silently=False)
+
+    def handle(self, *args, **options):
+        log_file = args[-1]
+        period_name = args[-2]
+
+        logger = Logger(log_file)
+        logger.logger.info('########### Processing #############')
+
+        period = Period.objects.get(name=period_name)
+        students = Student.objects.filter(period=period)
+        translation.activate(self.language_code)
+
+        for student in students:
+            if student.is_subscribed and student.confirmation_sent and student.user.email and student.user.is_active:
+                student.user.is_active = False
+                student.user.save()
+                self.email(student)
+                logger.logger.info('deactivated and email sent : ' + student.user.username)
+
+        logger.logger.info('############## Done #################')
index cc031e86240bf4910a5d5f5e8e25da0f9117a1af..fbd918369f35d863a4f08285bad78bb6d7213c58 100644 (file)
@@ -47,9 +47,9 @@ class Command(BaseCommand):
         translation.activate(self.language_code)
 
         today = datetime.date.today()
-        
+
         for student in students:
-            if student.is_subscribed and student.user.email and student.period == period:
+            if student.is_subscribed and student.user.email and student.period == period and student.user.is_active :
                 for payment in student.payments.filter(type = 'online').exclude(online_paid = True).filter(scheduled__lte = today):
                     if payment.scheduled == today:
                         self.email(student, 'initial', payment)