Пример #1
0
def send_election_results(election_id, is_secure, site_id, remote_addr, user_id):
    election = Election.objects.get(pk=election_id)

    user = User.objects.get(pk=user_id)

    context = get_base_email_context_task(is_secure, site_id)

    context.update(dict(
        election=election,
        election_url=reverse('election-view',
            kwargs=dict(username=election.agora.creator.username,
                agoraname=election.agora.name, electionname=election.name)),
        agora_url=reverse('agora-view',
            kwargs=dict(username=election.agora.creator.username,
                agoraname=election.agora.name)),
    ))

    # List of emails to send. tuples are of format:
    #
    # (subject, text, html, from_email, recipient)
    datatuples = []

    for vote in election.get_all_votes():

        if not vote.voter.get_profile().has_perms('receive_email_updates'):
            continue

        translation.activate(vote.voter.get_profile().lang_code)
        context['to'] = vote.voter
        try:
            context['delegate'] = get_delegate_in_agora(vote.voter, election.agora)
        except:
            pass
        datatuples.append((
            _('Election results for %s') % election.pretty_name,
            render_to_string('agora_core/emails/election_results.txt',
                context),
            render_to_string('agora_core/emails/election_results.html',
                context),
            None,
            [vote.voter.email]))

    translation.deactivate()

    send_mass_html_mail(datatuples)

    action.send(user, verb='published results', action_object=election,
        target=election.agora, ipaddr=remote_addr,
        geolocation=json.dumps(geolocate_ip(remote_addr)))
Пример #2
0
def start_election(election_id, is_secure, site_id, remote_addr):
    election = Election.objects.get(pk=election_id)
    if not election.is_approved or election.is_archived():
        election.voting_starts_at_date = None
        election.save()
        return

    if election.extra_data and "started" in election.extra_data:
        return

    if (
        not election.voting_starts_at_date
        or not election.frozen_at_date
        or election.voting_starts_at_date > datetime.datetime.now()
    ):
        return

    election.voting_starts_at_date = datetime.datetime.now()
    election.create_hash()
    if not election.extra_data:
        election.extra_data = dict(started=True)
    else:
        election.extra_data["started"] = True
    election.save()

    context = get_base_email_context(is_secure, site_id)

    context.update(
        dict(
            election=election,
            election_url=reverse(
                "election-view",
                kwargs=dict(
                    username=election.agora.creator.username, agoraname=election.agora.name, electionname=election.name
                ),
            ),
            agora_url=reverse(
                "agora-view", kwargs=dict(username=election.agora.creator.username, agoraname=election.agora.name)
            ),
        )
    )

    # List of emails to send. tuples are of format:
    #
    # (subject, text, html, from_email, recipient)
    datatuples = []

    # NOTE: for now, electorate is dynamic and just taken from the election's
    # agora members' list
    for voter in election.agora.members.all():

        if not voter.email or not voter.get_profile().email_updates:
            continue

        context["to"] = voter
        try:
            context["delegate"] = get_delegate_in_agora(voter, election.agora)
        except:
            pass
        datatuples.append(
            (
                _("Vote in election %s") % election.pretty_name,
                render_to_string("agora_core/emails/election_started.txt", context),
                render_to_string("agora_core/emails/election_started.html", context),
                None,
                [voter.email],
            )
        )

    # Also notify third party delegates
    for voter in election.agora.active_nonmembers_delegates():

        if not voter.email or not voter.get_profile().email_updates:
            continue

        context["to"] = voter
        datatuples.append(
            (
                _("Vote in election %s") % election.pretty_name,
                render_to_string("agora_core/emails/election_started.txt", context),
                render_to_string("agora_core/emails/election_started.html", context),
                None,
                [voter.email],
            )
        )

    send_mass_html_mail(datatuples)
