# Path: agora_site/agora_core/templatetags/agora_utils.py
# @register.filter
# def get_delegate_in_agora(user, agora):
#     return user.get_profile().get_delegation_in_agora(agora).get_delegate()


import datetime
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.models import User
from django.conf.urls import patterns, url, include
from django.conf import settings
from django.core.urlresolvers import reverse
from django.core.mail import EmailMultiAlternatives, EmailMessage, send_mass_mail
from django.utils import simplejson as json
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext as _
from django.utils.translation import check_for_language
from django.utils import timezone
from django.shortcuts import redirect, get_object_or_404, render_to_response
from django.template.loader import render_to_string
from django.views.generic import TemplateView, ListView, CreateView, RedirectView
from django.views.generic.edit import UpdateView, FormView
from django.views.i18n import set_language as django_set_language
from django import http
from django.db import transaction
from actstream.actions import follow, unfollow, is_following
from actstream.models import (object_stream, election_stream, Action,
    user_stream, actor_stream)
from actstream.signals import action
from guardian.shortcuts import *
from haystack.query import EmptySearchQuerySet
from haystack.forms import ModelSearchForm, FacetedSearchForm
from haystack.query import SearchQuerySet
from haystack.views import SearchView as HaystackSearchView
from agora_site.agora_core.templatetags.agora_utils import get_delegate_in_agora
from agora_site.agora_core.models import Agora, Election, Profile, CastVote
from agora_site.agora_core.backends.fnmt import fnmt_data_from_pem
from agora_site.agora_core.forms import *
from agora_site.agora_core.tasks import *
from agora_site.misc.utils import *
            kwargs=dict(username=election.agora.creator.username,
                agoraname=election.agora.name, electionname=election.name))

        if not election.ballot_is_open():
            messages.add_message(self.request, messages.ERROR, _('You can\'t '
                'cancel a vote in a closed election.'))
            return http.HttpResponseRedirect(election_url)

        vote = election.get_vote_for_voter(self.request.user)

        if not vote or not vote.is_direct:
            messages.add_message(self.request, messages.ERROR, _('You didn\'t '
                'participate in this election.'))
            return http.HttpResponseRedirect(election_url)

        vote.invalidated_at_date = timezone.now()
        vote.is_counted = False
        vote.save()

        context = get_base_email_context(self.request)
        context.update(dict(
            election=election,
            election_url=election_url,
            to=vote.voter,
            agora_url=reverse('agora-view',
                kwargs=dict(username=election.agora.creator.username,
                    agoraname=election.agora.name)),
        ))

        try:

# Path: agora_site/agora_core/templatetags/agora_utils.py
# @register.filter
# def get_delegate_in_agora(user, agora):
#     return user.get_profile().get_delegation_in_agora(agora).get_delegate()


# Path: agora_site/agora_core/views.py
import datetime
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.models import User
from django.conf.urls import patterns, url, include
from django.conf import settings
from django.core.urlresolvers import reverse
from django.core.mail import EmailMultiAlternatives, EmailMessage, send_mass_mail
from django.utils import simplejson as json
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext as _
from django.utils.translation import check_for_language
from django.utils import timezone
from django.shortcuts import redirect, get_object_or_404, render_to_response
from django.template.loader import render_to_string
from django.views.generic import TemplateView, ListView, CreateView, RedirectView
from django.views.generic.edit import UpdateView, FormView
from django.views.i18n import set_language as django_set_language
from django import http
from django.db import transaction
from actstream.actions import follow, unfollow, is_following
from actstream.models import (object_stream, election_stream, Action,
    user_stream, actor_stream)
from actstream.signals import action
from guardian.shortcuts import *
from haystack.query import EmptySearchQuerySet
from haystack.forms import ModelSearchForm, FacetedSearchForm
from haystack.query import SearchQuerySet
from haystack.views import SearchView as HaystackSearchView
from agora_site.agora_core.templatetags.agora_utils import get_delegate_in_agora
from agora_site.agora_core.models import Agora, Election, Profile, CastVote
from agora_site.agora_core.backends.fnmt import fnmt_data_from_pem
from agora_site.agora_core.forms import *
from agora_site.agora_core.tasks import *
from agora_site.misc.utils import *

            kwargs=dict(username=election.agora.creator.username,
                agoraname=election.agora.name, electionname=election.name))

        if not election.ballot_is_open():
            messages.add_message(self.request, messages.ERROR, _('You can\'t '
                'cancel a vote in a closed election.'))
            return http.HttpResponseRedirect(election_url)

        vote = election.get_vote_for_voter(self.request.user)

        if not vote or not vote.is_direct:
            messages.add_message(self.request, messages.ERROR, _('You didn\'t '
                'participate in this election.'))
            return http.HttpResponseRedirect(election_url)

        vote.invalidated_at_date = timezone.now()
        vote.is_counted = False
        vote.save()

        context = get_base_email_context(self.request)
        context.update(dict(
            election=election,
            election_url=election_url,
            to=vote.voter,
            agora_url=reverse('agora-view',
                kwargs=dict(username=election.agora.creator.username,
                    agoraname=election.agora.name)),
        ))

        try:

            context['delegate'] = get_delegate_in_agora(vote.voter, election.agora)