def get_user_notifications(user_data, page, limit=-1): """Returns a list of util_discussion.UserQuestion entities, representing a user's questions. They are sorted based on most recently answered. Arguments: user_data: The user whose questions need to be fetched page: The desired page number (>=1) sort: one of possible sort orders in voting.VotingSortOrder limit: Upper bound for the number of notifications to return """ added_question_keys = [] # Final set of questions to return. user_questions = [] # Get new notification feedback items. feedbacks = discussion_models.FeedbackNotification.get_feedback_for( user_data) # Sort by newest notification feedback items first. feedbacks = voting.VotingSortOrder.sort(feedbacks, voting.VotingSortOrder.NewestFirst) for feedback in feedbacks: question_key = str(feedback.question_key()) if not question_key in added_question_keys: user_question = util_discussion.UserQuestion.from_question( feedback.question(), user_data) if user_question: added_question_keys.append(question_key) user_question.mark_has_unread() user_questions.append(user_question) # Get all the questions. questions = util_discussion.get_all_feedback_by_author( discussion_models.FeedbackType.Question, user_data) # Collect questions with at least one new answer. questions_without_updates = [] for question in questions: question_key = str(question.key()) if not question_key in added_question_keys: user_question = util_discussion.UserQuestion.from_question( question, user_data) if (user_question and user_question.answerer_count > 0): questions_without_updates.append(user_question) # Sort questions based on the last update. key_fxn = lambda entity: entity.last_updated questions_without_updates = sorted(questions_without_updates, key=key_fxn, reverse=True) # Append the questions without updates to the ones with updates. user_questions.extend(questions_without_updates) return {"questions": user_questions}
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_notifications(user_data, page, limit=-1): """Returns a list of util_discussion.UserQuestion entities, representing a user's questions. They are sorted based on most recently answered. Arguments: user_data: The user whose questions need to be fetched page: The desired page number (>=1) sort: one of possible sort orders in voting.VotingSortOrder limit: Upper bound for the number of notifications to return """ added_question_keys = [] # Final set of questions to return. user_questions = [] # Get new notification feedback items. feedbacks = discussion_models.FeedbackNotification.get_feedback_for( user_data) # Sort by newest notification feedback items first. feedbacks = voting.VotingSortOrder.sort(feedbacks, voting.VotingSortOrder.NewestFirst) for feedback in feedbacks: question_key = str(feedback.question_key()) if not question_key in added_question_keys: user_question = util_discussion.UserQuestion.from_question( feedback.question(), user_data) if user_question: added_question_keys.append(question_key) user_question.mark_has_unread() user_questions.append(user_question) # Get all the questions. questions = util_discussion.get_all_feedback_by_author( discussion_models.FeedbackType.Question, user_data) # Collect questions with at least one new answer. questions_without_updates = [] for question in questions: question_key = str(question.key()) if not question_key in added_question_keys: user_question = util_discussion.UserQuestion.from_question( question, user_data) if (user_question and user_question.answerer_count > 0): questions_without_updates.append(user_question) # Sort questions based on the last update. key_fxn = lambda entity: entity.last_updated questions_without_updates = sorted(questions_without_updates, key=key_fxn, reverse=True) # Append the questions without updates to the ones with updates. user_questions.extend(questions_without_updates) return { "questions": user_questions }