def get_user_answers(user_data, viewer_data, page, sort, limit=-1): """Returns a list of util_discussion.UserAnswer entities, representing a user's answers. Arguments: user_data: The user whose answers need to be fetched viewer_data: The user who is going to view the answers page: The desired page number (>=1) sort: one of possible sort orders in voting.VotingSortOrder limit: Upper bound for the number of answers to return """ if not (1 <= limit <= 10): limit = 10 answers = util_discussion.get_feedback_by_author( discussion_models.FeedbackType.Answer, user_data, page, limit, sort) user_answers = filter(lambda x: x is not None, [util_discussion.UserAnswer.from_answer( answer, viewer_data) for answer in answers]) # If any answers without a question were removed, # fetch all answers and remove the ones without a question, # then return the answers respective to the limit and page. # Don't do this if the number of answers fetched is less than # the fetch limit. answers_count = len(answers) if answers_count == limit and len(user_answers) < answers_count: answers = util_discussion.get_all_feedback_by_author( discussion_models.FeedbackType.Answer, user_data, sort) user_answers = filter(lambda x: x is not None, [util_discussion.UserAnswer.from_answer( answer, viewer_data) for answer in answers]) # Filter out answers based on the page and limit. start = ((page - 1) * limit) end = (page * limit) user_answers = user_answers[start:end] return user_answers
def get_user_answers(user_data, viewer_data, page, sort, limit=-1): """Returns a list of util_discussion.UserAnswer entities, representing a user's answers. Arguments: user_data: The user whose answers need to be fetched viewer_data: The user who is going to view the answers page: The desired page number (>=1) sort: one of possible sort orders in voting.VotingSortOrder limit: Upper bound for the number of answers to return """ if not (1 <= limit <= 10): limit = 10 answers = util_discussion.get_feedback_by_author( discussion_models.FeedbackType.Answer, user_data, page, limit, sort) user_answers = filter(lambda x: x is not None, [ util_discussion.UserAnswer.from_answer(answer, viewer_data) for answer in answers ]) # If any answers without a question were removed, # fetch all answers and remove the ones without a question, # then return the answers respective to the limit and page. # Don't do this if the number of answers fetched is less than # the fetch limit. answers_count = len(answers) if answers_count == limit and len(user_answers) < answers_count: answers = util_discussion.get_all_feedback_by_author( discussion_models.FeedbackType.Answer, user_data, sort) user_answers = filter(lambda x: x is not None, [ util_discussion.UserAnswer.from_answer(answer, viewer_data) for answer in answers ]) # Filter out answers based on the page and limit. start = ((page - 1) * limit) end = (page * limit) user_answers = user_answers[start:end] return user_answers
def get_user_comments(user_data, viewer_data, page, sort, limit=-1): """Returns a list of util_discussion.UserComment entities, representing a user's comments. Arguments: user_data: The user whose comments need to be fetched viewer_data: The user who is going to view the comments page: The desired page number (>=1) sort: one of possible sort orders in voting.VotingSortOrder limit: Upper bound for the number of comments to return """ if not (1 <= limit <= 10): limit = 10 comments = util_discussion.get_feedback_by_author( discussion_models.FeedbackType.Comment, user_data, page, limit, sort) return filter(lambda x: x is not None, [util_discussion.UserComment.from_comment( comment, viewer_data) for comment in comments])
def get_user_questions(user_data, viewer_data, page, sort, limit=-1): """Return a list of util_discussion.UserQuestion entities, representing a user's questions. Arguments: user_data: The user whose questions need to be fetched viewer_data: The user who is going to view the questions page: The desired page number (>=1) sort: one of possible sort orders in voting.VotingSortOrder limit: Upper bound for the number of questions to return """ if not (1 <= limit <= 10): limit = 10 questions = util_discussion.get_feedback_by_author( discussion_models.FeedbackType.Question, user_data, page, limit, sort) return filter(lambda x: x is not None, [util_discussion.UserQuestion.from_question( question, viewer_data) for question in questions])
def get_user_comments(user_data, viewer_data, page, sort, limit=-1): """Returns a list of util_discussion.UserComment entities, representing a user's comments. Arguments: user_data: The user whose comments need to be fetched viewer_data: The user who is going to view the comments page: The desired page number (>=1) sort: one of possible sort orders in voting.VotingSortOrder limit: Upper bound for the number of comments to return """ if not (1 <= limit <= 10): limit = 10 comments = util_discussion.get_feedback_by_author( discussion_models.FeedbackType.Comment, user_data, page, limit, sort) return filter(lambda x: x is not None, [ util_discussion.UserComment.from_comment(comment, viewer_data) for comment in comments ])
def get_user_questions(user_data, viewer_data, page, sort, limit=-1): """Return a list of util_discussion.UserQuestion entities, representing a user's questions. Arguments: user_data: The user whose questions need to be fetched viewer_data: The user who is going to view the questions page: The desired page number (>=1) sort: one of possible sort orders in voting.VotingSortOrder limit: Upper bound for the number of questions to return """ if not (1 <= limit <= 10): limit = 10 questions = util_discussion.get_feedback_by_author( discussion_models.FeedbackType.Question, user_data, page, limit, sort) return filter(lambda x: x is not None, [ util_discussion.UserQuestion.from_question(question, viewer_data) for question in questions ])