def data_today(request): if os.environ.get("TEST", None): request.session["userId"] = request.GET["userId"] if not "userId" in request.session: data = {"error": {"title": u"未绑定", "content": u"请先到公众号页面绑定手环"}} return HttpResponse(json.dumps(data), content_type="application/json") try: if not request.GET["userId"] == request.session["userId"]: data = {"error": {"title": u"权限问题", "content": u"您无权访问此页面"}} return HttpResponse(json.dumps(data), content_type="application/json") data = {} date = tools.getDate() data["userId"] = userId = request.session["userId"] data["href"] = "%s/basic/redirect/profile" % wechat_tools.domain user = User.objects.get(openId=userId) dayData = DayData.objects.filter(user=user, date=date) if len(dayData) == 0: dayData = DayData(user=user, date=date) dayData.save() else: dayData = dayData[0] data_list = Data.objects.filter(user=user, date=date) data["steps_now"] = dayData.steps data["steps_plan"] = user.dayPlan if data["steps_now"] < data["steps_plan"]: data["steps_per"] = data["steps_now"] * 100 / data["steps_plan"] else: data["steps_per"] = 100 data["sleep_now_h"] = dayData.sleep / 60 data["sleep_now_m"] = dayData.sleep % 60 data["sleep_plan"] = user.sleepPlan if data["sleep_now_h"] < data["sleep_plan"]: data["sleep_per"] = int( (data["sleep_now_h"] + float(data["sleep_now_m"]) / 60) * 100 / data["sleep_plan"]) else: data["sleep_per"] = 100 data["data_list"] = [] for data_item in data_list: item = {} item["date"] = "%4d-%2d-%2d" % (date / 10000, date % 10000 / 100, date % 100) item["time"] = data_item.startTime.split(" ")[1][:5] type = data_item.type subType = data_item.subType item["content"] = tools.bong_activity[type - 1][subType] data["data_list"].append(item) except: data = {"error": {"title": u"出错啦", "content": u"页面找不到啦!"}} return HttpResponse(json.dumps(data), content_type="application/json")
def data_rank(request): if os.environ.get("TEST", None): request.session["userId"] = request.GET["userId"] if not "userId" in request.session: data = {"error": {"title": u"未绑定", "content": u"请先到公众号绑定手环"}} return HttpResponse(json.dumps(data), content_type="application/json") try: data = {} date = tools.getDate() users = User.objects.all() userId = data["userId"] = request.session["userId"] data["href"] = "%s/basic/redirect/profile" % wechat_tools.domain owner = User.objects.get(openId=userId) data["data_list"] = [] num = 1 for user in users: item = {} item["identity"] = 2 if user.openId == userId: item["identity"] = 0 else: flag = owner.friends.filter(openId=user.openId) if len(flag) > 0: item["identity"] = 1 flag = Good.objects.filter(user=owner, type=0, target=user.id) if len(flag) > 0: item["isGood"] = 1 item["userId"] = user.openId item["num"] = 0 item["username"] = user.name item["image"] = user.image dayData = DayData.objects.filter(user=user, date=date) if len(dayData) == 0: dayData = DayData(user=user, date=date) dayData.save() else: dayData = dayData[0] item["steps"] = dayData.steps item["goods"] = user.goods data["data_list"].append(item) data["data_list"] = sorted(data["data_list"], key=lambda user: user["steps"], reverse=True) ld = len(data["data_list"]) for i in xrange(ld): data["data_list"][i]["num"] = i + 1 except: data = {"error": {"title": u"出错啦", "content": u"页面找不到啦!"}} return HttpResponse(json.dumps(data), content_type="application/json")
def data_rank(request): if os.environ.get("TEST", None): request.session["userId"] = request.GET["userId"] if not "userId" in request.session: data = {"error":{"title":u"未绑定","content":u"请先到公众号绑定手环"}} return HttpResponse(json.dumps(data), content_type="application/json") try: data = {} date = tools.getDate() users = User.objects.all() userId = data["userId"] = request.session["userId"] data["href"] = "%s/basic/redirect/profile" % wechat_tools.domain owner = User.objects.get(openId=userId) data["data_list"] = [] num = 1 for user in users: item = {} item["identity"] = 2 if user.openId == userId: item["identity"] = 0 else: flag = owner.friends.filter(openId=user.openId) if len(flag) > 0: item["identity"] = 1 flag = Good.objects.filter(user=owner,type=0,target=user.id) if len(flag) > 0: item["isGood"] = 1 item["userId"] = user.openId item["num"] = 0 item["username"] = user.name item["image"] = user.image dayData = DayData.objects.filter(user=user,date=date) if len(dayData) == 0: dayData = DayData(user=user,date=date) dayData.save() else: dayData = dayData[0] item["steps"] = dayData.steps item["goods"] = user.goods data["data_list"].append(item) data["data_list"] = sorted(data["data_list"], key=lambda user : user["steps"], reverse=True) ld = len(data["data_list"]) for i in xrange(ld): data["data_list"][i]["num"] = i+1 except: data = {"error":{"title":u"出错啦","content":u"页面找不到啦!"}} return HttpResponse(json.dumps(data), content_type="application/json")
def data_today(request): if os.environ.get("TEST", None): request.session["userId"] = request.GET["userId"] if not "userId" in request.session: data = {"error":{"title":u"未绑定","content":u"请先到公众号页面绑定手环"}} return HttpResponse(json.dumps(data), content_type="application/json") try: if not request.GET["userId"] == request.session["userId"]: data = {"error":{"title":u"权限问题","content":u"您无权访问此页面"}} return HttpResponse(json.dumps(data), content_type="application/json") data = {} date = tools.getDate() data["userId"] = userId = request.session["userId"] data["href"] = "%s/basic/redirect/profile" % wechat_tools.domain user = User.objects.get(openId=userId) dayData = DayData.objects.filter(user=user,date=date) if len(dayData) == 0: dayData = DayData(user=user,date=date) dayData.save() else: dayData = dayData[0] data_list = Data.objects.filter(user=user,date=date) data["steps_now"] = dayData.steps data["steps_plan"] = user.dayPlan if data["steps_now"] < data["steps_plan"]: data["steps_per"] = data["steps_now"] * 100 / data["steps_plan"] else: data["steps_per"] = 100 data["sleep_now_h"] = dayData.sleep / 60 data["sleep_now_m"] = dayData.sleep % 60 data["sleep_plan"] = user.sleepPlan if data["sleep_now_h"] < data["sleep_plan"]: data["sleep_per"] = int((data["sleep_now_h"] + float(data["sleep_now_m"]) / 60) * 100 / data["sleep_plan"]) else: data["sleep_per"] = 100 data["data_list"] = [] for data_item in data_list: item = {} item["date"] = "%4d-%2d-%2d" % (date / 10000, date % 10000 / 100, date % 100) item["time"] = data_item.startTime.split(" ")[1][:5] type = data_item.type subType = data_item.subType item["content"] = tools.bong_activity[type-1][subType] data["data_list"].append(item) except: data = {"error":{"title":u"出错啦","content":u"页面找不到啦!"}} return HttpResponse(json.dumps(data), content_type="application/json")