# Path: memopol/mps/models.py
# class MP(Representative):
#     active = models.BooleanField()
#     birth_department = models.CharField(max_length=255)
#     an_id = models.IntegerField()
#     an_speeches = models.URLField()
#     an_debates = models.URLField(null=True)
#     an_commissions = models.URLField()
#     an_reports = models.URLField()
#     an_questions = models.URLField()
#     an_propositions = models.URLField()
#     an_webpage = models.URLField()
#     functions = models.ManyToManyField(Function, through='FunctionMP')
#     profession = models.CharField(max_length=255, null=True)
#     department = models.ForeignKey(Department)
#     circonscription = models.ForeignKey(Circonscription, null=True)
#     group = models.ForeignKey(Group)
#     group_role = models.CharField(max_length=63, null=True)
#     hemicycle_sit = models.IntegerField()
# 
#     def get_absolute_url(self):
#         return reverse('mps:mp', args=(self.id,))
# 
#     @property
#     def scores(self):
#         return self.score_set.all()
# 
#     def total_score(self):
#         return self.score_set.aggregate(Avg('value'))['value__avg']
# 
#     @reify
#     def optimized_phones(self):
#         phones = []
#         for i in self.address:
#             for p in i.phones:
#                 phones.append(p.number)
#         return phones
# 
#     @reify
#     def phones(self):
#         values = []
#         for addr in self.address_set.all():
#             values.extend(
#                 [p.number for p in addr.phone_set.filter(type='phone')]
#               )
#         return values


from django.views.generic import DetailView
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from memopol.votes.models import Proposal
from memopol.mps.models import MP
from memopol.mps.views import optimize_mp_query



class VoteRecommendation(DetailView):
    template_name='mps/recommendation_detail.html'
    redirect="mps:votes:recommendation"

    def get_context_data(self, *args, **kwargs):
        context = super(VoteRecommendation, self).get_context_data(**kwargs)
        context['choice_listing'] = True
        context['proposal'] = self.object.proposal
        self.redirect_args = [self.object.proposal.id, self.object.id]
        return context

    def render_to_response(self, context):
        if self.kwargs["proposal_id"] != self.object.proposal.id:
            return HttpResponseRedirect(reverse(self.redirect, args=self.redirect_args))
        return DetailView.render_to_response(self, context)


class VoteRecommendationChoice(VoteRecommendation):
    template_name='mps/mp_list.html'
    redirect="mps:votes:recommendation_choice"

    def get_context_data(self, *args, **kwargs):
        context = super(VoteRecommendationChoice, self).get_context_data(**kwargs)
        context['choice'] = self.kwargs['recommendation']

# Path: memopol/mps/models.py
# class MP(Representative):
#     active = models.BooleanField()
#     birth_department = models.CharField(max_length=255)
#     an_id = models.IntegerField()
#     an_speeches = models.URLField()
#     an_debates = models.URLField(null=True)
#     an_commissions = models.URLField()
#     an_reports = models.URLField()
#     an_questions = models.URLField()
#     an_propositions = models.URLField()
#     an_webpage = models.URLField()
#     functions = models.ManyToManyField(Function, through='FunctionMP')
#     profession = models.CharField(max_length=255, null=True)
#     department = models.ForeignKey(Department)
#     circonscription = models.ForeignKey(Circonscription, null=True)
#     group = models.ForeignKey(Group)
#     group_role = models.CharField(max_length=63, null=True)
#     hemicycle_sit = models.IntegerField()
# 
#     def get_absolute_url(self):
#         return reverse('mps:mp', args=(self.id,))
# 
#     @property
#     def scores(self):
#         return self.score_set.all()
# 
#     def total_score(self):
#         return self.score_set.aggregate(Avg('value'))['value__avg']
# 
#     @reify
#     def optimized_phones(self):
#         phones = []
#         for i in self.address:
#             for p in i.phones:
#                 phones.append(p.number)
#         return phones
# 
#     @reify
#     def phones(self):
#         values = []
#         for addr in self.address_set.all():
#             values.extend(
#                 [p.number for p in addr.phone_set.filter(type='phone')]
#               )
#         return values


# Path: memopol/mps_votes/views.py
from django.views.generic import DetailView
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from memopol.votes.models import Proposal
from memopol.mps.models import MP
from memopol.mps.views import optimize_mp_query




class VoteRecommendation(DetailView):
    template_name='mps/recommendation_detail.html'
    redirect="mps:votes:recommendation"

    def get_context_data(self, *args, **kwargs):
        context = super(VoteRecommendation, self).get_context_data(**kwargs)
        context['choice_listing'] = True
        context['proposal'] = self.object.proposal
        self.redirect_args = [self.object.proposal.id, self.object.id]
        return context

    def render_to_response(self, context):
        if self.kwargs["proposal_id"] != self.object.proposal.id:
            return HttpResponseRedirect(reverse(self.redirect, args=self.redirect_args))
        return DetailView.render_to_response(self, context)


class VoteRecommendationChoice(VoteRecommendation):
    template_name='mps/mp_list.html'
    redirect="mps:votes:recommendation_choice"

    def get_context_data(self, *args, **kwargs):
        context = super(VoteRecommendationChoice, self).get_context_data(**kwargs)
        context['choice'] = self.kwargs['recommendation']

        context['mps'] = optimize_mp_query(MP.objects.filter(vote__recommendation=self.object,