Ejemplo n.º 1
0
def add_poll():
    if current_user.is_admin:
        form = PollForm()

        if form.validate_on_submit():
            poll = Poll(name=form.name.data,
                        description=form.description.data,
                        creator_id=current_user.id)

            db.session.add(poll)
            db.session.flush()  # lets you access the generated poll.id

            for r in form.recipes:
                recipe = Recipe(name=r.data['name'],
                                description=r.data['description'],
                                contributor_id=current_user.id,
                                poll_id=poll.id)

                db.session.add(recipe)

            db.session.commit()  # commits all the changes in the database
            flash('The poll is added', 'success')
            return redirect(url_for('add_poll'))

        return render_template('poll_form.html',
                               title='Create Poll',
                               description='Fill in the details of the poll',
                               legend='Create a Poll',
                               form=form)
    else:
        abort(403)
Ejemplo n.º 2
0
def update_poll(poll_id):
    if current_user.is_admin:
        poll = Poll.query.get_or_404(poll_id)
        form = PollForm()

        if form.validate_on_submit():
            poll.name = form.name.data
            poll.description = form.description.data

            for r in form.recipes.data:
                recipe = Recipe.query.get(r.id)
                recipe.name = r.data['name']
                recipe.description = r.data['description']

            db.session.commit()
            flash('The poll has been updated!', 'success')
            return redirect(url_for('poll', poll_id=poll.id))

        elif request.method == 'GET':
            form.name.data = poll.name
            form.description.data = poll.description

            # TODO existing recipes show up

        return render_template('poll_form.html',
                               title='Update - ' + poll.name + ' - Poll',
                               description='Fill in the details to be changed',
                               legend='Update - ' + poll.name + ' - Poll',
                               form=form)
    else:
        abort(403)
Ejemplo n.º 3
0
def create_vote():
    users = [(user.id, user.login) for user in User.query.all()]
    form = PollForm()
    form.access_participation.choices = users
    form.access_results.choices = users
    if form.validate_on_submit():
        new_poll = Poll(title=form.title.data, kind='vote', repeat_type=form.repeat_type.data,
                        creator=current_user)
        for participant_id in form.access_participation:
            new_poll.access_participation.append(User.query.get(int(participant_id.data)))

        for user_id in form.access_results:
            new_poll.access_results.append(User.query.get(int(user_id.data)))

        question = form.vote_questions[0]
        if question.question.data:
            new_question = Question(type='variants', question=question.question.data,
                                    multiple_answers=question.multiple_answers.data)
            for option in question.options:
                if option.data:
                    possible_answer = PossibleAnswer(option=option.data, question=new_question)
                    new_question.possible_answers.append(possible_answer)
            new_poll.questions.append(new_question)
            db.session.add(new_poll)
            db.session.commit()
            return redirect(url_for('index'))
    if len(form.questions) == 0:
        form.vote_questions.append_entry()
    return render_template('createVote.html', form=form)
Ejemplo n.º 4
0
def create_new_poll():
  errors=["An error occurred while creating a poll."]
  form = PollForm()
  form['csrf_token'].data = request.cookies['csrf_token']

  if form.validate_on_submit():
    new_poll = Poll(title=form.data['title'], question_text=form.data["question_text"], user_id=current_user.get_id())
    db.session.add(new_poll)
    db.session.commit()
    return { new_poll.get_id(): new_poll.to_dict() }

  return { "errors": errors }
Ejemplo n.º 5
0
def add_poll():
    """ Function that allows admin to create a new poll """
    error = None
    form = PollForm(csrf_enabled=False)
    if form.validate_on_submit():
        new_poll = Poll(form.question.data)
        db.session.add(new_poll)
        db.session.commit()
        flash('New entry was successfully posted. Thanks.')
    else:
        flash_errors(form)
    return redirect(url_for('admin_main'))
Ejemplo n.º 6
0
def update_poll(pollId):
  errors=["An error occurred while updating your poll."]
  the_poll = Poll.query.get(pollId)

  form = PollForm()
  form['csrf_token'].data = request.cookies['csrf_token']
  if form.validate_on_submit():
    the_poll.update_poll_data(form.data['title'], form.data["question_text"])
    db.session.add(the_poll)
    db.session.commit()
    return { the_poll.get_id(): the_poll.to_dict() }

  return { "errors": errors }
