def api_friend_set_photo(friend_id: int):

    friend = Friend.find_by_id(friend_id)

    if friend is None:
        return jsonify({'error': {'message': 'friend not exists'}}), 404

    if is_access_denied(friend.profile_id):
        return jsonify({'error': {'message': 'forbidden'}}), 403

    data = request.get_json()

    if not isinstance(data, dict):
        return jsonify({'error': {'message': 'invalid input'}}), 400

    image = data.get('image', None)

    if not isinstance(image, dict):
        return jsonify({'error': {'message': 'invalid input'}}), 400

    image_data = image.get('data', None)
    image_type = image.get('type', None)

    #print("PUT: ", image_data)
    #print("PUT: image_data size: ", len(image_data))

    #image_data = bytes(image_data, "utf-8")

    #print("PUT: ", image_data)

    error = friend.set_photo(image_data, image_type)

    #print("{}: {}".format(image_type, image_data))

    return '', 204
def api_friend_get(friend_id: int):

    friend = Friend.find_by_id(friend_id)

    if friend is None:
        return jsonify({'error': {'message': 'friend not found'}}), 404

    output = {'friend_id': friend_id}
    output.update(friend.get_fields())

    return jsonify(output), 200
Exemplo n.º 3
0
def FriendMake(friendName, imgFilePath, image, g):
    '''
    This function makes a Friend() given just a friendName, imageName, and an image.
    
    Inputs:
        friendName          - the name of the friend, an identifier.
        imgFilePath         - the full path to the file image, just so we can record it.
        image               - the image data itself.
        g                   - Our global constants.

    '''
    
    #Make the Photo:
    photo=Photo(g)
    
    #Make it so we don't save this in the database.
    make_transient(photo)
    
    #Add the image data and compute descriptors:
    photo.set_binary(image,'myType')
    
    #Make a profile:
    profile=Profile('*****@*****.**', 'password', {'phone': '555-555-5555', 
                                                   'use_phone': False,
                                                   'use_msg': False})
    #Make it so we don't save this in the database.
    make_transient(profile)
    
    friend=Friend(profile, {'name'      : friendName,
                            'breed'     : imgFilePath,
                            'sex'       : 'Male',
                            'location'  : 'Minneapolis',
                            'staus'     : 'alive'
                                }, g)
    
    make_transient(friend)
    
    #Update our photo:
    friend.photo=photo
    
    return friend
Exemplo n.º 4
0
def profile_photo(friend_id: int):

    friend = Friend.find_by_id(friend_id)

    if friend is None:
        return 'Not found', 404, {'Content-Type': 'text/plain; charset=utf-8'}

    data, type = friend.photo.get_binary()

    if not data or not type:
        return 'Not found', 404, {'Content-Type': 'text/plain; charset=utf-8'}

    return data, 200, {'Content-Type': type}
def api_friend_delete(friend_id: int):

    friend = Friend.find_by_id(friend_id)

    if friend is None:
        return jsonify({'error': {'message': 'friend not found'}}), 404

    if is_access_denied(friend.profile_id):
        return jsonify({'error': {'message': 'forbidden'}}), 403

    db.session.delete(friend)
    db.session.commit()

    return '', 204
def api_profile_friends_get(profile_id: int):
    """
    @return list of friends for profile
    """

    if is_access_denied(profile_id):
        return jsonify({'error': {'message': 'forbidden'}}), 403

    friends = Friend.find_by_profile_id(profile_id)

    if friends is None:
        return jsonify({'error': {'message': 'not found'}}), 404

    out = [f.get_fields(with_id=True) for f in friends]

    return jsonify(out), 200
def api_friend_put(friend_id: int):

    friend = Friend.find_by_id(friend_id)

    if friend is None:
        return jsonify({'error': {'message': 'friend not exists'}}), 404

    if is_access_denied(friend.profile_id):
        return jsonify({'error': {'message': 'forbidden'}}), 403

    fields = decode_input()

    if fields is None:
        return jsonify({'error': {'message': 'invalid input'}}), 400

    friend.update_fields(fields)

    return '', 204
def api_friend_get_photo(friend_id: int):

    #print("accept: ", request.headers.getlist('accept'))
    #print("application/json: ", request.accept_mimetypes["application/json"])
    #print("image/*: ", request.accept_mimetypes["image/*"])

    friend = Friend.find_by_id(friend_id)

    if request.accept_mimetypes["application/json"] >= 1:

        if friend is None:
            return jsonify({'error': {'message': 'friend not exists'}}), 404

        image_data, type = friend.photo.get_base64()
        if image_data is None:
            return jsonify({'error': {'message': 'Photo not uploaded'}}), 404

        #print("GET: ", image_data)
        #print("GET: image_data size: ", len(image_data))

        return jsonify({'image': {'data': image_data, 'type': type}}), 200

    else:

        if friend is None:
            return 'Not found', 404, {
                'Content-Type': 'text/plain; charset=utf-8'
            }

        data, type = friend.photo.get_binary()

        if not data or not type:
            return 'Not found', 404, {
                'Content-Type': 'text/plain; charset=utf-8'
            }

        return data, 200, {'Content-Type': type}
def api_friend_create(profile_id: int):

    if is_access_denied(profile_id):
        return jsonify({'error': {'message': 'forbidden'}}), 403

    profile = Profile.find_by_id(profile_id)

    if profile is None:
        print('profile not found')
        return jsonify({'error': {'message': 'profile not found'}}), 404

    fields = decode_input()

    if fields is None:
        return jsonify({'error': {'message': 'invalid input'}}), 400

    friend, error = Friend.create(profile, fields)

    if friend is None:
        return jsonify({'error': error}), error['status']

    return jsonify({'friend_id': friend.id}), 201, {
        'location': '/api/friend/{}'.format(friend.id)
    }