From 4569c50bf316e655559342284991a25cd1dd39d8 Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Tue, 16 Mar 2021 15:35:04 +0100 Subject: [PATCH] add deactivate command --- .../commands/teleforma-deactivate-students.py | 69 +++++++++++++++++++ .../commands/teleforma-send-payment-emails.py | 4 +- 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 teleforma/management/commands/teleforma-deactivate-students.py diff --git a/teleforma/management/commands/teleforma-deactivate-students.py b/teleforma/management/commands/teleforma-deactivate-students.py new file mode 100644 index 00000000..3aca50eb --- /dev/null +++ b/teleforma/management/commands/teleforma-deactivate-students.py @@ -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 #################') diff --git a/teleforma/management/commands/teleforma-send-payment-emails.py b/teleforma/management/commands/teleforma-send-payment-emails.py index cc031e86..fbd91836 100644 --- a/teleforma/management/commands/teleforma-send-payment-emails.py +++ b/teleforma/management/commands/teleforma-send-payment-emails.py @@ -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) -- 2.39.5