# Path: memopol/meps/models.py
# class MEP(Representative):
#     active = models.BooleanField()
#     ep_id = models.IntegerField(unique=True)
#     ep_opinions = models.URLField()
#     ep_debates = models.URLField()
#     ep_questions = models.URLField()
#     ep_declarations = models.URLField()
#     ep_reports = models.URLField()
#     ep_motions = models.URLField()
#     ep_webpage = models.URLField()
#     bxl_building = models.ForeignKey(Building, related_name="bxl_building", null=True)
#     bxl_floor = models.CharField(max_length=255, null=True)
#     bxl_office_number = models.CharField(max_length=255, null=True)
#     bxl_fax = models.CharField(max_length=255, null=True)
#     bxl_phone1 = models.CharField(max_length=255, null=True)
#     bxl_phone2 = models.CharField(max_length=255, null=True)
#     stg_building = models.ForeignKey(Building, related_name="stg_building", null=True)
#     stg_floor = models.CharField(max_length=255, null=True)
#     stg_office_number = models.CharField(max_length=255, null=True)
#     stg_fax = models.CharField(max_length=255, null=True)
#     stg_phone1 = models.CharField(max_length=255, null=True)
#     stg_phone2 = models.CharField(max_length=255, null=True)
#     groups = models.ManyToManyField(Group, through='GroupMEP')
#     countries = models.ManyToManyField(Country, through='CountryMEP')
#     delegations = models.ManyToManyField(Delegation, through='DelegationRole')
#     committees = models.ManyToManyField(Committee, through='CommitteeRole')
#     organizations = models.ManyToManyField(Organization, through='OrganizationMEP')
#     position = models.IntegerField(default=None, null=True)
#     total_score = models.FloatField(default=None, null=True)
# 
#     def age(self):
#         if date.today().month > self.birth_date.month:
#             return date.today().year - self.birth_date.year
#         elif date.today().month == self.birth_date.month and date.today().day > self.birth_date.day:
#             return date.today().year - self.birth_date.year
#         else:
#             return date.today().year - self.birth_date.year + 1
# 
#     @reify
#     def get_ep_webpage(self):
#         if self.active and self.ep_webpage:
#             return self.ep_webpage
#         return None
# 
#     def get_absolute_url(self):
#         return reverse('meps:mep', args=(self.id,))
# 
#     @reify
#     def bxl_office(self):
#         return self.bxl_floor + self.bxl_office_number
# 
#     @reify
#     def stg_office(self):
#         return self.stg_floor + self.stg_office_number
# 
#     @reify
#     def groups_mep(self):
#         return self.groupmep_set.select_related('group')
# 
#     @reify
#     def group(self):
#         try:
#             return self.groups_mep.latest('end').group
#         except GroupMEP.DoesNotExist:
#             return None
# 
#     @reify
#     def country(self):
#         return self.countries_mep.latest('end').country
# 
#     @reify
#     def party(self):
#         return self.countries_mep.latest('end').party
# 
#     @reify
#     def countries_mep(self):
#         return self.countrymep_set.select_related('country', 'party')
# 
#     @reify
#     def delegations_roles(self):
#         return self.delegationrole_set.select_related('delegation')
# 
#     @reify
#     def committees_roles(self):
#         return self.committeerole_set.select_related('committee')
# 
#     @reify
#     def organizations_mep(self):
#         return self.organizationmep_set.select_related('organization')
# 
#     @reify
#     def assistants_mep(self):
#         return self.assistantmep_set.select_related('assistant')
# 
#     @property
#     def score_color(self):
#         return "rgb(%s, %s, %s)" % color(self.total_score)
# 
#     @snippet(template='meps/snippets/country.html')
#     def country_tag(self):
#         return dict(country=self.country)
# 
#     @snippet(template='meps/snippets/party.html')
#     def party_tag(self):
#         return dict(party=self.party)
# 
#     @reify
#     def important_posts(self):
#         all_roles = list(self.organizations_mep.only_current())
#         for i in (self.groups_mep.only_current().exclude(role="").exclude(role="Member").exclude(role="Substitute"),
#                   self.committees_roles.only_current()):
#             roles = i.filter(mep=self)
#             if roles:
#                 all_roles += list(roles)
#         return all_roles
# 
#     class Meta:
#         ordering = ['last_name']


from django.db.models import Q
from django.views.generic import DetailView
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from memopol.meps.models import MEP
from memopol.votes.models import Proposal, Vote
from memopol.meps.views import optimise_mep_query
from memopol.base.utils import stripdiacritics
# -*- coding: utf-8 -*-




class ProposalView(DetailView):
    model = Proposal
    context_object_name = "vote"
    template_name = "meps/proposal_detail.html"

    def get_context_data(self, *args, **kwargs):
        context = super(ProposalView, self).get_context_data(**kwargs)
        recommendations = self.object.recommendation_set.all()
        context['vote'].meps = optimise_mep_query(context['vote'].meps, proposal_score=context['vote'])

        # Group subvotes by subject
        subvotes = {}

