Ejemplo n.º 1
0
 def get_context(self, *args, **kwargs):
     context = super(AgendaListView, self).get_context(*args, **kwargs)
     # optimization - create query for votes per agenda
     # store in context as dictionary votes[agendaid]=<votenum> 
     agenda_votes_results = Agenda.objects.values("id").annotate(Count("votes"))
     agenda_votes = dict(map(lambda vote:(vote["id"],str(vote["votes__count"])),agenda_votes_results))
     allAgendaPartyVotes = cache.get('AllAgendaPartyVotes')
     if not allAgendaPartyVotes:
         allAgendaPartyVotes = getAllAgendaPartyVotes()
         cache.set('AllAgendaPartyVotes',allAgendaPartyVotes,1800)
     parties_lookup = dict(map(lambda party:(party.id,party.name),Party.objects.all()))
     if self.request.user.is_authenticated():
         p = self.request.user.get_profile()
         watched = p.agendas
     else:
         watched = None
     agendaEditorIds = getAgendaEditorIds()
     allEditorIds = list(set(chain.from_iterable(agendaEditorIds.values())))
     editors = User.objects.filter(id__in=allEditorIds)
     context['agenda_editors'] = agendaEditorIds
     context['editors'] = dict(map(lambda obj:(obj.id,obj),editors))
     context['watched'] = watched
     context['agenda_votes']=agenda_votes
     context['agenda_party_values']=allAgendaPartyVotes
     context['parties_lookup']=parties_lookup
     return context
Ejemplo n.º 2
0
    def get_context(self, *args, **kwargs):
        context = super(AgendaListView, self).get_context(*args, **kwargs)
        # optimization - create query for votes per agenda
        # store in context as dictionary votes[agendaid]=<votenum>
        agenda_votes_results = Agenda.objects.values("id").annotate(Count("votes"))
        agenda_votes = dict(map(lambda vote:(vote["id"],str(vote["votes__count"])),agenda_votes_results))
        allAgendaPartyVotes = cache.get('AllAgendaPartyVotes')

        if not allAgendaPartyVotes:
            allAgendaPartyVotes = queries.getAllAgendaPartyVotes()
            cache.set('AllAgendaPartyVotes',allAgendaPartyVotes,1800)

        parties_lookup = dict(map(lambda party:(party.id,party.name),Party.objects.all()))
        if self.request.user.is_authenticated():
            p = self.request.user.get_profile()
            watched = p.agendas
        else:
            watched = None
        agendaEditorIds = queries.getAgendaEditorIds()
        allEditorIds = list(set(chain.from_iterable(agendaEditorIds.values())))
        editors = User.objects.filter(id__in=allEditorIds)
        context['agenda_editors'] = agendaEditorIds
        context['editors'] = dict(map(lambda obj:(obj.id,obj),editors))
        context['watched'] = watched
        context['agenda_votes']=agenda_votes
        context['agenda_party_values']=allAgendaPartyVotes
        context['parties_lookup']=parties_lookup
        return context
Ejemplo n.º 3
0
    def get_context(self, *args, **kwargs):
        context = super(AgendaListView, self).get_context(*args, **kwargs)
        # optimization - create query for votes per agenda
        # store in context as dictionary votes[agendaid]=<votenum>
        agenda_votes_results = Agenda.objects.values("id").annotate(Count("votes"))
        agenda_votes = dict(map(lambda vote: (vote["id"], str(vote["votes__count"])), agenda_votes_results))
        parties_lookup = {party.id: party.name for party in Party.current_knesset.all()}

        allAgendaPartyVotes = cache.get("AllAgendaPartyVotes")
        if not allAgendaPartyVotes:
            # filtering for current knesset is done here

            allAgendaPartyVotes = queries.getAllAgendaPartyVotes()

            for agenda_id, party_votes in allAgendaPartyVotes.iteritems():
                allAgendaPartyVotes[agenda_id] = [x for x in party_votes if x[0] in parties_lookup]

            cache.set("AllAgendaPartyVotes", allAgendaPartyVotes, 1800)

        if self.request.user.is_authenticated():
            p = self.request.user.get_profile()
            watched = p.agendas
        else:
            watched = None
        agendaEditorIds = queries.getAgendaEditorIds()
        allEditorIds = list(set(chain.from_iterable(agendaEditorIds.values())))
        editors = User.objects.filter(id__in=allEditorIds)
        context["agenda_editors"] = agendaEditorIds
        context["editors"] = dict(map(lambda obj: (obj.id, obj), editors))
        context["watched"] = watched
        context["agenda_votes"] = agenda_votes
        context["agenda_party_values"] = allAgendaPartyVotes
        context["parties_lookup"] = parties_lookup
        return context
