示例#1
0
    def save(self, domain_override=None,
             subject_template_name='registration/password_reset_subject.txt',
             email_template_name='registration/password_reset_email.html',
             use_https=False, token_generator=default_token_generator,
             from_email=None, request=None):

        email_template_name_plain = 'registration/password_reset_email.txt'

        site = Site.objects.get_current()
        from_email = settings.DEFAULT_FROM_EMAIL
        notify_messages = []
        for user in self.users_cache:
            # lang = user.get_profile().language
            # translation.activate(lang)

            context = {
                'domain': site.domain,
                'site_name': site.name,
                'uid': int_to_base36(user.id),
                'user': user,
                'token': token_generator.make_token(user),
            }

            subject = render_to_string(subject_template_name, context)
            subject = ''.join(subject.splitlines())

            context["title"] = subject
            plain_text = render_to_string(email_template_name_plain, context)
            html = render_to_string(email_template_name, context)
            # translation.deactivate()

            notify_messages.append((subject, plain_text, html, from_email, [user.email]))
        if notify_messages:
            send_mass_mail_html(notify_messages)
示例#2
0
文件: __init__.py 项目: znick/anytask
    def save(self, domain_override=None,
             subject_template_name='registration/password_reset_subject.txt',
             email_template_name='registration/password_reset_email.html',
             use_https=False, token_generator=default_token_generator,
             from_email=None, request=None):

        email_template_name_plain = 'registration/password_reset_email.txt'

        site = Site.objects.get_current()
        from_email = settings.DEFAULT_FROM_EMAIL
        notify_messages = []
        for user in self.users_cache:
            # lang = user.get_profile().language
            # translation.activate(lang)

            context = {
                'domain': site.domain,
                'site_name': site.name,
                'uid': int_to_base36(user.id),
                'user': user,
                'token': token_generator.make_token(user),
            }

            subject = render_to_string(subject_template_name, context)
            subject = ''.join(subject.splitlines())

            context["title"] = subject
            plain_text = render_to_string(email_template_name_plain, context)
            html = render_to_string(email_template_name, context)
            # translation.deactivate()

            notify_messages.append((subject, plain_text, html, from_email, [user.email]))
        if notify_messages:
            send_mass_mail_html(notify_messages)
示例#3
0
文件: models.py 项目: znick/anytask
    def send_mail_update_user(self, email):

        subject = render_to_string('email_update_subject.txt')
        subject = ''.join(subject.splitlines())

        context = {}

        plain_text = render_to_string('email_update.txt', context)
        html = render_to_string('email_update.html', context)

        send_mass_mail_html([(subject, plain_text, html, settings.DEFAULT_FROM_EMAIL, [email])])
示例#4
0
    def send_mail_update_user(self, email):

        subject = render_to_string('email_update_subject.txt')
        subject = ''.join(subject.splitlines())

        context = {}

        plain_text = render_to_string('email_update.txt', context)
        html = render_to_string('email_update.html', context)

        send_mass_mail_html([(subject, plain_text, html,
                              settings.DEFAULT_FROM_EMAIL, [email])])
示例#5
0
    def handle(self, **options):
        start_time = time.time()
        num_sent = 0
        sleep_time = 0
        all_events = Event.objects \
            .filter(sended_notify=False) \
            .exclude(Q(author__isnull=True) | Q(author__username="******") | Q(field__name='review_id')) \
            .distinct() \
            .select_related("issue", "author") \
            .prefetch_related("file_set") \
            .order_by("issue")
        domain = Site.objects.get_current().domain
        from_email = settings.DEFAULT_FROM_EMAIL
        for issue in Issue.objects\
                .filter(event__in=all_events) \
                .distinct() \
                .select_related("student", "responsible", "task", "task__course") \
                .prefetch_related("followers"):
            events = all_events.filter(issue=issue)
            if not events:
                continue
            notify_messages = []
            excluded_ids = []
            if issue.student.email:
                message = get_message(issue.student, _noop(u'studentom'),
                                      issue, events, from_email, domain)
                if message:
                    notify_messages.append(message)
                excluded_ids.append(issue.student.id)

            if issue.responsible and issue.responsible.id not in excluded_ids:
                excluded_ids.append(issue.student.id)
                if issue.responsible.email:
                    message = get_message(issue.responsible,
                                          _noop(u'proverjaushim'), issue,
                                          events, from_email, domain)
                    if message:
                        notify_messages.append(message)

            for follower in issue.followers.exclude(id__in=excluded_ids):
                if follower.email:
                    message = get_message(follower, _noop(u'nabludatelem'),
                                          issue, events, from_email, domain)
                    if message:
                        notify_messages.append(message)

            if notify_messages:
                num_sent += send_mass_mail_html(notify_messages)
                time.sleep(1)
                sleep_time += 1

            for event in events:
                event.sended_notify = True
                event.save()

        # logging to cron log
        print "Command send_issue_notifications send {0} email(s) and took {1} seconds (sleep {2} seconds)" \
            .format(num_sent, time.time() - start_time, sleep_time)
