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
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
def problem_photo(problem_id): """Controller for handling adding problem photos. **param** problem_id - id of problem instance for uploading new photos. :content-type: multipart/form-data :fparam file: image file in base64. Content-Type: image/png :fparam name: image name (`'image.jpg'`) :fparam description: description of image (`'some text'`) :return: json object with success message or message with error status. - if success: ``{"added_file": "/uploads/problems/77/df4c22114eb24442e8b6.png"}`` :statuscode 400: error with attaching image or request is invalid :statuscode 200: successfully added """ 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) basewidth = 100 img = Image.open(os.path.join(f_path, f_name)) wpercent = (basewidth / float(img.size[0])) hsize = int((float(img.size[1]) * float(wpercent))) img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS) f_name = '%s%s%s' % (hashed_name.hexdigest(), '.min', extension) img.save(os.path.join(f_path, 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
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
def add_profile_photo(): """Controller provides add and edit function for user's profile photo. :content-type: multipart/form-data :fparam name: name of image file ('photo.jpg') :fparam file: image file in base64. Content-Type: image/png :rtype: JSON :return: json object with image path if success or 400 error message - If request data is invalid: ``{'error': 'error with import file'}`` - If all ok: ``{added_file: "/uploads/user_profile/userid_6/profile_id6.png"}`` :statuscode 400: request is invalid :statuscode 200: photo was successfully added """ 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('%s%s' % (f_path, f_name))) img_file.save(os.path.join(f_path, f_name)) img_path = '%s%s' % (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