예제 #1
0
def fazer_pergunta():
    if not session.get('logged_user_id'):
        return render_template('login.html')
    else:
        if request.method == 'POST':
            question = Question.Question()
            tags = request.form['tag'].split(',')

            dict_tag = {
                'Tags':
                dict(('tag ' + str(i), item) for i, item in enumerate(tags))
            }
            dict_tag = json.dumps(dict_tag)

            if question.validate_question_post(request.form['title'],
                                               request.form['body'],
                                               session.get('logged_user_id'),
                                               dict_tag, False):
                utils.set_alert('success', 'Pergunta postada!')
                return redirect(url_for('home'))
            else:
                return popup(msg="Erro ao cadastrar pergunta",
                             links=[{
                                 'url': '/fazer-pergunta',
                                 'text': 'Tentar Novamente'
                             }])
        else:
            return render_template('fazer-pergunta.html')
예제 #2
0
def pergunta(pergunta_id):
    question = Question.Question()
    pergunta = question.get_by_id(str(pergunta_id))
    pergunta['tags'] = json.loads(pergunta['tags'])
    print(pergunta['tags'])
    try:
        pergunta['tags'] = [p for key, p in pergunta['tags']['Tags'].items()]
    except:
        pergunta['tags'] = ""
    if request.method == 'POST':
        answer = Answer.Answer()
        if session['logged_user_id']:
            if answer.validate_answer_post(str(pergunta_id),
                                           session.get('logged_user_id'),
                                           request.form['resposta']):
                utils.set_alert('success', 'Resposta postada!')
                return redirect(url_for('pergunta', pergunta_id=pergunta_id))
        return popup(msg="Erro ao cadastrar resposta",
                     links=[{
                         'url': '/pergunta/' + str(pergunta_id),
                         'text': 'Voltar'
                     }])
    else:
        answer = Answer.Answer()
        respostas = answer._select_all_by_questionid(str(pergunta_id))
        return render_template('pergunta.html',
                               pergunta_id=pergunta_id,
                               respostas=respostas,
                               usuario_logado=session.get('logged_user_id'),
                               pergunta_votes=str(pergunta['votes']),
                               pergunta_fullname=str(pergunta['fullname']),
                               pergunta_title=str(pergunta['title']),
                               pergunta_data=str(pergunta['data']),
                               pergunta_desc=str(pergunta['description']),
                               pergunta_tag=pergunta['tags'])
예제 #3
0
def add_new_question(challenge_id):
    os.system("cls")
    print(30 * "-", "NEW QUESTION", 30 * "-")

    content = getinput.string_input_between("Question", 5, 75)
    answer = getinput.string_input_between("Answer", 1, 30)
    while True:
        try:
            score = int(input("\nEnter score [100 - 500]: "))
            if 100 <= score <= 500:
                break
            else:
                print("\nScore is not valid")
        except ValueError:
            print("\nIncorrect format")

    while True:
        try:
            choice = int(
                input(
                    "\nDo you want to add this question? 1 = YES / 2 = NO: "))
            if choice == 1:
                new_question = question.Question(challenge_id, content, answer,
                                                 score)
                new_question.add_question()
                break
            elif choice == 2:
                break
            else:
                print("\nIncorrect option")
        except ValueError:
            print("\nIncorrect format")
예제 #4
0
def remover_pergunta_confirmado(pergunta_id):
    if not session.get('logged_user_id'):
        return render_template('login.html')
    else:
        Question.Question().remove(pergunta_id,
                                   str(session.get('logged_user_id')))
        utils.set_alert('success', 'Pergunta removida!')
    return redirect(url_for('minhas_perguntas'))
예제 #5
0
def home():
    question = Question.Question()
    perguntas = question.get_all()
    for perg in perguntas:
        perg['tags'] = json.loads(perg['tags'])
        try:
            perg['tags'] = [p for key, p in perg['tags']['Tags'].items()]
        except:
            perg['tags'] = ""

    return render_template('home.html', perguntas=perguntas)
예제 #6
0
def pesquisar():
    if request.method == 'POST':
        question = Question.Question()
        perguntas = question.get_by_title(request.form['pesquisa'])

        for perg in perguntas:
            perg['tags'] = json.loads(perg['tags'])
            try:
                perg['tags'] = [p for key, p in perg['tags']['Tags'].items()]
            except:
                perg['tags'] = ""

        return render_template('pesquisa.html', perguntas=perguntas)
예제 #7
0
def perguntas_por_tag(tag):
    question = Question.Question()
    perguntas = question.get_by_tag(str(tag))

    for perg in perguntas:
        perg['tags'] = json.loads(perg['tags'])
        try:
            perg['tags'] = [p for key, p in perg['tags']['Tags'].items()]
        except:
            perg['tags'] = ""

    return render_template('perguntas-por-tag.html',
                           perguntas=perguntas,
                           tag=tag)
예제 #8
0
def minhas_perguntas():
    if not session.get('logged_user_id'):
        return render_template('login.html')
    else:
        question = Question.Question()
        perguntas = question.get_by_user(str(session.get('logged_user_id')))

        for perg in perguntas:
            perg['tags'] = json.loads(perg['tags'])
            try:
                perg['tags'] = [p for key, p in perg['tags']['Tags'].items()]
            except:
                perg['tags'] = ""

        return render_template('minhas-perguntas.html', perguntas=perguntas)
예제 #9
0
def editar_pergunta(pergunta_id):
    question = Question.Question()
    pergunta = question.get_by_id(str(pergunta_id))
    if request.method == 'POST':
        if request.form['tag']:
            tags = request.form['tag'].split(',')
        else:
            tags = ""
        dict_tag = {
            'Tags': dict(
                ('tag ' + str(i), item) for i, item in enumerate(tags))
        }
        dict_tag = json.dumps(dict_tag)

        if question.validate_question_post(request.form['title'],
                                           request.form['description'],
                                           session.get('logged_user_id'),
                                           dict_tag, str(pergunta_id)):
            utils.set_alert('success', 'Pergunta editada!')
            return redirect(url_for('minhas_perguntas'))
        else:
            return popup(msg="Erro ao editar pergunta",
                         links=[{
                             'url': '/editar-pergunta/' + str(pergunta_id),
                             'text': 'Tentar Novamente'
                         }])
    else:
        if (int((pergunta['iduser'])) == int(session['logged_user_id'])):
            return render_template(
                'editar-pergunta.html',
                pergunta_id=pergunta_id,
                pergunta_title=str(pergunta['title']),
                #pergunta_tags = str(pergunta['tags']),
                pergunta_desc=str(pergunta['description']))
        else:
            return redirect(url_for('home'))