Exemplo n.º 1
0
def view():
    """ The default page when viewing a question """
    # We need to pull here several important things: Questions, comments for
    # questions, answers, comments for answers..
    view_info = {'errors': []}

    req = request.vars
    qid = req.qid if req.qid is not None else request.args[0]  # Question ID

    # Read the question here to see if the user is allowed to access it
    question = db(db.questions.id == qid).select(db.questions.ALL)
    user_id = auth_user.get_user_id()
    if not question or (not question[0].is_visible
                        and not auth_user.is_admin()):
        # Only admins may see hidden questions..
        redirect(URL(r=request, c='default', f='unauthorized'))

    featured_votes = db((db.score_log.l_type == 'Q')
                        & (db.score_log.subtype == 'featuredrequest')
                        & (db.score_log.qac_id == qid)).count()
    view_info.update(dict(featured_votes=featured_votes))

    offensive_votes = db((db.score_log.l_type == 'Q')
                         & (db.score_log.subtype == 'offensiverequest')
                         & (db.score_log.qac_id == qid)).count()
    view_info.update(dict(offensive_votes=offensive_votes))

    # This controls if the user is subscribed or not to this question
    view_info['is_subscribed'] = False
    if auth_user.is_auth():
        if stackhelper.user_is_subscribed(qid, user_id):
            view_info['is_subscribed'] = True

    # Only the following roles can add comments
    can_comment = auth_user.has_role('Reviewer,TeamLead,Manager,SysAdmin')
    view_info['can_comment'] = can_comment
    if req.form_submitted:
        view_info['form_submitted'] = True
        preview_answer = req.preview_answer
        post_answer = req.post_answer
        answer = req.get('answer', '').strip()
        view_info['answer'] = answer
        if preview_answer is not None:
            view_info['preview_answer'] = preview_answer
        else:
            # Posting an answer to this question here
            if answer:
                modified_by = user_id
                db.answers.insert(question_id=qid,
                                  description=answer,
                                  created_by=modified_by,
                                  created_on=request.now,
                                  modified_by=modified_by,
                                  is_outstanding=False,
                                  votes_up=0,
                                  votes_dn=0,
                                  is_visible=True,
                                  is_answer=False,
                                  modified_on=request.now)
                # Update the original question's last update date/user
                db(db.questions.id == qid).update(modified_by=modified_by,
                                                  modified_on=request.now)
                # Also, increment the number of answers this user has posted
                stackhelper.increment_member_property('m_answers', modified_by,
                                                      1)
            else:
                view_info['errors'].append(
                    'Please add a valid answer to continue')
    else:
        # Update the page views for this question, only
        # if the page is viewed via GET
        view_rec = db(db.questions.id == qid).select(db.questions.views)[0]
        db(db.questions.id == qid).update(views=view_rec.views + 1)

    question = db(
        (db.questions.id == qid)
        & (db.questions.created_by == db.member_properties.auth_user)
        & (db.member_properties.property_id == db.member_properties_skel.id) &
        (db.member_properties_skel.property_name == 'm_display_name')).select(
            db.questions.ALL, db.member_properties.property_value)[0]
    tags = db(
        (db.questions.id==db.question_tags.question_id) &\
        (db.question_tags.tag_id==db.tags.id) &\
        (db.questions.id==question.questions.id)).select(
        db.tags.tagname)
    q_comments = db(
        (db.comments.c_type=='Q') &
        (db.comments.qa_id==qid) &
        (db.comments.is_visible==True) &
        (db.comments.created_by==db.member_properties.auth_user) &
        (db.member_properties.property_id==db.member_properties_skel.id) &
        (db.member_properties_skel.property_name==\
         'm_display_name')).select(db.comments.ALL,
        db.member_properties.property_value,
        orderby=db.comments.modified_on)
    answers = db(
        (db.answers.question_id == qid) & (db.answers.is_visible == True)
        & (db.answers.created_by == db.member_properties.auth_user)
        & (db.member_properties.property_id == db.member_properties_skel.id) &
        (db.member_properties_skel.property_name == 'm_display_name')).select(
            db.answers.ALL,
            db.member_properties.property_value,
            orderby=~db.answers.is_answer | ~db.answers.modified_on)
    # Now every different proposed answer can have comments,
    # so I need the comments for each answer if applicable)
    comments_a = {}
    for answer in answers:
        comments = db(
            (db.comments.c_type=='A') &
            (db.comments.qa_id==answer.answers.id) &
            (db.comments.is_visible==True) &
            (db.comments.created_by==db.member_properties.auth_user) &
            (db.member_properties.property_id==db.member_properties_skel.id) &
            (db.member_properties_skel.property_name==\
             'm_display_name')).select(
            db.comments.ALL,
            db.member_properties.property_value,
            orderby=db.comments.modified_on)
        if comments:
            comments_a.update({answer.answers.id: comments})
    return dict(question=question,
                tags=tags,
                q_comments=q_comments,
                answers=answers,
                comments_a=comments_a,
                can_comment=can_comment,
                view_info=view_info)
