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 = {} for subvote in recommendations: if not subvote.subject in subvotes: subvotes[subvote.subject] = [] subvotes[subvote.subject].append(subvote) # Retrieve all recommendation choices positions = Vote.objects.filter(recommendation=subvote).select_related("representative").values('representative_id', 'choice') subvotes_per_mep[subvote.pk] = dict((p["representative_id"], p['choice']) for p in positions) # Link positions to representatives representatives_data = {} for mep in context["vote"].meps: positions = [] for subvote in recommendations: try: position = subvotes_per_mep[subvote.pk][mep.pk] except KeyError: # No proposition for this mep, don't break the order # so use a None value position = None finally: positions.append((position, subvote.recommendation)) representatives_data[mep.pk] = (mep, positions) context.update({ "representatives_data": sorted(representatives_data.values(), key=lambda x: stripdiacritics(x[0].last_name).lower()), "subvotes": subvotes, }) return context
def prepare_last_name(self, obj): return [stripdiacritics(obj.last_name), ]
def prepare_fulltext(self, obj): return [stripdiacritics(obj.content()), obj.content()]