Beispiel #1
0
def send_person_emails(frequency='weekly'):
    '''Send a email to all people.'''

    counter = 0
    # Check if site is development
    if settings.DEBUG:
        send_person_emails_staff.apply_async(args=[frequency], countdown=2)
        return "This is a dev environment!"
    else:

        people = Person.objects.filter(
            **{'receive_{0}_updates'.format(frequency): True})
        for person in people:
            email = get_email(person)
            if email:
                try:
                    logger.debug("Sending email to {0}".format(person))
                    result = send_status_email.apply_async(
                        args=[person.pk, frequency],
                        countdown=counter * 2,
                        task_id='send_{1}_email-{0}-{2}'.format(
                            person.pk, frequency,
                            timezone.now().strftime("%y%m%d-%H%M%S")))
                    logger.debug(result)
                    counter = counter + 1
                except:
                    logger.debug("Email did not send for {0}".format(person))

    return "Sent {0} {1} emails.".format(counter, frequency)
Beispiel #2
0
def send_status_email(person_pk, frequency='weekly'):
    from corpora.email_utils import EMail

    try:
        person = Person.objects.get(pk=person_pk)
    except ObjectDoesNotExist:
        return "No person with id {0} found.".format(person_pk)

    # Set the language - this is used when rendering the templates.
    language = translation.get_language()
    try:
        active_language = \
            KnownLanguage.objects.get(person=person, active=True)
        language = active_language.language
    except ObjectDoesNotExist:
        pass
    translation.activate(language)

    recordings = Recording.objects.all()
    stats = build_recordings_stat_dict(recordings)
    total_seconds = stats['total_seconds']
    hours = total_seconds / 60.0 / 60.0

    recordings = recordings.filter(person=person)
    stats = build_recordings_stat_dict(recordings)

    if 'daily' in frequency:
        time_delta = datetime.timedelta(days=1)
        period_of_time = _('today')
        previous_period_of_time = _('yesterday')

        this_period_dt = \
            datetime.datetime.combine(
                timezone.now(),
                datetime.time())

        last_period_dt = \
            this_period_dt-time_delta

    else:
        time_delta = datetime.timedelta(days=7)
        period_of_time = _('this week')
        previous_period_of_time = _('last week')

        now = timezone.now()
        week_day = now.weekday()

        this_period_dt = \
            datetime.datetime.combine(
                timezone.now()-datetime.timedelta(days=week_day),
                datetime.time())

        last_period_dt = \
            this_period_dt-time_delta

    period = recordings.filter(created__gt=this_period_dt)
    this_period_stats = build_recordings_stat_dict(period)

    period = recordings\
        .filter(created__lte=this_period_dt)\
        .filter(created__gt=last_period_dt)
    last_period_stats = build_recordings_stat_dict(period)

    lastest_recording = recordings\
        .order_by('-created')\
        .first()

    if lastest_recording is None:
        # User hans't even recorded anything,
        # so no point sending emails.
        return "Not sending status email since {0} hasn't \
                recorded anything in a while.".format(person.pk)

    if lastest_recording.created < last_period_dt:
        # The user hasn't recorded anythign in ages.
        # Let's turn this off!
        return "Not sending status email since {0} hasn't \
                recorded anything in a while.".format(person.pk)

    # approval_rate =

    email = get_email(person)

    if email:

        url_append = 'https://' + Site.objects.get_current().domain

        subject = "Your {0} update!".format(frequency)

        e = EMail(to=email, subject=subject)
        context = {
            'subject': subject,
            'person': person,
            'stats': stats,
            'this_period_stats': this_period_stats,
            'last_period_stats': last_period_stats,
            'total_duration': "{0:.1f}".format(hours),
            'url_append': url_append,
            'site': Site.objects.get_current(),
            'period_of_time': period_of_time,
            'previous_period_of_time': previous_period_of_time,
            'frequency': frequency
        }

        e.text('people/email/freq_stats_update.txt', context)
        e.html('people/email/freq_stats_update.html', context)

        if settings.DEBUG:
            p_display = email
        else:
            p_display = person_pk

        try:
            result = e.send(from_addr='Kōrero Māori <*****@*****.**>',
                            fail_silently='False')
        except Exception, e:
            result = 10
            pass

        if result == 1:
            return "Sent email to {0}".format(p_display)
        else:
            return \
                "Error sending email to {0} - {1}.".format(p_display, result)
