def sendMsgToY(y): if not session["loggedIn"]: return render_template("loginForm.html") POST_MESSAGE = str(request.form["yourMsg"]) cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ INSERT INTO `msgs` ( `from`, `to`, `text` ) VALUES ( {}, {}, "{}" ) """.format( int(session["uId"]), int(y), POST_MESSAGE ) try: cursor.execute(q) cnx.commit() cnx.close() return redirect( "/%schatingTo%s" % ( str(session["uId"]), str(y) ) ) except mySQL.Error as e: print(e) cnx.close() return redirect("/")
def userProfile(): if not session["loggedIn"]: return render_template("loginForm.html") cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT * FROM `users` WHERE username = "******" """.format( str(session["userName"]) ) try: cursor.execute(q) R = cursor.fetchall() cnx.close() if len(R) != 0: return render_template( "userProfile.html", Home = Home(), Notifs = Notifs(), userName = R[0][1], gender = 0, realName = R[0][1 + 1], e_mail = R[0][5], pYDL = showBlocked() ) except mySQL.Error as e: print(e) cnx.close() return index() else: return index()
def passRetrieve(): POST_USERNAME = str(request.form["username"]) if POST_USERNAME == "": print("\nfailed to get the userName; type it in the form\n") else: cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT * FROM `users` WHERE username = "******" """.format( str(POST_USERNAME) ) try: cursor.execute(q) R = cursor.fetchall() cnx.close() if len(R) != 0: sendEmail(str(R[0][0]), str(R[0][1 + 1]), str(R[0][5])) print("\nan email was sent to %s\n" % str(R[0][5])) return index() except mySQL.Error as e: print(e) cnx.close() return redirect("/passRetrieveForm") else: return redirect("/passRetrieveForm") return index()
def deleteUser(): if not session["loggedIn"]: return render_template("loginForm.html") cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ DELETE FROM `users` WHERE username = "******" """.format( str(session["userName"]) ) try: cursor.execute(q) cnx.commit() cnx.close() print("User No. [\033[1m", end = " ") print(session["userName"], end = " ") print("\033[0m] just deleted their account") session["userName"] = None session["loggedIn"] = False return index() except mySQL.Error as e: print(e) cnx.close() return index() else: return index()
def blockUserNo(y): if not session["loggedIn"]: return render_template("loginForm.html") cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT `realName` FROM `users` WHERE `uId` = {} """.format( session["uId"] ) try: cursor.execute(q) R = cursor.fetchall() realName = str(R[0][0]) cnx.close() except mySQL.Error as e: print(e) cnx.close() else: return index() return render_template( "blockingForm.html", x = session["uId"], y = y, N = realName )
def verify(y): cnx, cursor = db_connect(credentials) # return str((cnx, cursor)) if (cnx and cursor): q = """ UPDATE `users` SET active = 1 WHERE uId = {} """.format( int(y) ) try: cursor.execute(q) cnx.commit() cnx.close() return index() except mySQL.Error as e: print(e) cnx.close() infoMsg = "SQL failure..." flash(infoMsg, "warning_login") return render_template("loginForm.html", e = infoMsg) else: infoMsg = "SQL failure..." flash(infoMsg, "warning_login") return render_template("loginForm.html", e = infoMsg) pass
def Notifs(): cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT * FROM `notifs` WHERE uId = {} """.format(str(session["uId"])) try: cursor.execute(q) R = cursor.fetchall() cnx.close() if len(R) != 0: for i in R: if i[3] is 0: return "<span style=\"color: red;\"> %s</span>" % u"•" return "<span style=\"color: #333;\"> %s</span>" % u"•" else: return "<span style=\"color: #333;\"> %s</span>" % u"•" except mySQL.Error as e: print(e) cnx.close() return redirect("/") return redirect("/")
def showBlocked(): cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT * FROM `hates` WHERE hater = {} """.format(session["uId"]) try: cursor.execute(q) hatedList = cursor.fetchall() cnx.close() except mySQL.Error as e: print(e) cnx.close() return redirect("/profile") if len(hatedList) is 0: return "" rStr = "" for i in hatedList: rStr += "".join( ("<h5 class=\"blockedList\">", u"•" + " [<a href=\"/unblockUserNo%s\">unBlock</a>] " % i[2] + i[4] + (", " + i[3] if i[4] != "#NoReason" else ""), "</h5>")) return rStr
def unblockUserNo(y): if not session["loggedIn"]: return render_template("loginForm.html") x = session["uId"] cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ DELETE FROM `hates` WHERE hater = {} AND hated = {} """.format( int(x), int(y) ) try: cursor.execute(q) cnx.commit() cnx.close() return redirect("/profile") except mySQL.Error as e: print(e) cnx.close() return redirect("/profile") else: return redirect("/profile")
def TheyLikeThem(x, y): cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT `liked` FROM `likes` WHERE liker = {} """.format(str(x)) try: cursor.execute(q) R = cursor.fetchall() cnx.close() if len(R) != 0: for i in R: if i[0] == y: return True return False else: return False except mySQL.Error as e: print(e) cnx.close() return redirect("/")
def Home(): cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT * FROM `users` WHERE username = "******" """.format(str(session["userName"])) try: cursor.execute(q) R = cursor.fetchall() cnx.close() if len(R) != 0: userPic = R[0][6] if R[0][6] is None: return "<a href=\"#\" class=\"noPicsHome\">Home</a>" else: return "<a href=\"/\">Home</a>" except mySQL.Error as e: print(e) cnx.close() return redirect("/") return redirect("/")
def grabUsers(): cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT * FROM `users` WHERE username != "{}" """.format(str(session["userName"])) try: cursor.execute(q) R = cursor.fetchall() cnx.close() if len(R) != 0: return R else: return [] except mySQL.Error as e: print(e) cnx.close() return redirect("/") return -1
def newPasswordForm(y): cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT * FROM `users` WHERE username = "******" """.format( str(POST_USERNAME) ) try: cursor.execute(q) R = cursor.fetchall() cnx.close() if len(R) != 0: return render_template( "newPasswordForm.html", y = y, realName = str(R[0][1 + 1]) ) except mySQL.Error as e: print(e) cnx.close() return redirect("/passRetrieveForm")
def notifList(): cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT content, seen FROM `notifs` WHERE uId = {} ORDER BY timedate DESC """.format(str(session["uId"])) try: cursor.execute(q) R = cursor.fetchall() q = """ UPDATE `notifs` SET seen = 1 WHERE uId = {} """.format(str(session["uId"])) try: cursor.execute(q) cnx.commit() except mySQL.Error as e: print(e) cnx.close() return redirect("/") cnx.close() if len(R) != 0: rStr = "" for j in R: if j[1] == 1: rStr += "".join(( "<div style=\"color: %s;\">" % "gray", "<h5 style=\"font-size: 13.5px; Margin-left: 15px;\">", u" • " + "%s, " % j[0], "</h5>", "</div>", "<hr>")) else: rStr += "".join(( "<div style=\"color: %s;\">" % "#303030", "<h5 style=\"font-size: 13.5px; Margin-left: 15px;\">", u" • " + "%s, " % j[0], "</h5>", "</div>", "<hr>")) else: rStr += "".join( ("<div style=\"text-align: center; color: gray;\">", "<h5>", "you don\"t have any notifications yet", "</h5>", "</div>")) return rStr except mySQL.Error as e: print(e) cnx.close() return redirect("/")
def MsgsBtwn(x, y): cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT * FROM `msgs` WHERE (`from` = {} AND `to` = {}) OR (`from` = {} AND `to` = {}) ORDER BY timedate DESC """.format(int(x), int(y), int(y), int(x)) try: cursor.execute(q) msgs = cursor.fetchall() rStr = "" q = """ SELECT `realName` FROM `users` WHERE `uId` = {} """.format(int(y)) try: cursor.execute(q) R = cursor.fetchall() otherName = str(R[0][0]) cnx.close() except mySQL.Error as e: print(e) cnx.close() if len(msgs) != 0: for i in msgs: if int(i[1]) == int(session["uId"]) or int(i[1]) == int(y): rStr += "".join(( "<div class=\"Msgs\">", "<h5 style=\"font-size: 13.5px\">", "%s : " % ("you" if int(i[1]) == int( session["uId"]) else otherName) + i[3] + "<br><br><span style=\"color: gray; float: left;\">{}</span>" .format(i[4]), "</h5>", "</div>", "<hr>")) else: rStr += "".join( ("<div style=\"text-align: center; color: gray;\">", "<h5>", "you didn't start a conversation with %s yet." % otherName, "</h5>", "</div>")) return rStr except mySQL.Error as e: print(e) cnx.close() return redirect("/")
def handleUserInfoChange(POST_USERNAME, POST_GENDER, POST_REALNAME, POST_E_MAIL, POST_PASSWORD, POST_CONFIRM, Biography, Interests, POST_SEXUALITY): ChangeFlag = False cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT * FROM `users` WHERE username = "******" """.format(str(session["userName"])) try: cursor.execute(q) R = cursor.fetchall() cnx.close() if len(R) != 0: Crnt_USERNAME = R[0][1] Crnt_GENDER = R[0][8] Crnt_REALNAME = R[0][2] Crnt_E_MAIL = R[0][5] Crnt_Biography = R[0][10] Crnt_Interests = R[0][11] Crnt_SEXUALITY = R[0][9] except mySQL.Error as e: print(e) cnx.close() return redirect("/") if POST_USERNAME != "" and POST_USERNAME != Crnt_USERNAME: cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT * FROM `users` WHERE username = "******" """.format(str(POST_USERNAME)) try: cursor.execute(q) R = cursor.fetchall() if len(R) != 0: print("failed to change; userName already exists") cnx.close() else: if len(POST_USERNAME) < 5: print( "failed to change; userName is shorter than 5 characters" ) cnx.close() else: q = """ UPDATE `users` SET username = "******" WHERE username = "******" """.format(str(POST_USERNAME), str(Crnt_USERNAME)) cursor.execute(q) cnx.commit() print("changed userName successfully") ChangeFlag = True cnx.close() Crnt_USERNAME = POST_USERNAME session["userName"] = Crnt_USERNAME except mySQL.Error as e: print(q) print(e) cnx.close() if POST_GENDER != Crnt_GENDER: cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ UPDATE `users` SET gender = "{}" WHERE username = "******" """.format(str(POST_GENDER), str(Crnt_USERNAME)) try: cursor.execute(q) cnx.commit() print("changed gender successfully") ChangeFlag = True cnx.close() except mySQL.Error as e: print(q) print(e) cnx.close() return redirect("/profile") if POST_REALNAME != "" and POST_REALNAME != Crnt_REALNAME: testName = POST_REALNAME.split() if len(testName) < 2: print("failed to change; Name & Surname are required") else: cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ UPDATE `users` SET realName = "{}" WHERE username = "******" """.format(str(POST_REALNAME), str(Crnt_USERNAME)) try: cursor.execute(q) cnx.commit() print("changed gender successfully") ChangeFlag = True cnx.close() except mySQL.Error as e: print(q) print(e) cnx.close() if POST_E_MAIL != "" and POST_E_MAIL != Crnt_E_MAIL: if not validate_email(POST_E_MAIL): print("failed to change; email isn't valid") else: cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ UPDATE `users` SET e_mail = "{}" WHERE username = "******" """.format(str(POST_E_MAIL), str(Crnt_USERNAME)) try: cursor.execute(q) cnx.commit() print("changed e_mail successfully") ChangeFlag = True cnx.close() except mySQL.Error as e: print(q) print(e) cnx.close() if POST_PASSWORD == "" and POST_CONFIRM != "": print("failed to change password; unconfirmed password") if POST_PASSWORD != "" and POST_CONFIRM == "": print("failed to change password; unconfirmed password") if POST_PASSWORD != "" and POST_CONFIRM != "": if POST_PASSWORD != POST_CONFIRM: print("failed to change password; unconfirmed password") else: # password check passWordChng = 1 upperCFlag = False lowerCFlag = False numberFlag = False for i in POST_PASSWORD: if i.isalpha(): if i.isupper(): upperCFlag = True elif i.islower(): lowerCFlag = True elif i.isdigit(): numberFlag = True if upperCFlag is False: print( "failed to change password; password missing capital latter(s)" ) if lowerCFlag is False: print( "failed to change password; password missing small latter(s)" ) if numberFlag is False: print("failed to change password; password missing number(s)") if upperCFlag is False or lowerCFlag is False or numberFlag is False: passWordChng = -1 if len(POST_PASSWORD) < 10: print("failed to change password; password is too short") passWordChng = -1 if passWordChng > 0: cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ UPDATE `users` SET password = "******" WHERE username = "******" """.format(str(POST_PASSWORD), str(Crnt_USERNAME)) try: cursor.execute(q) cnx.commit() print("changed password successfully") ChangeFlag = True cnx.close() except mySQL.Error as e: print(q) print(e) cnx.close() if Biography != "": cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ UPDATE `users` SET Biography = "{}" WHERE username = "******" """.format(str(Biography), str(Crnt_USERNAME)) try: cursor.execute(q) cnx.commit() print("changed Biography successfully") ChangeFlag = True cnx.close() except mySQL.Error as e: print(q) print(e) cnx.close() if Interests != "": cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ UPDATE `users` SET Interests = "{}" WHERE username = "******" """.format(str(Interests), str(Crnt_USERNAME)) try: cursor.execute(q) cnx.commit() print("changed Interests successfully") ChangeFlag = True cnx.close() except mySQL.Error as e: print(q) print(e) cnx.close() if POST_SEXUALITY != Crnt_SEXUALITY: cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ UPDATE `users` SET Sexuality = "{}" WHERE username = "******" """.format(str(POST_SEXUALITY), str(Crnt_USERNAME)) try: cursor.execute(q) cnx.commit() print("changed Sexuality successfully") ChangeFlag = True cnx.close() except mySQL.Error as e: print(q) print(e) cnx.close() if ChangeFlag is False: print("failed to change; nothing to change") return redirect("/profile")
def signup(): POST_USERNAME = str(request.form["username"]) POST_REALNAME = str(request.form["realname"]) POST_PASSWORD = str(request.form["password"]) POST_REPEAT = str(request.form["repeat"]) POST_E_MAIL = str(request.form["e_mail"]) POST_SEXUALITY = str(request.form["sexuality"]) if not (POST_USERNAME and POST_REALNAME and POST_PASSWORD and POST_REPEAT and POST_E_MAIL and POST_SEXUALITY): return redirect("/nwusrForm/01") elif POST_USERNAME == "": return redirect("/nwusrForm/01") elif len(POST_USERNAME) < 5: return redirect("/nwusrForm/10") cnx, cursor = db_connect(credentials) # return str((cnx, cursor)) if (cnx and cursor): q = """ SELECT * FROM `users` WHERE username = "******" """.format( str(POST_USERNAME) ) try: cursor.execute(q) if len(cursor.fetchall()) != 0: return redirect("/nwusrForm/02") cnx.close() except mySQL.Error as e: print(e) cnx.close() return redirect("/nwusrForm/03") else: return redirect("/nwusrForm/03") # --- if POST_PASSWORD != POST_REPEAT: return redirect("/nwusrForm/04") # --- if not validate_email(POST_E_MAIL): return redirect("/nwusrForm/05") # --- if request.method == "POST": gender = request.form.getlist("gender") if len(gender) is 1: POST_GENDER = "0" else: POST_GENDER = "1" # --- return userSignUp( POST_USERNAME, POST_REALNAME, POST_PASSWORD, POST_E_MAIL, POST_GENDER, POST_SEXUALITY )
def xHatesNoY(x, y): if not session["loggedIn"]: return render_template("loginForm.html") POST_REASON = str(request.form["why"]) if POST_REASON == "": POST_REASON = "#NoReason" cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT `realName` FROM `users` WHERE `uId` = {} """.format( int(y) ) try: cursor.execute(q) R = cursor.fetchall() realName = str(R[0][0]) except mySQL.Error as e: print(e) cnx.close() return index() q = """ INSERT INTO `hates` ( hater, hated, reason, name ) VALUES ( {}, {}, "{}", "{}" ) """.format( int(x), int(y), POST_REASON, realName ) try: cursor.execute(q) cnx.commit() cnx.close() return index() except mySQL.Error as e: print(e) cnx.close() return index() else: return index()
def showUsers(usersToLeave): users = grabUsers() if len(users) == 0: return "" for i in users: users.remove(i) if i[6] is None else 0 for i in users: users.remove(i) if i[7] is False else 0 if usersToLeave is not None and usersToLeave != []: for i in usersToLeave: users.remove(i) if i in users else 0 myInfo = [] cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT * FROM `users` WHERE username = "******" """.format(str(session["userName"])) try: cursor.execute(q) R = cursor.fetchall() cnx.close() if len(R) != 0: myInfo = R[0] except mySQL.Error as e: print(e) cnx.close() # removing blocked & blocking users (another approach): cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT `hated` FROM `hates` WHERE hater = {} """.format(session["uId"]) try: cursor.execute(q) hatedList = [i[0] for i in cursor.fetchall()] if len(R) != 0: for i in hatedList: for j in users: if j[0] == i: users.remove(j) cnx.close() unH = [i[0] for i in users] except mySQL.Error as e: print(e) cnx.close() return redirect("/") # --- cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT `hater` FROM `hates` WHERE hated = {} """.format(session["uId"]) try: cursor.execute(q) hatedList = [i[0] for i in cursor.fetchall()] if len(R) != 0: for i in hatedList: for j in users: if j[0] == i: users.remove(j) cnx.close() except mySQL.Error as e: print(e) cnx.close() return redirect("/") # --- Cards = "<div class=\"dashRow\">" C = 0 while C < len(users): if C % 4 is 0: Cards += "</div><div class=\"dashRow\">" # --- Cards += "".join(( "<div class=\"column\">", "<div class=\"card\" title=\"%s\">" % moreUserInfo(users[C]), cardPic(users[C][6]), "<div class=\"blockBtn\">", "<a href=\"/blockUserNo%s\" class=\"blockBtn\">" % str(users[C][0]), u"✘", "</a>", "</div>", "<div class=\"likeBtn\">", "<a href=\"/%sLikesNo%s\" class=\"likeBtn\" style=\"color: %s\">" % (myInfo[0], str(users[C][0]), "indianred" if TheyLikeThem( myInfo[0], users[C][0]) else "lightgray"), u"♥", "</a>", "</div>", "<h2 class=\"fameR\">%s</h2>" % str(users[C][15]), "<div class=\"container\">", "<h4><b>%s <a href=\"%schatingTo%s\" class=\"chatBtn\">%s</a></b></h4>" % (users[C][2], myInfo[0], str( users[C][0]), common_interest(myInfo[0], users[C][0])), "<p>%s</p>" % users[C][10] if users[C][10] is not None else "_", "</div>", "</div>", "</div>")) # --- C += 1 R = (4 - (len(users) % 4)) if (len(users) % 4) > 0 else 0 while R > 0: Cards += "".join( ("<div class=\"column\">", "<!-- Nothing.. -->", "</div>")) R -= 1 return Cards
def xLikesNoY(x, y): if not session["loggedIn"]: return render_template("loginForm.html") cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT `liked` FROM `likes` WHERE liker = {} """.format( int(x) ) try: cursor.execute(q) R = cursor.fetchall() if len(R) == 0: q = """ INSERT INTO `likes` ( liker, liked ) VALUES ( {}, {} ) """.format( str(x), str(y) ) try: cursor.execute(q) cnx.commit() except mySQL.Error as e: print(e) cnx.close() return redirect("/") else: R = [i[0] for i in R] print(R) if int(y) in R: q = """ DELETE FROM `likes` WHERE liker = {} AND liked = {} """.format( str(x), str(y) ) try: cursor.execute(q) cnx.commit() except mySQL.Error as e: print(e) cnx.close() return redirect("/") # --- q = """ UPDATE `users` SET `fameR` = `fameR` - 1 WHERE uId = {} """.format( int(y) ) try: cursor.execute(q) cnx.commit() except mySQL.Error as e: print(e) cnx.close() # --- else: q = """ INSERT INTO `likes` ( liker, liked ) VALUES ( {}, {} ) """.format( str(x), str(y) ) try: cursor.execute(q) cnx.commit() except mySQL.Error as e: print(e) cnx.close() # --- q = """ UPDATE `users` SET `fameR` = `fameR` + 1 WHERE uId = {} """.format( int(y) ) try: cursor.execute(q) cnx.commit() except mySQL.Error as e: print(e) cnx.close() # --- except mySQL.Error as e: print(e) cnx.close() cnx.close() #--- cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ SELECT `realName` FROM `users` WHERE userName = "******" """.format( str(session["userName"]) ) try: cursor.execute(q) R = cursor.fetchall() realName = R[0][0] except mySQL.Error as e: print(e) cnx.close() return redirect("/") q = """ INSERT INTO `notifs` ( uId, content, seen ) VALUES ( {}, "{}", {} ) """.format( str(y), "%s liked your profile (at %s)" % (realName, ctime(time())), 0 ) try: cursor.execute(q) cnx.commit() cnx.close() except mySQL.Error as e: print(e) cnx.close() #--- return redirect("/")
def infoChange(): if not session["loggedIn"]: return render_template("loginForm.html") POST_USERNAME = str(request.form["username"]) # --- if request.method == "POST": gender = request.form.getlist("gender") if len(gender) is 1: POST_GENDER = "0" else: POST_GENDER = "1" # --- POST_REALNAME = str(request.form["realname"]) # --- POST_E_MAIL = str(request.form["e_mail"]) # --- POST_PASSWORD = str(request.form["password"]) POST_CONFIRM = str(request.form["confirm"]) # --- Biography = str(request.form["biography"]) Interests = str(request.form["interests"]) # --- POST_SEXUALITY = str(request.form["sexuality"]) # --- handleUserInfoChange( POST_USERNAME, POST_GENDER, POST_REALNAME, POST_E_MAIL, POST_PASSWORD, POST_CONFIRM, Biography, Interests, POST_SEXUALITY ) if request.method == "POST": if "file" not in request.files: print("No picture chosen") else: file = request.files["file"] if file.filename == "": print("No file selected for uploading") elif file and allowed_file(file.filename): inputStr = base64.b64encode(file.read()) cnx, cursor = db_connect(credentials) if (cnx and cursor): q = """ UPDATE `users` SET pic = "{}" WHERE username = "******" """.format( str(inputStr)[1 + 1:-1], str(session["userName"]) ) try: cursor.execute(q) cnx.commit() print("Picture successfully uploaded") ChangeFlag = True cnx.close() except mySQL.Error as e: print(q) print(e) cnx.close() return index() else: print("Not a valied format") return index()