Esempio n. 1
0
def add_item_images(uid, item_id):
    """
    Save item's images

    :param uid:
    :param item_id:
    :return string: JSON
    """

    images = list()

    # validate numbers
    try:
        uid = int(uid)
        item_id = int(item_id)
    except ValueError or TypeError:
        return jsonify({'error': "wrong address"})

    # get user data
    user_profile = get_user_by_id(uid)

    # check the user is the owner of the account
    if user_profile.id != g.user.id:
        return jsonify({'error': 'permission denied'}), 403

    # get list of images
    upload_images = request.files.getlist('file')

    # validate images
    if upload_images is []:
        return jsonify({'error': "server didn't get any images"}), 206
    if len(upload_images) > 10:
        return jsonify({'error': "too many images, maximum 10"}), 206

    # prepare data for saving
    for image in upload_images:
        filename = get_path(filename=secure_filename(image.filename),
                            folder=app.config['UPLOAD_FOLDER'])
        abs_path = '%s%s' % (BASE_DIR, filename)
        image.save(abs_path)
        images.append(filename)

    # prepare response
    item_images = [item.serialize for item in add_images(images, item_id)]
    return jsonify(item_images), 200
Esempio n. 2
0
def edit_photo(uid):
    """
    Update user's photo (avatar)

    :param uid:
    :return string: JSON
    """

    # check the user is the owner
    user_profile = get_user_by_id(uid)
    if user_profile.id != g.user.id:
        return jsonify({'error': 'permission denied'}), 403

    # check if the post request has the file part
    if 'file' not in request.files:
        return jsonify({'error': "Server don't get image"}), 206
    photo = request.files['file']

    # if user does not select file, browser also
    # submit a empty part without filename
    if photo.filename == '':
        return jsonify({'error': 'No selected file'}), 200
    if photo and allowed_file(photo.filename, ALLOWED_EXTENSIONS):
        # prepare relative path to the image for database
        filename = get_path(filename=secure_filename(photo.filename),
                            folder=app.config['UPLOAD_FOLDER'])

        # prepare absolute path to the image for saving
        abs_path = '%s%s' % (BASE_DIR, filename)

        # save image
        photo.save(abs_path)

        # update user data
        user = update_user_photo(filename, g.user.id)

        return jsonify(user.serialize), 200
    else:
        return jsonify({'error', "Can't update user photo"}), 200
Esempio n. 3
0
 def test_get_path(self):
     result = get_path('img.jpg', '/image')
     pattern = re_compile(r'^\/image\/\w{2}\/\w{2}\/\w{14}\.jpg$')
     self.assertTrue(pattern.match(result) is not None)