Пример #3
0
def end_election(election_id, is_secure, site_id, remote_addr, user_id):
    election = Election.objects.get(pk=election_id)
    if not election.is_approved or election.is_archived():
        election.voting_extended_until_date = election.voting_ends_at_date = None
        election.save()
        return

    if election.extra_data and "ended" in election.extra_data:
        return

    if (
        not election.voting_extended_until_date
        or not election.frozen_at_date
        or election.voting_extended_until_date > datetime.datetime.now()
    ):
        return

    user = User.objects.get(pk=user_id)

    election.voting_extended_until_date = datetime.datetime.now()
    if not election.extra_data:
        election.extra_data = dict(ended=True)
    else:
        election.extra_data["ended"] = True
    election.save()
    election.compute_result()

    context = get_base_email_context(is_secure, site_id)

    context.update(
        dict(
            election=election,
            election_url=reverse(
                "election-view",
                kwargs=dict(
                    username=election.agora.creator.username, agoraname=election.agora.name, electionname=election.name
                ),
            ),
            agora_url=reverse(
                "agora-view", kwargs=dict(username=election.agora.creator.username, agoraname=election.agora.name)
            ),
        )
    )

    # List of emails to send. tuples are of format:
    #
    # (subject, text, html, from_email, recipient)
    datatuples = []

    for vote in election.get_all_votes():

        if not vote.voter.email or not vote.voter.get_profile().email_updates:
            continue

        context["to"] = vote.voter
        try:
            context["delegate"] = get_delegate_in_agora(vote.voter, election.agora)
        except:
            pass
        datatuples.append(
            (
                _("Election results for %s") % election.pretty_name,
                render_to_string("agora_core/emails/election_results.txt", context),
                render_to_string("agora_core/emails/election_results.html", context),
                None,
                [vote.voter.email],
            )
        )

    send_mass_html_mail(datatuples)

    action.send(
        user,
        verb="published results",
        action_object=election,
        target=election.agora,
        ipaddr=remote_addr,
        geolocation=json.dumps(geolocate_ip(remote_addr)),
    )
Пример #4
0
def start_election(election_id, is_secure, site_id, remote_addr, user_id):
    election = Election.objects.get(pk=election_id)
    if not election.is_approved or election.is_archived():
        election.voting_starts_at_date = None
        election.voting_ends_at_date = None
        election.voting_extended_until_date = None
        election.save()
        return

    if election.extra_data and "started" in election.extra_data:
        return

    if not election.voting_starts_at_date or\
            election.voting_starts_at_date > timezone.now():
        return

    election.voting_starts_at_date = timezone.now()
    if election.frozen_at_date is None:
        election.frozen_at_date = election.voting_starts_at_date
    election.create_hash()
    if not election.extra_data:
        election.extra_data = dict(started=True)
    else:
        election.extra_data["started"] = True
    election.save()

    context = get_base_email_context_task(is_secure, site_id)

    context.update(
        dict(
            election=election,
            election_url=reverse('election-view',
                                 kwargs=dict(
                                     username=election.agora.creator.username,
                                     agoraname=election.agora.name,
                                     electionname=election.name)),
            agora_url=reverse('agora-view',
                              kwargs=dict(
                                  username=election.agora.creator.username,
                                  agoraname=election.agora.name)),
        ))

    # List of emails to send. tuples are of format:
    #
    # (subject, text, html, from_email, recipient)
    datatuples = []

    # NOTE: for now, electorate is dynamic and just taken from the election's
    # agora members' list
    for voter in election.agora.members.all():

        if not voter.get_profile().has_perms('receive_email_updates'):
            continue

        translation.activate(voter.get_profile().lang_code)
        context['to'] = voter
        context['allow_delegation'] = (
            election.agora.delegation_policy == Agora.DELEGATION_TYPE[0][0])
        try:
            context['delegate'] = get_delegate_in_agora(voter, election.agora)
        except:
            pass
        datatuples.append(
            (_('Vote in election %s') % election.pretty_name,
             render_to_string('agora_core/emails/election_started.txt',
                              context),
             render_to_string('agora_core/emails/election_started.html',
                              context), None, [voter.email]))

    # Also notify third party delegates
    for voter in election.agora.active_nonmembers_delegates():

        if not voter.get_profile().has_perms('receive_email_updates'):
            continue

        translation.activate(voter.get_profile().lang_code)
        context['to'] = voter
        datatuples.append(
            (_('Vote in election %s') % election.pretty_name,
             render_to_string('agora_core/emails/election_started.txt',
                              context),
             render_to_string('agora_core/emails/election_started.html',
                              context), None, [voter.email]))

    translation.deactivate()

    send_mass_html_mail(datatuples)

    user = User.objects.get(pk=user_id)

    action.send(user,
                verb='started voting period',
                action_object=election,
                target=election.agora,
                ipaddr=remote_addr,
                geolocation=json.dumps(geolocate_ip(remote_addr)))
