From: yomguy Date: Mon, 5 Nov 2012 16:07:58 +0000 (+0100) Subject: add ae init pass X-Git-Tag: 0.9-probarreau~327 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=06b2ed6e2fc9763faf6984621478418c88d70fe6;p=teleforma.git add ae init pass --- diff --git a/teleforma/management/commands/teleforma-reset-all-passwords-with-mail-ae.py b/teleforma/management/commands/teleforma-reset-all-passwords-with-mail-ae.py new file mode 100644 index 00000000..b7930fd7 --- /dev/null +++ b/teleforma/management/commands/teleforma-reset-all-passwords-with-mail-ae.py @@ -0,0 +1,46 @@ +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 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' + + 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): + users = User.objects.all() + translation.activate(self.language_code) + for user in users: + profile, c = Profile.objects.get_or_create(user=user) + student = user.ae_student.all() + professor = user.professor.all() + if student or professor: + 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() + print user.username +