Ejemplo n.º 7
0
def admin_main():
    """ Skipping django main with CRUD for all models.
    Going instead to url with 'add_poll' and 'change_poll'.
    """
    form = PollForm(csrf_enabled=False)
    polls = Poll.query.all()
    return render_template('admin_main.html', form=form, polls=polls)
Ejemplo n.º 8
0
def hostPoll():
    header = "Enter your desired poll!"

    form = PollForm()

    if form.validate_on_submit():
        # We need to create the session and the poll, then link them
        # together.
        sesh = Session(session_id=form.sessionID.data)
        poll = Poll(question=form.questionText.data)

        session = Session.query.filter_by(
            session_id=form.sessionID.data).first()

        # If the session doesn't exist
        if not (session is None):
            flash('Session in use!')
            return redirect('/host_poll')

        poll.a = form.a.data
        poll.b = form.b.data
        poll.c = form.c.data
        poll.d = form.d.data

        poll.a_num = 0
        poll.b_num = 0
        poll.c_num = 0
        poll.d_num = 0

        # Link them
        poll.session = sesh
        sesh.poll = poll

        db.session.add(sesh)
        db.session.commit()

        db.session.add(poll)
        db.session.commit()

        flash("Generating poll!")
        return redirect('/pollData/' + sesh.session_id)

    return render_template('create_poll.html',
                           title='Host a Poll',
                           header=header,
                           form=form)
Ejemplo n.º 9
0
def edit_poll(poll_id):
    users = [(user.id, user.login) for user in User.query.all()]
    poll = Poll.query.get_or_404(poll_id)

    if poll.creator != current_user:
        return redirect(url_for('view_poll', poll_id=poll.id))

    form = PollForm()
    form.access_participation.choices = users
    form.access_results.choices = users

    if form.validate_on_submit():
        poll.title = form.title.data
        poll.repeat_type = form.repeat_type.data
        if form.access_participation:
            print(form.access_participation.data)
            for participant_id in form.access_participation.data:
                poll.access_participation.clear()
                poll.access_participation.append(User.query.get(int(participant_id)))
        else:
            poll.access_participation.clear()

        if form.access_results:
            for user_id in form.access_results.data:
                print('asdas', User.query.get(int(user_id)))
                poll.access_results.clear()
                poll.access_results.append(User.query.get(int(user_id)))
        else:
            poll.access_results.clear()

        for i in range(len(poll.questions)):
            question = poll.questions[i]
            form_question = form.questions[i]
            question.question = form_question.question.data
            question.multiple_answers = form_question.multiple_answers.data
            if question.type == 'variants':
                if form_question.kind.data == 'variants':
                    for j in range(len(question.possible_answers)):
                        question.possible_answers[j].option = form_question.options[j].data
                else:
                    question.type = 'text'
                    question.possible_answers.clear()
            else:
                if form_question.kind.data == 'variants':
                    for option in form_question.options:
                        question.type = 'variants'
                        possible_answer = PossibleAnswer(option=option.data, question=question)
                        question.possible_answers.append(possible_answer)

        db.session.add(poll)
        db.session.commit()
        return redirect(url_for('view_poll', poll_id=poll.id))

    # data = {
    #
    #     'repeat_type': poll.repeat_type,
    #     'access_participation': [str(user.id) for user in poll.access_participation],
    #     'access_results': [str(user.id) for user in poll.access_results],
    # }
    form.title.data = poll.title
    form.repeat_type.data = poll.repeat_type
    form.access_participation.data = [str(user.id) for user in poll.access_participation]
    form.title.access_results = [str(user.id) for user in poll.access_results]

    for question in poll.questions:
        if question.type == 'variants':

            form.questions.append_entry({
                'kind': question.type,
                'question': question.question,
                'multiple_answers': question.multiple_answers,
                'options': [option.option for option in question.possible_answers]
            })
        else:
            form.questions.append_entry({
                'kind': question.type,
                'multiple_answers': question.multiple_answers,
                'question': question.question
            })

    return render_template('edit_poll.html', form=form)