Exemplo n.º 2
0
def view():
    """ The default page when viewing a question """
    # We need to pull here several important things: Questions, comments for
    # questions, answers, comments for answers..
    view_info = {'errors': []}

    req = request.vars
    qid = req.qid if req.qid is not None else request.args[0] # Question ID

    # Read the question here to see if the user is allowed to access it
    question = db(db.questions.id==qid).select(db.questions.ALL)
    user_id = auth_user.get_user_id()
    if not question or (
        not question[0].is_visible and not auth_user.is_admin()):
        # Only admins may see hidden questions..
        redirect(URL(r=request, c='default', f='unauthorized'))

    featured_votes = db(
        (db.score_log.l_type=='Q') &
        (db.score_log.subtype=='featuredrequest') &
        (db.score_log.qac_id==qid)).count()
    view_info.update(dict(featured_votes=featured_votes))

    offensive_votes = db(
        (db.score_log.l_type=='Q') &
        (db.score_log.subtype=='offensiverequest') &
        (db.score_log.qac_id==qid)).count()
    view_info.update(dict(offensive_votes=offensive_votes))

    # This controls if the user is subscribed or not to this question
    view_info['is_subscribed'] = False
    if auth_user.is_auth():
        if stackhelper.user_is_subscribed(qid, user_id):
            view_info['is_subscribed'] = True

    # Only the following roles can add comments
    can_comment = auth_user.has_role('Reviewer,TeamLead,Manager,SysAdmin')
    view_info['can_comment'] = can_comment
    if req.form_submitted:
        view_info['form_submitted'] = True
        preview_answer = req.preview_answer
        post_answer = req.post_answer
        answer = req.get('answer', '').strip()
        view_info['answer'] = answer
        if preview_answer is not None:
            view_info['preview_answer'] = preview_answer
        else:
            # Posting an answer to this question here
            if answer:
                modified_by = user_id
                db.answers.insert(question_id=qid,
                                  description=answer,
                                  created_by=modified_by,
                                  created_on=request.now,
                                  modified_by=modified_by,
                                  is_outstanding=False,
                                  votes_up=0,
                                  votes_dn=0,
                                  is_visible=True,
                                  is_answer=False,
                                  modified_on=request.now)
                # Update the original question's last update date/user
                db(db.questions.id==qid).update(modified_by=modified_by,
                                                modified_on=request.now)
                # Also, increment the number of answers this user has posted
                stackhelper.increment_member_property('m_answers',
                                                      modified_by,
                                                      1)
            else:
                view_info['errors'].append(
                    'Please add a valid answer to continue')
    else:
        # Update the page views for this question, only
        # if the page is viewed via GET
        view_rec = db(db.questions.id==qid).select(db.questions.views)[0]
        db(db.questions.id==qid).update(views=view_rec.views+1)

    question = db(
        (db.questions.id==qid) &
        (db.questions.created_by==db.member_properties.auth_user) &
        (db.member_properties.property_id==db.member_properties_skel.id) &
        (db.member_properties_skel.property_name=='m_display_name')).select(
        db.questions.ALL,
        db.member_properties.property_value)[0]
    tags = db(
        (db.questions.id==db.question_tags.question_id) &\
        (db.question_tags.tag_id==db.tags.id) &\
        (db.questions.id==question.questions.id)).select(
        db.tags.tagname)
    q_comments = db(
        (db.comments.c_type=='Q') &
        (db.comments.qa_id==qid) &
        (db.comments.is_visible==True) &
        (db.comments.created_by==db.member_properties.auth_user) &
        (db.member_properties.property_id==db.member_properties_skel.id) &
        (db.member_properties_skel.property_name==\
         'm_display_name')).select(db.comments.ALL,
        db.member_properties.property_value,
        orderby=db.comments.modified_on)
    answers = db(
        (db.answers.question_id==qid) &
        (db.answers.is_visible==True) &
        (db.answers.created_by==db.member_properties.auth_user) &
        (db.member_properties.property_id==db.member_properties_skel.id) &
        (db.member_properties_skel.property_name=='m_display_name')).select(
        db.answers.ALL,
        db.member_properties.property_value,
        orderby=~db.answers.is_answer|~db.answers.modified_on)
    # Now every different proposed answer can have comments,
    # so I need the comments for each answer if applicable)
    comments_a = {}
    for answer in answers:
        comments = db(
            (db.comments.c_type=='A') &
            (db.comments.qa_id==answer.answers.id) &
            (db.comments.is_visible==True) &
            (db.comments.created_by==db.member_properties.auth_user) &
            (db.member_properties.property_id==db.member_properties_skel.id) &
            (db.member_properties_skel.property_name==\
             'm_display_name')).select(
            db.comments.ALL,
            db.member_properties.property_value,
            orderby=db.comments.modified_on)
        if comments:
            comments_a.update({answer.answers.id: comments})
    return dict(
        question=question,
        tags=tags,
        q_comments=q_comments,
        answers=answers,
        comments_a=comments_a,
        can_comment=can_comment,
        view_info=view_info)