Exemple #1
0
def sms_find_poll():
    """Find poll from sms"""
    # Start our response

    resp = MessagingResponse()
    sms = request.values['Body']
    poll = Poll.get_from_code(sms)
    # del session['short_code']

    if 'short_code' in session:

        short_code = session['short_code']
        poll = Poll.get_from_code(short_code)

        if poll.poll_type.multi_select:
            if sms[0].upper() == 'N':
                del session['short_code']
                resp.message('Thank you for responding.')
                return str(resp)
            elif sms[0].upper() == 'Y':
                resp.redirect('/sms/' + short_code)
            else:
                resp.message('Please type "Y" for Yes or "N" for No.')
        resp.redirect('/sms/' + short_code + '/input')
    elif poll:
        resp.redirect('/sms/' + sms)
    else:
        resp.message('That poll does not exist')

    return str(resp)
Exemple #2
0
def render_poll(short_code):
    """Show poll in React."""

    poll = Poll.get_from_code(short_code)
    user = User.get_user()

    logger.info(session)
    logger.info(f'poll: {poll}')

    if poll is not None:  # Ensure this is a valid poll route
        if user.is_admin(poll):
            logger.info("Admin view")
            return render_template('poll-react.html',
                                   poll=poll,
                                   async_mode=socketio.async_mode)
        elif not poll.is_open:
            logger.info("Poll not open")
            return render_template('poll-closed.html', poll=poll)
        elif user.may_respond(poll):
            logger.info('User may respond')
            return render_template('poll-react.html',
                                   poll=poll,
                                   async_mode=socketio.async_mode)
        elif not poll.is_results_visible:
            "User has voted but results not visible"
            route = '/' + poll.short_code + '/success'
        else:
            "User has voted, showing results"
            route = '/' + short_code + '/results'
    else:
        flash('Sorry, that page does not exist.')
        route = '/'

    return redirect(route)
Exemple #3
0
def edit_poll(short_code):
    """Edit poll."""
    poll = Poll.get_from_code(short_code)

    if current_user.is_authenticated and current_user.is_admin(poll):
        return render_template('poll-react.html', poll=poll)
    else:
        return redirect('/' + short_code)
Exemple #4
0
def update_poll(poll_id):
    """Update poll data for a poll id"""
    poll = Poll.query.get(poll_id)
    data = request.form.to_dict()
    resp_data = {'poll_id': poll_id}

    for attr, val in data.iteritems():

        if attr == 'short_code' and Poll.get_from_code(val):
            return 'This short code is already in use.'

        setattr(poll, attr, val)
        resp_data[attr] = val
        poll.updated_at = datetime.now()
        db.session.add(poll)
        db.session.commit()

    return jsonify(resp_data)
Exemple #5
0
def show_results(short_code):

    poll = Poll.get_from_code(short_code)
    user = User.get_user()

    if poll is not None:  # Ensure this is a valid poll route
        if user.is_admin(poll):
            return render_template('poll-react.html', poll=poll)
        elif not poll.is_open:
            return render_template('poll-closed.html', poll=poll)
        elif not poll.is_results_visible:
            route = '/' + poll.short_code + '/success'
        else:
            return render_template('poll-react.html', poll=poll)
    else:
        flash('Sorry, that page does not exist.')
        route = '/'

    return redirect(route)
Exemple #6
0
def sms_show_prompt(short_code):
    poll = Poll.get_from_code(short_code)
    phone = request.values['From']
    user = User.get_from_phone(phone)
    resp = MessagingResponse()

    if ((poll.poll_type.collect_response
         and Response.get_response(poll=poll, user=user))
            or (user in poll.get_users_from_tally()
                and not poll.poll_type.multi_select)):

        resp.message('You have already submitted a response.')

    elif poll.poll_type.collect_response:
        resp.message(poll.prompt + '\n\nEnter your response.')

    else:
        resp.message(poll.prompt + '\n\nEnter # of response option.')

    session['short_code'] = poll.short_code

    return str(resp)
Exemple #7
0
def sms_add_input(short_code):
    poll = Poll.get_from_code(short_code)
    resp = MessagingResponse()
    sms = request.values['Body']
    phone = request.values['From']
    user = User.get_from_phone(phone)

    # Handle responses
    if poll.poll_type.collect_response:
        weight = Response.query.filter(
            Response.poll_id == poll.poll_id).count()
        response = Response(poll_id=poll.poll_id,
                            user_id=user.user_id,
                            text=sms,
                            weight=weight)
        db.session.add(response)
        db.session.commit()

        resp.message('Your response "{}" has been recorded.'.format(
            response.text))
        del session['short_code']

    # Handle tallys
    else:
        # Check that sms is a number
        try:
            index = int(sms)
            responses = poll.responses

            try:
                response = responses[index - 1]

                # Check that response option exists
                if response:

                    # Check that user hasn't already responded
                    if Tally.query.filter(
                            Tally.response_id == response.response_id,
                            Tally.user_id == user.user_id).first():

                        resp.message(
                            'You have already responsed for "{}".'.format(
                                response.text))

                    # Add tally
                    else:
                        tally = Tally(response_id=response.response_id,
                                      user_id=user.user_id)
                        db.session.add(tally)
                        db.session.commit()

                        # emit_new_result(response)
                        emit_response_update(response)

                        resp.message(
                            'Your response "{}" has been recorded.'.format(
                                response.text))

                    if poll.poll_type.multi_select:
                        resp.message('Continue responding? Y/N')
                        return str(resp)
                        # redirect for route for Y/N

                    del session['short_code']

            except:
                resp.message(
                    'Sorry that response does not exist. Please enter another number.'
                )

        except:
            resp.message(
                'Sorry, please enter your response option as a number.')

    return str(resp)
Exemple #8
0
def success(short_code):
    """Show success page."""

    poll = Poll.get_from_code(short_code)

    return render_template('success.html')