コード例 #1
0
def MatchsCheck(user, data):
    matchs = user.user_match_members.filter(finished=0)
    now = tools.getNow()
    for match in matchs:
        if match.startTime < now:
            continue
        progress = MatchProgress.objects.filter(match=match, user=user)
        if progress.count() > 0:
            progress = progress[0]
        else:
            continue
        progress.value += tools.caloriesToStep(data.calories, user.height, user.weight)
        progress.save()
        MatchCheck(user, match, progress)
コード例 #2
0
def PlansCheck(user, data):
    plans = user.user_plan_members.filter(finished=0)
    now = tools.getNow()
    for plan in plans:
        if plan.startTime < now:
            continue
        progress = PlanProgress.objects.filter(plan=plan, user=user)
        if progress.count() > 0:
            progress = progress[0]
        else:
            continue
        oldValue = progress.value
        progress.value += tools.caloriesToStep(data.calories, user.height, user.weight)
        progress.save()
        PlanCheck(user, plan, progress, oldValue)
コード例 #3
0
def MatchsCheck(user, data):
    matchs = user.user_match_members.filter(finished=0)
    now = tools.getNow()
    for match in matchs:
        if match.startTime < now:
            continue
        progress = MatchProgress.objects.filter(match=match, user=user)
        if progress.count() > 0:
            progress = progress[0]
        else:
            continue
        progress.value += tools.caloriesToStep(data.calories, user.height,
                                               user.weight)
        progress.save()
        MatchCheck(user, match, progress)
コード例 #4
0
def PlansCheck(user, data):
    plans = user.user_plan_members.filter(finished=0)
    now = tools.getNow()
    for plan in plans:
        if plan.startTime < now:
            continue
        progress = PlanProgress.objects.filter(plan=plan, user=user)
        if progress.count() > 0:
            progress = progress[0]
        else:
            continue
        oldValue = progress.value
        progress.value += tools.caloriesToStep(data.calories, user.height,
                                               user.weight)
        progress.save()
        PlanCheck(user, plan, progress, oldValue)
