]> git.parisson.com Git - teleforma.git/commitdiff
add account init test commands
authorGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Thu, 23 Nov 2017 08:44:21 +0000 (09:44 +0100)
committerGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Thu, 23 Nov 2017 08:44:21 +0000 (09:44 +0100)
teleforma/management/commands/teleforma-reset-all-passwords-with-mail-test.py [new file with mode: 0644]
teleforma/management/commands/teleforma-send-subscription-email-test.py [new file with mode: 0644]
teleforma/management/commands/teleforma-students-activate-test.py [new file with mode: 0644]
teleforma/management/commands/teleforma-students-activate.py

diff --git a/teleforma/management/commands/teleforma-reset-all-passwords-with-mail-test.py b/teleforma/management/commands/teleforma-reset-all-passwords-with-mail-test.py
new file mode 100644 (file)
index 0000000..ea04c39
--- /dev/null
@@ -0,0 +1,72 @@
+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 = "Reset the password for all (active) users "
+    message_template = 'postman/email_user_init.txt'
+    subject_template = 'postman/email_user_subject_init.txt'
+    language_code = 'fr_FR'
+    username = test
+
+    def init_password_email(self, user):
+        site = Site.objects.get_current()
+        ctx_dict = {'site': site, 'organization': settings.TELEMETA_ORGANIZATION, 'usr': user}
+        subject = render_to_string(self.subject_template, ctx_dict)
+        subject = ''.join(subject.splitlines())
+        message = render_to_string(self.message_template, ctx_dict)
+        send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [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)
+        users = User.objects.filter(username=self.username)
+        translation.activate(self.language_code)
+
+        for user in users:
+            profile, c = Profile.objects.get_or_create(user=user)
+            students = user.student.all()
+            if students:
+                student = students[0]
+            else:
+                student = Student()
+            professors = user.professor.all()
+            quotas = user.quotas.all()
+            if student.period == period or professors or quotas:
+                if profile and user.is_active:
+                    if not profile.init_password and user.email:
+                        self.init_password_email(user)
+                        profile.init_password = True
+                        profile.save()
+                        logger.logger.info('init : ' + user.username)
+
+        logger.logger.info('############## Done #################')
diff --git a/teleforma/management/commands/teleforma-send-subscription-email-test.py b/teleforma/management/commands/teleforma-send-subscription-email-test.py
new file mode 100644 (file)
index 0000000..a751e60
--- /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 = "Send an email to new subscribed student"
+    language_code = 'fr_FR'
+    username = 'test'
+
+    def email(self, student):
+        site = Site.objects.get_current()
+        if student.platform_only:
+            mode = 'E-learning'
+        else:
+            mode = 'Presentielle'
+        ctx_dict = {'site': site, 'organization': settings.TELEMETA_ORGANIZATION, 'student': student, 'mode': mode}
+        subject_template = 'teleforma/messages/email_inscr_sujet.txt'
+        message_template = 'teleforma/messages/email_inscription.txt'
+        subject = render_to_string(subject_template, ctx_dict)
+        subject = ''.join(subject.splitlines())
+        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)
+        user = User.objects.get(username=self.username)
+        students = Student.objects.filter(user=user)
+        translation.activate(self.language_code)
+
+        for student in students:
+            if student.is_subscribed and not student.confirmation_sent and student.user.email and student.period == period:
+                self.email(student)
+                student.confirmation_sent = True
+                student.save()
+                # student.user.is_active = True
+                student.user.save()
+                logger.logger.info('email send : ' + student.user.username)
+
+        logger.logger.info('############## Done #################')
diff --git a/teleforma/management/commands/teleforma-students-activate-test.py b/teleforma/management/commands/teleforma-students-activate-test.py
new file mode 100644 (file)
index 0000000..7e9fc0c
--- /dev/null
@@ -0,0 +1,50 @@
+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 = "Activate all user account for subscribed students"
+    
+    def handle(self, *args, **options):
+        period_name = args[-2]
+        log_file = args[-1]
+        logger = Logger(log_file)
+        logger.logger.info('########### Processing #############')
+        users = User.objects.all()
+        period = Period.objects.get(name=period_name)
+
+        for user in users:
+            students = user.student.all()
+            if students:
+                student = students[0]
+                if student.is_subscribed and not user.is_active and student.period == period:
+                    user.is_active = True
+                    user.save()
+                    logger.logger.info('init : ' + user.username)
+
+        logger.logger.info('############## Done #################')
index 9fe557093fba14be6678e4ed8e67ae2a4b9db2c8..ba79ed9444e6443e6f01f88c5340f05434815004 100644 (file)
@@ -29,13 +29,14 @@ class Logger:
 
 class Command(BaseCommand):
     help = "Activate all user account for subscribed students"
+    username = 'test'
 
     def handle(self, *args, **options):
         period_name = args[-2]
         log_file = args[-1]
         logger = Logger(log_file)
         logger.logger.info('########### Processing #############')
-        users = User.objects.all()
+        users = User.objects.filter(username=self.username)
         period = Period.objects.get(name=period_name)
 
         for user in users: