def handle_request(): follower_id = request.form['follower_id'] idol_id = request.form['idol_id'] follower = User.get_by_id(follower_id) request_type = request.form['request_type'] # Change privacy setting if request_type == 'accept_request': query = Relationship.get_or_none( Relationship.follower_id == follower_id, Relationship.idol_id == idol_id) approve_request = Relationship.update(approve=True).where( Relationship.id == query) approve_request.execute() flash(f"You have approved {follower.username}'s request.") return render_template('home.html') elif request_type == 'cancel_request': breakpoint() result = Relationship.get_or_none( Relationship.follower_id == follower_id, Relationship.idol_id == idol_id) delete_request = result.delete_instance() flash(f"You have denied {follower.username}'s request. ") return render_template('home.html')
def approval(self, target_user, approval): from models.relationship import Relationship if approval == 'approve': r = Relationship.get_or_none(followed=self, following=target_user) r.pending = False r.save() return True else: r = Relationship.get_or_none(followed=self, following=target_user) r.delete_instance() return False
def show(username): user = User.get(User.username == username) relationship = Relationship.get_or_none( Relationship.idol == user.id, Relationship.fan == current_user.id) return render_template('users/show.html', user=user, relationship=relationship)
def index(): if current_user.is_authenticated: # Get profiles of other users # Method 1: With N+1 issue # profiles = [] # users = User.select().where(User.id!=current_user.id).order_by(User.id.asc()) # for user in users: # images = user.images # relationship = Relationship.get_or_none(Relationship.fan_id==current_user.id, Relationship.idol_id==user.id) # profiles.append({ # "user": user, # "images": images, # "relationship": relationship # }) # return render_template('users/index.html', users=users, profiles=profiles) # Method 2: Without N+1 issue profiles_wo_np1 = [] users_wo_np1 = User.select().where(User.id!=current_user.id).order_by(User.id.asc()).prefetch(Image) # same effect as Image.select().where(Image.user<<users) for user in users_wo_np1: relationship = Relationship.get_or_none(Relationship.fan_id==current_user.id, Relationship.idol_id==user.id) profiles_wo_np1.append({ "user": user, "images": user.images, "relationship": relationship }) return render_template('users/index.html', users=users_wo_np1, profiles=profiles_wo_np1) else: return redirect(url_for('sessions.new'))
def show_profile(id_or_username, is_profile): # Get user by id or username if id_or_username.isdigit(): user = User.get_or_none(User.id==id_or_username) else: user = User.get_or_none(User.username==id_or_username) # Render page if user exist if user: # Get list of images images = user.images # Get list of fans approve_fans = User.select().join(Relationship, on=(Relationship.fan_id==User.id)).where(Relationship.idol_id==user.id, Relationship.status=="approve") pending_fans = User.select().join(Relationship, on=(Relationship.fan_id==User.id)).where(Relationship.idol_id==user.id, Relationship.status=="pending") # Get list of idols approve_idols = User.select().join(Relationship, on=(Relationship.idol_id==User.id)).where(Relationship.fan_id==user.id, Relationship.status=="approve") pending_idols = User.select().join(Relationship, on=(Relationship.idol_id==User.id)).where(Relationship.fan_id==user.id, Relationship.status=="pending") # Check has follow if current_user.is_authenticated: relationship = Relationship.get_or_none(Relationship.fan_id==current_user.id, Relationship.idol_id==user.id) # Get count of images, fans and idols total = { "images": len(images), "fans": len(approve_fans), "idols": len(approve_idols) } return render_template('users/show.html', user=user, images=images, relationship=relationship, total=total) else: flash('User not exist', 'alert alert-danger') return redirect(url_for('home'))
def create(): fan_id = current_user.id idol_id = request.form.get('idol_id') idol = User.get_by_id(idol_id) #Check if fan has previously requested to follow this idol. follower = Relationship.get_or_none(Relationship.idol == idol_id, Relationship.fan == fan_id) #if they have, flash a message letting them know they have submitteed a request and redirect them to user proifle. Otherwise create a database entry. if follower: flash( f" You've already submitted a follow request to {idol.username} ") return redirect(url_for('users.show', username=idol.username)) else: follower = Relationship(fan=fan_id, idol=idol_id) if follower.save(): flash(f"You have submitted a request to follow {idol.username}") status = approval_email(fan=current_user, idol=idol) print(f'Email request sent:{status}') return redirect(url_for('users.show', username=idol.username)) else: flash(f"Something went wrong")
def show(username): user = User.get(User.username == username) if not current_user.is_authenticated: followed = None else: followed = Relationship.get_or_none( Relationship.fan == current_user.id, Relationship.idol == user.id) return render_template('users/show.html', user=user, followed=followed)
def toggle_follow(id): online_user = User.get_or_none(User.id == current_user.id) target_user = User.get_or_none(User.id == id) search_r = Relationship.get_or_none(followed=target_user, following=online_user) if search_r: online_user.unfollow(target_user) else: online_user.follow(target_user) return redirect(url_for('users.show', username=target_user.username))
def block(fan_id): if current_user.is_authenticated: fan = User.get_or_none(User.id == fan_id) idol = User.get_or_none(User.id == current_user.id) if fan and idol: relationship = Relationship.get_or_none(Relationship.fan == fan, Relationship.idol == idol) if relationship: if relationship.delete_instance(): flash('Block profile successful', 'alert alert-success') else: flash('Block profile failed', 'alert alert-danger') else: flash('Profile has not been followed', 'alert alert-danger') else: flash('Block profile failed', 'alert alert-danger') return redirect(url_for('relationship.show_fan')) else: flash('Log in required', 'alert alert-danger') return redirect(url_for('home'))
def approve(fan_id): if current_user.is_authenticated: fan = User.get_or_none(User.id == fan_id) idol = User.get_or_none(User.id == current_user.id) if fan and idol: relationship = Relationship.get_or_none(Relationship.fan == fan, Relationship.idol == idol) if relationship and relationship.status == "pending": if Relationship.update(status="approve").where( Relationship.id == relationship.id).execute(): flash('Approve follower successful', 'alert alert-success') else: flash('Approve follower failed', 'alert alert-danger') else: flash('Profile has not been followed', 'alert alert-danger') else: flash('Unfollow profile failed', 'alert alert-danger') return redirect(url_for('relationship.show_fan')) else: flash('Log in required', 'alert alert-danger') return redirect(url_for('home'))
def unfollow_user(): follower_username = request.form['follower_username'] idol_username = request.form['idol_username'] request_type = request.form['request_type'] idol_id = User.get(User.username == idol_username) result = Relationship.get_or_none( current_user.id == Relationship.follower_id, Relationship.idol_id == idol_id.id) # Delete record where Relationship.id = result query = Relationship.get_by_id(result) query.delete_instance() if request_type == 'cancel_request': flash(f'You have cancelled your request to follow {idol_username}') elif request_type == 'unfollower_user': flash(f'You have unfollowed {idol_username}') return render_template('home.html')
def follow_status(self, target_user): from models.relationship import Relationship search_r = Relationship.get_or_none(followed=target_user, following=self) return search_r
def unfollow(self, target_user): from models.relationship import Relationship r = Relationship.get_or_none(followed=target_user, following=self) return r.delete_instance()
def show(username): # Object of profile page user user = User.get_or_none(User.username == username) # Validation to toggle buttons follower_check = Relationship.get_or_none( current_user.id == Relationship.follower_id, user.id == Relationship.idol_id) user_authenticated = current_user.is_authenticated # Toggle "Follow" or "Unfollow" and "Request Pending" if follower_check: toggle_unfollow = True if follower_check.approve == False: toggle_request_btn = True else: toggle_request_btn = False else: toggle_unfollow = False toggle_request_btn = False # Get relationship objects - gets only one instance relationship_following = Relationship.get_or_none( Relationship.follower_id == user.id) #To see who I am following, look in the follower_id column relationship_followers = Relationship.get_or_none( Relationship.idol_id == user.id) # Get count of who this user is following if relationship_following: following = relationship_following.count_idols() else: following = 0 if relationship_followers: followers = relationship_followers.count_fans() else: followers = 0 # Private vs. Public Profile relationship_obj = Relationship.get_or_none( Relationship.follower_id == current_user.id, Relationship.idol_id == user.id) if relationship_obj and relationship_obj.approve == True: toggle_view = True # Display full page elif relationship_obj and relationship_obj.approve == False: # Not approved yet - display private profile toggle_view = False elif relationship_obj == None and user.private == False: #Not following, but public profile toggle_view = True print('Not following and public page') elif relationship_obj == None and user.private == True: #Not following and private page toggle_view = False print('Not following and private page') else: toggle_view = False # Get list of who current user is following list_following = User.select().join( Relationship, on=Relationship.idol_id).where(Relationship.follower_id == user.id, Relationship.approve == True) list_follower = User.select().join( Relationship, on=Relationship.follower_id).where(Relationship.idol_id == user.id) return render_template('users/profile.html', user=user, followers=followers, following=following, toggle_unfollow=toggle_unfollow, user_authenticated=user_authenticated, toggle_request_btn=toggle_request_btn, toggle_view=toggle_view, list_following=list_following, list_follower=list_follower)