Example #1
0
def send_archive_warning_emails():

    """
    Send warning emails to inform users that their workbook is scheduled to be archived.
    """

    cases = Case.objects.filter(state='email',
                                action_completed=False,
                                infraction__infraction_type__enable_archive=True)

    for case in cases:

        # Define warning email variables for use in template.
        user_friendly_name = case.infraction.user_friendly_name
        user_email = case.infraction.user_email
        infraction_value = case.infraction.infraction_value
        email_count = case.infraction.infraction_type.email_count
        interval = case.infraction.infraction_type.interval
        archive_date = case.infraction.date_added + timedelta(days=(email_count + 1)*interval)
        random_greeting = core.random_greeting()
        object_name = case.infraction.object_name
        msg_subject = 'Workbook Archive Warning: %s' % object_name

        # Define template "context" - variables passed into the template for rendering.
        c = Context({'user_friendly_name': user_friendly_name,
                     'user_email': user_email,
                     'greeting': random_greeting,
                     'archive_date': archive_date.date(),
                     'object_name': object_name,
                     'infraction_value': infraction_value})

        # Get the email template for the group infraction type.
        email_template = InfractionType.objects.get(id=case.infraction.infraction_type.id).archive_email_template
        t = Template(email_template)

        # Render the template using the context defined above.
        email_msg = t.render(c)

        # Send email to the current user.
        core.send_email(recipient=user_email,
                        subject=msg_subject,
                        html_msg=email_msg)

    # Update case email_count and action_completed.
    for case in cases:
        case.action_completed = 'True'
        case.save()
Example #2
0
def send_aggregate_emails():

    """
    Send aggregate emails. Each email, sent to a single user, contains information pertaining to one or
    more infractions of the same infraction type.

    """

    # Get a list of dictionaries of 'groups' of cases by user and infraction type - i.e distinct pairs of
    # users and infraction types.
    case_groups = Case.objects.filter(state='email',
                                      action_completed=False,
                                      infraction__infraction_type__enable_archive=False)\
                                      .values('infraction__user_name',
                                              'infraction__infraction_type_id',
                                              'infraction__infraction_type__infraction_type').distinct()

    # For each 'group', a single email is sent with aggregated infraction information.
    for group in case_groups:

        user_name = group['infraction__user_name']
        infraction_type_id = group['infraction__infraction_type_id']
        infraction_type_friendly = group['infraction__infraction_type__infraction_type']

        # Get all cases in current group.
        cases = Case.objects.filter(infraction__user_name=user_name,
                                    infraction__infraction_type_id=infraction_type_id,
                                    state='email',
                                    action_completed=False)

        # Get a list of the infraction ID's for all cases in the current group.
        case_infraction_ids = Case.objects.filter(infraction__user_name=user_name,
                                                  infraction__infraction_type_id=infraction_type_id,
                                                  state='email',
                                                  action_completed=False).values_list('infraction__id', flat=True)

        # Get a list of dictionaries containing data for all infractions in group.
        group_infractions = Infraction.objects.filter(id__in=case_infraction_ids).values()

        user_friendly_name = group_infractions[0]['user_friendly_name']
        user_email = group_infractions[0]['user_email']
        random_greeting = core.random_greeting()

        # Define the 'context' variables that will be used to render the email template.
        c = Context({'infractions': group_infractions,
                     'user_name': user_friendly_name,
                     'user_email': user_email,
                     'infraction_type_id': infraction_type_id,
                     'greeting': random_greeting})

        # Get the email template for the group infraction type.
        email_template = InfractionType.objects.get(id=infraction_type_id).email_template
        t = Template(email_template)

        # Render the template using the context defined above.
        email_msg = t.render(c)

        # Send email to the current user.
        core.send_email(recipient=user_email,
                        subject=infraction_type_friendly,
                        html_msg=email_msg)

        # Update case email_count and action_completed.
        for case in cases:
            case.action_completed = 'True'
            case.save()