Ejemplo n.º 4
0
    def get_context_data(self, *args, **kwargs):
        context = super(AgendaDetailView, self).get_context_data(*args, **kwargs)
        agenda = context['object']
        try:
            context['title'] = "%s" % agenda.name
        except AttributeError:
            context['title'] = _('None')

        if self.request.user.is_authenticated():
            p = self.request.user.get_profile()
            watched = agenda in p.agendas
            watched_members = self.request.user.get_profile().members
        else:
            watched = False
            watched_members = False
        context.update({'watched_object': watched})
        context['watched_members'] = watched_members

        all_mks = 'all_mks' in self.request.GET.keys()
        allAgendaMkVotes = cache.get('AllAgendaMkVotes')
        if not allAgendaMkVotes:
            allAgendaMkVotes = getAllAgendaMkVotes()
            cache.set('AllAgendaMkVotes',allAgendaMkVotes,1800)
        if agenda.id not in allAgendaMkVotes:
            allAgendaMkVotes = getAllAgendaMkVotes()
            cache.set('AllAgendaMkVotes',allAgendaMkVotes,1800)
        context['agenda_mk_values']=dict(allAgendaMkVotes.setdefault(agenda.id,[]))
        if all_mks:
            context['all_mks_ids']=map(itemgetter(0),sorted(allAgendaMkVotes[agenda.id],key=itemgetter(1),reverse=True)[:200])
            context['all_mks']=True
        else:
            context['mks_top']=map(itemgetter(0),sorted(allAgendaMkVotes[agenda.id],key=itemgetter(1),reverse=True)[:5])
            context['mks_bottom']=map(itemgetter(0),sorted(sorted(allAgendaMkVotes[agenda.id],key=itemgetter(1),reverse=False)[:5],key=itemgetter(1),reverse=True))

        allAgendaPartyVotes = cache.get('AllAgendaPartyVotes')
        if not allAgendaPartyVotes:
            allAgendaPartyVotes = getAllAgendaPartyVotes()
            cache.set('AllAgendaPartyVotes',allAgendaPartyVotes,1800)
        context['agenda_party_values']=dict(allAgendaPartyVotes.setdefault(agenda.id,[]))
        context['agendaTopParties']=map(itemgetter(0),sorted(allAgendaPartyVotes[agenda.id],key=itemgetter(1),reverse=True)[:20])

        cached_context = cache.get('agenda_votes_%d' % agenda.id)
        if not cached_context:
            agenda_votes = agenda.agendavotes.order_by('-vote__time')\
                                             .select_related('vote')
            cached_context = {'agenda_votes': agenda_votes }
            cache.set('agenda_votes_%d' % agenda.id, cached_context, 900)
        context.update(cached_context)

        # Optimization: get all parties and members before rendering
        # Further possible optimization: only bring parties/members needed for rendering
        parties_objects = Party.objects.all()
        partiesDict = dict(map(lambda party:(party.id,party),parties_objects))
        context['parties']=partiesDict

        member_objects = Member.objects.all()
        membersDict = dict(map(lambda mk:(mk.id,mk),member_objects))
        context['members']=membersDict
        return context
