def ref_post(self, post_id): ret_str = '' logging.info('ref_post called with id: ' + post_id) try: post = self.client.post(post_id).data ref = lib.referee.Referee(post) decision = ref.referee_decision if decision is not None: # post here try: new_comment = buzz.Comment(content=decision, post_id=post_id) self.client.create_comment(new_comment) # cache this decision pid = str(post.id[25:]) if DecisionCache.get_by_key_name(pid) is None: new_decision = DecisionCache(key_name=pid) new_decision.winner_id = str(ref.winner.id) new_decision.put() ret_str = "Writing comment: " + decision + "<br />" + \ "to post.id = " + post.id except: ret_str = "<h2>Error:</h2><br />" + "Writing comment: " + \ decision + "<br />" + "to post.id = " + post.id + \ "<br />" + "<pre>" + traceback.format_exc() + "</pre>" except: ret_str = "<h2>Error:</h2><br />" + "Getting post: " + post_id + \ "<br />" + "<pre>" + traceback.format_exc() + "</pre>" logging.info('returning: ' + ret_str) return ret_str
def referee_decision(self): if not hasattr(self, '_ref_decision') or not self._ref_decision: attributes = lib.post_attributes.PostAttributes(self.post) comments = self.post.comments() comments_list = [] for comment in comments: attributes.update_attributes(comment) comments_list.append(comment) if len(comments_list) > 0: attributes.set('last', comments_list[-1].actor.id) else: attributes.set('last', self.post.actor.id) attributes.finalize_attributes() winner_id = random.choice(attributes.commenters.keys()) self.winner = attributes.commenters_o[winner_id] self._ref_decision = self.build_referee_decision(attributes) if self._ref_decision == '' or self._ref_decision == None: self._ref_decision = None # cache this post as already decided on pid = str(self.post.id[25:]) if DecisionCache.get_by_key_name(pid) is None: new_decision = DecisionCache(key_name=pid) new_decision.put() return self._ref_decision