예제 #1
0
    def get_context_data(self, **kwargs):
        context = \
            super(PersonRecordingStatsView, self).get_context_data(**kwargs)

        person = get_person(self.request)
        language = get_current_language(self.request)

        recordings = Recording.objects.filter(sentence__language=language,
                                              person=person)

        # Assume for now user is in NZ timezone = UTC + 12 hours
        time_offset = 0
        now = timezone.now() + datetime.timedelta(hours=time_offset)

        # Find the day for NZ, then take us back to utc time.
        today_begining = \
            datetime.datetime.combine(now, datetime.time()) - \
            datetime.timedelta(hours=time_offset)

        # This logical is compared against utc time - the default timezone for our data
        # I presume django deals with this timezoen stuff anyway?
        todays_recordings = recordings.filter(
            created__gte=today_begining).order_by('created')

        stats = {
            'recordings': build_recordings_stat_dict(recordings),
            'recordings_today': build_recordings_stat_dict(todays_recordings)
        }

        stats['recordings_today']['start_time'] = today_begining
        stats['recordings_today']['end_time'] = timezone.now()
        if todays_recordings:
            stats['recordings_today']['earliest_time'] = todays_recordings[
                0].created
            stats['recordings_today']['latest_time'] = todays_recordings[
                todays_recordings.count() - 1].created

        context['person'] = person
        context['stats'] = stats

        return context
예제 #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)