def upload(): '''adds a post's information into the database''' roomCode = request.form.get("rCode") roomNum = request.form.get("rNum") rating = request.form.get("rating") review = request.form.get("review") rmID = roomCode + roomNum username = session['CAS_USERNAME'] try: postconn = db.getConn(DB) uid = db.getUid(postconn, username) if db.checkReview(postconn, uid, rmID): flash( 'Already posted a review. Go to your profile to edit your review!' ) return redirect(url_for('index')) #upload folder path, and allowed extension of file images #check if this exists path = 'static/img/{}'.format(username) if not os.path.exists(path): os.mkdir('static/img/{}'.format(username)) UPLOAD_FOLDER = 'static/img/{}/'.format(username) ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER #add code to change file name #check allowed files def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS uid = db.getUid(postconn, username) file = request.files['upload'] # print("FILE:") #print(file) if file.filename == '': #check if they uploaded an img filePath = 'NA' db.insertReview(postconn, uid, rmID, rating, review, filePath) return redirect(url_for('roomReview', rmID=rmID)) filePath = None if file and allowed_file(file.filename): filename = secure_filename(file.filename) #get the filename file.save(os.path.join( app.config['UPLOAD_FOLDER'], filename)) #save the file to the upload folder destination filePath = os.path.join( 'img/{}/'.format(username), filename ) #make a modified path so the profile.html can read it db.insertReview(postconn, uid, rmID, rating, review, filePath) return redirect(url_for('roomReview', rmID=rmID)) return redirect(url_for('index')) except Exception as err: # print("upload failed because " + str(err)) flash('Upload failed {why}'.format(why=err)) return redirect(request.referrer)
def followingList(username): '''gets a list of users links that the current profile is following''' conn = db.getConn(DB) profUID = db.getUid(conn, username) users = db.followingUsers(conn, profUID) print(users) return render_template("listofFollowing.html", page_title="{} Following".format(username), users=users, options=False)
def likes(post): ''' ajax function that updates the number of likes when either a like is added or deleted''' try: conn = db.getConn(DB) profUID = db.getUid(conn, username) numberLikes = db.countLikes(conn, post) conn.close() return jsonify(numLikes=numberLikes) except Exception as err: print(err) return jsonify({'error': True, 'err': str(err)})
def Unsave(rmID): ''' ajax function for unsaving a room''' try: username = session['CAS_USERNAME'] conn = db.getConn(DB) uid = db.getUid(conn, username) db.removeSave(conn, rmID, uid) conn.close() return jsonify() except Exception as err: #print(err) return jsonify({'error': True, 'err': str(err)})
def edit(rmID): try: conn = db.getConn(DB) username = session['CAS_USERNAME'] rating = request.form.get("rating") review = request.form.get("review") uid = db.getUid(conn, username) db.editReview(db.getConn(DB), uid, rmID, rating, review) conn.close() return redirect(url_for('roomReview', rmID=rmID)) except Exception as err: #print(err) return (redirect(request.referrer))
def dFollow(username): ''' ajax function deletes a follower to the Follows table and updates the follower and following count and returns that as a jsonify ''' try: conn = db.getConn(DB) profUID = db.getUid(conn, username) db.deletefollower(conn, session['uid'], profUID) numFollowing = db.numFollowing(conn, profUID) numFollowers = db.numFollowers(conn, profUID) return jsonify(updateFollowers=numFollowers, updateFollowing=numFollowing) except Exception as err: print(err) return jsonify({'error': True, 'err': str(err)})
def deleteReview(): try: conn = db.getConn(DB) room = request.form.get('rmID') username = session['CAS_USERNAME'] uid = db.getUid(conn, session['CAS_USERNAME']) img = db.getImgfromRmID(conn, room) #print(imgPath) db.deleteReview(conn, uid, room) filePath = 'static/{}'.format(img.get('imgPath')) os.remove(filePath) #print(room, " review deleted") return redirect(request.referrer) except Exception as err: #print(err) return (redirect(request.referrer))
def profile(username): ''' gets the uid of the profile, sees whether match is true so we know if the its the session's own profile so the edit profile button can appear on their page ''' conn = db.getConn(DB) uid = db.getUid(conn, username) #if not uid: # flash("User not found") # return render_template("home.html") uid = uid match = False if session[ 'uid'] == uid: #if the session user is on their profile or someone elses match = True fullName = db.getFullName(conn, uid) bioText = db.getBioText(conn, uid) profPic = db.getPPic(conn, uid) posts = db.getPostsByUser(conn, uid) numPosts = db.numPostsUser(conn, uid) numFollowing = db.numFollowing(conn, uid) numFollowers = db.numFollowers(conn, uid) followingBoolean = db.following_trueFalse( conn, session['uid'], uid ) #note that that a session username cannot follow itself, so jinja2 in profile.page will #figure out whether the button should show up using match variable as well, it can carry over but it's used depending on the profile if followingBoolean == True: #checks if there are already following that profile buttonText = "Following" else: buttonText = "Follow" return render_template('profile.html', profName=username, uid=uid, fname=fullName['fullname'], bio=bioText['biotxt'], ppic=profPic['profpicPath'], posts=posts, postNum=numPosts, match=match, numFing=numFollowing, numFers=numFollowers, tButton=buttonText)
def pic(): conn = db.getConn(DB) username = session['CAS_USERNAME'] uid = db.getUid(conn, username) #uid = 1 #upload folder path, and allowed extension of file images #check if this exists # username = "******" path = 'static/img/{}'.format(username) # print(path) # print(os.path.exists(path)) try: if not os.path.exists(path): os.mkdir('static/img/{}'.format(username)) UPLOAD_FOLDER = 'static/img/{}/'.format(username) ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER #add code to change file name #check allowed files def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS file = request.files['newpic'] filePath = None if file and allowed_file(file.filename): filename = secure_filename(file.filename) #get the filename file.save(os.path.join( app.config['UPLOAD_FOLDER'], filename)) #save the file to the upload folder destination filePath = os.path.join( 'img/{}/'.format(username), filename ) #make a modified path so the profile.html can read it db.changePfp(conn, uid, filePath) conn.close() conn.close() return redirect(request.referrer) except Exception as err: flash(repr(err)) return redirect(request.referrer)
def profile(): if session['CAS_USERNAME']: try: conn = db.getConn(DB) username = session['CAS_USERNAME'] uid = db.getUid(conn, username) rooms = db.getMyRooms(conn, uid) path = db.getPicPath(conn, uid) savedRooms = db.getSaved(conn, uid) conn.close() return render_template('profile.html', page_title='Dormir', my_rooms=rooms, pic=path, username=username, starred_rooms=savedRooms) except Exception as err: flash('profile failed to load due to {why}'.format(why=err)) return redirect(request.referrer) else: return redirect(url_for('index'))
def index(): try: conn = db.getConn(DB) if 'CAS_USERNAME' in session: username = session['CAS_USERNAME'] profpicPath = 'img/default_profilepic.jpg' username = session['CAS_USERNAME'] attribs = session['CAS_ATTRIBUTES'] firstName = attribs['cas:givenName'] lastName = attribs['cas:sn'] fullname = firstName + ' ' + lastName curs = dbi.cursor(conn) curs.execute('''SELECT * FROM Users WHERE username = %s''', [username]) row = curs.fetchone() if row is None: curs.execute( '''INSERT INTO Users(fullname, username, profpicPath) VALUES(%s,%s,%s)''', [fullname, username, profpicPath]) conn.commit() uid = db.getUid(conn, username) conn.close() return render_template('home.html') else: if db.existsReview(conn) == True: randomrmID = db.randomReviewoftheDay(conn) randomRoom = db.getRoomInfo(conn, randomrmID.get('rmID')) allRooms = db.getallRooms(conn) topRooms = {} for roomID in allRooms: rating = db.getAverageRating(conn, roomID.get('rmID')) topRooms[roomID.get('rmID')] = rating.get('rate') sort_rooms = sorted(topRooms.items(), key=lambda x: x[1], reverse=True) if len(allRooms) >= 3: three = True two = False one = False top1 = sort_rooms[0][0] top1Rating = sort_rooms[0][1] img1 = db.getImgfromRmID(conn, top1) top2 = sort_rooms[1][0] top2Rating = sort_rooms[1][1] img2 = db.getImgfromRmID(conn, top2) top3 = sort_rooms[2][0] top3Rating = sort_rooms[2][1] img3 = db.getImgfromRmID(conn, top3) conn.close() return render_template('base.html', randomRoom=randomRoom[0], top1=top1, top1Rating=top1Rating, img1=img1, top2=top2, top2Rating=top2Rating, img2=img2, top3=top3, top3Rating=top3Rating, img3=img3, three=three, two=two, one=one) elif len(allRooms) == 2: two = True three = False one = False top1 = sort_rooms[0][0] top1Rating = sort_rooms[0][1] img1 = db.getImgfromRmID(conn, top1) top2 = sort_rooms[1][0] top2Rating = sort_rooms[1][1] img2 = db.getImgfromRmID(conn, top2) print(img2) conn.close() return render_template('base.html', randomRoom=randomRoom[0], top1=top1, top1Rating=top1Rating, img1=img1, top2=top2, top2Rating=top2Rating, img2=img2, two=two, one=one, three=three) elif len(allRooms) == 1: one = True two = False three = False top1 = sort_rooms[0][0] top1Rating = sort_rooms[0][1] img1 = db.getImgfromRmID(conn, top1) conn.close() return render_template('base.html', randomRoom=randomRoom[0], top1=top1, top1Rating=top1Rating, img1=img1, one=one, two=two, three=three) else: one = False two = False three = False return render_template('base.html', one=one, two=two, three=three) except Exception as err: flash('login error ' + str(err)) return redirect(request.referrer)
def roomReview(rmID): try: if 'CAS_USERNAME' in session: conn = db.getConn(DB) result = db.getRoomInfo(conn, rmID) building = '' #print("RMID first three letters") #print(rmID[0:3]) if rmID[0:3] == 'MCA': building = 'McAfee' elif rmID[0:3] == 'BEB': building = 'Beebe' elif rmID[0:3] == 'BAT': building = 'Bates' elif rmID[0:3] == 'CAS': building = 'Casa Cervantes' elif rmID[0:3] == 'CAZ': building = 'Cazenove' elif rmID[0:3] == 'CLA': building = 'Claflin' elif rmID[0:3] == 'DAV': building = 'Stone-DAVIS' elif rmID[0:3] == 'DOW': building = 'Dower' elif rmID[0:3] == 'FRE': building = 'Freeman' elif rmID[0:3] == 'FRH': building = 'French House' elif rmID[0:3] == 'HEM': building = 'Hemlock Apartments' elif rmID[0:3] == 'INS': building = 'Instead' elif rmID[0:3] == 'LAK': building = 'Lakehouse' elif rmID[0:3] == 'MUN': building = 'Munger' elif rmID[0:3] == 'ORC': building = 'Orchid Apartments' elif rmID[0:3] == 'POM': building = 'Pomeroy' elif rmID[0:3] == 'SEV': building = 'Severance' elif rmID[0:3] == 'SHA': building = 'Shafer' elif rmID[0:3] == 'STO': building = 'STONE-davis' elif rmID[0:3] == 'TCE': building = 'Tower Court' elif rmID[0:3] == 'TCW': building = 'Tower Court' if (building == 'Tower Court' or building == 'Lakehouse' or building == 'Severance' or building == 'Claflin'): diningHall = 'Lulu/Tower' elif (building == 'Beebe' or building == 'Munger' or building == 'Shafer' or building == 'Pomeroy' or building == 'Cazenove'): diningHall = 'Pomeroy/Lulu' else: diningHall = 'Bates/Stone-Davis' username = session['CAS_USERNAME'] uid = db.getUid(conn, username) saved = db.save_trueFalse(conn, rmID, uid) r = db.getAverageRating(conn, rmID) conn.close() return render_template('review.html', rmID=rmID, reviews=result, avg=r, username=username, building=building, diningHall=diningHall, saved=saved) else: conn.close() return redirect(url_for('index')) except Exception as err: flash(repr(err)) return (redirect(request.referrer))