Пример #5
0
def end_election(election_id, is_secure, site_id, remote_addr, user_id):
    election = Election.objects.get(pk=election_id)
    if not election.is_approved or election.is_archived():
        election.voting_extended_until_date = election.voting_ends_at_date = None
        election.save()
        return

    if election.extra_data and "ended" in election.extra_data:
        return

    if not election.voting_extended_until_date or not election.frozen_at_date or\
        election.voting_extended_until_date > timezone.now():
        return

    user = User.objects.get(pk=user_id)

    election.voting_extended_until_date = timezone.now()
    if not election.extra_data:
        election.extra_data = dict(ended=True)
    else:
        election.extra_data["ended"] = True
    election.save()
    election.compute_result()

    context = get_base_email_context_task(is_secure, site_id)

    context.update(
        dict(
            election=election,
            election_url=reverse('election-view',
                                 kwargs=dict(
                                     username=election.agora.creator.username,
                                     agoraname=election.agora.name,
                                     electionname=election.name)),
            agora_url=reverse('agora-view',
                              kwargs=dict(
                                  username=election.agora.creator.username,
                                  agoraname=election.agora.name)),
        ))

    # List of emails to send. tuples are of format:
    #
    # (subject, text, html, from_email, recipient)
    datatuples = []

    for vote in election.get_all_votes():

        if not vote.voter.get_profile().has_perms('receive_email_updates'):
            continue

        translation.activate(vote.voter.get_profile().lang_code)
        context['to'] = vote.voter
        try:
            context['delegate'] = get_delegate_in_agora(
                vote.voter, election.agora)
        except:
            pass
        datatuples.append(
            (_('Election results for %s') % election.pretty_name,
             render_to_string('agora_core/emails/election_results.txt',
                              context),
             render_to_string('agora_core/emails/election_results.html',
                              context), None, [vote.voter.email]))

    translation.deactivate()

    send_mass_html_mail(datatuples)

    action.send(user,
                verb='published results',
                action_object=election,
                target=election.agora,
                ipaddr=remote_addr,
                geolocation=json.dumps(geolocate_ip(remote_addr)))
Пример #6
0
def start_election(election_id, is_secure, site_id, remote_addr):
    election = Election.objects.get(pk=election_id)
    if not election.is_approved or election.is_archived():
        election.voting_starts_at_date = None
        election.save()
        return

    if election.extra_data and "started" in election.extra_data:
        return

    if not election.voting_starts_at_date or not election.frozen_at_date or\
        election.voting_starts_at_date > datetime.datetime.now():
        return

    election.voting_starts_at_date = datetime.datetime.now()
    election.create_hash()
    if not election.extra_data:
        election.extra_data = dict(started=True)
    else:
        election.extra_data["started"]=True
    election.save()

    context = get_base_email_context(is_secure, site_id)

    context.update(dict(
        election=election,
        election_url=reverse('election-view',
            kwargs=dict(username=election.agora.creator.username,
                agoraname=election.agora.name, electionname=election.name)),
        agora_url=reverse('agora-view',
            kwargs=dict(username=election.agora.creator.username,
                agoraname=election.agora.name)),
    ))

    # List of emails to send. tuples are of format:
    #
    # (subject, text, html, from_email, recipient)
    datatuples = []

    # NOTE: for now, electorate is dynamic and just taken from the election's
    # agora members' list
    for voter in election.agora.members.all():

        if not voter.email or not voter.get_profile().email_updates:
            continue

        context['to'] = voter
        try:
            context['delegate'] = get_delegate_in_agora(voter, election.agora)
        except:
            pass
        datatuples.append((
            _('Vote in election %s') % election.pretty_name,
            render_to_string('agora_core/emails/election_started.txt',
                context),
            render_to_string('agora_core/emails/election_started.html',
                context),
            None,
            [voter.email]))

    # Also notify third party delegates
    for voter in election.agora.active_nonmembers_delegates():

        if not voter.email or not voter.get_profile().email_updates:
            continue

        context['to'] = voter
        datatuples.append((
            _('Vote in election %s') % election.pretty_name,
            render_to_string('agora_core/emails/election_started.txt',
                context),
            render_to_string('agora_core/emails/election_started.html',
                context),
            None,
            [voter.email]))

    send_mass_html_mail(datatuples)