Beispiel #3
0
def send_email_to_group(group_pk, message_pk, ma_pk):
    from corpora.email_utils import EMail

    try:
        group = Group.objects.get(pk=group_pk)
    except ObjectDoesNotExist:
        return "No group with id {0} found.".format(group_pk)

    try:
        message = Message.objects.get(pk=message_pk)
    except ObjectDoesNotExist:
        return "No message with id {0} found.".format(message_pk)

    try:
        ma = MessageAction.objects.get(pk=ma_pk)
    except ObjectDoesNotExist:
        return "No message action with id {0} found.".format(message_pk)

    people = Person.objects.filter(groups=group)

    task_message = []
    for person in people:

        # Set the language - this is used when rendering the templates.
        language = translation.get_language()
        try:
            active_language = \
                KnownLanguage.objects.get(person=person, active=True)
            language = active_language.language
        except ObjectDoesNotExist:
            pass
        translation.activate(language)

        email = get_email(person)

        if email:

            url_append = 'https://' + Site.objects.get_current().domain
            subject = message.subject

            valid = get_valid_group_members(group)
            invalid = get_invalid_group_members(group)

            e = EMail(to=email, subject=subject)
            context = {
                'subject': subject,
                'person': person,
                'people': people,
                'group': group,
                'valid': valid,
                'invalid': invalid,
                'content': message.content,
                'url_append': url_append,
                'site': Site.objects.get_current()
            }

            e.text('message/email/email_message_group.txt', context)
            e.html('message/email/email_message_group.html', context)

            if settings.DEBUG:
                p_display = email
            else:
                p_display = person.pk

            # Do not send emails from dev/local environment.
            if person.user is None:
                result = 3
            elif not person.user.is_staff and settings.DEBUG:
                result = 2
            else:
                result = e.send(
                    from_addr='Kōrero Māori <*****@*****.**>',
                    fail_silently='False')

            if result == 1:
                # note that we save the state for the entire action yet it's
                # depended on many little actions. This needs to be rethough or
                # improved.
                ma.completed = True
                ma.save()
                task_message.append("Sent email to {0}".format(p_display))
            elif result == 2:
                task_message.append(
                    "Not sending email to {0} since not production".format(
                        p_display))
            else:
                task_message.append(
                    "Error sending email to {0} - {1}.".format(
                        p_display, result)
                )

        else:
            task_message.append("No email associated with person.")

    return ' ,'.join(task_message)
Beispiel #4
0
def send_email_to_person(person_pk, message_pk, ma_pk):
    from corpora.email_utils import EMail

    try:
        person = Person.objects.get(pk=person_pk)
    except ObjectDoesNotExist:
        return "No person with id {0} found.".format(person_pk)

    try:
        message = Message.objects.get(pk=message_pk)
    except ObjectDoesNotExist:
        return "No message with id {0} found.".format(message_pk)

    try:
        ma = MessageAction.objects.get(pk=ma_pk)
    except ObjectDoesNotExist:
        return "No message action with id {0} found.".format(message_pk)

    # Set the language - this is used when rendering the templates.
    language = translation.get_language()
    try:
        active_language = \
            KnownLanguage.objects.get(person=person, active=True)
        language = active_language.language
    except ObjectDoesNotExist:
        pass
    translation.activate(language)

    email = get_email(person)

    if email:

        url_append = 'https://' + Site.objects.get_current().domain

        subject = message.subject

        e = EMail(to=email, subject=subject)
        context = {
            'subject': subject,
            'person': person,
            'content': message.content,
            'url_append': url_append,
            'site': Site.objects.get_current()
        }

        e.text('message/email/email_message.txt', context)
        e.html('message/email/email_message.html', context)

        if settings.DEBUG:
            p_display = email
        else:
            p_display = person_pk

        result = e.send(
            from_addr='Kōrero Māori <*****@*****.**>',
            fail_silently='False')
        if result == 1:
            ma.completed = True
            ma.save()
            return "Sent email to {0}".format(p_display)
        else:
            return \
                "Error sending email to {0} - {1}.".format(p_display, result)

    else:
        return "No email associated with person."
Beispiel #5
0
def email(model):
    return get_email(model)