Exemplo n.º 1
0
def video_qa_context(video, page=0, qa_expand_id=None, questions_hidden=True):

    limit_per_page = 10

    if qa_expand_id:
        # If we're showing an initially expanded question,
        # make sure we're on the correct page
        question = models_discussion.Feedback.get_by_id(qa_expand_id)
        if question:
            question_preceding_query = models_discussion.Feedback.gql("WHERE types = :1 AND targets = :2 AND deleted = :3 AND date > :4 ORDER BY date DESC", models_discussion.FeedbackType.Question, video.key(), False, question.date)
            count_preceding = question_preceding_query.count()
            page = 1 + (count_preceding / limit_per_page)

    if page > 0:
        questions_hidden = False # Never hide questions if specifying specific page
    else:
        page = 1

    limit_initially_visible = 3 if questions_hidden else limit_per_page

    question_query = models_discussion.Feedback.gql("WHERE types = :1 AND targets = :2 AND deleted = :3 ORDER BY date DESC", models_discussion.FeedbackType.Question, video.key(), False)
    answer_query = models_discussion.Feedback.gql("WHERE types = :1 AND targets = :2 AND deleted = :3 ORDER BY date", models_discussion.FeedbackType.Answer, video.key(), False)

    count_total = question_query.count()
    questions = question_query.fetch(limit_per_page, (page - 1) * limit_per_page)

    dict_questions = {}
    # Store each question in this page in a dict for answer population
    for question in questions:
        dict_questions[question.key()] = question

    # Just grab all answers for this video and cache in page's questions
    for answer in answer_query:
        # Grab the key only for each answer, don't run a full gql query on the ReferenceProperty
        question_key = answer.parent_key()
        if (dict_questions.has_key(question_key)):
            question = dict_questions[question_key]
            question.children_cache.append(answer)

    count_page = len(questions)
    pages_total = max(1, ((count_total - 1) / limit_per_page) + 1)
    return {
            "user": users.get_current_user(),
            "is_mod": is_current_user_moderator(),
            "video": video,
            "questions": questions,
            "count_total": count_total,
            "questions_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,
            "qa_expand_id": qa_expand_id,
            "issue_labels": ('Component-Videos,Video-%s' % video.youtube_id),
            "login_url": users.create_login_url("/video?v=%s" % video.youtube_id)
           }
Exemplo n.º 2
0
    def post(self):

        # Must be a moderator to delete anything
        if not is_current_user_moderator():
            return

        key = self.request.get("entity_key")
        if key:
            entity = db.get(key)
            if entity:
                entity.deleted = True
                db.put(entity)
Exemplo n.º 3
0
    def post(self):

        # Must be a moderator to change types of anything
        if not is_current_user_moderator():
            return

        key = self.request.get("entity_key")
        target_type = self.request.get("target_type")
        if key and models_discussion.FeedbackType.is_valid(target_type):
            entity = db.get(key)
            if entity:
                entity.types = [target_type]
                db.put(entity)
Exemplo n.º 4
0
    def get(self):

        question_key = self.request.get("question_key")
        question = db.get(question_key)

        if question:
            answer_query = models_discussion.Feedback.gql("WHERE types = :1 AND targets = :2 AND deleted = :3 ORDER BY date", models_discussion.FeedbackType.Answer, question.key(), False)
            template_values = {
                "answers": answer_query,
                "is_mod": is_current_user_moderator()
            }
            path = os.path.join(os.path.dirname(__file__), 'question_answers.html')
            html = render_block_to_string(path, 'answers', template_values)
            json = simplejson.dumps({"html": html}, ensure_ascii=False)
            self.response.out.write(json)

        return
Exemplo n.º 5
0
def video_comments_context(video, page=0, comments_hidden=True):

    if page > 0:
        comments_hidden = False  # Never hide questions if specifying specific page
    else:
        page = 1

    limit_per_page = 10
    limit_initially_visible = 3 if comments_hidden else limit_per_page

    comments_query = models_discussion.Feedback.gql(
        "WHERE types = :1 AND targets = :2 AND deleted = :3 ORDER BY date DESC",
        models_discussion.FeedbackType.Comment,
        video.key(),
        False,
    )
    count_total = comments_query.count()
    comments = comments_query.fetch(limit_per_page, (page - 1) * limit_per_page)

    count_page = len(comments)
    pages_total = max(1, ((count_total - 1) / limit_per_page) + 1)
    return {
        "user": users.get_current_user(),
        "is_mod": is_current_user_moderator(),
        "video": video,
        "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,
        "login_url": users.create_login_url("/video?v=%s" % video.youtube_id),
    }
Exemplo n.º 6
0
def signature(target=None, verb=None):
    return {
                "target": target, 
                "verb": verb, 
                "is_mod": is_current_user_moderator()
            }