Esempio n. 1
0
    def get_context_data(self, **kwargs):
        context = \
            super(GroupStatsView, self).get_context_data(**kwargs)

        group = context['group']

        context['x_title'] = _('Group Leaderboard: {0}').format(group)
        context['x_description'] = _("Leaderboard for members of {0}.").format(
            group)

        language = get_current_language(self.request)

        people = Person.objects.all()

        valid_members = people
        valid_members = get_valid_group_members(group, valid_members)

        invalid_members = people
        invalid_members = get_invalid_group_members(group, invalid_members)

        # people = people\
        #     .filter(recording__sentence__language=language)

        # score, count = get_competition_group_score(group)
        num_recordings = 0
        if valid_members:
            for member in valid_members:
                recordings = filter_qs_for_competition(
                    Recording.objects.filter(person=member))
                member.num_recordings = recordings.count()

        if invalid_members:
            invalid_members = invalid_members\
                .annotate(num_groups=Count('groups', distinct=True))

            for member in invalid_members:
                recordings = filter_qs_for_competition(
                    Recording.objects.filter(person=member))
                member.num_recordings = recordings.count()
                member.verified = email_verified(member)

        form = ResendEmailVerificationForm()

        group.duration_hours = group.duration / 60 / 60

        context['group'] = group
        context['score'] = group.score
        context['form'] = form
        context['people'] = people.filter(groups=group)
        context['valid_members'] = valid_members
        context['invalid_members'] = invalid_members

        return context
Esempio n. 2
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)