Ejemplo n.º 1
0
  def update(votings=None):
    tmp = StringIO.StringIO() # mb we can zip it, memcache allows only 1MB limit
    writer = csv.writer(tmp,dialect=VotesDialect)
    query = WineryRequest.all().order("-timestamp");
    vote_counts = {}
    if votings == None:
      votings = Votings.get_votings()    
    # first pass, we gather vote_counts info
    # and write votes.csv
    
    # head row for votes
    head_row = ("Timestamp","Name","City","State","IP")
    writer.writerow(head_row)
    votings.content_votes_head = tmp.getvalue() # add heading
    tmp.seek(0) # reset string IO
    
    for vote in query:
      item = WS.get_by_id(vote.hash_id)
      if item: # tuple (name,city,state)
        row = (vote.timestamp.strftime(TIME_FORMAT),)+item+(vote.ip,)
        writer.writerow(row)
        # gathering vote counts
        vote_counts[item] = vote_counts.get(item,0)+1
      
    

    tmp.flush()
    votings.content_votes = tmp.getvalue() # votes.csv
    
    # second pass
    # writing vote_counts.csv
    
    # storing vote counts dict in memcache
    # with that dict stored in memcache we will not redo full update again
    # instead of full update() we will update by Votings.regenerate... functions
    Votings.set_vote_counts_dict(vote_counts)
    
    vote_counts_results = sorted(vote_counts.items(), key=itemgetter(1),reverse=True)
    tmp = StringIO.StringIO() # mb we can zip it, memcache allows only 1MB limit
    writer = csv.writer(tmp,dialect=VotesDialect)
    
    # head row for vote counts
    head_row = ("Total votes count","Name","City","State")
    writer.writerow(head_row)
    votings.content_vote_counts_head = tmp.getvalue() # add heading
    tmp.seek(0) # reset string IO
        
    for (item,count) in vote_counts_results:
      writer.writerow((count,)+item)
      
    tmp.flush()
    
    votings.content_vote_counts = tmp.getvalue() # votes.csv
    votings.put()