def delete_pill(name): exists = get_pills().find_one({'name': name}) if exists: get_pills().remove({'name': name}) return str(exists) else: return str("ERROR: Pill does not exists")
def schedule_error_check(data): pill = {} # do error checking if 'name' in data and data['name']: pill_name = data.getlist('name')[0] pill_obj = get_pills().find_one({'name': pill_name}) if not pill_obj: return False, "ERROR: Pill not found.", pill pill['name'] = pill_obj['name'] pill['pill_id'] = pill_obj['_id'] else: return False, "ERROR: No pill specified.", pill if 'day' in data and data['day']: pill['day'] = data.getlist('day')[0] try: pill['day'] = int(pill['day']) except: return False, "ERROR: Incorrect day specification.", pill else: return False, "ERROR: No day specified.", pill if 'time' in data and data['time']: pill['time'] = data.getlist('time')[0] try: datetime.strptime(pill['time'], "%H:%M:%S").time() except: return False, "ERROR: Incorrect time specification.", pill else: return False, "ERROR: No time specified.", pill return True, str(pill['pill_id']), pill
def create_pill(): #user_id = session.get('user_id') pill = {} data = request.form if 'name' in data and data['name']: pill['name'] = data.getlist('name')[0] else: pill['name'] = 'Unknown Pill' if 'description' in data and data['description']: pill['description'] = data.getlist('description')[0] else: pill['description'] = 'No Description' if 'icon' in data and data['icon']: pill['icon'] = data.getlist('icon')[0] else: pill['icon'] = 'icons/pill.png' duplicate = get_pills().find_one({'name': pill['name']}) if duplicate: return str("ERROR: Pill already exists") else: get_pills().insert(pill) return redirect(url_for('pillbox.index'), code=302)
def get_pillbox_simple(): user_profile = get_users().find_one({'_id': ObjectId(user_id_str) }) #hardcoded user pillbox_list = user_profile['pillbox'] pillbox = [] for day in range(7): day = pillbox_list[day] for index, pill in enumerate(day): pill_obj = get_pills().find_one(pill['pill_id']) stripped_pill = {'time': pill['time'], 'name': pill_obj['name']} day[index] = stripped_pill sorted_day = sorted(day, key=lambda pill: pill['time'], reverse=False) pillbox.append(sorted_day) return str(pillbox)
def get_pillbox_timesheet(): user_profile = get_users().find_one({'_id': ObjectId(user_id_str) }) #hardcoded user pillbox_list = user_profile['pillbox'] pillbox = [] for day in range(7): day = pillbox_list[day] day_collections = {} for index, pill in enumerate(day): pill_obj = get_pills().find_one(pill['pill_id']) time = pill['time'] name = pill_obj['name'] if time in day_collections: day_collections[time].append(name) else: day_collections[time] = [name] pillbox.append(day_collections) return str(pillbox)
def get_pilltimes(day_index, single=False): try: day_index = int(day_index) if day_index not in range(7): return "ERROR: invalid day." except: return "ERROR: invalid day." user_profile = get_users().find_one({'_id': ObjectId(user_id_str) }) #hardcoded user pillbox_list = user_profile['pillbox'] time_list = [] for pill in pillbox_list[day_index]: time = pill['time'] if time not in time_list: pill_obj = get_pills().find_one({'_id': pill['pill_id']}) pill['name'] = pill_obj['name'] if 'taken' in pill_obj and pill_obj['taken']: pill['taken'] = 1 else: pill['taken'] = 0 #time_list.append(time) time_list.append(pill) pill_list = sorted(time_list, key=lambda pill: pill['time'], reverse=False) if single: current_time = datetime.now().time() for index, pill in enumerate(pill_list): if current_time <= datetime.strptime(pill['time'], "%H:%M:%S").time(): pill['time'] = pill['time'][0:5] pill.pop('pill_id', None) return str(pill) return "{}" else: return str(time_list)
def schedule_add_frompage(): data = request.form hour = data.getlist('hour')[0] minute = data.getlist('minute')[0] meridian = data.getlist('meridian')[0] hour = int(hour) minute = int(minute) if meridian == 'pm': hour = hour + 12 hour = "%02d" % hour minute = "%02d" % minute name = data.getlist('name')[0] pill_obj = get_pills().find_one({'name': name}) current_datetime = datetime.now() current_day = (current_datetime.weekday() + 1) % 7 pill = {'pill_id': pill_obj['_id'], 'time': hour + ":" + minute + ":00"} #add to pillbox user_profile = get_users().find_one({'_id': ObjectId(user_id_str) }) #hardcoded user pillbox_list = user_profile['pillbox'] day = pillbox_list[current_day] day.append(pill) pillbox_list[current_day] = day get_users().find_one_and_update({'_id': user_profile['_id']}, {'$set': { 'pillbox': pillbox_list }}) #return "Successfully added " + pill_name + " on day " + str(pill_day_index) + " at " + pill['time'] return redirect(url_for('pillbox.index'), code=302)
def get_pillbox_day(day_index): try: day_index = int(day_index) if day_index not in range(7): return "ERROR: invalid day." except: return "ERROR: invalid day." user_profile = get_users().find_one({'_id': ObjectId(user_id_str) }) #hardcoded user pillbox_list = user_profile['pillbox'] day = pillbox_list[day_index] day_collection = {} for index, pill in enumerate(day): pill_obj = get_pills().find_one(pill['pill_id']) time = pill['time'] name = pill_obj['name'] if time in day_collection: day_collection[time].append(name) else: day_collection[time] = [name] return str(day_collection)
def get_all_pills(): pills = get_pills().find() result = "" for pill in pills: result += str(pill) + "\n" return result