def login(): # request: http://127.0.0.1:5000/login?username=asdf # response: json array of chatrooms username = request.args.get('username') userid = libdb.getUserIdFromUserName(username) if userid is None: libdb.addUser(username) chatrooms = libdb.getChatroomsForUser(username) return jsonify({'data' : chatrooms})
def register(): try: data_source = request.get_data(as_text=True) data = json.loads(data_source) except Exception as e: print('err: param err:{}'.format(e)) return resp.ReplyFail(resp.PARAM_ERR_CODE, resp.ParamErr) user_name = data['name'] password = data['password'] avatar = data['avatar'] print("info: username:{} password:{}".format(user_name, password)) # user已存在 if mongo.userExists(user_name): print("err: code:{} msg:{} username:{}".format(resp.EXISTED_ERR_CODE, resp.ExistedErr, user_name)) return resp.ReplyFail(resp.EXISTED_ERR_CODE, resp.ExistedErr) ct = int(round(time.time() * 1000)) hl = hashlib.md5() hl.update(password.encode(encoding='utf-8')) uid = mongo.addUser(user_name, hl.hexdigest(), avatar, ct) if uid is not None: print("info: new user:{} id:{} ct:{}".format(user_name, uid, ct)) return resp.ReplyOK({"uid": uid, "name": user_name}) else: print("err: add user err code:{} msg:{} username:{}".format( resp.SRV_ERR_CODE, resp.SrvErrRet, user_name)) return resp.ReplyFail(resp.SRV_ERR_CODE, resp.SrvErrRet)
def add(): if request.method == "POST": username = "" password = "" current = "" name = "" if 'username' in request.form: username = request.form.get("username") password = request.form.get("password") name = mongo.getName2(username,password) session['user'] = name crushlist = "" if name == 0: return render_template("add.html",name=True,crush=False,crushlist="") crushl = mongo.getPeopleYouLike2(str(name)) return render_template("add.html",name=False,crush=True,crushlist=crushl,person=name,current=current) elif 'usernamereg' in request.form: namereg = request.form.get("namereg") usernamereg = request.form.get("usernamereg") passwordreg1 = request.form.get("passwordreg1") passwordreg2 = request.form.get("passwordreg2") crushlist = "" current = "" if passwordreg1 != passwordreg2: return render_template("add.html",name=True,crush=False,crushlist="") mongo.addUser(namereg,usernamereg,passwordreg1) session['user'] = namereg crushlist = mongo.getPeopleYouLike2(str(namereg)) return render_template("add.html",name=False,crush=True,crushlist=crushlist,person=namereg,current=current) elif 'remove' in request.form: name = session['user'] mongo.removeUser(name) return render_template("add.html",name=True,crush=False,crushlist="") elif 'add' in request.form: meep = request.form.get("crushes") crushl = [x.strip() for x in meep.split('\n')] name = session['user'] for item in crushl: item.strip() x = item.split(", ") print x print len(x) if len(x)>2: mongo.addPerson2(str(name),x[0],x[1],x[2]) elif len(x) == 1: continue else: mongo.addPerson2(str(name),x[0],x[1],"") crushl = mongo.getPeopleYouLike2(str(name)) return render_template("add.html",name=False,crush=True,crushlist=crushl,person=name,current=meep) else: name = session['user'] print request.form crush = request.form.keys()[0] print crush mongo.removeCrush(name,crush) crushl = mongo.getPeopleYouLike2(str(name)) return render_template("add.html",name=False,crush=True,crushlist=crushl,person=name) return render_template("add.html",name=True,crush=False,crushlist="")
def add(): if request.method == "POST": username = "" password = "" current = "" name = "" if 'username' in request.form: username = request.form.get("username") password = request.form.get("password") name = mongo.getName2(username, password) session['user'] = name crushlist = "" if name == 0: return render_template("add.html", name=True, crush=False, crushlist="") crushl = mongo.getPeopleYouLike2(str(name)) return render_template("add.html", name=False, crush=True, crushlist=crushl, person=name, current=current) elif 'usernamereg' in request.form: namereg = request.form.get("namereg") usernamereg = request.form.get("usernamereg") passwordreg1 = request.form.get("passwordreg1") passwordreg2 = request.form.get("passwordreg2") crushlist = "" current = "" if passwordreg1 != passwordreg2: return render_template("add.html", name=True, crush=False, crushlist="") mongo.addUser(namereg, usernamereg, passwordreg1) session['user'] = namereg crushlist = mongo.getPeopleYouLike2(str(namereg)) return render_template("add.html", name=False, crush=True, crushlist=crushlist, person=namereg, current=current) elif 'remove' in request.form: name = session['user'] mongo.removeUser(name) return render_template("add.html", name=True, crush=False, crushlist="") elif 'add' in request.form: meep = request.form.get("crushes") crushl = [x.strip() for x in meep.split('\n')] name = session['user'] for item in crushl: item.strip() x = item.split(", ") print x print len(x) if len(x) > 2: mongo.addPerson2(str(name), x[0], x[1], x[2]) elif len(x) == 1: continue else: mongo.addPerson2(str(name), x[0], x[1], "") crushl = mongo.getPeopleYouLike2(str(name)) return render_template("add.html", name=False, crush=True, crushlist=crushl, person=name, current=meep) else: name = session['user'] print request.form crush = request.form.keys()[0] print crush mongo.removeCrush(name, crush) crushl = mongo.getPeopleYouLike2(str(name)) return render_template("add.html", name=False, crush=True, crushlist=crushl, person=name) return render_template("add.html", name=True, crush=False, crushlist="")
def home(): print "HOME PAGE ###" #global poem poem = [] poems = mongo.getAllPoems() poems.reverse() poems = poems[:10] error = "" #If the user is in the session if 'user' in session: print "USER IN SESSION" if request.method == "GET": return render_template("home.html" ,poems=poems ,loggedin=True ,error=error) #If the user is not in the session elif request.method == "POST": print "USER NOT IN SESSION" username = request.form.get("username") password = request.form.get("password") button = request.form['button'] #If the button pressed is Login if button=='Login': print "LOGGING IN #############" if mongo.exists(username,password): if mongo.checkUser(username, password): session['user'] = username poems = mongo.getPoems(username) poems.reverse() return render_template("profile.html" ,user=username ,poems=poems) else: error = "Incorrect password" return render_template("home.html" ,poems=poems ,loggedin=False ,error=error) else: error = "Username does not exist" return render_template("home.html" ,poems=poems ,loggedin=False ,error=error) #If the button pressed is Register elif button=='Register': print "ADDING USER ########" if not mongo.exists(username, password): session['user'] = username mongo.addUser(username, password) poem = [] print "ADDED USER" return redirect("/generate") else: error = "Username already exists" return render_template("home.html" ,poems=poems ,loggedin=False ,error=error) return render_template("home.html",poems=poems,loggedin=False,error=error)