示例#6
0
    def handle(self, **options):
        start_time = time.time()
        num_sent = 0
        sleep_time = 0
        all_events = Event.objects \
            .filter(sended_notify=False) \
            .exclude(Q(author__isnull=True) | Q(author__username="******") | Q(field__name='review_id')) \
            .distinct() \
            .select_related("issue", "author") \
            .prefetch_related("file_set") \
            .order_by("issue")
        domain = Site.objects.get_current().domain
        from_email = settings.DEFAULT_FROM_EMAIL
        for issue in Issue.objects\
                .filter(event__in=all_events) \
                .distinct() \
                .select_related("student", "responsible", "task", "task__course") \
                .prefetch_related("followers"):
            events = all_events.filter(issue=issue)
            if not events:
                continue
            notify_messages = []
            excluded_ids = []
            if issue.student.email:
                message = get_message(issue.student, _noop(u'studentom'), issue, events, from_email, domain)
                if message:
                    notify_messages.append(message)
                excluded_ids.append(issue.student.id)

            if issue.responsible and issue.responsible.id not in excluded_ids:
                excluded_ids.append(issue.student.id)
                if issue.responsible.email:
                    message = get_message(
                        issue.responsible, _noop(u'proverjaushim'), issue, events, from_email, domain
                    )
                    if message:
                        notify_messages.append(message)

            for follower in issue.followers.exclude(id__in=excluded_ids):
                if follower.email:
                    message = get_message(follower, _noop(u'nabludatelem'), issue, events, from_email, domain)
                    if message:
                        notify_messages.append(message)

            if notify_messages:
                num_sent += send_mass_mail_html(notify_messages)
                time.sleep(1)
                sleep_time += 1

            for event in events:
                event.sended_notify = True
                event.save()

        # logging to cron log
        print "Command send_issue_notifications send {0} email(s) and took {1} seconds (sleep {2} seconds)" \
            .format(num_sent, time.time() - start_time, sleep_time)
示例#7
0
文件: models.py 项目: znick/anytask
    def send_activation_email(self, site=None):
        if not site:
            site = Site.objects.get_current()

        subject = render_to_string('email_activate_subject.txt')
        subject = ''.join(subject.splitlines())

        context = {
            'user': self.user,
            'user_info': json.loads(self.user_info),
            'domain': str(site),
            'activation_key': self.activation_key,
            'is_updating': self.is_updating
        }

        plain_text = render_to_string('email_activate.txt', context)
        html = render_to_string('email_activate.html', context)

        send_mass_mail_html([(subject, plain_text, html, settings.DEFAULT_FROM_EMAIL, [self.user.email])])
示例#8
0
    def send_activation_email(self, site=None):
        if not site:
            site = Site.objects.get_current()

        subject = render_to_string('email_activate_subject.txt')
        subject = ''.join(subject.splitlines())

        context = {
            'user': self.user,
            'user_info': json.loads(self.user_info),
            'domain': str(site),
            'activation_key': self.activation_key,
            'is_updating': self.is_updating
        }

        plain_text = render_to_string('email_activate.txt', context)
        html = render_to_string('email_activate.html', context)

        send_mass_mail_html([(subject, plain_text, html,
                              settings.DEFAULT_FROM_EMAIL, [self.user.email])])
