def my_patients_delete(patient_cf, claims, mongo): if patient_cf is None: return error_message("patient_cf is a mandatory field") if claims["type"] != "doctor": return error_message("only a doctor can remove a patient!") try: doctor = mongo.db.doctors.find_one({'_id': claims["identity"]}) if patient_cf not in doctor["patients"] and patient_cf not in doctor["patient_requests"]: return error_message("this patient is not subscribed nor asked for subscription") mongo.db.doctors.update_one( { "_id": claims["identity"] }, { "$pull": { "patients": patient_cf, "patient_requests": patient_cf } } ) return jsonify(status="ok") except Exception as e: return make_response(str(e), 500)
def doctor_register(params, mongo): if params is None: return False, error_message("mime type not accepted") fields = ["id", "email", "password", "name", "surname", "address", "phone_number"] for field in fields: if field not in params: return False, error_message(field + " is a mandatory field") try: if mongo.db.doctors.find_one({'_id': params["id"]}) is not None: return False, error_message("a user with this id already exists!") mongo.db.doctors.insert_one( { "_id": str(params["id"]), "email": params["email"], "password": sha256.hash(params["password"]), "name": params["name"], "surname": params["surname"], "address": params["address"], "phone_number": params["phone_number"], "patients": [], "patient_requests": [], "signals": [], "profile_picture": "" } ) return True, None except Exception as e: return False, make_response(str(e), 500)
def my_alerts_delete(identity, cf, claims, mongo): if claims["type"] != "doctor": return error_message("only doctors can delete signals!") if "id" is None: return error_message("id is a mandatory field!") if "cf" is None: return error_message("cf is a mandatory field!") try: doc = mongo.db.doctors.find_one({"_id": claims["identity"]}) if doc is None: return error_message("doctor does not exists!") mongo.db.doctors.find_one_and_update( {"_id": claims["identity"]}, {"$pull": { "signals": { "cf": cf, "id": identity } }}) return jsonify(status="ok") except Exception as e: return error_message(str(e))
def my_doctors_delete(doctor_id, claims, mongo): if doctor_id is None: return error_message("doctor_id is a mandatory field") if claims["type"] != "user": return error_message( "only users can delete their subscription to a doctor!") try: doctor = mongo.db.doctors.find_one({'_id': doctor_id}) if doctor is None: return error_message("doctor does not exists!") if claims["identity"] not in doctor["patients"] and claims[ "identity"] not in doctor["patient_requests"]: return error_message( "you are not subscribed or asked for subscription to this doctor" ) mongo.db.doctors.update_one( {'_id': doctor_id}, {"$pull": { "patients": claims["identity"] }}) mongo.db.doctors.update_one( {'_id': doctor_id}, {"$pull": { "patient_requests": claims["identity"] }}) return jsonify(status="ok") except Exception as e: return make_response(str(e), 500)
def facts_post(params, mongo): if params is None or "fact" not in params: return error_message("no fact provided!") try: mongo.db.facts.insert_one({"text": params["fact"]}) return jsonify(status="ok") except Exception as e: return error_message(str(e))
def my_alerts_get(claims, mongo): if claims["type"] != "doctor": return error_message("only doctors can see signals!") try: doc = mongo.db.doctors.find_one({"_id": claims["identity"]}) if doc is None: return error_message("doctor does not exists!") return jsonify(status="ok", signals=doc["signals"]) except Exception as e: return error_message(str(e))
def coordinates_get(mongo): try: response = [] for x in mongo.db.doctors.find({}, {"name": 1, "surname": 1, "address": 1}): response.append(( { "doctor": x # vuoto } )) if len(response) == 0: return error_message("empty list!") return jsonify(status='ok', payload=response) except Exception as e: return error_message(str(e))
def login(user_type, identity, password, mongo): if identity is None or password is None: return False, error_message("Id and password are mandatory fields!") try: user = mongo.db[user_type+"s"].find_one({"_id": identity}) if user is None: return False, error_message("user does not exists!") pwd = user["password"] if not sha256.verify(password, pwd): return False, error_message("password is not correct") return True, None except Exception as e: return False, make_response(str(e), 500)
def my_recordings_put(params, claims, mongo): if claims["type"] != "user": return error_message("only users can send recordings") fields = {"spo2", "hr"} for json in params: for field in fields: if field not in json: return error_message("malformed data packet, " + field + " missing!") if "timestamp" not in json: return error_message("timestamp is a mandatory parameter!") update = { "$set": { "type": "recording", "spo2_rate": json["spo2_rate"], "hr_rate": json["hr_rate"], }, "$push": { "spo2": json["spo2"], "hr": json["hr"], "movements_count": json["movements_count"] } } if "oxy_event" in json and json["oxy_event"] is not None: update["$push"]["oxy_events"] = json["oxy_event"] if "dia_event" in json and json["dia_event"] is not None: update["$push"]["dia_events"] = json["dia_event"] try: mongo.db[claims["identity"]].find_one_and_update( {"_id": json["timestamp"]}, update, upsert=True) except Exception as e: return error_message(str(e)) return jsonify(status="ok")
def facts_get(mongo): try: facts = [] for document in mongo.db.facts.find(): facts.append(document["text"]) return jsonify(status="ok", payload=facts) except Exception as e: return error_message(str(e))
def my_doctors_post(params, claims, mongo): if params is None: return error_message("mime type not accepted") if claims["type"] != "user": return error_message("only users can subscribe to doctors!") if "doctor_id" not in params: return error_message("doctor id is a mandatory parameter") try: doctor = mongo.db.doctors.find_one({'_id': params["doctor_id"]}) if doctor is None: return error_message("this doctor does not exists!") elif claims["identity"] in doctor["patients"]: return error_message("already a patient of this doctor") elif claims["identity"] in doctor["patient_requests"]: return error_message("already sent a request to this doctor") mongo.db.doctors.update_one( {"_id": params["doctor_id"]}, {"$push": { "patient_requests": claims["identity"] }}) return jsonify(status="ok") except Exception as e: return make_response(str(e), 500)
def habits_post(params, claims, mongo): if claims["type"] != "user": return error_message("Only users can add habits!") fields = ["caffe", "drink", "isSport", "isCena"] for field in fields: if field not in params: return error_message(field + " is a mandatory parameter!") try: today = date.today().strftime("%d/%m/%Y") mongo.db[claims["identity"]].find_one_and_update( { "_id": today, "type": "habit" }, { "$set": { "type": "habit", "coffee": { "type": params["caffe"]["tipo"], "qty": params["caffe"]["totale"] }, "drink": { "type": params["drink"]["tipo"], "qty": params["drink"]["totale"] }, "sport": params["isSport"], "dinner": params["isCena"] } }, upsert=True) return jsonify(status="ok") except Exception as e: return error_message(str(e))
def send_to_doctor(params, claims, mongo): if claims["type"] != "user": return error_message("only users can send requests to doctors") if "id" not in params: return error_message("id is a mandatory field!") if "doctor" not in params: return error_message("doctor is a mandatory field!") try: doc = mongo.db.doctors.find_one({"_id": params["doctor"]}) if doc is None: return error_message("doctor does not exists!") mongo.db[claims["identity"]].find_one_and_update( {"_id": params["id"]}, {"$addToSet": { "signaled": doc["name"] + " " + doc["surname"] }}) mongo.db.doctors.find_one_and_update({"_id": params["doctor"]}, { "$push": { "signals": { "cf": claims["identity"], "id": params["id"] } } }) return jsonify(status="ok", message="ok") except Exception as e: return error_message(str(e))
def my_recordings_get(identifier, cf, claims, mongo): if claims["type"] == "doctor": if cf is None: return error_message("user is a mandatory parameter!") try: doc = mongo.db.doctors.find_one({'_id': claims["identity"]}) if doc is None: return error_message("doctor is not registered!") if cf not in doc["patients"]: return error_message("patient not registered!") user = cf except Exception as e: return error_message(str(e)) else: user = claims["identity"] if identifier is None: try: ret = [] for document in mongo.db[user].find({"type": "recording"}, { "preview": 1, "signaled": 1 }): ret.append(document) return jsonify(status="ok", payload=ret) except Exception as e: return error_message(str(e)) else: try: doc = mongo.db[user].find_one({"_id": identifier}) if doc is None: return error_message("recording does not exists!") return jsonify(status="ok", payload=doc["aggregate"]) except Exception as e: return error_message(str(e))
def me_get(claims, mongo): collection = "users" if claims["type"] == "doctor": collection = "doctors" try: document = mongo.db[collection].find_one({"_id": claims["identity"]}) ret = {} for key in document: if key != "password": ret[key] = document[key] return jsonify(status="ok", message=ret) except Exception as e: return error_message(str(e), 500)
def my_patients_post(params, claims, mongo): if params is None: return error_message("mime type not accepted") if claims["type"] != "doctor": return error_message("only doctors are allowed to accept patients!") if "user_cf" not in params: return error_message("user_cf is a mandatory parameter") try: user = mongo.db.users.find_one({'_id': params["user_cf"]}) if user is None: return error_message("user does not exists") doctor = mongo.db.doctors.find_one({'_id': claims["identity"]}) if doctor is None: return error_message("doctor does not exists") if params["user_cf"] in doctor["patients"]: return error_message("patient already accepted") if params["user_cf"] not in doctor["patient_requests"]: return error_message("patient did not sent a request") mongo.db.doctors.update_one( { "_id": claims["identity"] }, { "$pull": {"patient_requests": params["user_cf"]}, "$push": {"patients": params["user_cf"]} } ) return jsonify(status="ok") except Exception as e: return make_response(str(e), 500)
def processing(params, claims, mongo): if claims["type"] != "user": return error_message("only users can access their recordings!") user = claims["identity"] if params is None: return error_message("No parameters sent!") if "id" not in params: return error_message("id is a mandatory parameter!") if "stop" not in params: return error_message("stop is a mandatory parameter!") rec_id = params["id"] stop = params["stop"] try: record = mongo.db[user].find_one({"_id": rec_id}) if record is None: return error_message("no records with the specified id!") date = parser.parse(rec_id) date_id = str(date.day) + "/" + str(date.month) + "/" + str(date.year) habit = mongo.db[user].find_one({"_id": date_id}) aggregate, preview = prepare_packet(record, stop, habit) mongo.db[user].find_one_and_update({ "_id": rec_id, }, {"$set": { "aggregate": aggregate, "preview": preview }}) return jsonify(status="ok") except Exception as e: traceback.print_exc(file=open("/root/oniro-server/error.log", "w")) return error_message(str(e))
def my_doctors_get(claims, mongo): if claims["type"] != "user": return error_message("only users can subscribe to doctors!") try: ret_keys = { "_id", "address", "email", "name", "phone_number", "profile_picture", "surname" } results = [] for document in mongo.db.doctors.find({'patients': claims["identity"]}): tmp_dict = {} for key in document: if key in ret_keys: tmp_dict[key] = document[key] tmp_dict["type"] = "subscribed" results.append(tmp_dict) for document in mongo.db.doctors.find( {'patient_requests': claims["identity"]}): tmp_dict = {} for key in document: if key in ret_keys: tmp_dict[key] = document[key] tmp_dict["type"] = "requested" results.append(tmp_dict) return jsonify(status="ok", message=results) except Exception as e: return make_response(str(e), 500)
def my_patients_get(claims, mongo): if claims["type"] != "doctor": return error_message("only doctors can see their patients!") try: results = [] doctor = mongo.db.doctors.find_one({'_id': claims["identity"]}) for value in doctor["patients"]: user = mongo.db.users.find_one({"_id": value}) if user is not None: results.append( { "patient": value, "name": user["name"], "surname": user["surname"], "type": "registered" } ) for value in doctor["patient_requests"]: user = mongo.db.users.find_one({"_id": value}) if user is not None: results.append( { "patient": value, "name": user["name"], "surname": user["surname"], "type": "request" } ) return jsonify(status="ok", results=results) except Exception as e: return make_response(str(e), 500)
def me_post(params, claims, mongo, file): collection = "users" fields = ["age", "phone_number"] if claims["type"] == "doctor": collection = "doctors" fields = ["address", "phone_number"] update = dict() image_path = "" if "image" in params and file is not None: return error_message("two images provided!") if "image" in params: formatted_string = params["image"].split(',')[1] formatted_string += "=" * (4 - len(formatted_string) % 4) image = base64.b64decode(formatted_string) files = {"file": ("image.png", image)} if file is not None: files = {"file": ("image.png", file)} if file is not None or "image" in params: payload = {"user": claims["identity"]} r = requests.post("http://localhost:8082/mediaserver", files=files, data=payload) if r.status_code != 200: return error_message("error in media server, code: " + str(r.status_code)) elif r.json()["status"] == "error": return error_message("error in media server, message: " + r.json()["message"]) else: update["profile_picture"] = r.json()["path"] image_path = r.json()["path"] try: if "old_password" in params and "new_password" in params: document = mongo.db[collection].find_one({'_id': claims["identity"]}) if document is not None: if sha256.verify(params["old_password"], document["password"]): update["password"] = sha256.hash(params["new_password"]) else: return error_message("password is not correct") for field in fields: if field in params: update[field] = params[field] if update: mongo.db[collection].find_one_and_update( { "_id": claims["identity"] }, { "$set": update } ) return jsonify(status="ok", message=image_path) else: return error_message("no data provided!") except Exception as e: return error_message(str(e))