コード例 #1
0
ファイル: models.py プロジェクト: temp123/Open-Knesset
 def get_mks_values(self):
     mks_values = cache.get('agendas_mks_values')
     if not mks_values:
         q = queries.agendas_mks_grade()
         # outer join - add missing mks to agendas
         newAgendaMkVotes = {}
         # generates a set of all the current mk ids that have ever voted for any agenda
         # its not perfect, but its better than creating another query to generate all known mkids
         allMkIds = set(map(itemgetter(0), chain.from_iterable(q.values())))
         for agendaId, agendaVotes in q.items():
             # the newdict will have 0's for each mkid, the update will change the value for known mks
             newDict = {}.fromkeys(allMkIds, (0, 0, 0))
             newDict.update(
                 dict(
                     map(
                         lambda (mkid, score, volume, numvotes):
                         (mkid, (score, volume, numvotes)), agendaVotes)))
             newAgendaMkVotes[agendaId] = newDict.items()
         mks_values = {}
         for agenda_id, scores in newAgendaMkVotes.items():
             mks_values[agenda_id] = \
                 map(lambda x: (x[1][0], dict(score=x[1][1][0], rank=x[0], volume=x[1][1][1], numvotes=x[1][1][2])),
                     enumerate(sorted(scores,key=lambda x:x[1][0],reverse=True), 1))
         cache.set('agendas_mks_values', mks_values, 1800)
     return mks_values
コード例 #2
0
ファイル: models.py プロジェクト: pninaelias/Open-Knesset
 def get_mks_values(self):
     mks_values = cache.get('agendas_mks_values')
     if not mks_values:
         q = queries.agendas_mks_grade()
         # outer join - add missing mks to agendas
         newAgendaMkVotes = {}
         # generates a set of all the current mk ids that have ever voted for any agenda
         # its not perfect, but its better than creating another query to generate all known mkids
         allMkIds = set(map(itemgetter(0),chain.from_iterable(q.values())))
         for agendaId,agendaVotes in q.items():
             # the newdict will have 0's for each mkid, the update will change the value for known mks
             newDict = {}.fromkeys(allMkIds,(0,0,0))
             newDict.update(dict(map(lambda (mkid,score,volume,numvotes):(mkid,(score,volume,numvotes)),agendaVotes)))
             newAgendaMkVotes[agendaId]=newDict.items()
         mks_values = {}
         for agenda_id, scores in newAgendaMkVotes.items():
             mks_values[agenda_id] = \
                 map(lambda x: (x[1][0], dict(score=x[1][1][0], rank=x[0], volume=x[1][1][1], numvotes=x[1][1][2])),
                     enumerate(sorted(scores,key=lambda x:x[1][0],reverse=True), 1))
         cache.set('agendas_mks_values', mks_values, 1800)
     return mks_values
コード例 #3
0
ファイル: models.py プロジェクト: elazarg/Open-Knesset
 def get_mks_values(self):
     mks_values = cache.get('agendas_mks_values')
     if not mks_values:
         q = queries.agendas_mks_grade()
         # outer join - add missing mks to agendas
         # generates a set of all the current mk ids that have ever voted for any agenda
         # its not perfect, but its better than creating another query to generate all known mkids
         all_mks = {x[0] for x in chain.from_iterable(q.values())}
         mks_values = {}
         for agendaId, agendaVotes in q.items():
             # the newdict will have 0's for each mkid, the update will change the value for known mks
             agenda_mks = set()
             res=[]
             for mkid, score, volume, numvotes in agendaVotes:
                 agenda_mks.add(mkid)
                 res.append( (mkid,(score, volume, numvotes)) )
             res.sort(key=lambda x:x[1][0], reverse=True)
             
             scores = enumerate(chain(res, ((mkid, (0,0,0)) for mkid in all_mks-agenda_mks)), 1)
             mks_values[agendaId] = [(mkid, {'rank':rank, 'score':score, 'volume':volume, 'numvotes':numvotes})
                                      for rank, (mkid, (score, volume, numvotes)) in  scores]
         cache.set('agendas_mks_values', mks_values, 1800)
     return mks_values