Пример #1
0
def problem_photo(problem_id):
    """ Connected with problem_post. Creating for uploading photos
        with problem.
        :return: json with success if photo have been uploaded
    """
    response = jsonify(), 400
    extension = '.png'
    static_url = '/uploads/problems/%s/' % problem_id
    f_path = os.environ['STATICROOT'] + static_url
    user_id = current_user.uid
    now = time.time()*100000
    unique_key = (int(now)+user_id)
    hashed_name = hashlib.md5(str(unique_key))
    f_name = '%s%s' % (hashed_name.hexdigest(), extension)

    if request.method == 'POST':
        problem_img = request.files['file']
        photo_descr = request.form['description']

        if problem_img and validator.validate_image_file(problem_img):
            if not os.path.exists(f_path):
                os.makedirs(os.path.dirname('%s%s' % (f_path, f_name)))
            problem_img.save(os.path.join(f_path, f_name))
            img_path = '%s%s' % (static_url, f_name)
            db.add_problem_photo(problem_id, img_path, photo_descr, user_id)
            response = json.dumps({'added_file': img_path})
        else:
            response = jsonify(error='error with import file'), 400
    return response
Пример #2
0
def add_profile_photo():
    """Controller provides add and edit function for user's profile photo.
    :return: json object with image path if success or 400 error message
    """
    response = jsonify(), 400
    extension = '.png'
    f_name = 'profile_id%s' % current_user.uid + extension
    static_url = '/uploads/user_profile/userid_%d/' % current_user.uid
    f_path = os.environ['STATICROOT'] + static_url

    if request.method == 'POST':
        img_file = request.files['file']
        if img_file and validator.validate_image_file(img_file):
            if not os.path.exists(f_path):
                os.makedirs(os.path.dirname(f_path + f_name))
            img_file.save(os.path.join(f_path, f_name))
            img_path = static_url + f_name
            db.insert_user_avatar(current_user.uid, img_path)
            response = json.dumps({'added_file': img_path})
        else:
            response = jsonify(error='error with import file'), 400
    return response