Exemple #1
0
def api_get_love():
    sender = request.args.get('sender')
    recipient = request.args.get('recipient')

    limit = request.args.get('limit')
    if limit:
        try:
            limit = int(limit)
        except ValueError:
            return make_response(
                'Invalid value for "limit": {0!r}'.format(limit),
                LOVE_BAD_PARAMS_STATUS_CODE)

    if not (sender or recipient):
        return make_response(
            'You must provide either a sender or a recipient.',
            LOVE_BAD_PARAMS_STATUS_CODE)

    love_found = get_love(
        sender_username=sender,
        recipient_username=recipient,
        limit=limit,
    ).get_result()

    return make_json_response([{
        'sender':
        Employee.key_to_username(love.sender_key),
        'recipient':
        Employee.key_to_username(love.recipient_key),
        'message':
        love.message,
        'timestamp':
        love.timestamp.isoformat(),
    } for love in love_found])
Exemple #2
0
def autocomplete(request):
    """This is the implementation of the autocomplete API endpoint. It's shared between
    the api view and web view that is called from Javascript, only the authorization
    checks are different.
    """
    matches = employees_matching_prefix(request.args.get('term', None))
    users = [{
        'label': u'{} ({})'.format(full_name, username),
        'value': unicode(username),
        'avatar_url': photo_url,
    } for full_name, username, photo_url in matches]
    return make_json_response(users)
Exemple #3
0
def api_get_leaderboard():
    department = request.args.get('department', None)
    timespan = request.args.get('timespan', TIMESPAN_THIS_WEEK)

    (top_lover_dicts,
     top_loved_dicts) = get_leaderboard_data(timespan, department)

    top_lover = [{
        'full_name': lover['employee'].full_name,
        'username': lover['employee'].username,
        'department': lover['employee'].department,
        'love_count': lover['num_sent'],
    } for lover in top_lover_dicts]

    top_loved = [{
        'full_name': loved['employee'].full_name,
        'username': loved['employee'].username,
        'department': loved['employee'].department,
        'love_count': loved['num_received'],
    } for loved in top_loved_dicts]
    final_result = {'top_loved': top_loved, 'top_lover': top_lover}
    return make_json_response(final_result)