def generate_daily_starting_stock(): print('Job triggered to generate daily starting stock', file=sys.stdout) collection_medicine = db.getc_medicine() collection_starting_stock = db.getc_daily_starting_stock() today = utils.date_today() print('Date : ' + today, file=sys.stdout) all_medicine = collection_medicine.find() for medicine in all_medicine: daily_stock_document = collection_starting_stock.find_one( {"mid": medicine["mid"]}) if daily_stock_document is None: insert_new_daily_starting_stock(medicine["mid"], medicine["stock"]) else: daily_stock = daily_stock_document["stock"] if daily_stock is None: daily_stock = {} elif daily_stock.get(today) is not None: print('Entry already exists - Medicine :', file=sys.stderr) print(medicine, file=sys.stderr) continue daily_stock[today] = medicine["stock"] update = collection_starting_stock.update_one( {"mid": medicine["mid"]}, {"$set": { "stock": daily_stock }}) if not update.acknowledged: print('Error while updating daily starting stock - Medicine :', file=sys.stderr) print(medicine, file=sys.stderr) print('Job completed', file=sys.stdout) return True
def insert_new_daily_starting_stock(mid, stock): collection_starting_stock = db.getc_daily_starting_stock() daily_stock_document = {} daily_stock_document["mid"] = int(mid) daily_stock = {} daily_stock[utils.date_today()] = int(stock) daily_stock_document["stock"] = daily_stock insert = collection_starting_stock.insert_one(daily_stock_document) if not insert.acknowledged: print('Error while inserting new daily starting stock - Medicine :', file=sys.stderr) print(medicine, file=sys.stderr)
def exit_script(bool): """ Exits the script with code 1 if at least one restaurant hasn't posted its lunch yet. :param bool: boolean, return value of _send_mail """ if not bool: print("One of the restaurants has not posted about today's lunch yet") print("Try again later.") sys.exit(1) else: print('Successfully found and posted/mailed lunch menus on {}. ' 'Enjoy your meal!'.format(utils.date_today()))
def new_patient_log(patient_dict): collection_patient = db.getc_patient() today = utils.date_today() patient_pid = int(patient_dict["num"]) patient_name = patient_dict["name"] patient_age = int(patient_dict["age"]) patient_address = patient_dict["address"] patient_contact = patient_dict["contact"] patient_diagnosis = patient_dict["diagnosis"] patient_gender = patient_dict["gender"] patient_medicine_list = patient_dict["medList"] document = collection_patient.find_one({"date": today, "id": patient_pid}) if document is None: patient_doc = {} patient_doc["date"] = today patient_doc["id"] = patient_pid patient_doc["name"] = patient_name patient_doc["age"] = patient_age patient_doc["address"] = patient_address patient_doc["contact"] = patient_contact patient_doc["diagnosis"] = patient_diagnosis patient_doc["gender"] = patient_gender med_list = [] for med in patient_medicine_list: med_list.append( utils.make_patient_log_medicine_doc(med, patient_pid)) patient_doc["medicines"] = med_list # print(patient_doc, file=sys.stderr) insert = collection_patient.insert_one(patient_doc) if insert.acknowledged: inserted_patient_id = insert.inserted_id success_list = [] success_log_list = [] for medicine in med_list: success = consume_stock(medicine) if success is not None: if success == -4: rollback_unsuccessful_patient_log( success_list, success_log_list, inserted_patient_id) return { "code": -4, "message": "Not enough stock for " + medicine["name"] } success_list.append(success) log_id = log_medicine(medicine, False, patient_pid) if log_id is not None: success_log_list.append(log_id) else: rollback_unsuccessful_patient_log( success_list, success_log_list, inserted_patient_id) return { "code": -8, "message": "Database insert unsuccessful" } else: rollback_unsuccessful_patient_log(success_list, success_log_list, inserted_patient_id) return { "code": -8, "message": "Database insert unsuccessful" } return {"code": 1, "message": "New patient logged successfully"} else: return {"code": -8, "message": "Database insert unsuccessful"} else: return {"code": -10, "message": "Invalid patient number"}
def get_data(): response = {} response["medicines"] = da.get_medicines() response["categories"] = da.get_categories() response["patients"] = da.get_patient_list(utils.date_today()) return jsonify(response)
class Meta: index = date_today()