def updateMsg(request): # 检查是否是POST方式 if request.method != 'POST': return HttpResponse('2') # 检查用户id userId = request.session.get('_id') if userId is None: return HttpResponse('3') # 获取提交的用户名和密码 username = request.POST.get('username') password = request.POST.get('password') print('username:'******',password:'******'4') # 检查密码长度 if len(password) < 3: return HttpResponse('5') # 查询旧用户名,为了避免Session级别的bug污染该处,重新从数据库里查 oldname = mainapp_dao.firstDocInUser({ '_id': ObjectId(userId) }).get('username') if oldname is None: print('[debug]没能获取到旧用户名') return HttpResponse('-1') # 如果旧用户名和新用户名相同,可以直接修改密码 if oldname == username: mainapp_dao.updateOneUser({'_id': ObjectId(userId)}, {'$set': { 'password': password }}) return HttpResponse('1') # 检查用户名占用情况,前面已经检查过了和自己的用户名不同 if mainapp_dao.docCountInUser({"username": username}) > 0: return HttpResponse('6') # 所有的验证都通过,修改用户名和密码 mainapp_dao.updateOneUser( {'_id': ObjectId(userId)}, {'$set': { 'username': username, 'password': password }}) # 将修改后的用户名更新到Session里,因为可能修改失败,所以不能直接用username而是再从数据库里查一次 request.session['username'] = mainapp_dao.firstDocInUser({ '_id': ObjectId(userId) }).get('username') return HttpResponse('1')
def getIndexPage(request): mylst = [1 for i in range(12)] # 方便开发用 # 看看Session里有没有,有就直接进不做校验 print("从Session里检查") if request.session.get('_id') is not None and request.session.get( 'username') is not None: print("Session校验成功") return render(request, r'web/index.html', {'mylst': mylst}) # 如果是登录操作 elif request.method == 'POST': # 获取用户名和密码 username = request.POST.get('username', None) password = request.POST.get('password', None) # 检查字段缺失 if username is None or password is None or \ username == "" or password == "": return render(request, r'web/login.html', {'stat': -1}) # FIXME 使用用户名和密码校验身份,并从DB中获取该用户id user = mainapp_dao.firstDocInUser({ "username": username, "password": password }) if user is None: # 登录失败 return render(request, r'web/login.html', {'stat': -4}) # 登录成功,将登录身份存进session里 request.session['_id'] = user.get('_id').__str__() # 转成str request.session['username'] = user.get('username') print("存进了Session里") return render(request, r'web/index.html', {'mylst': mylst}) else: # 请先登录! return render(request, r'web/login.html', {'stat': -5})
def getPlanPage(request): userId = request.session.get('_id') if userId is None: return render(request, r'web/login.html', {'stat': -5}) # 获取用户 try: user = mainapp_dao.firstDocInUser({'_id': ObjectId(userId)}) user['BMI'] = (int(user['weight']) / 2 / np.square(int(user['height']) / 100)) serverDate = datetime.datetime.now().strftime('%Y-%m-%d') return render( request, r'web/plan.html', { 'user': user, 'sporttime': mainapp_dao.weekspoleep(userId, serverDate), 'weekday': mainapp_dao.Week(serverDate), 'standard': [ mainapp_health.avgstandard(), mainapp_health.avgstandard('优秀', user['sex']) ], 'status': mainapp_dao.bodystatus(userId) }) except: return render(request, r'web/bdymsg.html', {'user': user, 'bmi': ''})
def getBdyMsg(request): # 通过检查Session检验是否登录了 userId = request.session.get('_id') if userId is None: return render(request, r'web/login.html', {'stat': -5}) # 获取用户(字典形式) user = mainapp_dao.firstDocInUser({"_id": ObjectId(userId)}) # 计算BMI指数 weight = None height = None BMI = '' if user.get('weight') is None: BMI += '缺少身高!' else: weight = float(user.get('weight')) if user.get('height') is None: BMI += '缺少体重!' else: height = float(user.get('height')) if weight is not None and height is not None: BMI = (weight / 2) / pow((height / 100), 2) # 计算BMI的体重使用kg而不是斤 if BMI < 18.5: BMI = str(BMI) + ' (体重过轻)' elif BMI < 24: BMI = str(BMI) + ' (正常范围)' elif BMI < 27: BMI = str(BMI) + ' (体重偏重)' elif BMI < 30: BMI = str(BMI) + ' (轻度肥胖)' elif BMI < 35: BMI = str(BMI) + ' (中度肥胖)' else: BMI = str(BMI) + ' (重度肥胖)' return render(request, r'web/bdymsg.html', {'user': user, 'bmi': BMI})
def getIndexPage(request): mylst = [1 for i in range(12)] # 方便开发用 hotFood = mainapp_dao.hotFood() # 无论如何都要有热门食物 print(hotFood) # 看看Session里有没有,有就直接进不做校验 if request.session.get('_id') is not None and request.session.get('username') is not None: favourFood = mainapp_dao.favouriateFood(request.session.get('_id')) return render(request, r'web/index.html', {'favourlist': favourFood, 'hotlist': hotFood}) # 如果是登录操作 elif request.method == 'POST': # 获取用户名和密码 username = request.POST.get('username', None) password = request.POST.get('password', None) # 检查字段缺失 if username is None or password is None or \ username == "" or password == "": return render(request, r'web/login.html', {'stat': -1}) # 使用用户名和密码校验身份,并从DB中获取该用户id user = mainapp_dao.firstDocInUser({"username": username, "password": password}) if user is None: # 登录失败 return render(request, r'web/login.html', {'stat': -4}) # 登录成功,将登录身份存进session里 userid = user.get('_id').__str__() request.session['_id'] = userid # 转成str request.session['username'] = user.get('username') favourFood = mainapp_dao.favouriateFood(userid) # 根据用户名查询最喜爱的食物 print("存进了Session里") return render(request, r'web/index.html', {'favourlist': favourFood, 'hotlist': hotFood}) else: # 更新:不登录也可以去index页,不登陆不能获取最喜爱的食物 return render(request, r'web/index.html', {'favourlist': None, 'hotlist': hotFood})
def getPropPage(request): userId = request.session.get('_id') if userId is None: return render(request, r'web/login.html', {'stat': -5}) # 从DB中查询 user = mainapp_dao.firstDocInUser({'_id': ObjectId(userId)}) return render(request, r'web/prop.html', {'discussion': user.get('discussion', '')})
def getCntMsg(request): # 通过检查Session检验是否登录了 userId = request.session.get('_id') if userId is None: return render(request, r'web/login.html', {'stat': -5}) # 查询用户名和密码 user = mainapp_dao.firstDocInUser({"_id": ObjectId(userId)}) username = user.get('username') password = user.get('password') return render(request, r'web/cntmsg.html', {'userId': userId, 'username': username, 'password': password})