コード例 #1
0
    def post(self):
        quote_uuid = self.request.get(self.http_post_key)
        current_vote_count = tlq_util.get_from_cache("%s%s" % (self.cache_uuid_prefix, quote_uuid), 0)
        # its possible that this memcache entry expired, we should check the original list for a rating
        if not current_vote_count:
            cached_quote = tlq_util.get_cache_quote(quote_uuid)
            if cached_quote is None:
                logging.warning("Someone just tried voting on a quote that isn't in the cache.")
                return
            else:
                if cached_quote["rating"] != current_vote_count:
                    logging.info("Apparently the voting key expired.  Good thing the list still existed.")
                    current_vote_count = cached_quote["rating"]

        new_rating = 0

        if self.request.get(self.vote_up_or_down_key) == "up":
            logging.info("Setting vote count to %s." % (current_vote_count + 1))
            new_rating = current_vote_count + 1
        elif self.request.get(self.vote_up_or_down_key) == "down":
            logging.info("Setting vote count to %s." % (current_vote_count - 1))
            new_rating = current_vote_count - 1
            memcache.set("%s%s" % (self.cache_uuid_prefix, quote_uuid), current_vote_count - 1)
        else:
            logging.info("Thumbs up / down not specified correctly.")

        if new_rating <= 0:
            new_rating = 0

        memcache.set("%s%s" % (self.cache_uuid_prefix, quote_uuid), new_rating)
        tlq_util.update_rating_in_memcache(quote_uuid, new_rating)

        tlq_util.write_out_json(self.response, True)
コード例 #2
0
 def get(self):
     if self.request.get(self.remove_item_key) != "":
         memcache.delete(self.request.get(self.remove_item_key))
     elif self.request.get(self.return_item_key) != "":
         tlq_util.write_out_json(
             self.response, tlq_util.get_from_cache(self.request.get(self.return_item_key), None)
         )
     elif self.request.get(self.persist_quotes_key) != "":
         tlq_util.store_quotes_in_datastore()
         tlq_util.write_out_json(self.response, {"result": "success"})
     elif self.request.get(self.generate_fake_quotes_key) != "":
         tlq_util.generate_fake_quotes()
     else:
         logging.info("GET request handling entire cache clearing.")
         quotes = tlq_util.get_all_quotes()
         if len(quotes) > self.maximum_quotes_before_reset or self.request.get(self.force_clear_key) == "true":
             memcache.flush_all()
         tlq_util.write_out_json(self.response, True)