예제 #1
0
def accept_answer():
    answer_id = request.form['answer_id']
    question_id = request.form['question_id']
    
    questions = Question.query.filter(Question.id == question_id)
    if questions.count > 0:
        question = questions[0]
        # Make sure the current user submitted this question
        if session['email'] != question.submitter.email:
            return '{ success = false, message = "You do not own this question." }'

        # Make sure this question hasn't already been answered
        if question.is_answered:
            return '{ success = false, message = "This question has already been answered." }'
        
        # Mark this question as answered
        question.is_answered = True
    else:
        return '{ success: false, message = "Question not found" }'
    
    answers = Answer.query.filter(Answer.id == answer_id)
    if answers.count() > 0:
        answer = answers[0]
        answer.is_accepted = True
    else:
        return '{ success: false, message = "Answer not found" }'

    db_session.add(question)
    db_session.add(answer)
    db_session.commit()

    return "{ success: true }"
예제 #2
0
def users_new():
    if request.method == 'POST':
        # Builder User object from form values
        user = User()
        user.first_name = request.form['first_name']
        user.last_name = request.form['last_name']
        user.email = request.form['email']
        user.set_password(request.form['password_text'])

        # Save user to database
        db_session.add(user)
        db_session.commit()
        
        return redirect(url_for('root'))
    else:
        return render_template('users/new.html')
예제 #3
0
def vote():
    entity_type = request.form['entity_type']
    vote = request.form['vote']
    entities = {
        'question': Question,
        'answer': Answer,
        }
    entity = entities[entity_type]
    objects = entity.query.filter(entity.id == request.form['entity_id'])
    if objects.count() > 0:
        obj = objects[0]
        if vote == 'up':
            obj.votes += 1
        elif vote == 'down':
            obj.votes -= 1
        db_session.add(obj)
        db_session.commit()
    else:
        return "{ success: false }"
    return "{ success: true }"
예제 #4
0
def answers_new():
    users = User.query.filter(User.email == session['email'])
    if users.count() == 0:
        raise Exception('No user found')
    user = users[0]

    question_id = request.form['question_id']
    # Build Answer object from form values
    answer = Answer()
    answer.text = request.form['text']
    answer.submitter_id = user.id
    answer.question_id = question_id
    answer.votes = 0

    # Save answer to database
    db_session.add(answer)
    db_session.commit()

    flash('You have answered this question.')

    return redirect(url_for('question', id=question_id))
예제 #5
0
def questions_new():
    if request.method == 'POST':
        users = User.query.filter(User.email == session['email'])
        if users.count() == 0:
            raise Exception('No user found')
        user = users[0]
        
        # Build Question object from form values
        question = Question()
        question.title = request.form['title']
        question.body = request.form['body']
        question.submitter_id = user.id
        question.votes = 0

        # Save question to database
        db_session.add(question)
        db_session.commit()
        
        return redirect(url_for('questions'))
    else:
        return render_template('questions/new.html')