Esempio n. 1
0
 def getmethod(self):
   if self.getval("clear"):
     global WS
     del WS
     from app.wineriessearcher import WineriesSearcher
     WS = WineriesSearcher()
     WS.init()
   self.render("index.html", {
                              "q" : self.getval("q"),
                              "state" : self.getval("state"),
                              "numas" : self.getval("numas"),
                              })
Esempio n. 2
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()
Esempio n. 3
0
 def get(self):
   name_part = self.request.get("name")
   state_part = self.request.get("state")
   
   numas = self.request.get("numas")
   if numas == "all":
     numas = 9999
   else:
     try:
       numas = int(numas)
     except:
       numas = 100 # by default
   result = WS.search_by_name(name_part.lower(), state_part = state_part)[:numas] # limit to 'numas'
   self.response.headers["content-type"] = "text/json"
   self.response.out.write(simplejson.dumps(result))
Esempio n. 4
0
 def get(self):
   name = self.request.get("name",False)
   city = self.request.get("city",False)
   if name:
     # adding bookmark if it is not already added
     bookmarks = self.get_bookmarks()
     if bookmarks.has_key(name):
       # assuming all wineries name are unique
       pass # already there
     else:
       # ading it as bookmark
       object = WS.get_by_name(name)
       if object == False:
         logging.error("WARNING: no such winery name '%s', city: '%s'" % (name, city))
       else:
         # yes winery with 'name' exists in db, adding it
         # check for city
         if object[0] == city:
           bookmarks[name] = object # object = (city,state) tuple
           # also we need to save bookmarks
           self.set_bookmarks(bookmarks)
   self.response.out.write("ok") # success
Esempio n. 5
0
 def get(self):
   link = ""
   ids_param = self.getval("ids", False)
   prepared_bookmarks = []
   if ids_param:
     #log("HWA");
     # someone shared bookmarks with current user
     # we must extract them and save in cookie
     ids = ids_param.split("|")
     if ids:
       # that will be very long calculation
       # TODO: mb we should optimise it with memcache key 
       # for getting already prepared bookmarks by some key provided in url
       # and not to recalc (filter) them all again
       # for every user
       bookmarks = WS.get_by_ids(ids) # will be dict in fmt: dict[name]=(city,state)
       self.set_bookmarks(bookmarks)
       for name, citystate in bookmarks.items():
         prepared_bookmarks.append((name,)+citystate)
       link = "?ids="+ids_param        
   else:
     prepared_bookmarks =[]
     ids = []
     for name, citystate in self.get_bookmarks().items():
       obj = (name,)+citystate
       prepared_bookmarks.append(obj)
       ids.append(make_hash_id(obj))
     if ids:
       # making link-to-this-page
       # that allow user to quick-share their bookmarks
       link = "?ids=%s" % urllib.quote("|".join(ids))
     else:
       link = False
   
   self.render("bookmarks.html", {"bookmarks" : prepared_bookmarks,
                                  "link" : link})