def hoat_dong(): if request.method == "GET": act_list = Activities.objects() return render_template("hoat_dong.html", act = act_list) else: if "token" in session: user = session["token"] form = request.form act_list = Activities.objects() att_list = Attribute.objects(username = user).first() hstr_list = All_history.objects(user = user) for att in att_list: sort = form.get(att) if sort != None: return redirect("/hoat-dong-sx-ttt-" + att) for act in act_list: action = form.get(act["tit"]) if action != None: hstr = All_history(user = user, tit = act["tit"], cre = act["cre"], st = act["st"], knl = act["knl"], per = act["per"], soc = act["soc"] ) for att in att_list : if att in act and att != "id": att_list[att] = att_list[att] + act[att] if att_list[att] < 0: att_list[att] = 0 att_list.save() hstr.save() break return redirect("/save_post") else: return redirect(url_for("sign_in"))
def sign_up(): if request.method == "GET": return render_template("sign_up.html") else: form = request.form fullname = form["fullname"] username = form["username"] email = form["email"] password = form["password"] confirm = form["confirm"] bday = form["bday"] gender = form["gender"] phone = form["telephone"] existUsername = User.objects(username=username).first() existEmail = User.objects(email=email).first() error = None if fullname == "" or username == "" or email == "" or password == "" or confirm == "" or bday == "" or gender == "" or phone == "": error = "Bạn phải điền đầy đủ thông tin" return render_template("sign_up.html", error=error) elif existUsername is not None: error = "Tên đăng nhập đã tồn tại" return render_template("sign_up.html", error=error) elif len(username) < 8: error = "Tên người dùng không khả dụng" return render_template("sign_up.html", error=error) elif len(password) < 8: error = "Mật khẩu không khả dụng" return render_template("sign_up.html", error=error) elif confirm != password: error = "Rất tiếc. Xác nhận mật khẩu sai" return render_template("sign_up.html", error=error) else: m = User( fullname=fullname, username=username, email=email, password=password, birthday=bday, gender=gender, phone=phone, avt= "https://cdn1.iconfinder.com/data/icons/ninja-things-1/1772/ninja-simple-512.png" ) m.save() att = Attribute(username=username) att.save() hstr_list = All_history(user=username) hstr_list.save() Friend(username=username).save() return redirect("/sign_in")
def ca_nhan(): if "token" in session: user = session["token"] info = User.objects(username=user).first() att = Attribute.objects(username=user).first() hstr_list = All_history.objects(user=user) qt_list = Quote.objects(username=user) if request.method == "GET": return render_template("ca_nhan.html", info=info, att=att, quotes=qt_list, history=hstr_list) else: form = request.form quote = form["quote"] author = form["author"] if author == "": q = Quote(username=user, quote=quote) else: q = Quote(username=user, quote=quote, author=author) q.save() return render_template("ca_nhan.html", info=info, att=att, quotes=qt_list, history=hstr_list) else: return redirect(url_for("sign_in"))
def hoat_dong_sx_ttt(sort): if request.method == "GET": act_list = Activities.objects() return render_template("hoat_dong_sx_ttt.html", acts = act_list, sort = sort,name = session["token"],avt="https://cdn1.iconfinder.com/data/icons/ninja-things-1/1772/ninja-simple-512.png") else: if "token" in session: user = session["token"] form = request.form att_list = Attribute.objects(username = user).first() act_list = Activities.objects() toan_bo = form.get("all") if toan_bo != None: return redirect("/hoat-dong") for att in att_list: if att in form: return redirect("/hoat-dong-sx-ttt-" + att) else: pass for act in act_list: act_tit = form.get(act["tit"]) if act_tit != None: hstr_list = All_history.objects(user = user) des = form["description"] img = form["image"] share = form["share"] for att in att_list : if att in act and att != "id": att_list[att] = att_list[att] + act[att] if att_list[att] < 0: att_list[att] = 0 st = act["st"] cre = act["cre"] soc = act["soc"] knl = act["knl"] per = act["per"] att_list.save() All_history(tit = act["tit"], img=img,user=session["token"], des = des, soc = soc, cre = cre, knl = knl, st = st, per = per, time = str(datetime.now().strftime("%Y%m%d"))).save() if share == "yes": post = Post(tit = act["tit"] ,img = img, user = session["token"], descript = des) post.save() break return render_template("hoat_dong_sx_ttt.html", acts = act_list) else: return redirect(url_for("sign_in"))
def save_share(): if request.method == "GET": return render_template("save_share.html") else: form = request.form des = form["description"] img = form["image"] share = form["share"] if share == "yes": post = Post(img=img, user=session["token"], descript=des) post.save() All_history(img=img, user=session["token"], des=des).save() return redirect(url_for("home"))
def save_share(): if request.method == "GET": user = User.objects(username=session["token"]).first() return render_template("save_share.html",name = session["token"],avt=user.avt) else: form = request.form des = form["description"] img = form["image"] share = form["share"] if share == "yes": post = Post(img=img,user=session["token"],descript=des) post.save() All_history(img=img,user=session["token"],des=des).save() return redirect(url_for("ca_nhan"))
def hoat_dong_sx_ttt(sort): if request.method == "GET": act_list = Activities.objects() return render_template("hoat_dong_sx_ttt.html", act=act_list, sort=sort) else: if "token" in session: user = session["token"] form = request.form act_list = Activities.objects() att_list = Attribute.objects(username=user).first() hstr_list = All_history.objects(user=user).first toan_bo = form.get("all") if toan_bo != None: return redirect("/hoat-dong") for att in att_list: sort = form.get(att) if sort != None: return redirect("/hoat-dong-sx-ttt-" + att) for act in act_list: action = form.get(act["tit"]) if action != None: hstr_list.tit.append(act["tit"]) for att in att_list: if att in act and att != "id": att_list[att] = att_list[att] + act[att] if att_list[att] < 0: att_list[att] = 0 att_list.save() hstr_list.save() break return render_template("hoat_dong_sx_ttt.html", act=act_list) else: return redirect(url_for("sign_in"))
def habit(): if "token" in session: user = session["token"] habit_list = Habit.objects(username=user) hstr_list = All_history.objects(user=user) act_list = Activities.objects() acts = [] habits = [] for hstr in hstr_list: if hstr["tit"] not in acts: acts.append(hstr["tit"]) for habit in habit_list: if habit["tit"] not in habit: habits.append(habit["tit"]) for a in acts: act_in_hstr = All_history.objects(user=user, tit=a) combo = 1 if a not in habits: if len(act_in_hstr) >= 3: for i in range(len(act_in_hstr) - 1): if int(act_in_hstr[i + 1].time[6:8]) != int( act_in_hstr[i].time[6:8]) + 1: combo = 0 combo += 1 if i == len(act_in_hstr) - 2: x_data = Activities.objects(tit=a).first() soc = x_data["soc"] per = x_data["per"] st = x_data["st"] knl = x_data["knl"] cre = x_data["cre"] Habit(username=user, tit=a, streak=combo, soc=soc, per=per, knl=knl, st=st, cre=cre).save() else: c_habit = Habit.objects(tit=a).first() if c_habit["streak"] < len(act_in_hstr): if len(act_in_hstr) >= 3: for i in range(len(act_in_hstr) - 1): if int(act_in_hstr[i + 1].time[6:8]) != int( act_in_hstr[i].time[6:8]) + 1: combo = 0 combo += 1 if combo > c_habit["streak"]: c_habit["streak"] = combo c_habit.save() if request.method == "GET": habit_list = Habit.objects(username=user) return render_template("habit.html", habits=habit_list) else: att_list = Attribute.objects(username=user).first() form = request.form for act in act_list: act_tit = form.get(act["tit"]) if act_tit != None: hstr_list = All_history.objects(user=user) des = form["description"] img = form["image"] share = form["share"] for att in att_list: if att in act and att != "id": att_list[att] = att_list[att] + act[att] if att_list[att] < 0: att_list[att] = 0 st = act["st"] cre = act["cre"] soc = act["soc"] knl = act["knl"] per = act["per"] att_list.save() All_history(tit=act["tit"], img=img, user=session["token"], des=des, soc=soc, cre=cre, knl=knl, st=st, per=per, time=str( datetime.now().strftime("%Y%m%d"))).save() if share == "yes": post = Post(tit=act["tit"], img=img, user=session["token"], descript=des) post.save() break return redirect("/ca_nhan") else: return redirect("/sign_in")