コード例 #1
0
ファイル: polls.py プロジェクト: best-upm/IBST
def newPoll():
    title = 'Crear nueva votación'
    poll = Poll(start_date=datetime.datetime.utcnow())
    form = NewPollForm(obj=poll,
                       start_time=datetime.time(18, 15),
                       end_time=datetime.time(18, 30))
    if (form.validate_on_submit()):
        form.populate_obj(poll)
        poll.start_date = datetime.datetime.combine(form.start_date.data,
                                                    form.start_time.data)
        poll.end_date = datetime.datetime.combine(form.end_date.data,
                                                  form.end_time.data)
        poll.id_creator = current_user.id
        options = list(form.Opciones.data.split("; "))
        try:
            for option in options:
                if ("$user: "******"user_id: x" para clasificar
                    analized_option = option.split(" ", 1)[1]
                    user = User.query.filter_by(
                        Usuario=analized_option).first_or_404()
                    if (user is not None):
                        option = "user_id: " + str(user.id)  #
                elif ("$date: " in option):
                    #Y aqui un elemento "date" Aunque, quizas habria que hacer el template_html mas dinamico
                    analized_option = option.split(" ", 1)[1]
                opcion = PollOption(option_name=option)
                poll.options.append(opcion)
            db.session.add(poll)
            db.session.commit()
            #print(options, flush=True)
            return render_template('ShowNewPoll.html', options=options)
        except:
            flash(
                'Error!!! Ha sucedido un error, revisa que todos los datos han sido introducidos correctamente'
            )
            return render_template('Newpoll.html', title=title, form=form)
    return render_template('Newpoll.html', title=title, form=form)