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")
Beispiel #2
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")
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")