示例#9
0
    def handle(self, **options):
        start_time = time.time()

        domain = Site.objects.get_current().domain
        from_email = settings.DEFAULT_FROM_EMAIL

        if hasattr(settings, 'SEND_MESSAGE_FULLTEXT') and settings.SEND_MESSAGE_FULLTEXT:
            notify_messages = send_fulltext(domain, from_email)
        else:
            notify_messages = [send_only_notify(domain, from_email)]

        num_sent = 0
        sleep_time = 0
        for messages in notify_messages:
            if messages:
                num_sent += send_mass_mail_html(messages)
                time.sleep(1)
                sleep_time += 1

        # logging to cron log
        print "Command send_mail_notifications send {0} email(s) and took {1} seconds (sleep {2} seconds)" \
            .format(num_sent, time.time() - start_time, sleep_time)
示例#10
0
    def handle(self, **options):
        start_time = time.time()

        domain = Site.objects.get_current().domain
        from_email = settings.DEFAULT_FROM_EMAIL

        if hasattr(settings,
                   'SEND_MESSAGE_FULLTEXT') and settings.SEND_MESSAGE_FULLTEXT:
            notify_messages = send_fulltext(domain, from_email)
        else:
            notify_messages = [send_only_notify(domain, from_email)]

        num_sent = 0
        sleep_time = 0
        for messages in notify_messages:
            if messages:
                num_sent += send_mass_mail_html(messages)
                time.sleep(1)
                sleep_time += 1

        # logging to cron log
        print "Command send_mail_notifications send {0} email(s) and took {1} seconds (sleep {2} seconds)" \
            .format(num_sent, time.time() - start_time, sleep_time)
    def handle(self, **options):
        start_time = time.time()

        DIFF_FIELDS = [u'title', u'task_text', u'score_max', u'deadline_time']

        students_tasks_info = {}
        for task in Task.objects.filter(
                sended_notify=False).prefetch_related("groups"):
            course = task.course
            if task.is_hidden:
                continue

            task_created = False
            task_changed = False

            version_list = reversion.get_unique_for_object(task)

            task_info = [''] * len(DIFF_FIELDS)
            for i_version, version in enumerate(version_list):
                if version.field_dict['sended_notify']:
                    break

                i_version_next = i_version + 1
                if i_version_next == len(version_list):
                    task_created = True
                    break
                if not version.field_dict['send_to_users']:
                    continue

                for i_field, field in enumerate(DIFF_FIELDS):
                    if field == u'deadline_time':
                        prev = add_timezone(version.field_dict[field])
                        cur = add_timezone(
                            version_list[i_version_next].field_dict[field])
                    else:
                        prev, cur = version.field_dict[field], version_list[
                            i_version_next].field_dict[field]
                    if prev != cur:
                        task_info[i_field] = field
                        task_changed = True
                if not version.field_dict['is_hidden'] and version_list[
                        i_version_next].field_dict['is_hidden']:
                    task_created = True

            if task_created or task_changed:
                for group in task.groups.all():
                    for student in group.students.all():
                        translation.activate(student.get_profile().language)
                        diff_fields_str = {
                            u'title': _(u'nazvanie').lower(),
                            u'task_text': _(u'formulirovka').lower(),
                            u'score_max': _(u'max_ball').lower(),
                            u'deadline_time': _(u'data_sdachi').lower()
                        }
                        task_info_changed = (task, task_created) + tuple(
                            map(lambda a: diff_fields_str[a],
                                filter(lambda a: a != '', task_info)))

                        if student.id in students_tasks_info:
                            if course.id in students_tasks_info[student.id]:
                                students_tasks_info[student.id][course.id][
                                    task.id] = task_info_changed
                            else:
                                students_tasks_info[student.id][course.id] = {
                                    'course': course,
                                    task.id: task_info_changed
                                }
                        else:
                            students_tasks_info[student.id] = {
                                'user': student,
                                course.id: {
                                    'course': course,
                                    task.id: task_info_changed
                                }
                            }

                        translation.deactivate()

            with reversion.create_revision():
                task.sended_notify = True
                task.save()
                reversion.set_comment("Send notification")

        domain = Site.objects.get_current().domain
        from_email = settings.DEFAULT_FROM_EMAIL
        notify_messages = []
        for key_user, courses_info in students_tasks_info.iteritems():
            user = courses_info['user']
            if not user.email:
                continue

            lang = user.get_profile().language
            translation.activate(lang)

            subject = u"{0}, ".format(
                user.first_name) + _(u'proizoshli_izmeneniya_v_kursah')

            context = {
                "user": user,
                "domain": domain,
                "title": subject,
                "courses_info": courses_info,
            }
            plain_text = render_to_string('email_notification_task.txt',
                                          context)
            html = render_to_string('email_notification_task.html', context)
            notify_messages.append(
                (subject, plain_text, html, from_email, [user.email]))
            translation.deactivate()

        num_sent = 0
        if notify_messages:
            num_sent = send_mass_mail_html(notify_messages)

        # logging to cron log
        print "Command send_task_notifications send {0} email(s) and took {1} seconds"\
            .format(num_sent, time.time() - start_time)