コード例 #5
0
def MatchCheck(user, match, progress):
    now = tools.getNow()
    url = "%s/match/redirect/profile?page=3&id=%d" % (wechat_tools.domain,
                                                      match.id)
    if now > match.endTime:
        match.finished = 1
        match.save()
        data = {
            "title": {
                "value": u"比赛结束",
                "color": "#ff0000"
            },
            "content": {
                "value": u"%s比赛已经截止,快来看看你们的战况吧!" % match.name,
                "color": "#007fff"
            },
            "remark": {
                "value": u"点击查看比赛详情",
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["msg"],
                                        "#000000", data, url)
    elif match.endTime - now < 7200:
        data = {
            "object": {
                "value": u"%s比赛" % match.name,
                "color": "#007fff"
            },
            "lastTime": {
                "value": tools.left_time(now, match.endTime),
                "color": "#ff0000"
            },
            "steps": {
                "value": str(progress.value),
                "color": "#007fff"
            },
            "remark": {
                "value": u"点击查看比赛详情",
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["progress"],
                                        "#000000", data, url)
コード例 #6
0
def MatchCheck(user, match, progress):
    now = tools.getNow()
    url = "%s/match/redirect/profile?page=3&id=%d" % (wechat_tools.domain, match.id)
    if now > match.endTime:
        match.finished = 1
        match.save()
        data = {
            "title": {"value": u"比赛结束", "color": "#ff0000"},
            "content": {"value": u"%s比赛已经截止,快来看看你们的战况吧!" % match.name, "color": "#007fff"},
            "remark": {"value": u"点击查看比赛详情", "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["msg"], "#000000", data, url)
    elif match.endTime - now < 7200:
        data = {
            "object": {"value": u"%s比赛" % match.name, "color": "#007fff"},
            "lastTime": {"value": tools.left_time(now, match.endTime), "color": "#ff0000"},
            "steps": {"value": str(progress.value), "color": "#007fff"},
            "remark": {"value": u"点击查看比赛详情", "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["progress"], "#000000", data, url)
コード例 #7
0
def submit_make(request):
    if os.environ.get("TEST", None):
        request.session["userId"] = request.GET["userId"]
    if not "userId" in request.session:
        return HttpResponseRedirect("/static/not_bind.html")
    if not "userId" in request.POST:
        return HttpResponseRedirect("/static/404.html")
    if not request.POST["userId"] == request.session["userId"]:
        return HttpResponseRedirect("/static/404.html")
    try:
        userId = request.POST["userId"]
        user = User.objects.get(openId=userId)
        now = basic_tools.getNow()
        plan = Plan(name=request.POST["plan_name"],
                    description=request.POST["comment"],
                    createTime=now,
                    startTime=basic_tools.DateToInt(
                        "%s:00:00" % request.POST["begintime"][:13]),
                    endTime=basic_tools.DateToInt(
                        "%s:00:00" % request.POST["endtime"][:13]),
                    owner=user)
        if request.POST["goal"] == "":
            goal = 0
        else:
            goal = int(request.POST["goal"])
        plan.goal = goal
        plan.save()
        if goal > 0:
            progress = PlanProgress(plan=plan, user=user)
            progress.save()
        tags = []
        tag = request.POST["tags"]
        if not tag == "":
            item = PTag.objects.filter(name=tag)
            if len(item) == 0:
                item = PTag(name=tag)
                item.save()
            else:
                item = item[0]
            if not item.plans.filter(id=plan.id).exists():
                tags.append(tag)
                item.plans.add(plan)
        i = 0
        while ("tag%d" % i) in request.POST:
            tag = request.POST["tag%d" % i]
            i += 1
            if not tag == "":
                item = PTag.objects.filter(name=tag)
                if len(item) == 0:
                    item = PTag(name=tag)
                    item.save()
                else:
                    item = item[0]
                if not item.plans.filter(id=plan.id).exists():
                    tags.append(tag)
                    item.plans.add(plan)
        if len(tags) == 0 and goal > 0:
            tag = "%d步" % goal
            item = PTag.objects.filter(name=tag)
            if len(item) == 0:
                item = PTag(name=tag)
                item.save()
            else:
                item = item[0]
            if not item.plans.filter(id=plan.id).exists():
                tags.append(tag)
                item.plans.add(plan)
        prefix = os.environ.get("WRIST_HOME")
        path = "/media/plan/"
        if not os.path.exists(prefix + path):
            os.mkdir(prefix + path)
        if "image" in request.FILES:
            file = request.FILES["image"]
            file_name = "%s%s_%s_%s" % (path, plan.name.encode("utf-8"),
                                        str(now), file.name.encode("utf-8"))
            des = open(prefix + file_name, "wb")
            for chunk in file.chunks():
                des.write(chunk)
            des.close()
        else:
            file_name = tools.getDefaultImageByTag(tags)
        plan.image = file_name
        plan.save()
        plan.members.add(user)
        i = 0
        while ("friend%d" % i) in request.POST:
            tools.sendInvite(user, plan.id, request.POST["friend%d" % i])
            i += 1
        return HttpResponseRedirect("/plan/redirect/profile?page=4&id=%d" %
                                    plan.id)
    except:
        return HttpResponseRedirect("/static/404.html")
コード例 #8
0
def clickEvent(msg):
    reply = TextReply(content=u"I am 菜单拉取事件", message=msg)
    user = User.objects.get(openId=msg.source)
    id = user.id
    data = tools.client.user.get(msg.source)
    #更新用户姓名、头像
    user.name = data["nickname"]
    if "headimgurl" in data:
        user.image = data["headimgurl"]
    else:
        user.image = ""
    user.save()
    date = basic_tools.getDate()
    datetime = basic_tools.getDateTime()
    now = basic_tools.getNow()
    if msg.key == "V1001_DATA_TODAY":  #今日战况
        data = DayData.objects.filter(user=user, date=date)
        if len(data) == 0:
            data = DayData(user=user, date=date)
            basic_tools.updateDayData(data, user)
            data.save()
        else:
            data = data[0]
        steps = data.steps
        dayPlan = user.dayPlan
        if steps < dayPlan:
            per = steps * 100 / dayPlan
        else:
            per = 100
        if per <= 25:
            remark = u"前路漫漫,要加油噢!"
        elif per >= 40 and per <= 60:
            remark = u"成功的路已经走了一半,继续努力!"
        elif per == 100:
            remark = u"已经完成预定计划了!你真棒!"
        elif per >= 90:
            remark = u"就快要成功了!加油加油!"
        else:
            remark = ""
        url = "%s/basic/redirect/profile?page=0" % tools.domain
        data = {
            "steps": {
                "value": str(steps),
                "color": "#ff0000",
            },
            "per": {
                "value": str(per),
                "color": "#007fff",
            },
            "remark": {
                "value": remark,
                "color": "#666666"
            }
        }
        tools.customSendTemplate(msg.source, tools.template_id["data"],
                                 "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_DATA_BIND":  #绑定手环
        url = "%s/basic/bind?openId=%s" % (tools.domain, msg.source)
        data = {"content": {"value": u"请点击进入绑定", "color": "#ff5656"}}
        tools.customSendTemplate(msg.source, tools.template_id["msg"],
                                 "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_DATA_HELP":  #帮助
        reply = TextReply(content=tools.help_text, message=msg)
    elif msg.key == "V1001_DATA_PROFILE":  #个人主页
        dayPlan = user.dayPlan
        sleepPlan = user.sleepPlan
        if dayPlan == 0 or sleepPlan == 0:
            remark = u"您的计划设置不完整,请点击进入设置"
        else:
            remark = user.comment
            if remark == "":
                remark = u"点击查看个人信息"
        url = "%s/basic/redirect/profile?page=3" % tools.domain
        data = {
            "dayPlan": {
                "value": str(dayPlan),
                "color": "#ff0000"
            },
            "sleepPlan": {
                "value": str(sleepPlan),
                "color": "#007fff"
            },
            "remark": {
                "value": remark,
                "color": "#666666"
            }
        }
        tools.customSendTemplate(msg.source, tools.template_id["profile"],
                                 "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_PLAN_MAKE":  #制定计划
        articles = []
        articles.append({
            "title": u"制定计划",
            "description": u"快来制定你自己的运动计划吧!",
            "image": "%s/static/img/plan_make.jpg" % tools.domain,
            "url": "%s/plan/redirect/profile?page=0" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_OWN":  #我的计划
        articles = []
        articles.append({
            "title": u"查看我的",
            "description": u"来看看你都有什么运动计划吧",
            "image": "%s/static/img/plan_own.jpg" % tools.domain,
            "url": "%s/plan/redirect/profile?page=3" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_SQUARE":  #计划广场
        articles = []
        articles.append({
            "title": u"计划广场",
            "description": u"新鲜出炉的运动计划",
            "image": "%s/static/img/plan_square.jpg" % tools.domain,
            "url": "%s/plan/redirect/profile?page=2" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_RANK":  #计划排行榜
        articles = []
        articles.append({
            "title": u"计划排行榜",
            "description": u"想知道什么计划更受欢迎么",
            "image": "%s/static/img/plan_rank.jpg" % tools.domain,
            "url": "%s/plan/redirect/profile?page=1" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_MATCH_MAKE":  #创建比赛
        articles = []
        articles.append({
            "title":
            u"创建比赛",
            "description":
            u"开始一场新的比赛吧!",
            "image":
            "%s/static/img/match_make.jpg" % tools.domain,
            "url":
            "%s/match/redirect/profile?page=0" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_MATCH_CHECK":  #我的比赛进度查看
        closest_match = match_tools.closest_match(user, now)
        if not closest_match:  #没有未结束的比赛
            data = {
                "title": {
                    "value": u"比赛进度提醒",
                    "color": "#000000"
                },
                "content": {
                    "value": u"没有未结束的比赛",
                    "color": "#ff0000"
                },
                "remark": {
                    "value": u"来亲自发起一场比赛吧!",
                    "color": "#666666"
                }
            }
            url = "%s/match/redirect/profile?page=0" % tools.domain
            tools.customSendTemplate(msg.source, tools.template_id["msg"],
                                     "#000000", data, url)
        else:  #返回最近的比赛的进度
            left_time = basic_tools.left_time(now, closest_match.endTime)
            steps = match_tools.getProgress(closest_match, user)
            data = {
                "object": {
                    "value": u"%s 比赛" % closest_match.title,
                    "color": "#007fff"
                },
                "lastTime": {
                    "value": left_time,
                    "color": "#ff0000"
                },
                "steps": {
                    "value": str(steps),
                    "color": "#007fff"
                },
                "remark": {
                    "value": u"请继续努力!",
                    "color": "#666666"
                }
            }
            url = "%s/match/redirect/profile?page=1" % tools.domain
            tools.customSendTemplate(msg.source, tools.template_id["progress"],
                                     "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_MATCH_SQUARE":  #比赛广场
        articles = []
        articles.append({
            "title":
            u"比赛广场",
            "description":
            u"新鲜出炉的比赛",
            "image":
            "%s/static/img/match_square.jpg" % tools.domain,
            "url":
            "%s/match/redirect/profile?page=2" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    return HttpResponse(reply)
コード例 #9
0
def submit_make(request):
    if os.environ.get("TEST", None):
        request.session["userId"] = request.GET["userId"]
    if not "userId" in request.session:
        return HttpResponseRedirect("/static/not_bind.html")
    if not "userId" in request.POST:
        return HttpResponseRedirect("/static/404.html")
    if not request.POST["userId"] == request.session["userId"]:
        return HttpResponseRedirect("/static/404.html")
    try:
        userId = request.POST["userId"]
        user = User.objects.get(openId=userId)
        now = basic_tools.getNow()
        plan = Plan(name=request.POST["plan_name"],description=request.POST["comment"],createTime=now,startTime=basic_tools.DateToInt("%s:00:00" % request.POST["begintime"][:13]),endTime=basic_tools.DateToInt("%s:00:00" % request.POST["endtime"][:13]),owner=user)
        if request.POST["goal"] == "":
            goal = 0
        else:
            goal = int(request.POST["goal"])
        plan.goal=goal
        plan.save()
        if goal > 0:
            progress = PlanProgress(plan=plan,user=user)
            progress.save()
        tags = []
        tag = request.POST["tags"]
        if not tag == "":
            item = PTag.objects.filter(name=tag)
            if len(item) == 0:
                item = PTag(name=tag)
                item.save()
            else:
                item = item[0]
            if not item.plans.filter(id=plan.id).exists():
                tags.append(tag)
                item.plans.add(plan)
        i = 0
        while ("tag%d" % i) in request.POST:
            tag = request.POST["tag%d" % i]
            i += 1
            if not tag == "":
                item = PTag.objects.filter(name=tag)
                if len(item) == 0:
                    item = PTag(name=tag)
                    item.save()
                else:
                    item = item[0]
                if not item.plans.filter(id=plan.id).exists():
                    tags.append(tag)
                    item.plans.add(plan)
        if len(tags) == 0 and goal > 0:
            tag = "%d步" % goal
            item = PTag.objects.filter(name=tag)
            if len(item) == 0:
                item = PTag(name=tag)
                item.save()
            else:
                item = item[0]
            if not item.plans.filter(id=plan.id).exists():
                tags.append(tag)
                item.plans.add(plan)
        prefix = os.environ.get("WRIST_HOME")
        path = "/media/plan/"
        if not os.path.exists(prefix+path):
            os.mkdir(prefix+path)
        if "image" in request.FILES:
            file = request.FILES["image"]
            file_name = "%s%s_%s_%s" % (path, plan.name.encode("utf-8"), str(now), file.name.encode("utf-8"))
            des = open(prefix+file_name, "wb")
            for chunk in file.chunks():
                des.write(chunk)
            des.close()
        else:
            file_name = tools.getDefaultImageByTag(tags)
        plan.image = file_name
        plan.save()
        plan.members.add(user)
        i = 0
        while ("friend%d" % i) in request.POST:
            tools.sendInvite(user, plan.id, request.POST["friend%d" % i])
            i += 1
        return HttpResponseRedirect("/plan/redirect/profile?page=4&id=%d" % plan.id)
    except:
        return HttpResponseRedirect("/static/404.html")
コード例 #10
0
def PlanCheck(user, plan, progress, oldValue):
    now = tools.getNow()
    url = "%s/plan/redirect/profile?page=4&id=%d" % (wechat_tools.domain, plan.id)
    if now > plan.endTime:
        plan.finished = 1
        plan.save()
        if plan.goal > 0 and progress.value >= plan.goal:
            content = u"恭喜您完成了计划"
        elif plan.goal > 0:
            content = u"很遗憾您没能完成计划"
        else:
            content = u"不知道您完成得怎么样呢"
        data = {
            "title": {"value": u"计划截止", "color": "#ff0000"},
            "content": {"value": u"%s计划已经截止,%s" % (plan.name, content), "color": "#007fff"},
            "remark": {"value": u"点击查看计划详情", "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["msg"], "#000000", data, url)
        return
    old_per = per = 0
    if plan.goal > 0:
        per = int(progress.value * 100 / plan.goal)
        old_per = int(oldValue * 100 / plan.goal)
    if per > 100:
        per = 100
    if per == 100:
        plan.finished = 1
        plan.save()
        data = {
            "title": {"value": u"计划完成", "color": "#ff0000"},
            "content": {"value": u"恭喜您提前完成了%s计划已经完成" % plan.name, "color": "#007fff"},
            "remark": {"value": u"点击查看计划详情", "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["msg"], "#000000", data, url)
    elif plan.endTime - now < 7200:
        if per < 90:
            remark = u"请抓紧时间!"
        else:
            remark = u"别错过了ddl噢"
        data = {
            "object": {"value": u"%s计划" % plan.name, "color": "#007fff"},
            "lastTime": {"value": tools.left_time(now, plan.endTime), "color": "#ff0000"},
            "steps": {"value": str(progress.value), "color": "#007fff"},
            "content": {"value": u"您已完成了%d\%" % per, "color": "#ff0000"},
            "remark": {"value": remark, "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["progress"], "#000000", data, url)
    elif (per > 80 and old_per < 80) or (per > 50 and old_per < 50) or (per > 20 and old_per < 20):
        if per > 80:
            remark = u"请继续努力"
        elif per > 50:
            remark = u"长路漫漫,还有一半"
        else:
            remark = u"好的开始是成功的一半"
        data = {
            "object": {"value": u"%s计划" % plan.name, "color": "#007fff"},
            "lastTime": {"value": tools.left_time(now, plan.endTime), "color": "#ff0000"},
            "steps": {"value": str(progress.value), "color": "#007fff"},
            "content": {"value": u"您已完成了%d\%" % per, "color": "#ff0000"},
            "remark": {"value": remark, "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["progress"], "#000000", data, url)
コード例 #11
0
def submit_make(request):
    if os.environ.get("TEST", None):
        request.session["userId"] = request.GET["userId"]
    try:
        userId = request.POST["userId"]
        user = User.objects.get(openId=userId)
        now = basic_tools.getNow()
        match = Match(title=request.POST["match_name"],description=request.POST["comment"],createTime=now,startTime=basic_tools.DateToInt("%s:00:00" % request.POST["begintime"][:13]),endTime=basic_tools.DateToInt("%s:00:00" % request.POST["endtime"][:13]),creator=user)
        match.save()
        tags = []
        tag = request.POST["tags"]
        if not tag == "":
            item = MTag.objects.filter(name=tag)
            if len(item) == 0:
                item = MTag(name=tag)
                item.save()
            else:
                item = item[0]
            if not item.matchs.filter(id=match.id).exists():
                tags.append(tag)
                item.matchs.add(match)
        i = 0
        while ("tag%d" % i) in request.POST:
            tag = request.POST["tag%d" % i]
            i += 1
            if not tag == "":
                item = MTag.objects.filter(name=tag)
                if len(item) == 0:
                    item = MTag(name=tag)
                    item.save()
                else:
                    item = item[0]
                if not item.matchs.filter(id=match.id).exists():
                    tags.append(tag)
                    item.matchs.add(match)
        match.user_members.add(user)
        team = user.user_team_members.get(type=0)
        match.members.add(team)
        prefix = os.environ.get("WRIST_HOME")
        path = "/media/match/"
        if not os.path.exists(prefix+path):
            os.mkdir(prefix+path)
        if "image" in request.FILES:
            file = request.FILES["image"]
            file_name = "%s%s_%s_%s" % (path, match.title.encode("utf-8"), str(now), file.name.encode("utf-8"))
            des = open(prefix+file_name, "wb")
            for chunk in file.chunks():
                des.write(chunk)
            des.close()
        else:
            file_name = tools.getDefaultImageByTag(tags)
        match.image = file_name
        match.save()
        i = 0
        while ("friend%d" % i) in request.POST:
            tools.sendInvite(user, plan.id, request.POST["friend%d" % i], 0)
            i += 1
        i = 0
        while ("opponent%d" % i) in request.POST:
            tools.sendInvite(user, plan.id, request.POST["opponent%d" % i], 1)
            i += 1
        return HttpResponseRedirect("/match/redirect/profile?page=3&id=%d" % match.id)
    except:
        return HttpResponse("/static/404.html")
コード例 #12
0
def match_profile(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")
    if not "userId" in request.GET:
        data = {"error":{"title":u"出错啦","content":u"这个页面找不到啦!"}}
        return HttpResponse(json.dumps(data), content_type="application/json")
    try:
        data = {}
        userId = data["userId"] = request.session["userId"]
        user = User.objects.get(openId=userId)
        data["href"] = "%s/match/redirect/profile" % wechat_tools.domain
        id = request.GET["id"]
        match = Match.objects.get(id=id)
        data["id"] = match.id
        data["title"] = match.title
        data["description"] = match.description
        data["image"] = match.image
        data["startTime"] = basic_tools.IntToDate(match.startTime)
        data["endTime"] = basic_tools.IntToDate(match.endTime)
        if match.finished == 1:
            data["isFinished"] = 1
        if basic_tools.getNow() > match.startTime:
            data["isStarted"] = 1
        data["tags"] = []
        tags = match.match_mtag_matchs.all()
        for tag in tags:
            data["tags"].append(tag.name)
        tmp = match.user_members.filter(openId=userId).count()
        if tmp > 0:
            data["isFollow"] = 1
            data["step"] = tools.getProgress(match, user)
        data["data_list"] = []
        teams = match.members.all()
        for team in teams:
            item = {}
            item["num"] = 0
            item["name"] = team.name
            item["members"] = []
            steps = 0
            members = team.members.all()
            item["ld"] = members.count()
            num = 0
            for member in members:
                step = tools.getSingleProgress(match, member)
                steps += step
                item["members"].append({
                    "image":member.image,
                    "name":member.name,
                    "step":step,
                    "num":num
                    })
                num += 1
            item["step"] = steps
            data["data_list"].append(item)
        data["data_list"] = sorted(data["data_list"], key=lambda item : item["step"], 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")
コード例 #13
0
def clickEvent(msg):
    reply = TextReply(content=u"I am 菜单拉取事件", message=msg)
    user = User.objects.get(openId=msg.source)
    id = user.id
    data = tools.client.user.get(msg.source)
    #更新用户姓名、头像
    user.name = data["nickname"]
    if "headimgurl" in data:
        user.image = data["headimgurl"]
    else:
        user.image = ""
    user.save()
    date = basic_tools.getDate()
    datetime = basic_tools.getDateTime()
    now = basic_tools.getNow()
    if msg.key == "V1001_DATA_TODAY": #今日战况
        data = DayData.objects.filter(user=user,date=date)
        if len(data) == 0:
            data = DayData(user=user,date=date)
            basic_tools.updateDayData(data, user)
            data.save()
        else:
            data = data[0]
        steps = data.steps
        dayPlan = user.dayPlan
        if steps < dayPlan:
           per = steps * 100 / dayPlan
        else:
           per = 100
        if per <= 25:
           remark = u"前路漫漫,要加油噢!"
        elif per >= 40 and per <= 60:
           remark = u"成功的路已经走了一半,继续努力!"
        elif per == 100:
           remark = u"已经完成预定计划了!你真棒!"
        elif per >= 90:
           remark = u"就快要成功了!加油加油!"
        else:
           remark = ""
        url = "%s/basic/redirect/profile?page=0" % tools.domain
        data = {
          "steps":{
            "value": str(steps),
            "color": "#ff0000",
          },
          "per":{
            "value": str(per),
            "color": "#007fff",
          },
          "remark":{
            "value": remark,
            "color": "#666666"
          }
        }
        tools.customSendTemplate(msg.source, tools.template_id["data"], "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_DATA_BIND": #绑定手环
        url = "%s/basic/bind?openId=%s" % (tools.domain, msg.source)
        data = {
          "content":{
            "value": u"请点击进入绑定",
            "color": "#ff5656"
          }
        }
        tools.customSendTemplate(msg.source, tools.template_id["msg"], "#000000", data, url) 
        reply = ""
    elif msg.key == "V1001_DATA_HELP": #帮助
        reply = TextReply(content=tools.help_text, message=msg)
    elif msg.key == "V1001_DATA_PROFILE": #个人主页
        dayPlan = user.dayPlan
        sleepPlan = user.sleepPlan
        if dayPlan == 0 or sleepPlan == 0:
            remark = u"您的计划设置不完整,请点击进入设置"
        else:
            remark = user.comment
            if remark == "":
                remark = u"点击查看个人信息"
        url = "%s/basic/redirect/profile?page=3" % tools.domain
        data = {
            "dayPlan":{
              "value": str(dayPlan),
              "color": "#ff0000"
            },
            "sleepPlan":{
              "value": str(sleepPlan),
              "color": "#007fff"
            },
            "remark":{
              "value": remark,
              "color": "#666666"
            }
        }
        tools.customSendTemplate(msg.source, tools.template_id["profile"], "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_PLAN_MAKE":  #制定计划
        articles = []
        articles.append({"title":u"制定计划","description":u"快来制定你自己的运动计划吧!","image":"%s/static/img/plan_make.jpg" % tools.domain,"url":"%s/plan/redirect/profile?page=0" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_OWN":   #我的计划
        articles = []
        articles.append({"title":u"查看我的","description":u"来看看你都有什么运动计划吧","image":"%s/static/img/plan_own.jpg" % tools.domain,"url":"%s/plan/redirect/profile?page=3" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_SQUARE":    #计划广场
        articles = []
        articles.append({"title":u"计划广场","description":u"新鲜出炉的运动计划","image":"%s/static/img/plan_square.jpg" % tools.domain,"url":"%s/plan/redirect/profile?page=2" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_RANK":  #计划排行榜
        articles = []
        articles.append({"title":u"计划排行榜","description":u"想知道什么计划更受欢迎么","image":"%s/static/img/plan_rank.jpg" % tools.domain,"url":"%s/plan/redirect/profile?page=1" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_MATCH_MAKE": #创建比赛
        articles = []
        articles.append({"title":u"创建比赛","description":u"开始一场新的比赛吧!","image":"%s/static/img/match_make.jpg" % tools.domain,"url":"%s/match/redirect/profile?page=0" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_MATCH_CHECK":    #我的比赛进度查看
        closest_match = match_tools.closest_match(user, now)
        if not closest_match:   #没有未结束的比赛
            data = {
                "title":{
                    "value": u"比赛进度提醒",
                    "color": "#000000"
                },
                "content":{
                    "value": u"没有未结束的比赛",
                    "color": "#ff0000"
                },
                "remark":{
                    "value": u"来亲自发起一场比赛吧!",
                    "color": "#666666"
                }
            }
            url = "%s/match/redirect/profile?page=0" % tools.domain
            tools.customSendTemplate(msg.source, tools.template_id["msg"], "#000000", data, url)
        else:   #返回最近的比赛的进度
            left_time = basic_tools.left_time(now, closest_match.endTime)
            steps = match_tools.getProgress(closest_match, user)
            data = {
                "object":{
                    "value": u"%s 比赛" % closest_match.title,
                    "color": "#007fff"
                },
                "lastTime":{
                    "value": left_time,
                    "color": "#ff0000"
                },
                "steps":{
                    "value": str(steps),
                    "color": "#007fff"
                },
                "remark":{
                    "value": u"请继续努力!",
                    "color": "#666666"
                }
            }
            url = "%s/match/redirect/profile?page=1" % tools.domain
            tools.customSendTemplate(msg.source, tools.template_id["progress"], "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_MATCH_SQUARE": #比赛广场
        articles = []
        articles.append({"title":u"比赛广场","description":u"新鲜出炉的比赛","image":"%s/static/img/match_square.jpg" % tools.domain,"url":"%s/match/redirect/profile?page=2" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    return HttpResponse(reply)
コード例 #14
0
def PlanCheck(user, plan, progress, oldValue):
    now = tools.getNow()
    url = "%s/plan/redirect/profile?page=4&id=%d" % (wechat_tools.domain,
                                                     plan.id)
    if now > plan.endTime:
        plan.finished = 1
        plan.save()
        if plan.goal > 0 and progress.value >= plan.goal:
            content = u"恭喜您完成了计划"
        elif plan.goal > 0:
            content = u"很遗憾您没能完成计划"
        else:
            content = u"不知道您完成得怎么样呢"
        data = {
            "title": {
                "value": u"计划截止",
                "color": "#ff0000"
            },
            "content": {
                "value": u"%s计划已经截止,%s" % (plan.name, content),
                "color": "#007fff"
            },
            "remark": {
                "value": u"点击查看计划详情",
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["msg"],
                                        "#000000", data, url)
        return
    old_per = per = 0
    if plan.goal > 0:
        per = int(progress.value * 100 / plan.goal)
        old_per = int(oldValue * 100 / plan.goal)
    if per > 100:
        per = 100
    if per == 100:
        plan.finished = 1
        plan.save()
        data = {
            "title": {
                "value": u"计划完成",
                "color": "#ff0000"
            },
            "content": {
                "value": u"恭喜您提前完成了%s计划已经完成" % plan.name,
                "color": "#007fff"
            },
            "remark": {
                "value": u"点击查看计划详情",
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["msg"],
                                        "#000000", data, url)
    elif plan.endTime - now < 7200:
        if per < 90:
            remark = u"请抓紧时间!"
        else:
            remark = u"别错过了ddl噢"
        data = {
            "object": {
                "value": u"%s计划" % plan.name,
                "color": "#007fff"
            },
            "lastTime": {
                "value": tools.left_time(now, plan.endTime),
                "color": "#ff0000"
            },
            "steps": {
                "value": str(progress.value),
                "color": "#007fff"
            },
            "content": {
                "value": u"您已完成了%d\%" % per,
                "color": "#ff0000"
            },
            "remark": {
                "value": remark,
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["progress"],
                                        "#000000", data, url)
    elif (per > 80 and old_per < 80) or (per > 50 and old_per < 50) or (
            per > 20 and old_per < 20):
        if per > 80:
            remark = u"请继续努力"
        elif per > 50:
            remark = u"长路漫漫,还有一半"
        else:
            remark = u"好的开始是成功的一半"
        data = {
            "object": {
                "value": u"%s计划" % plan.name,
                "color": "#007fff"
            },
            "lastTime": {
                "value": tools.left_time(now, plan.endTime),
                "color": "#ff0000"
            },
            "steps": {
                "value": str(progress.value),
                "color": "#007fff"
            },
            "content": {
                "value": u"您已完成了%d\%" % per,
                "color": "#ff0000"
            },
            "remark": {
                "value": remark,
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["progress"],
                                        "#000000", data, url)