예제 #1
0
def contact_admin():
    """ Contact Admin - This can allow anonymous users to post spam,
    so for them, I'll add some "poor man's captcha" """
    view_info = {}
    view_info['errors'] = []
    captcha = stackhelper.gen_pwd()
    view_info['anon_captcha'] = captcha
    view_info['anon_captcha_base64'] = base64.standard_b64encode(captcha)
    req = request.vars
    if req.form_submitted:
        if req.send_b:
            if req.subject and req.message:
                if auth_user.is_auth() or\
                (not auth_user.is_auth() and\
                 base64.standard_b64encode(req.captcha_response) == req.c):
                    db.admin_messages.insert(
                        auth_user_id=auth_user.get_user_id(),
                        subject=parse_content(req.subject),
                        message=parse_content(req.message),
                        creation_date=request.now,
                        read_flag=False)
                    redirect(URL(r=request, c='default', f='index'))
                else:
                    view_info['errors'].append(
                        'Invalid humanity challenge response, please try '
                        'again')
                    return dict(request=request, view_info=view_info)
            else:
                view_info['errors'].append(
                    'Both Subject and Message are required fields')
                return dict(request=request, view_info=view_info)
        else:
            redirect(URL(r=request, c='default', f='index'))
    else:
        return dict(request=request, view_info=view_info)
예제 #2
0
def contact_admin():
    """ Contact Admin - This can allow anonymous users to post spam,
    so for them, I'll add some "poor man's captcha" """
    view_info = {}
    view_info['errors'] = []
    captcha = stackhelper.gen_pwd()
    view_info['anon_captcha'] = captcha
    view_info['anon_captcha_base64'] = base64.standard_b64encode(captcha)
    req = request.vars
    if req.form_submitted:
        if req.send_b:
            if req.subject and req.message:
                if auth_user.is_auth() or\
                (not auth_user.is_auth() and\
                 base64.standard_b64encode(req.captcha_response) == req.c):
                    db.admin_messages.insert(
                        auth_user_id=auth_user.get_user_id(),
                        subject=parse_content(req.subject),
                        message=parse_content(req.message),
                        creation_date=request.now,
                        read_flag=False)
                    redirect(URL(r=request, c='default', f='index'))
                else:
                    view_info['errors'].append(
                        'Invalid humanity challenge response, please try '
                        'again')
                    return dict(request=request, view_info=view_info)
            else:
                view_info['errors'].append(
                    'Both Subject and Message are required fields')
                return dict(request=request, view_info=view_info)
        else:
            redirect(URL(r=request, c='default', f='index'))
    else:
        return dict(request=request, view_info=view_info)
예제 #3
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)
예제 #4
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)