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 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_add_heart_rate(): """ Tests main.add_heart_rate function """ try: from pymodm import connect from main import add_heart_rate import pytest import models import datetime except ImportError as e: print("Necessary import failed: {}".format(e)) return d = datetime.datetime.now() connect("mongodb://*****:*****@test.test", age=0, age_units="year", heart_rate=[1], heart_rate_times=[d]) u.save() u = models.User.objects.raw({"_id": "*****@*****.**"}).first() ret = add_heart_rate("*****@*****.**", heart_rate=4, time=d) assert ret["user_email"] == "*****@*****.**" assert ret["user_age"] == 0 assert ret["age_units"] == "year" assert ret["heart_rates"] == [1, 4] assert len(ret["heart_rate_times"]) == 2
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 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 test_get_av_hr(): """ Test main.get_av_hr function """ try: from pymodm import connect from main import add_heart_rate, get_av_hr import models import pytest import datetime import time except ImportError as e: print("Necessary import failed: {}".format(e)) return connect("mongodb://*****:*****@test.test", age=0, age_units="day", heart_rate=[1], heart_rate_times=[datetime.datetime.now()]) u.save() ret = get_av_hr("*****@*****.**") assert ret[0] == 1.0 add_heart_rate("*****@*****.**", heart_rate=3, time=datetime.datetime.now()) ret = get_av_hr("*****@*****.**") assert ret[0] == 2.0 d = datetime.datetime.today() time.sleep(3) add_heart_rate("*****@*****.**", heart_rate=3, time=datetime.datetime.now()) ret = get_av_hr("*****@*****.**", since_time=d) assert ret[0] == 3.0 add_heart_rate("*****@*****.**", heart_rate=2, time=datetime.datetime.now()) ret = get_av_hr("*****@*****.**", since_time=d) assert ret[0] == 2.5 add_heart_rate("*****@*****.**", heart_rate=1, time=datetime.datetime.now()) ret = get_av_hr("*****@*****.**", since_time=d) assert ret[0] == 2.0
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