예제 #1
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))
예제 #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'))
예제 #3
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
예제 #4
0
def get_poll_result():
    args = request.args
    poll_id = args.get('poll_id')
    if poll_id is not None:
        poll = Poll.query.get(poll_id)
        if poll is not None:
            return jsonify(form.show_poll_result(poll))

    user_id = args['user_id']
    return jsonify(form.make_ready_form(user_id))
예제 #5
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)
예제 #6
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)
예제 #7
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)
예제 #8
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')))
예제 #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)