def video_comments_context( video, topic, page=0, comments_hidden=True, sort_order=voting.VotingSortOrder.HighestPointsFirst): user_data = models.UserData.current() if page > 0: comments_hidden = False # Never hide questions if specifying specific page else: page = 1 limit_per_page = 10 limit_initially_visible = 2 if comments_hidden else limit_per_page comments = util_discussion.get_feedback_by_type_for_video( video, models_discussion.FeedbackType.Comment, user_data) comments = voting.VotingSortOrder.sort(comments, sort_order=sort_order) count_total = len(comments) comments = comments[((page - 1) * limit_per_page):(page * limit_per_page)] dict_votes = models_discussion.FeedbackVote.get_dict_for_user_data_and_video( user_data, video) for comment in comments: voting.add_vote_expando_properties(comment, dict_votes) count_page = len(comments) pages_total = max(1, ((count_total - 1) / limit_per_page) + 1) return { "is_mod": user_util.is_current_user_moderator(), "video": video, "topic": topic, "comments": comments, "count_total": count_total, "comments_hidden": count_page > limit_initially_visible, "limit_initially_visible": limit_initially_visible, "pages": range(1, pages_total + 1), "pages_total": pages_total, "prev_page_1_based": page - 1, "current_page_1_based": page, "next_page_1_based": page + 1, "show_page_controls": pages_total > 1, }
def video_comments_context( video, playlist, page=0, comments_hidden=True, sort_order=voting.VotingSortOrder.HighestPointsFirst ): user_data = models.UserData.current() if page > 0: comments_hidden = False # Never hide questions if specifying specific page else: page = 1 limit_per_page = 10 limit_initially_visible = 2 if comments_hidden else limit_per_page comments = util_discussion.get_feedback_by_type_for_video(video, models_discussion.FeedbackType.Comment, user_data) comments = voting.VotingSortOrder.sort(comments, sort_order=sort_order) count_total = len(comments) comments = comments[((page - 1) * limit_per_page) : (page * limit_per_page)] dict_votes = models_discussion.FeedbackVote.get_dict_for_user_data_and_video(user_data, video) for comment in comments: voting.add_vote_expando_properties(comment, dict_votes) count_page = len(comments) pages_total = max(1, ((count_total - 1) / limit_per_page) + 1) return { "is_mod": util_discussion.is_current_user_moderator(), "video": video, "playlist": playlist, "comments": comments, "count_total": count_total, "comments_hidden": count_page > limit_initially_visible, "limit_initially_visible": limit_initially_visible, "pages": range(1, pages_total + 1), "pages_total": pages_total, "prev_page_1_based": page - 1, "current_page_1_based": page, "next_page_1_based": page + 1, "show_page_controls": pages_total > 1, }
def video_qa_context(user_data, video, topic=None, page=0, qa_expand_key=None, sort_override=-1): limit_per_page = 5 if page <= 0: page = 1 sort_order = voting.VotingSortOrder.HighestPointsFirst if user_data: sort_order = user_data.question_sort_order if sort_override >= 0: sort_order = sort_override questions = util_discussion.get_feedback_by_type_for_video( video, models_discussion.FeedbackType.Question, user_data ) questions = voting.VotingSortOrder.sort(questions, sort_order=sort_order) if qa_expand_key: # If we're showing an initially expanded question, # make sure we're on the correct page question = models_discussion.Feedback.get(qa_expand_key) if question: count_preceding = 0 for question_test in questions: if question_test.key() == question.key(): break count_preceding += 1 page = 1 + (count_preceding / limit_per_page) answers = util_discussion.get_feedback_by_type_for_video(video, models_discussion.FeedbackType.Answer, user_data) answers.reverse() # Answers are initially in date descending -- we want ascending before the points sort answers = voting.VotingSortOrder.sort(answers) dict_votes = models_discussion.FeedbackVote.get_dict_for_user_data_and_video(user_data, video) count_total = len(questions) questions = questions[((page - 1) * limit_per_page) : (page * limit_per_page)] dict_questions = {} # Store each question in this page in a dict for answer population for question in questions: voting.add_vote_expando_properties(question, dict_votes) dict_questions[question.key()] = question # Just grab all answers for this video and cache in page's questions for answer in answers: # Grab the key only for each answer, don't run a full gql query on the ReferenceProperty question_key = answer.question_key() if dict_questions.has_key(question_key): question = dict_questions[question_key] voting.add_vote_expando_properties(answer, dict_votes) question.children_cache.append(answer) count_page = len(questions) pages_total = max(1, ((count_total - 1) / limit_per_page) + 1) return { "is_mod": user_util.is_current_user_moderator(), "video": video, "topic": topic, "questions": questions, "count_total": count_total, "pages": range(1, pages_total + 1), "pages_total": pages_total, "prev_page_1_based": page - 1, "current_page_1_based": page, "next_page_1_based": page + 1, "show_page_controls": pages_total > 1, "qa_expand_key": qa_expand_key, "sort_order": sort_order, }
def video_qa_context(user_data, video, page=0, qa_expand_key=None, sort_override=-1): limit_per_page = 5 if page <= 0: page = 1 sort_order = voting.VotingSortOrder.HighestPointsFirst if user_data: sort_order = user_data.question_sort_order if sort_override >= 0: sort_order = sort_override questions = util_discussion.get_feedback_by_type_for_video( video, discussion_models.FeedbackType.Question, user_data) questions = voting.VotingSortOrder.sort(questions, sort_order=sort_order) if qa_expand_key: # If we're showing an initially expanded question, # make sure we're on the correct page question = discussion_models.Feedback.get(qa_expand_key) if question: count_preceding = 0 for question_test in questions: if question_test.key() == question.key(): break count_preceding += 1 page = 1 + (count_preceding / limit_per_page) answers = util_discussion.get_feedback_by_type_for_video( video, discussion_models.FeedbackType.Answer, user_data) answers.reverse( ) # Answers are initially in date descending -- we want ascending before the points sort answers = voting.VotingSortOrder.sort(answers) dict_votes = discussion_models.FeedbackVote.get_dict_for_user_data_and_video( user_data, video) count_total = len(questions) questions = questions[((page - 1) * limit_per_page):(page * limit_per_page)] dict_questions = {} # Store each question in this page in a dict for answer population for question in questions: voting.add_vote_expando_properties(question, dict_votes) dict_questions[question.key()] = question # Just grab all answers for this video and cache in page's questions for answer in answers: # Grab the key only for each answer, don't run a full gql query on the ReferenceProperty question_key = answer.question_key() if (dict_questions.has_key(question_key)): question = dict_questions[question_key] voting.add_vote_expando_properties(answer, dict_votes) question.children_cache.append(answer) count_page = len(questions) pages_total = max(1, ((count_total - 1) / limit_per_page) + 1) return { "is_mod": user_util.is_current_user_moderator(), "video": video, "questions": questions, "count_total": count_total, "pages": range(1, pages_total + 1), "pages_total": pages_total, "prev_page_1_based": page - 1, "current_page_1_based": page, "next_page_1_based": page + 1, "show_page_controls": pages_total > 1, "qa_expand_key": qa_expand_key, "sort_order": sort_order, }