Example #1
0
def select_count_option(payload):
    data = payload.get('data')
    option_count_arr = data.get('option_count', [])

    if (not isinstance(option_count_arr, list)) or len(option_count_arr) != 1:
        return form.make_msg(_('Parameters Error'), {
            'name': submit.CANCEL_SELECT_OPTION_COUNT,
            'text': _('Go Back')
        })

    option_count = option_count_arr[0]
    try:
        option_count = int(option_count)
    except ValueError:
        return form.make_msg(_('Parameters Error'), {
            'name': submit.CANCEL_SELECT_OPTION_COUNT,
            'text': _('Go Back')
        })

    if not 2 <= option_count <= 9:
        return form.make_msg(_('Parameters Error'), {
            'name': submit.CANCEL_SELECT_OPTION_COUNT,
            'text': _('Go Back')
        })

    return form.make_poll_form(option_count)
Example #2
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'))
Example #3
0
def do_poll():
    args = request.args
    id_ = args['poll_id']
    poll = Poll.query.get(id_)
    if poll is None:
        return jsonify(form.make_msg(_('Poll Not Found')))

    if datetime.utcnow() > poll.end_datetime:
        response = form.show_poll_result(poll)
        return jsonify(response)

    payload = deepcopy(request.json or {})
    payload.update(args.to_dict())

    response = process_vote(payload)
    if response is None:
        logging.getLogger('poll').info('none respond')
        return jsonify(form.make_msg(_('Action Not Found')))
    return jsonify(response)
Example #4
0
def confirm_poll(payload):
    poll_id = payload['poll_id']
    team_id = payload['team_id']
    user_id = payload['user_id']
    data = payload['data']
    poll_option_ids = data.get('poll_option', [])

    if (not isinstance(poll_option_ids, list)) or len(poll_option_ids) != 1:
        return form.make_msg(_('Please Choose Your Option'), {
            'name': submit.CANCEL_CONFIRM_POLL,
            'text': _('Go Back')
        })

    poll_option_id = poll_option_ids[0]

    poll_option = PollOption.query.get(poll_option_id)

    poll = Poll.query.get(poll_id)
    if (poll is None or poll.team_id != team_id):
        return form.make_msg(_('Poll Not Found'))

    if poll_option is None or poll_option.poll_id != poll.id:
        return form.make_msg(_('Invalid Option'), {
            'name': submit.CANCEL_CONFIRM_POLL,
            'text': _('Go Back')
        })

    us = UserSelection.get_by_poll_id_and_user_id(poll_id, user_id)
    if us is not None:
        return form.make_msg(_('You have voted'), {
            'name': submit.SHOW_RESULT,
            'text': _('Show Result')
        })

    us = UserSelection(poll_id=poll.id,
                       team_id=team_id,
                       user_id=user_id,
                       option_id=poll_option.id)
    us.save()

    return form.show_poll_result(poll)
Example #5
0
def handle_poll():
    args = request.args
    payload = deepcopy(request.json or {})
    payload.update(args.to_dict())
    response = process_create(payload)
    if response is None:
        logging.getLogger('poll').info('none respond')
        return jsonify(
            form.make_msg(_('Operation Failed'), {
                'text': _('Go Back'),
                'name': submit.SETUP_FORM
            }))
    return jsonify(response)
Example #6
0
def show_poll_result():
    args = request.args
    payload = deepcopy(request.json)
    payload.update(args.to_dict())
    user_id = args['user_id']

    if payload['action'] == submit.SHOW_CREATED_RESULT:
        return jsonify(form.show_created_polls(user_id))

    if payload['action'] == submit.SHOW_CREATED_RESULT:
        return jsonify(form.show_joined_polls(user_id))

    if payload['action'] == submit.SHOW_RESULT:
        data = payload['data']
        poll_id = data.get('poll_id')
        poll = Poll.query.get(poll_id)
        if poll:
            return form.show_poll_result(poll)
        else:
            return jsonify(form.make_msg(_('Poll Not Found')))

    return jsonify(form.make_msg(_('Action Not Found')))
Example #7
0
def show_poll(payload):
    id_ = payload['poll_id']
    user_id = payload['user_id']
    poll = Poll.query.get(id_)
    if poll is None:
        return form.make_msg(_('Poll Not Found'))

    if datetime.utcnow() > poll.end_datetime:
        return form.show_poll_result(poll)

    us = UserSelection.get_by_poll_id_and_user_id(poll.id, user_id)
    if us:
        response = form.show_poll_result(poll)
    else:
        response = form.show_poll(poll)

    return response
Example #8
0
def get_poll():
    args = request.args
    id_ = args['poll_id']
    user_id = args['user_id']
    poll = Poll.query.get(id_)
    if poll is None:
        return jsonify(form.make_msg(_('Poll Not Found')))

    if datetime.utcnow() > poll.end_datetime:
        response = form.show_poll_result(poll)
        return jsonify(response)

    us = UserSelection.get_by_poll_id_and_user_id(poll.id, user_id)
    if us:
        response = form.show_poll_result(poll)
    else:
        response = form.show_poll(poll)

    return jsonify(response)
Example #9
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)
Example #10
0
def getting_start(payload):
    visit_key = payload.get('visit_key', None)
    if visit_key is None:
        return form.make_msg(_('Failed'))
    return form.make_start_form(visit_key)