Beispiel #1
0
def get_prescriptions():
    """\
    Returns a list of all the prescriptions known in the database

    :return: A list of <Prescription> objects
    """
    c = get_cursor()
    cursor = c.execute('''SELECT * FROM Prescriptions''')

    res = []

    for row in cursor:
        res.append(Prescription.parse_raw(row))

    return res
Beispiel #2
0
def get_prescriptions_by_uid(uid):
    """\
    Returns a list of prescriptions for a certain patient

    :param uid: The userID of the patient
    :return: A list of <Prescription> objects for the given uid
    """
    c = get_cursor()
    cursor = c.execute('''SELECT * FROM Prescriptions WHERE uid=?''', (uid, ))

    res = []

    for row in cursor:
        res.append(Prescription.parse_raw(row))

    return res
Beispiel #3
0
    def run(self):
        print("Starting " + self.name)
        global running
        global doctor_test
        global doctor_id
        doctor_test = False

        # Prompt loop
        while running:
            cmd = input("> ")
            if cmd == "exit":
                running = False
                database.close()
                print("Exiting now")
                sys.exit(0)

            # command for logging in with username and password if the username and password are valid
            #  doctor_test is set to true and doctor id is set to the corresponding id so the database can be edited
            if cmd == "login":
                doctors = database.get_users_by_role('doc')
                print("Please login to make changes.")
                login_username = input("Username: "******"Password: "******"Logged in as doctor id: " + str(doctor_id))
                        doctor_test = True
                if not doctor_test:
                    print("Invalid credentials!")

            # when logging out doctor test is set to false so no changes can be made
            # doctor id is set to 0 so nobody can actually see who was the last doctor to log in
            if cmd == "logout" and doctor_test:
                doctor_test = False
                print("Logged out as doctor id: " + str(doctor_id))
                doctor_id = 0

            # to update credentials you must be logged in as a doctor and you have to verify your login data again
            if cmd == "update credentials" and doctor_test:
                user = database.get_user_by_uid(doctor_id)
                print("Please verify your login.")
                login_username = input("Old username: "******"Old password: "******"New username = "******"New password = "******"Credentials updated.")

            # to update a rfid tag you have to be logged in as a doctor and you have to input a user_id to which the new rfid will be bound
            if cmd == "update rfid" and doctor_test:
                user_id = input("User id = ")
                user = database.get_user_by_uid(user_id)
                if user is None:
                    print("No user with this id!")
                    continue

                new_rfid = input("New rfid = ")
                user.rfid = new_rfid
                database.update_user(user)
                database.commit()
                print("rfid updated.")

            # when logged in as a doctor you can get all users with their roles (but not password or rfid)
            if cmd == "get users" and doctor_test:
                users = database.get_users()
                print("id\trole")
                for user in users:
                    print(str(user.id) + "\t" + str(user.role))

            # when logged in you can get prescriptions for a single user or you can get all prescriptions
            if cmd == "get prescriptions" and doctor_test:
                choice = input("For all users y/n: ")
                prescriptions = database.get_prescriptions()
                if choice == "y":
                    print("id\tmedicine\tdescription")
                    for prescription in prescriptions:
                        print(str(prescription.id) + "\t"  + str(prescription.medicine_id) + "\t\t\t" + str(prescription.descr))
                elif choice == "n":
                    user_id = int(input("id = "))
                    print("id\tmedicine\tdescription")
                    for prescription in prescriptions:
                        if prescription.uid == user_id:
                            print(str(prescription.id) + "\t" + str(prescription.medicine_id) + "\t\t\t" + str(prescription.descr))
                else:
                    print("Invalid input!")

            # as a logged in doctor you can add prescriptions. you will be prompted for all the data
            if cmd == "add prescription" and doctor_test:
                prescriptions = database.get_prescriptions()
                prescription_list = []
                for prescription in prescriptions:
                    prescription_list.append(prescription.id)
                prescription_id = int(max(prescription_list) + 1)
                patient_id = int(input("Patient id = "))
                medicine_id = int(input("Medicine id = "))
                description = input("Description of use = ")
                max_dose = int(input("Daily max dose = "))
                min_time = int(input("Minimum time between dispenses in seconds = "))
                amount = int(input("Amount of medicine per dispense/dose = "))
                cur_dose = 0
                duration = int(input("Prescription duration in days = ")) * 86400
                date = int(calendar.timegm(time.gmtime()))

                # this part checks if the user is actually in the database else it prints "patient does not exist"
                users = database.get_users()
                patient_test = False
                for user in users:
                    if patient_id == user.id:
                        print("New prescription added with id: " + str(prescription_id))
                        database.insert_prescription(Prescription.parse_raw([prescription_id, patient_id, medicine_id, description, max_dose, min_time, amount, cur_dose, date, doctor_id, duration, date]))
                        database.commit()
                        patient_test = True
                if not patient_test:
                    print("Patient does not exist!")

            # as a logged in doctor you can remove a prescription by id
            if cmd == "remove prescription" and doctor_test:
                prescription_id = int(input("prescription id = "))
                database.remove_prescription(prescription_id)
                database.commit()
                print("Prescription removed.")

            # as a logged in doctor you can add new users. you get prompted for all data (and for username and pw if you are adding a doctor)
            if cmd == "add user" and doctor_test:
                users = database.get_users()
                user_list = []
                for user in users:
                    user_list.append(user.id)
                user_id = int(max(user_list) + 1)
                rfid = int(input("RFID = "))
                role = input("role(pat/doc/ref) = ")
                if role == 'doc':
                    new_username = input("New user username = "******"New user password = "******""
                    new_password = ""
                database.insert_user(User.parse_raw([user_id, rfid, role, new_username, new_password]))
                database.commit()
                print("New user added with id: " + str(user_id))

            # as a logged in doctor you can remove users by id
            if cmd == "remove user" and doctor_test:
                user_id = int(input("User id = "))
                database.remove_user(user_id)
                database.commit()
                print("User removed.")

            # you can always print out all existing commands
            if cmd == "help":
                commands = ["login",
                            "logout",
                            "exit",
                            "get prescriptions",
                            "add prescription",
                            "remove prescription",
                            "add user",
                            "remove user",
                            "update credentials",
                            "update rfid"]
                print("Commands:", commands)


         # threads.remove(self)
        print("Exiting " + self.name)