# Path: memopol/meps/models.py
# class MEP(Representative):
#     active = models.BooleanField()
#     ep_id = models.IntegerField(unique=True)
#     ep_opinions = models.URLField()
#     ep_debates = models.URLField()
#     ep_questions = models.URLField()
#     ep_declarations = models.URLField()
#     ep_reports = models.URLField()
#     ep_motions = models.URLField()
#     ep_webpage = models.URLField()
#     bxl_building = models.ForeignKey(Building, related_name="bxl_building", null=True)
#     bxl_floor = models.CharField(max_length=255, null=True)
#     bxl_office_number = models.CharField(max_length=255, null=True)
#     bxl_fax = models.CharField(max_length=255, null=True)
#     bxl_phone1 = models.CharField(max_length=255, null=True)
#     bxl_phone2 = models.CharField(max_length=255, null=True)
#     stg_building = models.ForeignKey(Building, related_name="stg_building", null=True)
#     stg_floor = models.CharField(max_length=255, null=True)
#     stg_office_number = models.CharField(max_length=255, null=True)
#     stg_fax = models.CharField(max_length=255, null=True)
#     stg_phone1 = models.CharField(max_length=255, null=True)
#     stg_phone2 = models.CharField(max_length=255, null=True)
#     groups = models.ManyToManyField(Group, through='GroupMEP')
#     countries = models.ManyToManyField(Country, through='CountryMEP')
#     delegations = models.ManyToManyField(Delegation, through='DelegationRole')
#     committees = models.ManyToManyField(Committee, through='CommitteeRole')
#     organizations = models.ManyToManyField(Organization, through='OrganizationMEP')
#     position = models.IntegerField(default=None, null=True)
#     total_score = models.FloatField(default=None, null=True)
# 
#     def age(self):
#         if date.today().month > self.birth_date.month:
#             return date.today().year - self.birth_date.year
#         elif date.today().month == self.birth_date.month and date.today().day > self.birth_date.day:
#             return date.today().year - self.birth_date.year
#         else:
#             return date.today().year - self.birth_date.year + 1
# 
#     @reify
#     def get_ep_webpage(self):
#         if self.active and self.ep_webpage:
#             return self.ep_webpage
#         return None
# 
#     def get_absolute_url(self):
#         return reverse('meps:mep', args=(self.id,))
# 
#     @reify
#     def bxl_office(self):
#         return self.bxl_floor + self.bxl_office_number
# 
#     @reify
#     def stg_office(self):
#         return self.stg_floor + self.stg_office_number
# 
#     @reify
#     def groups_mep(self):
#         return self.groupmep_set.select_related('group')
# 
#     @reify
#     def group(self):
#         try:
#             return self.groups_mep.latest('end').group
#         except GroupMEP.DoesNotExist:
#             return None
# 
#     @reify
#     def country(self):
#         return self.countries_mep.latest('end').country
# 
#     @reify
#     def party(self):
#         return self.countries_mep.latest('end').party
# 
#     @reify
#     def countries_mep(self):
#         return self.countrymep_set.select_related('country', 'party')
# 
#     @reify
#     def delegations_roles(self):
#         return self.delegationrole_set.select_related('delegation')
# 
#     @reify
#     def committees_roles(self):
#         return self.committeerole_set.select_related('committee')
# 
#     @reify
#     def organizations_mep(self):
#         return self.organizationmep_set.select_related('organization')
# 
#     @reify
#     def assistants_mep(self):
#         return self.assistantmep_set.select_related('assistant')
# 
#     @property
#     def score_color(self):
#         return "rgb(%s, %s, %s)" % color(self.total_score)
# 
#     @snippet(template='meps/snippets/country.html')
#     def country_tag(self):
#         return dict(country=self.country)
# 
#     @snippet(template='meps/snippets/party.html')
#     def party_tag(self):
#         return dict(party=self.party)
# 
#     @reify
#     def important_posts(self):
#         all_roles = list(self.organizations_mep.only_current())
#         for i in (self.groups_mep.only_current().exclude(role="").exclude(role="Member").exclude(role="Substitute"),
#                   self.committees_roles.only_current()):
#             roles = i.filter(mep=self)
#             if roles:
#                 all_roles += list(roles)
#         return all_roles
# 
#     class Meta:
#         ordering = ['last_name']


# Path: memopol/meps_votes/views.py
from django.db.models import Q
from django.views.generic import DetailView
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from memopol.meps.models import MEP
from memopol.votes.models import Proposal, Vote
from memopol.meps.views import optimise_mep_query
from memopol.base.utils import stripdiacritics

# -*- coding: utf-8 -*-




class ProposalView(DetailView):
    model = Proposal
    context_object_name = "vote"
    template_name = "meps/proposal_detail.html"

    def get_context_data(self, *args, **kwargs):
        context = super(ProposalView, self).get_context_data(**kwargs)
        recommendations = self.object.recommendation_set.all()
        context['vote'].meps = optimise_mep_query(context['vote'].meps, proposal_score=context['vote'])

        # Group subvotes by subject
        subvotes = {}

        subvotes_per_mep = {}