Пример #7
0
def start_election(election_id, is_secure, site_id, remote_addr, user_id):
    election = Election.objects.get(pk=election_id)
    if not election.is_approved or election.is_archived():
        election.voting_starts_at_date = None
        election.voting_ends_at_date = None
        election.voting_extended_until_date = None
        election.save()
        return

    if election.extra_data and "started" in election.extra_data:
        return

    if not election.voting_starts_at_date or\
            election.voting_starts_at_date > timezone.now():
        return

    election.voting_starts_at_date = timezone.now()
    if election.frozen_at_date is None:
        election.frozen_at_date = election.voting_starts_at_date
    election.create_hash()
    if not election.extra_data:
        election.extra_data = dict(started=True)
    else:
        election.extra_data["started"]=True
    election.save()

    context = get_base_email_context_task(is_secure, site_id)

    context.update(dict(
        election=election,
        election_url=reverse('election-view',
            kwargs=dict(username=election.agora.creator.username,
                agoraname=election.agora.name, electionname=election.name)),
        agora_url=reverse('agora-view',
            kwargs=dict(username=election.agora.creator.username,
                agoraname=election.agora.name)),
    ))

    # List of emails to send. tuples are of format:
    #
    # (subject, text, html, from_email, recipient)
    datatuples = []

    # NOTE: for now, electorate is dynamic and just taken from the election's
    # agora members' list
    for voter in election.agora.members.all():

        if not voter.get_profile().has_perms('receive_email_updates'):
            continue

        translation.activate(voter.get_profile().lang_code)
        context['to'] = voter
        context['allow_delegation'] = (election.agora.delegation_policy == Agora.DELEGATION_TYPE[0][0])
        try:
            context['delegate'] = get_delegate_in_agora(voter, election.agora)
        except:
            pass
        datatuples.append((
            _('Vote in election %s') % election.pretty_name,
            render_to_string('agora_core/emails/election_started.txt',
                context),
            render_to_string('agora_core/emails/election_started.html',
                context),
            None,
            [voter.email]))

    # Also notify third party delegates
    for voter in election.agora.active_nonmembers_delegates():

        if not voter.get_profile().has_perms('receive_email_updates'):
            continue

        translation.activate(voter.get_profile().lang_code)
        context['to'] = voter
        datatuples.append((
            _('Vote in election %s') % election.pretty_name,
            render_to_string('agora_core/emails/election_started.txt',
                context),
            render_to_string('agora_core/emails/election_started.html',
                context),
            None,
            [voter.email]))

    translation.deactivate()

    send_mass_html_mail(datatuples)

    user = User.objects.get(pk=user_id)

    action.send(user, verb='started voting period', action_object=election,
        target=election.agora, ipaddr=remote_addr,
        geolocation=json.dumps(geolocate_ip(remote_addr)))
Пример #8
0
def end_election(election_id, is_secure, site_id, remote_addr, user_id):
    election = Election.objects.get(pk=election_id)
    if not election.is_approved or election.is_archived():
        election.voting_extended_until_date = election.voting_ends_at_date = None
        election.save()
        return

    if election.extra_data and "ended" in election.extra_data:
        return

    if not election.voting_extended_until_date or not election.frozen_at_date or\
        election.voting_extended_until_date > timezone.now():
        return

    user = User.objects.get(pk=user_id)

    election.voting_extended_until_date = timezone.now()
    if not election.extra_data:
        election.extra_data = dict(ended=True)
    else:
        election.extra_data["ended"]=True
    election.save()
    election.compute_result()

    context = get_base_email_context_task(is_secure, site_id)

    context.update(dict(
        election=election,
        election_url=reverse('election-view',
            kwargs=dict(username=election.agora.creator.username,
                agoraname=election.agora.name, electionname=election.name)),
        agora_url=reverse('agora-view',
            kwargs=dict(username=election.agora.creator.username,
                agoraname=election.agora.name)),
    ))

    # List of emails to send. tuples are of format:
    #
    # (subject, text, html, from_email, recipient)
    datatuples = []

    for vote in election.get_all_votes():

        if not vote.voter.get_profile().has_perms('receive_email_updates'):
            continue

        translation.activate(vote.voter.get_profile().lang_code)
        context['to'] = vote.voter
        try:
            context['delegate'] = get_delegate_in_agora(vote.voter, election.agora)
        except:
            pass
        datatuples.append((
            _('Election results for %s') % election.pretty_name,
            render_to_string('agora_core/emails/election_results.txt',
                context),
            render_to_string('agora_core/emails/election_results.html',
                context),
            None,
            [vote.voter.email]))

    translation.deactivate()

    send_mass_html_mail(datatuples)

    action.send(user, verb='published results', action_object=election,
        target=election.agora, ipaddr=remote_addr,
        geolocation=json.dumps(geolocate_ip(remote_addr)))