示例#12
0
    def handle(self, **options):
        start_time = time.time()

        DIFF_FIELDS = [u'title', u'task_text', u'score_max', u'deadline_time']

        students_tasks_info = {}
        for task in Task.objects.filter(sended_notify=False).prefetch_related("groups"):
            course = task.course
            if task.is_hidden:
                continue

            task_created = False
            task_changed = False

            version_list = reversion.get_unique_for_object(task)

            task_info = [''] * len(DIFF_FIELDS)
            for i_version, version in enumerate(version_list):
                if version.field_dict['sended_notify']:
                    break

                i_version_next = i_version + 1
                if i_version_next == len(version_list):
                    task_created = True
                    break
                if not version.field_dict['send_to_users']:
                    continue

                for i_field, field in enumerate(DIFF_FIELDS):
                    if field == u'deadline_time':
                        prev = add_timezone(version.field_dict[field])
                        cur = add_timezone(version_list[i_version_next].field_dict[field])
                    else:
                        prev, cur = version.field_dict[field], version_list[i_version_next].field_dict[field]
                    if prev != cur:
                        task_info[i_field] = field
                        task_changed = True
                if not version.field_dict['is_hidden'] and version_list[i_version_next].field_dict['is_hidden']:
                    task_created = True

            if task_created or task_changed:
                for group in task.groups.all():
                    for student in group.students.all():
                        translation.activate(student.get_profile().language)
                        diff_fields_str = {
                            u'title': _(u'nazvanie').lower(),
                            u'task_text': _(u'formulirovka').lower(),
                            u'score_max': _(u'max_ball').lower(),
                            u'deadline_time': _(u'data_sdachi').lower()
                        }
                        task_info_changed = (task, task_created) + tuple(map(
                            lambda a: diff_fields_str[a],
                            filter(lambda a: a != '', task_info)
                        ))

                        if student.id in students_tasks_info:
                            if course.id in students_tasks_info[student.id]:
                                students_tasks_info[student.id][course.id][task.id] = task_info_changed
                            else:
                                students_tasks_info[student.id][course.id] = {
                                    'course': course,
                                    task.id: task_info_changed
                                }
                        else:
                            students_tasks_info[student.id] = {
                                'user': student,
                                course.id: {
                                    'course': course,
                                    task.id: task_info_changed
                                }
                            }

                        translation.deactivate()

            with reversion.create_revision():
                task.sended_notify = True
                task.save()
                reversion.set_comment("Send notification")

        domain = Site.objects.get_current().domain
        from_email = settings.DEFAULT_FROM_EMAIL
        notify_messages = []
        for key_user, courses_info in students_tasks_info.iteritems():
            user = courses_info['user']
            if not user.email:
                continue

            lang = user.get_profile().language
            translation.activate(lang)

            subject = u"{0}, ".format(user.first_name) + _(u'proizoshli_izmeneniya_v_kursah')

            context = {
                "user": user,
                "domain": domain,
                "title": subject,
                "courses_info": courses_info,
            }
            plain_text = render_to_string('email_notification_task.txt', context)
            html = render_to_string('email_notification_task.html', context)
            notify_messages.append((subject, plain_text, html, from_email, [user.email]))
            translation.deactivate()

        num_sent = 0
        if notify_messages:
            num_sent = send_mass_mail_html(notify_messages)

        # logging to cron log
        print "Command send_task_notifications send {0} email(s) and took {1} seconds"\
            .format(num_sent, time.time() - start_time)