def get_post(self, question_id, bypass_cache = False): """ Return a post object representing the question and the answers list Return None if question/answers are not found from Api call or db cache (deleted questions) """ try: question = self.get_question(question_id) if question: post = Post(question, self.get_answers(question_id, int(question['up_vote_count'])-int(question['down_vote_count']) > VOTES_ENTRY_LEVEL)) else: #StackPrinter loves the legendary deleted questions post = Post(dbquestion.get_question(question_id, self.service), dbquestion.get_answers(question_id, self.service)) post.deleted = True except (sepy.ApiRequestError, urlfetch.DownloadError): post = Post(dbquestion.get_question(question_id, self.service), dbquestion.get_answers(question_id, self.service)) if not post.is_printable(): raise if post.is_printable(): try: deferred.defer(worker.deferred_store_print_statistics, post.question['question_id'], self.service, post.question['title'], post.question['tags'], post.deleted) except: logging.info("%s - defer error trying to store print statistics : %s" % (self.service, question_id)) return post else: return None
def test_answers_save_and_get_from_datastore(self): answers = ['dict1', 'dict2', 'dict3', 'dict4'] question_id = 1 service = 'foo' deferred_store_answers_to_cache(question_id, service, answers) answers_from_cache = dbquestion.get_answers(question_id, service) self.assertEquals(answers, answers_from_cache) #636 answers answers = self.spdownloader.get_answers(58640) deferred_store_answers_to_cache(58640, 'stackoverflow', answers) answers_from_cache = dbquestion.get_answers(58640, 'stackoverflow') self.assertEquals(answers, answers_from_cache) #148 answers answers = self.spdownloader.get_answers(101268) deferred_store_answers_to_cache(101268, 'stackoverflow', answers) answers_from_cache = dbquestion.get_answers(101268, 'stackoverflow') self.assertEquals(answers, answers_from_cache) #0 answers answers = self.spdownloader.get_answers(3940165) deferred_store_answers_to_cache(3940165, 'stackoverflow', answers) answers_from_cache = dbquestion.get_answers(3940165, 'stackoverflow') self.assertEquals(answers, answers_from_cache)