def post_user(): input = request.get_json() try: email_v, age_v, hr_v = validate_user(input) user = models.User.objects.raw({"_id": email_v}).first() print('New heart rate added to existing user.') add_heart_rate(email_v, hr_v, datetime.datetime.now()) print_user(email_v) done = { "user": email_v, "status": "verified and updated", "latest heart_rate": hr_v } return jsonify(done), 200 except KeyError: data = {"message": 'POST to /api/heart_rate failed.'} return jsonify(data), 400 except TypeError: data = {"message": 'POST to /api/heart_rate failed.'} return jsonify(data), 400 except errors.DoesNotExist: data = {"message": 'New user was created.'} create_user(email=email_v, age=age_v, heart_rate=hr_v) print_user(email_v) return jsonify(data), 400
def test_createUser(): """ Function that tests whether createUser function from main is working properly """ from main import create_user email = '*****@*****.**' age = 21 heart_rate = 75 create_user(email, age, heart_rate) user = models.User.objects.raw({"_id": "*****@*****.**"}).first() assert user.email == '*****@*****.**' assert user.age == 21 assert user.heart_rate == [75]
def post_heart_rate(): '''Sends request to add a new user to the database. If user exists, then the heart rate is added to the existing user. ''' r = request.get_json() print(r) if is_subject_in_db(r['user_email']): # if subject already exists main.add_heart_rate(r['user_email'], r['heart_rate'], datetime.datetime.now()) text = 'Heart rate added' else: main.create_user(r['user_email'], r['user_age'], r['heart_rate'], datetime.datetime.now()) text = 'New user created' return jsonify({'info': text})
def postInfo(): """ Communicates with database to store user info :param json: json data containing the user email, age, heart rate, and date selection :raises ValueError: Error raised if data is not in the correct format """ time = datetime.datetime.now() logging.debug("/heart_rate: time = " + str(time)) try: r = request.get_json() email = r["user_email"] age = r["user_age"] heart_rate = r["heart_rate"] logging.info("/heart_rate: data received and extracted from json") logging.debug("/heart_rate: email = " + str(email)) logging.debug("/heart_rate: age = " + str(age)) logging.debug("/heart_rate: heart rate = " + str(heart_rate)) except: raise ValueError("Submit json data for email, age, and heart_rate") logging.warning("/heart_rate: not all data points found or not json") return 400 try: user = add_heart_rate(email, heart_rate, time) logging.info("/heart_rate: user located in database and info appended") print("New heart rate information was added") except: user = create_user(email, age, heart_rate, time) logging.info("/heart_rate: user was not found and was created") print("A new user was created") return 200
def test_addHR(): """ Function that tests whether add heart rate is functioning properly """ from main import create_user, add_heart_rate email = '*****@*****.**' age = 21 heart_rate = 75 create_user(email, age, heart_rate) HR2 = 72 time2 = datetime.datetime(2018, 3, 23, 20, 0, 0, 000000) add_heart_rate(email, HR2, time2) user = models.User.objects.raw({"_id": email}).first() assert user.email == '*****@*****.**' assert user.age == 21 assert user.heart_rate == [heart_rate, HR2]
def register(): if request.method == "POST": username = request.form["username"] password = request.form["password"] name = request.form['name'] error = create_user(name,username,generate_password_hash(password)) if error is None: return redirect(url_for("login")) else: return render_template("register.html",error=error) return render_template("register.html",error=None)
def post(): """ store heart rate measurement for the user with user email :param email: str email of the user :param heart_rate: number heart_rate measurement of the user :param time: the datetime of the heart_rate measurement :return: json file format """ r = request.get_json() email = r["user_email"] age = r["user_age"] heart_rate = r["heart_rate"] time = datetime.datetime.now() try: add_heart_rate(email, heart_rate, time) data = {"message": "Succeed"} return jsonify(data), 200 except: print("new user was created") create_user(email, age, heart_rate, time) return jsonify(r["user_email"]), 400
def test_max_adjacent(): from main import get_info, add_heart_rate, create_user, print_user from main import check_tachycardia import datetime create_user('*****@*****.**', '23', '55', datetime.datetime.now()) add_heart_rate('*****@*****.**', '60', datetime.datetime.now()) user_info = get_info('*****@*****.**') assert user_info.email == '*****@*****.**' assert user_info.age == 23 assert user_info.heart_rate == [55, 60] user_info = print_user('*****@*****.**') assert user_info.email == '*****@*****.**' assert user_info.age == 23 assert user_info.heart_rate == [55, 60] tachycardia = check_tachycardia(101, 23) assert tachycardia is True return
def create_user_route(): if request.method == 'POST': newuser = request.json user_status = create_user(newuser) if "Error" in user_status: return jsonify(user_status) else: message = { "Status": "Success", "Message": "User successfully created", "UserID": user_status } return jsonify(message)
def test_create_user(): try: from pymodm import connect from main import create_user import pytest import models import datetime except ImportError as e: print("Necessary import failed: {}".format(e)) return connect("mongodb://*****:*****@test.test", age=5, age_units="month", hr=1) u = models.User.objects.raw({"_id": "*****@*****.**"}).first() assert u.email == "*****@*****.**" assert u.age == 5 assert u.age_units == "month" assert u.heart_rate == [1] assert len(u.heart_rate_times) == 1 assert u.email == vals["user_email"] assert u.age == vals["user_age"] assert u.age_units == vals["age_units"] assert u.heart_rate == vals["heart_rates"]
def process(): """Function that processes the pre-processed image. :returns: json with information to display pre- and post- \ processed images """ logging.basicConfig(filename='back_end.log', format='%(asctime)s \ %(message)s', datefmt='%m/%d/%Y %I:%M:%S %pi') logging.info("Begin app route to /process") info = request.json try: email = info["user_email"] except KeyError: print("No email input") return jsonify("no email input"), 400 check_email = Check_For_User(email) if check_email.user_exists is False: create_user(email) print(str(email) + " was not found. User created") try: pre_img = info["pre_b64_string"] except KeyError: print("Please provide pre_image base64 string") return jsonify("no pre_image input"), 400 try: method = info["proc_method"] except KeyError: print("no processing method selected") return jsonify("no proc_method input"), 400 try: extension = info["file_type"] except KeyError: print("Please provide file_type") return jsonify("no file type provided"), 400 try: header = info["header"] except KeyError: print("no header string") return jsonify("no header string"), 400 print(extension) if extension == 'JPEG': extension = '.jpg' elif extension == 'PNG': extension = '.png' elif extension == 'TIFF': extension = '.tif' else: raise ValueError("Did not select an appropriate extension!") if isinstance(email, str) and isinstance(method, str): if check_list_of_string(pre_img): pass else: print("list of pre_img is not string") return jsonify("list of pre_img is not string!"), 400 else: print("Please provide information in string format!") return jsonify("Please provide information in string format!"), 400 if os.path.exists("images/"): pass else: os.mkdir("images/") jpgCount = get_user_pre_pics_count(email) print(jpgCount) current_time = datetime.datetime.now() processed_list = [] just_for_zip_list = [] return_size_list = [] processed_histograms = [] pre_img_list = [] pre_img_histograms = [] jpgFileNum = jpgCount if 'zip' in header: pre_img = pre_img[0] pre_img = b64_zip_to_b64_strings(pre_img) pass elif 'jpeg' in header: pass else: raise ValueError("Input is not a b64 zip or jpg list!") for i in range(len(pre_img)): img = pre_img[i] print(img[0:100]) print(type(img)) if method == "Histogram Equalization": if jpgFileNum == 0: os.chmod('images/', stat.S_IRWXU) os.makedirs(('images/' + str(email))) os.chmod(('images/' + str(email)), stat.S_IRWXU) jpgFileNum = jpgFileNum + 1 filename = 'images/' + str(email) + '/' + str(jpgFileNum) + '.jpg' try: with open(filename, "wb") as image_out: image_out.write(base64.b64decode(img)) except FileNotFoundError: os.chmod('images/', stat.S_IRWXU) os.makedirs(('images/' + str(email))) os.chmod(('images/' + str(email)), stat.S_IRWXU) filename = 'images/' + str(email) + '/' + str( jpgFileNum) + '.jpg' with open(filename, "wb") as image_out: image_out.write(base64.b64decode(img)) save_image(email, jpgFileNum) add_histo(email) imgArray, a_type, m, w, z = convert_image_to_np_array(img) hist_image = histo_equal(img) histogram_of_pre_img = create_histo(imgArray) histogram_of_post_img = create_histo(hist_image) hist_img64 = convert_processed_np_array_to_base64(hist_image) just_for_zip_list.append(hist_img64) hist_img64 = bytes_to_string(hist_img64) histogram_of_pre_img = bytes_to_string(histogram_of_pre_img) histogram_of_post_img = bytes_to_string(histogram_of_post_img) processed_list.append(getHeader(extension) + str(hist_img64)) pre_img_list.append(getHeader(extension) + img) processed_histograms.append(getHeader() + str(histogram_of_post_img)) pre_img_histograms.append(getHeader() + str(histogram_of_pre_img)) return_size = (str(m) + 'X' + str(w) + ' pixels') return_size_list.append(return_size) if i == len(pre_img) - 1 and i > 0: # we need to zip new_time = datetime.datetime.now() duration = new_time - current_time zipped_list = b64_strings_to_b64_zip(just_for_zip_list, extension) new_info = { "user_email": email, "proc_method": method, "pre_b64_string": pre_img_list, "post_b64_string": processed_list, "pre_histogram": pre_img_histograms, "post_histograms": processed_histograms, "action_time": str(duration), "upload_time": time2str(current_time), "pic_size": return_size_list, "b64_zip_out": getHeader(".zip") + zipped_list } make_tmp(new_info) return jsonify(new_info), 200 elif i == len(pre_img) - 1 and i == 0: # we don't need to zip. single image new_time = datetime.datetime.now() duration = new_time - current_time new_info = { "user_email": email, "proc_method": method, "pre_b64_string": pre_img_list, "post_b64_string": processed_list, "pre_histogram": pre_img_histograms, "post_histograms": processed_histograms, "action_time": str(duration), "upload_time": time2str(current_time), "pic_size": return_size_list } make_tmp(new_info) return jsonify(new_info), 200 elif method == "Contrast Stretching": if jpgFileNum == 0: os.chmod('images', stat.S_IRWXU) os.makedirs(('images/' + str(email))) os.chmod(('images/' + str(email)), stat.S_IRWXU) jpgFileNum = jpgFileNum + 1 filename = 'images/' + str(email) + '/' + str(jpgFileNum) + '.jpg' try: with open(filename, "wb") as image_out: image_out.write(base64.b64decode(img)) except FileNotFoundError: os.chmod('images/', stat.S_IRWXU) os.makedirs(('images/' + str(email))) os.chmod(('images/' + str(email)), stat.S_IRWXU) filename = 'images/' + str(email) + '/' + str( jpgFileNum) + '.jpg' with open(filename, "wb") as image_out: image_out.write(base64.b64decode(img)) save_image(email, jpgFileNum) add_contr(email) imgArray, a_type, m, w, z = convert_image_to_np_array(img) hist_image = contr_stretch(img) histogram_of_pre_img = create_histo(imgArray) histogram_of_post_img = create_histo(hist_image) hist_img64 = convert_processed_np_array_to_base64(hist_image) just_for_zip_list.append(hist_img64) hist_img64 = bytes_to_string(hist_img64) histogram_of_pre_img = bytes_to_string(histogram_of_pre_img) histogram_of_post_img = bytes_to_string(histogram_of_post_img) processed_list.append(getHeader(extension) + str(hist_img64)) pre_img_list.append(getHeader(extension) + img) processed_histograms.append(getHeader() + str(histogram_of_post_img)) pre_img_histograms.append(getHeader() + str(histogram_of_pre_img)) return_size = (str(m) + 'X' + str(w) + ' pixels') return_size_list.append(return_size) if i == len(pre_img) - 1 and i > 0: # we need to zip new_time = datetime.datetime.now() duration = new_time - current_time zipped_list = b64_strings_to_b64_zip(just_for_zip_list, extension) new_info = { "user_email": email, "proc_method": method, "pre_b64_string": pre_img_list, "post_b64_string": processed_list, "pre_histogram": pre_img_histograms, "post_histograms": processed_histograms, "action_time": str(duration), "upload_time": time2str(current_time), "pic_size": return_size_list, "b64_zip_out": getHeader(".zip") + zipped_list } make_tmp(new_info) return jsonify(new_info), 200 elif i == len(pre_img) - 1 and i == 0: # we don't need to zip. single image new_time = datetime.datetime.now() duration = new_time - current_time new_info = { "user_email": email, "proc_method": method, "pre_b64_string": pre_img_list, "post_b64_string": processed_list, "pre_histogram": pre_img_histograms, "post_histograms": processed_histograms, "action_time": str(duration), "upload_time": time2str(current_time), "pic_size": return_size_list } make_tmp(new_info) return jsonify(new_info), 200 elif method == "Log Compression": if jpgFileNum == 0: os.chmod('images', stat.S_IRWXU) os.makedirs(('images/' + str(email))) os.chmod(('images/' + str(email)), stat.S_IRWXU) jpgFileNum = jpgFileNum + 1 filename = 'images/' + str(email) + '/' + str(jpgFileNum) + '.jpg' try: with open(filename, "wb") as image_out: image_out.write(base64.b64decode(img)) except FileNotFoundError: os.chmod('images/', stat.S_IRWXU) os.makedirs(('images/' + str(email))) os.chmod(('images/' + str(email)), stat.S_IRWXU) filename = 'images/' + str(email) + '/' + str( jpgFileNum) + '.jpg' with open(filename, "wb") as image_out: image_out.write(base64.b64decode(img)) save_image(email, jpgFileNum) add_log(email) imgArray, a_type, m, w, z = convert_image_to_np_array(img) hist_image = logComp(img) histogram_of_pre_img = create_histo(imgArray) histogram_of_post_img = create_histo(hist_image) hist_img64 = convert_processed_np_array_to_base64(hist_image) just_for_zip_list.append(hist_img64) hist_img64 = bytes_to_string(hist_img64) histogram_of_pre_img = bytes_to_string(histogram_of_pre_img) histogram_of_post_img = bytes_to_string(histogram_of_post_img) processed_list.append(getHeader(extension) + str(hist_img64)) pre_img_list.append(getHeader(extension) + img) processed_histograms.append(getHeader() + str(histogram_of_post_img)) pre_img_histograms.append(getHeader() + str(histogram_of_pre_img)) return_size = (str(m) + 'X' + str(w) + ' pixels') return_size_list.append(return_size) if i == len(pre_img) - 1 and i > 0: # we need to zip new_time = datetime.datetime.now() duration = new_time - current_time zipped_list = b64_strings_to_b64_zip(just_for_zip_list, extension) new_info = { "user_email": email, "proc_method": method, "pre_b64_string": pre_img_list, "post_b64_string": processed_list, "pre_histogram": pre_img_histograms, "post_histograms": processed_histograms, "action_time": str(duration), "upload_time": time2str(current_time), "pic_size": return_size_list, "b64_zip_out": getHeader(".zip") + zipped_list } make_tmp(new_info) return jsonify(new_info), 200 elif i == len(pre_img) - 1 and i == 0: # we don't need to zip. single image new_time = datetime.datetime.now() duration = new_time - current_time new_info = { "user_email": email, "proc_method": method, "pre_b64_string": pre_img_list, "post_b64_string": processed_list, "pre_histogram": pre_img_histograms, "post_histograms": processed_histograms, "action_time": str(duration), "upload_time": time2str(current_time), "pic_size": return_size_list } make_tmp(new_info) return jsonify(new_info), 200 elif method == "Reverse Video": if jpgFileNum == 0: os.chmod('images', stat.S_IRWXU) os.makedirs(('images/' + str(email))) os.chmod(('images/' + str(email)), stat.S_IRWXU) jpgFileNum = jpgFileNum + 1 filename = 'images/' + str(email) + '/' + str(jpgFileNum) + '.jpg' try: with open(filename, "wb") as image_out: image_out.write(base64.b64decode(img)) except FileNotFoundError: os.chmod('images/', stat.S_IRWXU) os.makedirs(('images/' + str(email))) os.chmod(('images/' + str(email)), stat.S_IRWXU) filename = 'images/' + str(email) + '/' + str( jpgFileNum) + '.jpg' with open(filename, "wb") as image_out: image_out.write(base64.b64decode(img)) save_image(email, jpgFileNum) add_rever(email) imgArray, a_type, m, w, z = convert_image_to_np_array(img) hist_image = reverseVid(img) histogram_of_pre_img = create_histo(imgArray) histogram_of_post_img = create_histo(hist_image) hist_img64 = convert_processed_np_array_to_base64(hist_image) just_for_zip_list.append(hist_img64) hist_img64 = bytes_to_string(hist_img64) histogram_of_pre_img = bytes_to_string(histogram_of_pre_img) histogram_of_post_img = bytes_to_string(histogram_of_post_img) processed_list.append(getHeader(extension) + str(hist_img64)) pre_img_list.append(getHeader(extension) + img) processed_histograms.append(getHeader() + str(histogram_of_post_img)) pre_img_histograms.append(getHeader() + str(histogram_of_pre_img)) return_size = (str(m) + 'X' + str(w) + ' pixels') return_size_list.append(return_size) if i == len(pre_img) - 1 and i > 0: # we need to zip new_time = datetime.datetime.now() duration = new_time - current_time zipped_list = b64_strings_to_b64_zip(just_for_zip_list, extension) new_info = { "user_email": email, "proc_method": method, "pre_b64_string": pre_img_list, "post_b64_string": processed_list, "pre_histogram": pre_img_histograms, "post_histograms": processed_histograms, "action_time": str(duration), "upload_time": time2str(current_time), "pic_size": return_size_list, "b64_zip_out": getHeader(".zip") + zipped_list } make_tmp(new_info) return jsonify(new_info), 200 elif i == len(pre_img) - 1 and i == 0: # we don't need to zip. single image new_time = datetime.datetime.now() duration = new_time - current_time new_info = { "user_email": email, "proc_method": method, "pre_b64_string": pre_img_list, "post_b64_string": processed_list, "pre_histogram": pre_img_histograms, "post_histograms": processed_histograms, "action_time": str(duration), "upload_time": time2str(current_time), "pic_size": return_size_list } make_tmp(new_info) return jsonify(new_info), 200
from main import create_user, bcrypt from getpass import getpass name = raw_input('Name : ') access = 'superadmin' email = raw_input("Email : ") for i in range(3): pwd = getpass() confirmpwd = getpass("Confirm Password : "******"Super User created successfully !!" break else: print "Error creating super user contact the admin :(" break else: print "Error Passwords don't match try again !!"
def heart_rate(): """ Adds heart rate to dictionary related to user email Creates new user if user does not exist :param email: str email of the new user :param age: number age of the new user :param heart_rate: number initial heart_rate of this new user :returns message: json indicating either job complete, KeyError, TypeError """ r = request.get_json() # parses the POST request body as JSON import logging logging.basicConfig(filename="server_log.txt", format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') try: if not isinstance(r['user_email'], str): logging.debug('TypeError: user_email is not a string') message = { "TypeError": "User_email is not a string", } return jsonify(message), 400 except KeyError: logging.debug('KeyError: incorrect user_email key') message = { "KeyError": "Incorrect user_email key", } return jsonify(message), 400 try: if not isinstance(r['user_age'], int): logging.debug('TypeError: user_age is not an int') message = { "TypeError": "User_age is not an int", } return jsonify(message), 400 except KeyError: logging.debug('KeyError: incorrect user_age key') message = { "KeyError": "Incorrect user_age key", } return jsonify(message), 400 try: if isinstance(r['heart_rate'], int): a = 1 elif isinstance(r['heart_rate'], float): a = 1 else: logging.debug('TypeError: Heart_rate is not an int/float') message = { "TypeError": "Heart_rate is not an int or float", } return jsonify(message), 400 except KeyError: logging.debug('KeyError: incorrect heart_rate key') message = { "KeyError": "Incorrect heart_rate key", } return jsonify(message), 400 try: add_heart_rate(r['user_email'], r['heart_rate'], datetime.datetime.now()) except Exception: logging.debug('ExceptionError: new user will be created') create_user(r['user_email'], r['user_age'], r['heart_rate'], datetime.datetime.now()) message = { "message": "Post Completed", } return jsonify(message), 200