def create_operator(): new_operator=request.get_json() #new_operator = json.loads(data) if Operator.find_by_email(new_operator['Email']): return jsonify({"status":400,"message":"A user with same name already exists"}) else: response= Operator.register(new_operator) return jsonify(response)
def upadate_blood_bank(self, parameters, Operator_id): #update the special attributes of a particular blood unit if parameters["case"] == 1: if Operator.check_branch_id(Operator_id, parameters["Br_id"]): db = get_connection() cursor = db.cursor() update_query = "UPDATE BLOOD set Special_Attributes=%s where Blood_id=%s" try: cursor.execute(update_query, (parameters["Special_Attributes"], parameters["Blood_id"])) db.commit() return { "status": 201, "message": "Bloodunit updated Successfully" } except mysql.Error as err: print("Failed to update entry: {}".format(err)) return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"} #Move asked quantity of particular blood group blood from 1 branch to other branch elif parameters["case"] == 2: source = Operator.check_branch_id(Operator_id, parameters["from_branch"]) target = Operator.check_branch_id(Operator_id, parameters["to_branch"]) if source and target: #if True and True : db = get_connection() cursor = db.cursor() update_query = "UPDATE BLOOD SET Br_id=%s WHERE Br_id=%s AND Blood_Group=%s AND Date_of_Expiry > CURDATE() LIMIT %s" try: cursor.execute( update_query, (parameters["from_branch"], parameters["to_branch"], parameters["Blood_Group"], parameters["Count"])) db.commit() return { "status": 201, "message": "Bloodunit updated Successfully" } except mysql.Error as err: return {"status": 500, "message": str(err)} else: return {"status": 401, "message": "Unauthorised Access"} else: return {"status": 404, "message": "Case not found"}
def donor_contact(self, single_donor): # Member function only to delete ONE donor contact from the DONOR_CONTACT table db = get_connection() cursor = db.cursor() if Operator.check_bankid(single_donor["Operator_id"], single_donor["Bbank_id"]): try: cursor.execute(f"""SELECT * FROM EMERGENCY_CONTACT_INFO WHERE Donor_id = '{single_donor['Donor_id']}'""") t = cursor.fetchall() if (len(t) > 1): delete_query = f"""DELETE FROM EMERGENCY_CONTACT_INFO WHERE Donor_id = '{single_donor['Donor_id']}' AND Phone_no = '{single_donor['Phone_no']}'""" try: cursor.execute(delete_query) db.commit() except mysql.Error as err: print("Failed to delete entry: {}".format(err)) return {"status": 500, "message": str(err)} else: s = "Each Donor requires at least one Donor.Delete Fail" print(s) return {"status": 500, "message": s} except mysql.Error as err: print("Failed to get donor contact data: {}".format(err)) return {"status": 500, "message": str(err)} db.close() return {"status": 200, "message": "Success"} else: return {"status": 401, "message": "Unauthorised Access"}
def donor_contact(self, single_donor): db = get_connection() cursor = db.cursor() if Operator.check_bankid(single_donor["Operator_id"], single_donor["Bbank_id"]): update_query = """UPDATE EMERGENCY_CONTACT_INFO SET Name = %s, WHERE Donor_id =%s ;""" t = (single_donor['Name'], single_donor['Donor_id']) try: cursor.execute(update_query, t) db.commit() except mysql.Error as err: print("Failed to update donor contact entry: {}".format(err)) return {"status": 500, "message": str(err)} # Need to update this based on what Salman is expecting t = tuple(single_donor['Emails'].values()) try: delete_query = f"DELETE FROM EMERGENCY_CONTACT_EMAIL WHERE Donor_id = '{single_donor['Donor_id']}'" cursor.execute(delete_query) db.commit() insert_query = "INSERT INTO EMERGENCY_CONTACT_EMAIL (Phone_no,Donor_id,Email_id) VALUES (%s,%s,%s)" for x in t: cursor.execute(insert_query, (single_donor['Phone_no'], single_donor['Donor_id'], x)) db.commit() except mysql.Error as err: print("Failed to update donor comtact email entry: {}".format( err)) return {"status": 500, "message": str(err)} db.close() return {"status": 201, "message": "Success"} else: return {"status": 401, "message": "Unauthorised Access"}
def get_expired_units(self, parameters, Operator_id): parameters["Bbank_id"] = int(parameters["Bbank_id"]) if Operator.check_bankid(Operator_id, parameters["Bbank_id"]): db = get_connection() cursor = db.cursor() select_query = "SELECT * FROM BLOOD WHERE Date_of_Expiry < CURDATE() AND Br_id IN \ (SELECT Br_id FROM BRANCH WHERE Bbank_id=%s)" try: cursor.execute(select_query, (parameters["Bbank_id"], )) result = cursor.fetchall() blood_units = [] db.commit() for row in result: blood_units.append({ 'Blood_id': row[0], 'Blood_Group': row[1], 'Br_id': row[2], 'Special_Attributes': row[6], 'Donor_id': row[3], 'Donation_Date': row[4], 'Date_of_Expiry': row[5] }) return {"status": 200, "result": blood_units} except mysql.Error as err: print("Internal Server error: {}".format(err)) return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"}
def list_limits(self, Operator_id, parameter): Bbank_id = int(parameter["Bbank_id"]) if Operator.check_bankid(Operator_id, Bbank_id): db = get_connection() cursor = db.cursor() select_query = "select br.Br_id,br.Br_Type,br.Street,br.City,br.Zip,bstk.Blood_Group,bstk.Btype_Limits from \ BLOOD_STOCK as bstk join BRANCH as br on (bstk.Br_id=br.Br_id) \ where Bbank_id=%s" try: cursor.execute(select_query, (Bbank_id, )) result = cursor.fetchall() stocks = [] db.commit() for row in result: stocks.append({ 'Br_id': row[0], 'Br_Type': row[1], "Street": row[2], "City": row[3], "Zip": row[4], 'Blood_Group': row[5], 'Btype_Limits': row[6] }) db.commit() return {"status": 201, "list": stocks} except mysql.Error as err: print("Internal Server error: {}".format(err)) return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"}
def update_branch(self, branch, Operator_id): if Operator.check_branch_id(Operator_id, branch["Br_id"]): db = get_connection() cursor = db.cursor() try: update_query = "UPDATE BRANCH set Br_Type=%s, Street=%s, City=%s, Zip=%s where Br_id=%s" cursor.execute( update_query, (branch['Br_Type'], branch['Street'], branch['City'], branch['Zip'], branch['Br_id'])) db.commit() delete_query = "DELETE FROM BRANCH_PHONE where Br_id=%s" cursor.execute(delete_query, branch["Br_id"]) db.commit() add_contactno(branch["Phone_no"], branch["Br_id"], cursor) db.commit() update_query = "UPDATE BRANCH set Br_Type=%s, Street=%s, City=%s, Zip=%s where Br_id=%s" cursor.execute( update_query, (branch['Br_Type'], branch['Street'], branch['City'], branch['Zip'], branch['Br_id'])) db.commit() return {"status": 200, "message": "Branch updated"} except mysql.Error as err: return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"}
def get_all_branches(self, Bbank_id, Operator_id): Bbank_id = int(Bbank_id) if Operator.check_bankid(Operator_id, Bbank_id): db = get_connection() cursor = db.cursor() try: select_all_query = "Select * FROM BRANCH where Bbank_id=%s" cursor.execute(select_all_query, (Bbank_id, )) result = cursor.fetchall() if result: branches = [] for row in result: branches.append({ "Br_id": row[0], "Br_Type": row[1], "Bbank_id": row[2], "Street": row[3], "City": row[4], "Zip": row[5] }) return {"status": 200, "result": branches} else: return { "status": 200, "message": "No branches exist for the blood bank" } except mysql.Error as err: return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"}
def donor_contact(self, single_donor): db = get_connection() cursor = db.cursor() # Remove the single contact field if single_donor json directly sends E contact info # single_contact = single_donor['EMERGENCY_CONTACT'] if Operator.check_bankid(single_donor["Operator_id"], single_donor["Bbank_id"]): insert_query = "INSERT INTO EMERGENCY_CONTACT_INFO (Phone_no,Donor_id,Name) VALUES (%s,%s,%s)" t = (single_donor['Phone_no'], single_donor['Donor_id'], single_donor['Name']) try: cursor.execute(insert_query, t) db.commit() except mysql.Error as err: print("Failed to add donor contact entry: {}".format(err)) return {"status": 500, "message": str(err)} # Need to update this based on what Salman is expecting t = tuple(single_donor['Phones'].values()) insert_query = "INSERT INTO EMERGENCY_CONTACT_EMAIL (Phone_no,Donor_id,Email_id) VALUES (%s,%s,%s)" for x in t: try: cursor.execute(insert_query, (single_donor['Phone_no'], single_donor['Donor_id'], x)) db.commit() except mysql.Error as err: print("Failed to add contact phones entry: {}".format(err)) return {"status": 500, "message": str(err)} db.close() return {"status": 201, "message": "Success"} else: return {"status": 401, "message": "Unauthorised Access"}
def donor(self, single_donor): db = get_connection() cursor = db.cursor() if Operator.check_branch_id(single_donor["Operator_id"], single_donor["Br_id"]): update_query = """UPDATE DONOR SET Name = %s, Blood_group = %s,Street =%s,City= %s,Zip = %s,Paid_Unpaid =%s, Notification_Subscription = %s,Notification_Type = %s WHERE Donor_id =%s ;""" t = (single_donor['Name'], single_donor['Blood_group'], single_donor['Street'], single_donor['City'], single_donor['Zip'], single_donor['Paid_Unpaid'], single_donor['Notification_Subscription'], single_donor['Notification_Type'], single_donor['Donor_id']) try: cursor.execute(update_query, t) db.commit() except mysql.Error as err: print("Failed to add donor contact entry: {}".format(err)) return {"status": 500, "message": str(err)} # Need to update this based on what Salman is expecting t = tuple(single_donor['Emails'].values()) try: delete_query = f"DELETE FROM DONOR_EMAIL WHERE Donor_id = '{single_donor['Donor_id']}'" cursor.execute(delete_query) db.commit() insert_query = "INSERT INTO DONOR_EMAIL (Donor_id,Email_id) VALUES (%s,%s)" for x in t: cursor.execute(insert_query, (single_donor['Donor_id'], x)) db.commit() except mysql.Error as err: print("Failed to update donor email entry: {}".format(err)) return {"status": 500, "message": str(err)} # Need to update this based on what Salman is expecting t = tuple(single_donor['Phones'].values()) try: delete_query = f"DELETE FROM DONOR_PHONE WHERE Donor_id = '{single_donor['Donor_id']}'" cursor.execute(delete_query) db.commit() insert_query = "INSERT INTO DONOR_PHONE (Donor_id,Phone_no) VALUES (%s,%s)" for x in t: cursor.execute(insert_query, (single_donor['Donor_id'], x)) db.commit() except mysql.Error as err: print("Failed to update donor email entry: {}".format(err)) return {"status": 500, "message": str(err)} db.close() return {"status": 201, "message": "Success"} else: return {"status": 401, "message": "Unauthorised Access"}
def get_particular_branche(self, Br_id, Operator_id): Br_id = int(Br_id) if Operator.check_branch_id(Operator_id, Br_id): db = get_connection() cursor = db.cursor() try: select_all_query = "Select * FROM BRANCH where Br_id=%s" cursor.execute(select_all_query, (Br_id, )) row = cursor.fetchone() if row: branch = { "Br_id": row[0], "Br_Type": row[1], "Bbank_id": row[2], "Street": row[3], "City": row[4], "Zip": row[5] } select_all_query = "Select Phone_no FROM BRANCH_PHONE where Br_id=%s" cursor.execute(select_all_query, (Br_id, )) rows = cursor.fetchall() if rows: phone_no = {} i = 1 for row in rows: tmp = {f"{i}": row[0]} phone_no.update(tmp) i = i + 1 tmp = {"phone_no": phone_no} branch.update(tmp) return {"status": 200, "branch": branch} else: return { "status": 200, "message": "No branche with the given branch id exists" } except mysql.Error as err: return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"}
def donor(self, single_donor): # Member function only deletes donor from the DONOR table # Delete on cascade expected to take care of the rest db = get_connection() cursor = db.cursor() if Operator.check_branch_id(single_donor["Operator_id"], single_donor["Br_id"]): delete_query = f"DELETE FROM DONOR_PHONE WHERE Donor_id = '{single_donor['Donor_id']}'" try: cursor.execute(delete_query) db.commit() except mysql.Error as err: print("Failed to delete entry: {}".format(err)) return {"status": 500, "message": str(err)} db.close() return {"status": 200, "message": "Success"} else: return {"status": 401, "message": "Unauthorised Access"}
def limit_check(self, parameters, Operator_id): Bbank_id = int(parameters["Bbank_id"]) if Operator.check_bankid(Operator_id, Bbank_id): db = get_connection() cursor = db.cursor() try: cursor.callproc('limit_check', (Bbank_id, )) rows = [] for result in cursor.stored_results(): rows = result.fetchall() #result = cursor.stored_results() limit_fall_list = [] if rows: for row in rows: limit_fall_list.append({ 'Br_id': row[0], 'Br_Type': row[1], "Blood_Group": row[2], 'City': row[3], 'Street': row[4], 'Btype_Limits': row[5], 'Blood_Unit_Count': row[6] }) return {"status": 200, "result": limit_fall_list} else: return {"status": 200, "result": limit_fall_list} except mysql.Error as err: response = jsonify({ "get_blood_unitsstatus": 500, "message": str(err) }) response.status_code = 500 return response finally: db.close() else: response = jsonify({ "status": 401, "message": "Unauthorised Access" }) response.status_code = 401 return response
def delete_blood_unit(self, parameters, Operator_id): parameters["Br_id"] = int(parameters["Br_id"]) if Operator.check_branch_id(Operator_id, parameters["Br_id"]): db = get_connection() cursor = db.cursor() delete_query = "DELETE FROM BLOOD WHERE Blood_id=%s" try: cursor.execute(delete_query, (parameters["Blood_id"], )) db.commit() return { "status": 200, "message": "Blood unit deleted successfully" } except mysql.Error as err: print("Internal Server error: {}".format(err)) return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"}
def delete_expired_units(self, parameters, Operator_id): parameters["Bbank_id"] = int(parameters["Bbank_id"]) if Operator.check_bankid(Operator_id, parameters["Bbank_id"]): db = get_connection() cursor = db.cursor() select_query = "DELETE FROM BLOOD WHERE Date_of_Expiry < CURDATE() AND Br_id IN \ (SELECT Br_id FROM BRANCH WHERE Bbank_id=%s)" try: cursor.execute(select_query, (parameters["Bbank_id"], )) db.commit() return { "status": 200, "message": "Expired units deleted successfully" } except mysql.Error as err: print("Internal Server error: {}".format(err)) return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"}
def creat_new_branch(self, branch, Operator_id): if Operator.check_bankid(Operator_id, branch["Bbank_id"]): db = get_connection() cursor = db.cursor() try: insert_query = "INSERT INTO BRANCH (Br_Type,Bbank_id,Street,City,Zip) VALUES (%s,%s,%s,%s,%s)" cursor.execute( insert_query, (branch['Br_Type'], branch['Bbank_id'], branch['Street'], branch['City'], branch['Zip'])) branch_id = cursor.lastrowid branch.update({"Br_id": branch_id}) #var_Donor = cursor.execute("SELECT MAX(Donor_id) FROM DONOR;") db.commit() #add Phone nos add_contactno(branch["Phone_no"], branch_id, cursor) db.commit() #Add branch related information into BLOOD_STOCK bl_grp = ['O+', 'A+', 'B+', 'AB+', 'O-', 'A-', 'B-', 'AB-'] insert_query = "INSERT INTO BLOOD_STOCK (Br_id,Blood_Group) values (%s,%s)" stocks = [] for grp in bl_grp: T = (branch_id, grp) stocks.append(T) cursor.executemany(insert_query, stocks) db.commit() return { "status": 201, "message": "New branch created", "branch": branch } except mysql.Error as err: return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"}
def delete_delete(self, branch, Operator_id): branch["Br_id"] = int(branch["Br_id"]) if Operator.check_branch_id(Operator_id, branch["Br_id"]): db = get_connection() cursor = db.cursor() try: delete_query = "DELETE FROM BRANCH where Br_id=%s" cursor.execute(delete_query, (branch['Br_id'], )) db.commit() delete_query = "DELETE FROM BRANCH_PHONE where Br_id=%s" cursor.execute(delete_query, (branch['Br_id'], )) db.commit() return { "status": 200, "message": "Branch deleted successfully" } except mysql.Error as err: return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"}
def update_blood_stock_limit(self, parameters, Operator_id): if Operator.check_branch_id(Operator_id, parameters["Br_id"]): db = get_connection() cursor = db.cursor() update_query = "UPDATE BLOOD_STOCK SET Btype_Limits=%s WHERE Br_id=%s AND Blood_Group=%s" try: cursor.execute( update_query, (parameters["Btype_Limits"], parameters["Br_id"], parameters["Blood_Group"])) db.commit() return { "status": 201, "message": "Blood stock limit updated Successfully" } except mysql.Error as err: print("Internal Server error: {}".format(err)) return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"}
def insert_blood(self, bloodUnit, Operator_id): db = get_connection() cursor = db.cursor() if Operator.check_branch_id(Operator_id, bloodUnit["Br_id"]): date = datetime.today().strftime('%Y-%m-%d') bloodGroup_query = "SELECT Blood_Group from DONOR WHERE Donor_id = %s" try: cursor.execute(bloodGroup_query, (bloodUnit["Donor_id"], )) bloodGroup = cursor.fetchone() if bloodGroup: insert_query = "INSERT INTO BLOOD (Blood_Group,Br_id,Donor_id,Donation_Date, \ Special_Attributes) VALUES (%s,%s,%s,%s,%s)" try: cursor.execute(insert_query,(bloodGroup[0], \ bloodUnit["Br_id"], bloodUnit["Donor_id"],date,bloodUnit["Special_Attributes"])) db.commit() return { "status": 201, "message": "Bloodunit saved Successfully" } except mysql.Error as err: #print("Failed to add entry: {}".format(err)) return {"status": 500, "message": str(err)} else: return {"status": 404, "message": "Donor id not found"} except mysql.Error as err: print("Internal Server error: {}".format(err)) return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"}
def get_blood_units(self, parameters, Operator_id): db = get_connection() cursor = db.cursor() #return the total count of bolood in the blood bank parameters["case"] = int(parameters["case"]) # tmp = {"case":int(parameters["case"])} # parameters.update(tmp) if parameters["case"] == 4: parameters["Bbank_id"] = int(parameters["Bbank_id"]) if Operator.check_bankid(Operator_id, parameters["Bbank_id"]): try: cursor.callproc('bloodbank_wise_stock', (parameters["Bbank_id"], )) row = {} for result in cursor.stored_results(): row = result.fetchone() if row: blood_bank = { 'Blood_Bank_Name': row[1], 'Blood_Unit_Count': row[2] } return {"status": 200, "result": blood_bank} else: return { "status": 404, "message": "branch id or blood group wrong" } except mysql.Error as err: print("Internal Server error: {}".format(err)) return {"get_blood_unitsstatus": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"} #return the list containing count of blood units in each branch of a particular bank elif parameters["case"] == 1: parameters["Bbank_id"] = int(parameters["Bbank_id"]) if Operator.check_bankid(Operator_id, parameters["Bbank_id"]): try: cursor.callproc('branch_wise_stock', (parameters["Bbank_id"], )) rows = [] for result in cursor.stored_results(): rows = result.fetchall() #result = cursor.stored_results() if rows: blood_count = [] for row in rows: blood_count.append({ 'Br_id': row[0], 'Br_Type': row[1], 'Street': row[2], 'City': row[3], 'Zip': row[4], 'Blood_Unit_Count': row[5] }) return {"status": 200, "result": blood_count} else: return { "status": 404, "message": "No branches for given bank id" } except mysql.Error as err: print("Internal Server error: {}".format(err)) return {"get_blood_unitsstatus": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"} #return the list containing count of blood units of each blood group type in a particular branch elif parameters["case"] == 2: parameters["Br_id"] = int(parameters["Br_id"]) if Operator.check_branch_id(Operator_id, parameters["Br_id"]): try: cursor.callproc('branch_stock', (parameters["Br_id"], )) rows = [] for result in cursor.stored_results(): rows = result.fetchall() if rows: blood_count = [] for row in rows: blood_count.append({ 'Blood_Group': row[0], 'Blood_Unit_Count': row[1] }) return {"status": 200, "result": blood_count} else: return {"status": 404, "message": "branch id wrong"} except mysql.Error as err: print("Internal Server error: {}".format(err)) return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"} #return the list of blood units for a particular blood group in a particular branch of blood bank elif parameters["case"] == 3: parameters["Br_id"] = int(parameters["Br_id"]) parameters["Blood_Group"] = int(parameters["Blood_Group"]) if Operator.check_branch_id(Operator_id, parameters["Br_id"]): #select_query="SELECT Blood_id, Blood_Group, Donor_id, Donation_Date, Date_of_Expiry, Special_Attributes \ # FROM BLOOD \ # WHERE Br_id=%s AND Blood_Group='%s'" select_query = f"""SELECT Blood_id, Blood_Group, Donor_id, Donation_Date, Date_of_Expiry, Special_Attributes FROM BLOOD WHERE Br_id={parameters["Br_id"]} AND Blood_Group={parameters["Blood_Group"]} AND Date_of_Expiry > CURDATE()""" try: cursor.execute( select_query ) #,(parameters["Br_id"],parameters["Blood_Group"])) result = cursor.fetchall() if result: blood_units = [] for row in result: blood_units.append({ 'Blood_id': row[0], 'Blood_Group': row[1], 'Donor_id': row[2], 'Donation_Date': row[3], 'Date_of_Expiry': row[4], 'Special_Attributes': row[5], }) return {"status": 200, "result": blood_units} else: return { "status": 404, "message": "branch id or Blood_Group is wrong" } # if cursor.rowcount == 0: # return {"status":404, "message":"branch id or Blood_Group is wrong"} # else: # result = cursor.fetchall() # blood_units=[] # for row in result: # blood_units.append({'Blood_id':row[0], 'Blood_Group':row[1], # 'Donor_id':row[2],'Donation_Date':row[3], # 'Date_of_Expiry':row[4],'Special_Attributes':row[5],}) # return {"status": 200, "result":blood_units} except mysql.Error as err: print("Internal Server error: {}".format(err)) return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"} else: return {"status": 404, "message": "Case not found"}
@app.route('/admin_login', methods=['POST']) def admin_log(): admin = request.get_json() response = login(admin) return response #SIGNUP for Operator @app.route('/operator/signup', methods=['POST']) #API to create ne operator def create_operator(): case = authenticate_admin(request) if case == 1: new_operator = request.get_json() #new_operator = json.loads(data) if Operator.find_by_email(new_operator['Email']): return jsonify({ "status": 400, "message": "A user with same name already exists" }) else: response = Operator.register(new_operator) return jsonify(response) else: return (case) ############## BLOOD-Bank RELATED APIs ############################################# # ####################################################################################
def upadate_blood_bank(self, parameters, Operator_id): #update the special attributes of a particular blood unit if parameters["case"] == 1: if Operator.check_branch_id(Operator_id, int(parameters["Br_id"])): db = get_connection() cursor = db.cursor() update_query = "UPDATE BLOOD set Special_Attributes=%s where Blood_id=%s" try: cursor.execute(update_query, (parameters["Special_Attributes"], int(parameters["Blood_id"]))) db.commit() return { "status": 201, "message": "Bloodunit updated Successfully" } except mysql.Error as err: print("Failed to update entry: {}".format(err)) return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"} #Move asked quantity of particular blood group blood from 1 branch to other branch elif parameters["case"] == 2: source = Operator.check_branch_id(Operator_id, int(parameters["from_branch"])) target = Operator.check_branch_id(Operator_id, int(parameters["to_branch"])) blood_group = ['O+', 'A+', 'B+', 'AB+', 'O-', 'A-', 'B-', 'AB-'] try: db = get_connection() cursor = db.cursor() cursor.callproc('branch_stock', (int(parameters["from_branch"]), )) rows = [] for result in cursor.stored_results(): rows = result.fetchall() if rows: count = 0 for row in rows: if row[0] == blood_group[int(parameters["Blood_Group"]) - 1]: count = row[1] if count == 0 or int(parameters["Count"]) > count: return { "status": 200, "message": "Not enough blood units to move" } else: {"status": 401, "message": "Branch does not exists"} except mysql.Error as err: return {"status": 500, "message": str(err)} finally: db.close() if source and target: #if True and True : db = get_connection() cursor = db.cursor() update_query = "UPDATE BLOOD SET Br_id=%s WHERE Br_id=%s AND Blood_Group=%s AND Date_of_Expiry > CURDATE() LIMIT %s" try: cursor.execute(update_query, (int(parameters["to_branch"]), int(parameters["from_branch"]), int(parameters["Blood_Group"]), int(parameters["Count"]))) db.commit() return { "status": 201, "message": "Bloodunit moved Successfully" } except mysql.Error as err: return {"status": 500, "message": str(err)} finally: db.close() else: return {"status": 401, "message": "Unauthorised Access"} else: return {"status": 404, "message": "Case not found"}
def authenticate(email, password): operator = Operator.find_by_email(email) passwrd = base64.b64encode(password.encode("utf-8")) if operator and safe_str_cmp(operator.Password, passwrd): return operator
def identity(payload): user_id = payload['identity'] return Operator.find_by_id(user_id)
def donor(self, single_donor): # This method will update all Donor and donor contact related tables including # DONOR DONOR_EMAIL DONOR_PHONE AFFILIATED db = get_connection() cursor = db.cursor() if Operator.check_branch_id(single_donor["Operator_id"], single_donor["Br_id"]): insert_query = """INSERT INTO DONOR (Donor_id,Name,Blood_group,Street,City,Zip,Paid_Unpaid,Notification_Subscription,Notification_Type,Operator_id) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""" t = (None, single_donor['Name'], single_donor['Blood_group'], single_donor['Street'], single_donor['City'], single_donor['Zip'], single_donor['Paid_Unpaid'], single_donor['Notification_Subscription'], single_donor['Notification_Type'], single_donor['Operator_id']) try: cursor.execute(insert_query, t) db.commit() # Last Donor ID is assumed to be the MAX() # LAST_INSERT_ID() is not working with multiple connnects var_Donor = cursor.execute("SELECT MAX(Donor_id) FROM DONOR;") except mysql.Error as err: print("Failed to add donor entry: {}".format(err)) return {"status": 500, "message": str(err)} # Need to update this based on what Salman is expecting t = tuple(single_donor['Emails'].values()) insert_query = "INSERT INTO DONOR_EMAIL (Donor_id,Email_id) VALUES (%s,%s)" for x in t: try: cursor.execute(insert_query, (var_Donor, x)) db.commit() except mysql.Error as err: print("Failed to add donor email entry: {}".format(err)) return {"status": 500, "message": str(err)} # Need to update this based on what Salman is expecting # Mulitple phones updated assuming all infromation is present in a nested t = tuple(single_donor['Phones'].values()) insert_query = "INSERT INTO DONOR_PHONE (Donor_id,Phone_no) VALUES (%s,%s)" for x in t: try: cursor.execute(insert_query, (var_Donor, x)) db.commit() except mysql.Error as err: print("Failed to add donor phone entry: {}".format(err)) return {"status": 500, "message": str(err)} # Affiiate table entry is inserted directly when a new donor is added t = (var_Donor, single_donor['Br_id']) insert_query = "INSERT INTO AFFILIATED (Donor_id,Br_id) VALUES (%s,%s)" try: cursor.execute(insert_query, t) db.commit() except mysql.Error as err: print("Failed to add affiliated entry: {}".format(err)) return {"status": 500, "message": str(err)} db.close() print("Donor added successfully") return {"status": 201, "message": "Success"} else: print("Unauthorised Access") return {"status": 401, "message": "Unauthorised Access"}