Ejemplo n.º 1
0
def refresh_poll(payload):
    data = payload['data']
    token = data.get('token')
    poll = Poll.get_by_visit_key(token)
    if poll:
        return form.show_poll_result(poll)
    return form.make_msg(_('Poll Not Found'))
Ejemplo n.º 2
0
def main():
    app = create_app()
    with app.test_request_context():
        expired_polls = Poll.get_multi_expired(EXPIRED_DURATION)
        print('Schedule: {}'.format(expired_polls))
        for poll in expired_polls:
            notify_creator(poll)
Ejemplo n.º 3
0
def show_created_polls(user_id):
    polls = Poll.get_multi_by_user_id(user_id)
    options = [
        SelectOption(text=each.description, value=each.id) for each in polls
    ]
    form = Form()
    form.add_actions(
        Select(label=_('Polls I Created'),
               required=True,
               name='poll_id',
               placeholder=_('Choose poll'),
               options=options),
        Submit(name=submit.SHOW_RESULT, text=_('View Result')))
    return form.render()
Ejemplo n.º 4
0
def start_poll():
    visit_key = request.args.get('visit_key')
    user_id = request.args.get('user_id')
    poll = Poll.get_by_visit_key(visit_key)
    if poll:
        if datetime.utcnow() > poll.end_datetime:
            return jsonify(form.show_poll_result(poll))

        if poll.user_id == user_id:
            response = form.show_poll_result(poll)
        else:
            response = form.show_poll(poll)
        return jsonify(response)

    return jsonify(form.make_start_form(visit_key))
Ejemplo n.º 5
0
def preview_poll():
    file_type = request.args.get('file_type')
    poll_id = request.args.get('poll_id')
    visit_key = request.args.get('token')
    poll = Poll.get_by_id_and_visit_key(poll_id, visit_key)
    if poll is None:
        return abort(404)
    chart = create_result_chart(poll)
    if file_type == 'svg':
        return chart.render_response()
    png = chart.render_to_png()
    response = make_response(png)
    response.headers.set('Content-Type', 'image/png')
    response.headers.set('Content-Disposition',
                         'attachment',
                         filename='poll_{}.png'.format(poll_id))
    return response
Ejemplo n.º 6
0
def make_ready_form(user_id):
    created_poll_count = Poll.count_by_user_id(user_id)
    joined_poll_count = UserSelection.count_by_user_id(user_id)

    form = Form()

    if created_poll_count + joined_poll_count == 0:
        form.add_action(Section(value=_("You have not joined any polls yet")))

    if created_poll_count > 0:
        form.add_action(
            Submit(name=submit.SHOW_CREATED_RESULT, text=_('Polls I Created')))

    if joined_poll_count > 0:
        form.add_action(
            Submit(name=submit.SHOW_CREATED_RESULT, text=_('Polls I Joined')))

    return form.render()
Ejemplo n.º 7
0
def create_poll(payload):
    hubot_token = payload['token']
    visit_key = payload['visit_key']
    team_id = payload['team_id']
    user_id = payload['user_id']
    message_key = payload['message_key']
    data = payload['data']

    found_poll = Poll.get_by_visit_key(visit_key)
    if found_poll is not None:
        return form.make_msg(_("Parameters Error", ))

    option_count = 0
    try:
        option_count = int(data.get('option_count', 0))
    except (TypeError, ValueError):
        return form.make_msg(_("Parameters Error"), {
            'name': submit.CANCEL_SELECT_OPTION_COUNT,
            'text': _('Go Back')
        })

    options = []
    for idx in range(option_count):
        options.append(data['option_{}'.format(idx + 1)])

    raw_end_timestamp = data.get('end_timestamp', 0)
    try:
        end_timestamp = int(raw_end_timestamp)
    except (ValueError, TypeError) as e:
        logging.getLogger('stalls').info("end_timetamp {} error: {}".format(
            raw_end_timestamp, e.get_message()))
        return form.make_msg(_("Invalid Expiration"))

    end_datetime = datetime.fromtimestamp(end_timestamp)
    if end_datetime <= datetime.utcnow():
        return form.make_msg(_("Invalid Expiration"))

    poll = Poll(
        hubot_token=hubot_token,
        visit_key=visit_key or gen_visit_key(),
        team_id=team_id,
        user_id=user_id,
        description=data.get('description'),
        option_count=option_count,
        is_anonymous=data.get('is_anonymous', False),
        end_datetime=end_datetime,
        message_key=message_key,
    )

    poll.options = options
    poll.members = [_f for _f in data.get('members', []) if _f]
    poll.channels = [_f for _f in data.get('channels', []) if _f]
    poll.save()

    notify_members(payload, poll)
    notify_channels(payload, poll)

    poll.state = Poll.STATE_SENT
    poll.save()

    for each in options:
        option = PollOption(label=each, poll_id=poll.id)
        option.save(_commit=False)

    db.session.commit()

    return form.show_poll_result(poll)