def add_answer(self, question_id, source, author): question = Question.get_by_id(int(question_id)) if not question or not source: self.set_error_code(500) return #TODO: if answer is added by anonymouse, author should be set to None, # real_author is always set.(eg. question_relationship = get_relationshiop(question)) new_answer = Answer( parent = question.key, summary = source[0:100], question = question.key, author = author.key, real_author = author.key, content = source.replace("\n", "<br />"), source = source) new_answer.put() relation = AccountQuestionRelationService().get_relationship(author.key, question.key) relation.my_answer = new_answer.key relation.put() QuestionService().update_answer_num(question.key) return new_answer
def set_no_help(self, actor, question_id, answer_id): question = Question.get_by_id(int(question_id)) answer = Answer.get_by_id(parent = question.key, id = int(answer_id)) relation = AccountQuestionRelationService().get_relationship(actor.key, question.key) if not answer.key in relation.no_help_answers: relation.no_help_answers.append(answer.key) relation.put()
def remove_answer_from_question(self, question_id, answer_id): question = Question.get_by_id(int(question_id)) if not question: return answer = Answer.get_by_id(int(answer_id), parent = question.key) if not answer: return answer.key.delete() self.update_answer_num(question.key) return question
def voteup_answer(self, actor, question_id, answer_id): question = Question.get_by_id(int(question_id)) answer = Answer.get_by_id(parent = question.key, id = int(answer_id)) relation = AccountQuestionRelationService().get_relationship(actor.key, question.key) if answer.key in relation.up_voted_answers: relation.up_voted_answers = [ a for a in relation.up_voted_answers if a != answer.key ] answer.up_voted_users = [ u for u in answer.up_voted_users if u != actor.key ] else: relation.up_voted_answers.append(answer.key) answer.up_voted_users.append(actor.key) relation.down_voted_answers = [ a for a in relation.down_voted_answers if a != answer.key ] answer.down_voted_users = [ u for u in answer.down_voted_users if u != actor.key ] answer.put() relation.put()
def update_answer(self, question_id, answer_id, source): current_question = Question.get_by_id(int(question_id)) if not current_question: return answer = Answer.get_by_id(int(answer_id), parent = current_question.key) #TODO: raise 404 error. if not answer: return #TODO: filter desc content using hlper method. content = source.replace("\n", "<br />") answer.content = content answer.source = source answer.put()
def delete_question(self, question_id): question = Question.get_by_id(int(question_id)) if not question: return question.key.delete()
def focus_question_(self, is_focus, account_key, question_id): question = Question.get_by_id(int(question_id)) relation = AccountQuestionRelationService().get_relationship(account_key, question.key) relation.focused = is_focus relation.put()
def get_answers_by_question_id(self, question_id): #don't need call entity service for single get operation question = Question.get_by_id(int(question_id)) answers = Answer.query(ancestor = question.key).fetch() return answers
def cancel_no_help(self, actor, question_id, answer_id): question = Question.get_by_id(int(question_id)) answer = Answer.get_by_id(parent = question.key, id = int(answer_id)) relation = AccountQuestionRelationService().get_relationship(actor.key, question.key) relation.no_help_answers = [ a for a in relation.no_help_answers if a != answer.key ] relation.put()