def index(): user_id = session.get('user_id') if user_id: print(user_id) db = get_db() pills = db['pills'] users = db['users'] user = users.find_one(ObjectId(str(user_id))) pillbox_list = user['pillbox'] pillbox = join_pillbox(pillbox_list) pill_collection = db['pills'].find() the_path = os.path.join(APP_STATIC, 'img/newpill') try: files = os.listdir(the_path) except: files = [] return render_template('pillbox/index.html', pillbox=pillbox, pill_collection=pill_collection, files=files) else: return redirect(url_for('auth.login'))
def load_logged_in_user(): user_id = session.get('user_id') db = get_db() users = db['users'] if user_id is None: g.user = None else: try: object_id = ObjectId(str(user_id)) query = {'_id': object_id} g.user = users.find_one(query) except: session.clear()
def join_pillbox(pillbox_list, days=days_of_the_week): db = get_db() pillbox = [] for day, name in enumerate(days): day = pillbox_list[day] for index, pill in enumerate(day): pill_obj = db['pills'].find_one(pill['pill_id']) pill_obj['time'] = pill['time'] if 'taken' in pill: pill_obj['taken'] = pill['taken'] day[index] = pill_obj #sorted_day = sorted( (time.strptime(pill['time'], "%H:%M:%S") for pill in day), reverse=True) sorted_day = sorted(day, key=lambda pill: pill['time'], reverse=False) data = {'name': name, 'pills': sorted_day} pillbox.append(data) return pillbox
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() users = db['users'] error = None query = {'username': username} user = users.find_one(query) if user is None: error = 'Incorrect username.' elif not check_password_hash(user['password'], password): error = 'Incorrect password.' if error is None: session.clear() session['user_id'] = str(user['_id']) return redirect(url_for('index')) flash(error) return render_template('auth/login.html')
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() users = db['users'] error = None query = {'username': username} if not username: error = 'Username is required.' elif not password: error = 'Password is required.' elif list(users.find(query)): error = 'User {} is already registered.'.format(username) if error is None: user = create_new_user(username, generate_password_hash(password)) users.insert(user) return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')