def sendMessage(userID): #user userID to get both friendIDs to submit messages #Get both friendIDs associated with friendship cur, db = Tlbx.dbConnectDict() query = ("SELECT friendID from tFriend \ WHERE tFriend.user = %s \ AND tFriend.friend = %s;") data = (current_user.id, userID) cur.execute(query, data) UserFriendID = cur.fetchone() data = (userID, current_user.id) cur.execute(query, data) FriendFriendID = cur.fetchone() session = Database.Session() message = request.form['message'] addMessageFriend = Database.tMessage(friendID = FriendFriendID['friendID'], sender = current_user.id, \ recipient = userID, time_Sent = datetime.datetime.now(), message = message) addMessageUser = Database.tMessage(friendID = UserFriendID['friendID'], sender = current_user.id, \ recipient = userID, time_Sent = datetime.datetime.now(), message = message) session.add(addMessageUser) session.add(addMessageFriend) session.commit() return redirect('/Messages')
def AddFriend(userID): session = Database.Session() addFriend = Database.tFriend(friend=current_user.id, user=userID) addUser = Database.tFriend(user=current_user.id, friend=userID) session.add(addFriend) session.add(addUser) session.commit() return ('', 204)
def SearchUsers(): db = Database.Session() Name = session.get('Name') Users = db.query(Database.tUser) \ .filter(Database.tUser.firstName.contains(Name) | (Database.tUser.firstName.op('SOUNDS LIKE')(Name)) \ | (Database.tUser.lastName.contains(Name) | (Database.tUser.lastName.op('SOUNDS LIKE')(Name)))) \ .order_by(Database.Match([Database.tUser.email, Database.tUser.firstName, Database.tUser.lastName], Name)) \ .all() return render_template('/Search/SearchUsers.html', Users=Users)
def commitComment(userID, Comment): session = Database.Session() Comment = Database.tComments(postID=Comment['postID'], userID=userID, Comment=Comment['Comment'], ts=datetime.datetime.now(), image=None) session.add(Comment) session.commit()
def commitAvailability(dogID, userID, Begin_ts, End_ts, message): session = Database.Session() Availability = Database.tAvailability(dogID=dogID, userID=userID, Begin_ts=Begin_ts, End_ts=End_ts, message=message) session.add(Availability) session.commit()
def Comment(postID): postID = str(postID) Comment = request.form['Comment'] session = Database.Session() Comment = Database.tComments(postID=postID, userID=current_user.id, Comment=Comment, ts=datetime.datetime.now(), image=None) session.add(Comment) session.commit() return redirect('/View/Post/' + postID)
def JoinFamily(userID): session = Database.Session() familyID = session.query( Database.tUser.familyID).filter(Database.tUser.userID == userID) for family in familyID: familyID = family.familyID x = session.query(Database.tUser).get(current_user.id) x.familyID = familyID session.commit() dogIDs = session.query( Database.tDog.dogID).filter(Database.tDog.familyID == familyID) for dogID in dogIDs: dog = Database.tFollowers(dogID=dogID.dogID, userID=current_user.id) session.add(dog) session.commit() return redirect('/dashboard')
def index(): '''Do a normal select for comments and reacts. Then in jinja during the for loop for posts do a for loop for reacts and comments. Do an if post.postID == comment.postID and if post.postiD == react.postID''' session = Database.Session() currentUser = Tlbx.currentUserInfo(current_user.id) #Collect dogs in your family dogResults = session.query(Database.tDog).join(Database.tUser, Database.tDog.familyID == Database.tUser.familyID).filter(Database.tUser.userID == current_user.id) #Get comments of posts commentResults = Comments.getComments() #Get reacts of posts reactResults = Reacts.getReacts() yourReacts = Reacts.yourReacts() #Get Posts postResults = Posts.getPosts() likes={} like =[] for react in postResults: likes[react.postID] = 'Like' for yourReact in yourReacts: if yourReact.postID in likes.keys(): likes[yourReact.postID] = 'Unlike' for react in postResults: like.append(likes[react.postID]) return render_template('HomePage/Dashboard.html', currentUser = currentUser, dogResults = dogResults, \ postResults = zip(postResults, like), commentResults = commentResults, reactResults = reactResults)
def JoinFamilySearch(): session = Database.Session() Name = request.form['Search'] Users = session.query(Database.tUser, Database.tFamily.familyName).join(Database.tHeadofHouse, Database.tHeadofHouse.userID == Database.tUser.userID) \ .join(Database.tFamily, Database.tFamily.familyID == Database.tUser.userID) \ .filter(Database.tFamily.familyName.contains(Name) | (Database.tUser.email.contains(Name)) \ | (Database.tUser.email.op('SOUNDS LIKE')(Name))) \ .order_by(Database.tFamily.familyName.contains(Name).desc()).all() return render_template('/Family/JoinFamilySearch.html', Users=Users)
def Availability(dogID): dogID = str(dogID) session = Database.Session() dog = session.query(Database.tDog).filter(Database.tDog.dogID == dogID) for dog in dog: dog = dog user = session.query( Database.tUser).filter(Database.tUser.userID == current_user.id) for user in user: user = user return render_template('/Schedule/Availability.html', dog=dog, user=user)
def commitPlayDate(hostDogID, guestDogID, creatorID, availabilityID, Begin_ts, End_ts, message): session = Database.Session() PlayDate = Database.tPlayDate(hostDogID=hostDogID, guestDogID=guestDogID, creatorID=creatorID, AvailabilityID=availabilityID, Begin_ts=Begin_ts, End_ts=End_ts, addressID=None) session.add(PlayDate) session.commit() Playdate = Database.tPosts(dogID=hostDogID, userID=creatorID, PlayDateID=PlayDate.PlayDateID, Post=None, ts=datetime.now(), image=None) session.add(Playdate) session.commit()
def UnFriend(userID): session = Database.Session() session.query(Database.tFriend) \ .filter(Database.tFriend.user == userID) \ .filter(Database.tFriend.friend == current_user.id) \ .delete() session.query(Database.tFriend) \ .filter(Database.tFriend.user == current_user.id) \ .filter(Database.tFriend.friend == userID) \ .delete() session.commit() return ('', 204)
def Search(): db = Database.Session() Name = request.form['Search'] session['Name'] = Name #Get top 5 most relevant dogs dogs = db.query(Database.tDog).filter( Database.tDog.name.contains(Name) | (Database.tDog.name.op('SOUNDS LIKE')(Name))).order_by( Database.tDog.name.match(Name).desc()).limit(5).all() followed = db.query(Database.tFollowers).filter( Database.tFollowers.userID == current_user.id) texts = {} text = [] for dog in dogs: texts[dog.dogID] = 'Follow' for follower in followed: if follower.dogID in texts.keys(): texts[follower.dogID] = 'Unfollow' for dog in dogs: text.append(texts[dog.dogID]) #Get top 5 most relevant users Users = db.query(Database.tUser) \ .filter(Database.tUser.firstName.contains(Name) | (Database.tUser.firstName.op('SOUNDS LIKE')(Name)) \ | (Database.tUser.lastName.contains(Name) | (Database.tUser.lastName.op('SOUNDS LIKE')(Name)))) \ .filter(Database.tUser.userID != current_user.id) \ .order_by(Database.Match([Database.tUser.firstName, Database.tUser.lastName], Name)) \ .limit(5).all() friended = db.query( Database.tFriend).filter(Database.tFriend.user == current_user.id) friends = {} for user in Users: friends[user.userID] = 'Friend' for friend in friended: if friend.friend in friends.keys(): friends[friend.friend] = 'Unfriend' return render_template('/Search/Search.html', results=zip(dogs, text), users=Users, friends=friends)
def dogProfile(dogID): session = Database.Session() #Get information about the dog dog = session.query(Database.tDog, Database.tBreed).filter(Database.tDog.dogID == dogID) \ .join(Database.tBreed, Database.tBreed.breedID == Database.tDog.dogID) for dog in dog: dog = dog #Get comments of posts commentResults = Comments.getComments() #Get reacts of posts reactResults = Reacts.getReacts() yourReacts = Reacts.yourReacts() #Get Posts postResults = Posts.getPosts() likes = {} like = [] for react in postResults: likes[react.postID] = 'Like' for yourReact in yourReacts: if yourReact.postID in likes.keys(): likes[yourReact.postID] = 'Unlike' for react in postResults: like.append(likes[react.postID]) cur, db = Tlbx.dbConnectDict() query = "select tFamily.familyID from tFamily \ JOIN tUser on tFamily.familyID = tUser.familyID \ JOIN tDog on tUser.familyID = tDog.familyID \ WHERE tUser.userID = %s AND tDog.dogID = %s;" data = (current_user.id, dogID) cur.execute(query, data) family = cur.fetchone() if (family is not None): return render_template('Account/AdminDogProfile.html', postResults=zip(postResults, like), commentResults=commentResults, reactResults=reactResults, yourReacts=yourReacts, dog=dog) else: return render_template('Account/DogProfile.html', postResults=zip(postResults, like), commentResults=commentResults, reactResults=reactResults, yourReacts=yourReacts, dog=dog)
def familyCreation(): sqlalch = Database.Session() HeadOfHouse = Database.tHeadofHouse(userID=current_user.id) sqlalch.add(HeadOfHouse) id = sqlalch.commit() CreateFamilyform = CreateFamilyForm() if CreateFamilyform.validate_on_submit(): CreateDogform = CreateDogForm() familyName = CreateFamilyform.surName.data cur, db = Tlbx.dbConnectDict() cursor = Database.Session() query = ( "INSERT INTO tFamily (familyName, headofHouseID) VALUES(%s, %s);") data = (familyName, id) cur.execute(query, data) session['familyID'] = db.insert_id() x = cursor.query(Database.tUser).get(current_user.id) x.familyID = db.insert_id() cursor.commit() return render_template('Dog/NewDog.html', CreateDogform=CreateDogform) flash("Please add a family name") return render_template('/Family/FamilyCreate.html', CreateFamilyform=CreateFamilyform)
def dogInfo(dogID): session = Database.Session() user = session.query( Database.tUser).filter(Database.tUser.userID == current_user.id) for user in user: user = user #Collect dogs in your family dog = session.query(Database.tDog, Database.tBreed, Database.tFavoriteToy).filter(Database.tDog.dogID == dogID) \ .join(Database.tBreed, Database.tBreed.breedID == Database.tDog.dogID) \ .join(Database.tFavoriteToy, Database.tDog.favToyID == Database.tFavoriteToy.favToyID) for dog in dog: dog = dog return render_template('Account/DogInfo.html', dog=dog)
def CreateNewDog(): session = Database.Session() headOfHouse = session.query(Database.tHeadofHouse).filter( Database.tHeadofHouse.userID == current_user.id).all() if headOfHouse == []: flash("You must be the head of household to create a new dog") return redirect('/dashboard') followed = session.query(Database.tDog.dogID).join(Database.tUser, Database.tDog.familyID == Database.tUser.familyID) \ .filter(Database.tUser.userID == current_user.id) CreateDogform = CreateDogForm() if followed is not None: return render_template('Dog/NewDog.html', CreateDogform=CreateDogform) else: return render_template('Dog/Initial_NewDog.html', CreateDogForm=CreateDogform)
def Playedate(dogID): dogID = str(dogID) session = Database.Session() dog = session.query(Database.tDog).filter(Database.tDog.dogID == dogID) for dog in dog: dog = dog user = session.query( Database.tUser).filter(Database.tUser.userID == current_user.id) for user in user: user = user dogResults = session.query(Database.tDog).join( Database.tUser, Database.tDog.familyID == Database.tUser.familyID).filter( Database.tUser.userID == current_user.id) return render_template('/Playdate/Playdate.html', dog=dog, user=user, dogResults=dogResults)
def deletePlayDate(PlayDateID): session = Database.Session() '''To prevent FK exceptions, we need to delete Reacts, Comments, and Posts associated with the playdate.''' pIDs = session.query(Database.tPosts.postID).filter( Database.tPosts.PlayDateID == PlayDateID) for pID in pIDs: session.query(Database.tReacts).filter( Database.tReacts.postID == pID.postID).delete() session.commit() session.query(Database.tComments).filter( Database.tComments.postID == pID.postID).delete() session.commit() session.query(Database.tPosts).filter( Database.tPosts.PlayDateID == PlayDateID).delete() session.commit() session.query(Database.tPlayDate).filter( Database.tPlayDate.PlayDateID == PlayDateID).delete() session.commit()
def SearchDogs(): db = Database.Session() Name = session.get('Name') #Get top 5 most relevant dogs and whether or not they are being followed. dogs = db.query(Database.tDog).filter( Database.tDog.name.contains(Name) | (Database.tDog.name.op('SOUNDS LIKE')(Name))).order_by( Database.tDog.name.match(Name).desc()).all() followed = db.query(Database.tFollowers).filter( Database.tFollowers.userID == current_user.id) texts = {} text = [] for dog in dogs: texts[dog.dogID] = 'Follow' for follower in followed: if follower.dogID in texts.keys(): texts[follower.dogID] = 'Unfollow' for dog in dogs: text.append(texts[dog.dogID]) return render_template('/Search/SearchDogs.html', results=zip(dogs, text))
def StartPark(): CreateDogform = CreateDogForm() dbsession = Database.Session() if CreateDogform.validate_on_submit(): session['dogName'] = CreateDogform.dogName.data session['gender'] = CreateDogform.gender.data session['breed'] = CreateDogform.breed.data session['fixed'] = CreateDogform.fixed.data session['age'] = CreateDogform.age.data session['favToy'] = CreateDogform.favToy.data session['size'] = CreateDogform.size.data session['weight'] = CreateDogform.weight.data session['bio'] = CreateDogform.bio.data image = request.files[CreateDogform.profileImage.name] if (image.filename.encode('ascii', 'ignore') is not ""): image = request.files[CreateDogform.profileImage.name] session['image'] = Tlbx.imgToJPG("Profile", image) else: session['image'] = None FavoriteParkform = FavoriteParkForm() followed = dbsession.query(Database.tDog.dogID).join(Database.tUser, Database.tDog.familyID == Database.tUser.familyID) \ .filter(Database.tUser.userID == current_user.id) if followed is not None: return render_template('/Dog/NewPark.html', FavoriteParkform=FavoriteParkform) else: return render_template('Dog/Initial_NewPark.html', FavoriteParkform=FavoriteParkform) return render_template('/Dog/NewPark.html', FavoriteParkform=FavoriteParkform) flash("Please fill out all fields") if followed is not None: return render_template('Dog/NewDog.html', CreateDogform=CreateDogform) else: return render_template('/Dog/Initial_NewDog.html', CreateDogForm=CreateDogForm)
def userProfile(userID): session = Database.Session() user = session.query( Database.tUser).filter(Database.tUser.userID == userID) for user in user: user = user #Get comments of posts commentResults = Comments.getComments() #Get reacts of posts reactResults = Reacts.getReacts() yourReacts = Reacts.yourReacts() #Get Posts postResults = Posts.getPosts() likes = {} like = [] for react in postResults: likes[react.postID] = 'Like' for yourReact in yourReacts: if yourReact.postID in likes.keys(): likes[yourReact.postID] = 'Unlike' for react in postResults: like.append(likes[react.postID]) #Collect dogs in your family dogResults = session.query(Database.tDog).join( Database.tUser, Database.tDog.familyID == Database.tUser.familyID).filter( Database.tUser.userID == userID) return render_template('Account/Profile.html', user=user, dogResults=dogResults, postResults=zip(postResults, like), commentResults=commentResults, reactResults=reactResults)
def FollowDogs(dogID): session = Database.Session() addFollower = Database.tFollowers(dogID = dogID, userID= current_user.id) session.add(addFollower) session.commit() return '', 204
def UnfollowDogs(dogID): session = Database.Session() session.query(Database.tFollowers).filter(Database.tFollowers.dogID==dogID) \ .filter(Database.tFollowers.userID==current_user.id).delete() session.commit() return '', 204
def deleteAvailability(availabilityID): session = Database.Session() session.query(Database.tAvailability).filter( Database.tAvailability.AvailabilityID == availabilityID).delete() session.commit()
def getComments(): session = Database.Session() commentResults = session.query(Database.tPosts.postID, Database.tComments.Comment, Database.tUser.userID, Database.tUser.firstName, Database.tUser.lastName, Database.tUser.image) \ .join(Database.tUser, Database.tComments.userID == Database.tUser.userID)\ .join(Database.tPosts, Database.tComments.postID == Database.tPosts.postID).order_by(Database.tComments.commentID.desc()) return commentResults
def updateAvailability(availabilityID, Begin_ts, End_ts): session = Database.Session() session.query(Database.tAvailability).filter(Database.tAvailability.AvailabilityID == availabilityID) \ .update({Database.tAvailability.Begin_ts: Begin_ts, Database.tAvailability.End_ts: End_ts}) session.commit()
def DogCreation(): FavoriteParkform = FavoriteParkForm() if FavoriteParkform.validate_on_submit(): #insert Breed cur, db = Tlbx.dbConnectDict() breedQuery = "INSERT INTO tBreed (breed) VALUES (%s);" data = (session.get('breed')) cur.execute(breedQuery, data) db.commit() breedID = cur.lastrowid #Insert Address then insert Favorite Park image = None addressQuery = "INSERT INTO tAddress (address, city, state) VALUES (%s, %s, %s);" try: address = request.form['desktopstreet_address'] city = request.form['desktopadministrative_area_level_1'] state = request.form['desktoplocality'] except: address = request.form['mobilestreet_address'] city = request.form['mobileadministrative_area_level_1'] state = request.form['mobilelocality'] data = (address, city, state) cur.execute(addressQuery, data) favParkQuery = "INSERT INTO tFavoritePark (parkName, AddressID, image) VALUES (%s, LAST_INSERT_ID(), %s);" data = (FavoriteParkform.parkName.data, image) cur.execute(favParkQuery, data) db.commit() favParkID = cur.lastrowid #Insert favorite toy. image = None favToyQuery = "INSERT INTO tFavoriteToy (ToyName, image) VALUES (%s, %s);" data = (session['favToy'], image) cur.execute(favToyQuery, data) db.commit() favToyID = cur.lastrowid cursor = Database.Session() familyID = session.get('familyID') if familyID is None: familyID = cursor.query(Database.tUser.familyID).filter( Database.tUser.userID == current_user.id) for familyID in familyID: familyID = familyID.familyID # commit all the dog data into the database. dogQuery = "Insert into tDog (name, gender, breedID, fixed, age, Size, Weight, bio, image, favToyID, favParkID, familyID) VALUES (%s, %s, %s, %s,%s, %s, %s, %s,%s, %s, %s, %s)" data = (session.get('dogName'), session.get('gender'), breedID, session.get('fixed'), session.get('age'), session.get('size'), session.get('weight'), session.get('bio'), session.get('image'), favToyID, favParkID, familyID) cur.execute(dogQuery, data) db.commit() #Follow the dog sqlalch = Database.Session() Query = Database.tFollowers(dogID=cur.lastrowid, userID=current_user.id) sqlalch.add(Query) sqlalch.commit() return redirect('/dashboard') flash("please input your dogs favorite park") return render_template('/Dog/NewPark.html', FavoriteParkform=FavoriteParkform) #finally commit all the dog data into the database. dogQuery = "Insert into tDog (name, gender, breedID, fixed, age, Size, Weight, bio, image, favToyID, favParkID, familyID) VALUES (%s, %s, %s, %s,%s, %s, %s, %s,%s, %s, %s, %s)" data = (session.get('dogName'), session.get('gender'), breedID, session.get('fixed'), session.get('age'), session.get('size'), session.get('weight'), session.get('bio'), image, favToyID, favParkID, session.get('familyID')) cur.execute(dogQuery, data) db.commit() #Default to follow the dog dogID = cur.lastrowid FollowDogs(dogID) #Redirect to the dashboard return redirect('/dashboard')