Ejemplo n.º 5
0
    def get_context_data(self, *args, **kwargs):
        context = super(AgendaDetailView, self).get_context_data(*args, **kwargs)
        agenda = context['object']
        try:
            context['title'] = "%s" % agenda.name
        except AttributeError:
            context['title'] = _('None')

        if self.request.user.is_authenticated():
            p = self.request.user.get_profile()
            watched = agenda in p.agendas
            watched_members = self.request.user.get_profile().members
        else:
            watched = False
            watched_members = False
        context.update({'watched_object': watched})
        context['watched_members'] = watched_members

        all_mks = 'all_mks' in self.request.GET.keys()
        mks_values = agenda.get_mks_values()
        context['agenda_mk_values'] = dict(mks_values)
        cmp_rank = lambda x,y: x[1]['rank']-y[1]['rank']
        if all_mks:
            context['all_mks_ids'] = map(itemgetter(0),mks_values[:200])
            context['all_mks'] = True
        else:
            context['mks_top'] = map(itemgetter(0),mks_values[:5])
            context['mks_bottom'] = map(itemgetter(0),mks_values[-5:])

        allAgendaPartyVotes = cache.get('AllAgendaPartyVotes')
        if not allAgendaPartyVotes:
            allAgendaPartyVotes = getAllAgendaPartyVotes()
            cache.set('AllAgendaPartyVotes',allAgendaPartyVotes,1800)
        context['agenda_party_values']=dict(allAgendaPartyVotes.setdefault(agenda.id,[]))
        context['agendaTopParties']=map(itemgetter(0),sorted(allAgendaPartyVotes[agenda.id],key=itemgetter(1),reverse=True)[:20])

        cached_context = cache.get('agenda_votes_%d' % agenda.id)
        if not cached_context:
            agenda_votes = agenda.agendavotes.order_by('-vote__time')\
                                             .select_related('vote')
            cached_context = {'agenda_votes': agenda_votes }
            cache.set('agenda_votes_%d' % agenda.id, cached_context, 900)
        context.update(cached_context)

        # Optimization: get all parties and members before rendering
        # Further possible optimization: only bring parties/members needed for rendering
        parties_objects = Party.objects.all()
        partiesDict = dict(map(lambda party:(party.id,party),parties_objects))
        context['parties']=partiesDict

        member_objects = Member.objects.all()
        membersDict = dict(map(lambda mk:(mk.id,mk),member_objects))
        context['members']=membersDict
        return context
Ejemplo n.º 6
0
    def get_context(self, *args, **kwargs):
        context = super(AgendaListView, self).get_context(*args, **kwargs)
	# optimization - create query for votes per agenda
	# store in context as dictionary votes[agendaid]=<votenum> 
	agenda_votes_results = Agenda.objects.values("id").annotate(Count("votes"))
	agenda_votes = dict(map(lambda vote:(vote["id"],str(vote["votes__count"])),agenda_votes_results))
	allAgendaPartyVotes = getAllAgendaPartyVotes()
	parties_lookup = dict(map(lambda party:(party.id,party.name),Party.objects.all()))
        if self.request.user.is_authenticated():
            p = self.request.user.get_profile()
            watched = p.agendas
        else:
            watched = None
        context['watched'] = watched
	context['agenda_votes']=agenda_votes
	context['agenda_party_values']=allAgendaPartyVotes
	context['parties_lookup']=parties_lookup
        return context
Ejemplo n.º 7
0
 def get_context(self, *args, **kwargs):
     context = super(AgendaListView, self).get_context(*args, **kwargs)
     # optimization - create query for votes per agenda
     # store in context as dictionary votes[agendaid]=<votenum>
     agenda_votes_results = Agenda.objects.values("id").annotate(
         Count("votes"))
     agenda_votes = dict(
         map(lambda vote: (vote["id"], str(vote["votes__count"])),
             agenda_votes_results))
     allAgendaPartyVotes = getAllAgendaPartyVotes()
     parties_lookup = dict(
         map(lambda party: (party.id, party.name), Party.objects.all()))
     if self.request.user.is_authenticated():
         p = self.request.user.get_profile()
         watched = p.agendas
     else:
         watched = None
     context['watched'] = watched
     context['agenda_votes'] = agenda_votes
     context['agenda_party_values'] = allAgendaPartyVotes
     context['parties_lookup'] = parties_lookup
     return context
Ejemplo n.º 8
0
 def get_all_party_values(self):
     return queries.getAllAgendaPartyVotes()
Ejemplo n.º 9
0
 def get_all_party_values(self):
     allAgendaPartyVotes = cache.get('AllAgendaPartyVotes')
     if not allAgendaPartyVotes:
         allAgendaPartyVotes = queries.getAllAgendaPartyVotes()
         cache.set('AllAgendaPartyVotes',allAgendaPartyVotes,1800)
     return allAgendaPartyVotes
Ejemplo n.º 10
0
 def get_all_party_values(self):
     return queries.getAllAgendaPartyVotes()
Ejemplo n.º 11
0
 def get_all_party_values(self):
     allAgendaPartyVotes = cache.get('AllAgendaPartyVotes')
     if not allAgendaPartyVotes:
         allAgendaPartyVotes = queries.getAllAgendaPartyVotes()
         cache.set('AllAgendaPartyVotes', allAgendaPartyVotes, 1800)
     return allAgendaPartyVotes