def new(following_id): follower = User.get_by_id(current_user.id) following = User.get_by_id(following_id) if following.is_public: r = Relationship(follower=follower, following=following, is_approved=True) else: r = Relationship(follower=follower, following=following, is_approved=False) if r.save(): if following.is_public: flash(f"You are now following {following.name}.") return redirect(url_for('users.show', username=following.username)) else: flash(f"A follow request has been sent to {following.name}.") rq_job = app.task_queue.enqueue( 'tasks.' + 'send_follow_request_email', following, follower, url_for('relationships.index')) task = Task(redis_job_id=rq_job.get_id(), name='send_follow_request_email', description='Send user a follow request email.', relationship=r) task.save() return redirect(url_for('users.show', username=following.username)) else: return render_template( 'users/show.html', user=following, photos=Image.select().where(Image.user_id == following.id), errors=r.errors)
def follow_user(): follower_username = request.form['follower_username'] idol_username = request.form['idol_username'] # Get ID and add to DB follower_id = User.get_or_none(User.username == follower_username) idol_id = User.get_or_none(User.username == idol_username) # If user profile = Public, then add follower and approve if idol_id.private == False: add_follower = Relationship(follower_id=follower_id, idol_id=idol_id, approve=True) flash(f'You have followed {idol_username}') # If user profile = Private, then add follower but don't approve elif idol_id.private == True: add_follower = Relationship(follower_id=follower_id, idol_id=idol_id, approve=False) flash(f'A friend request has been sent to {idol_username}') add_follower.save() return render_template('home.html')
def follow(self, target_user): from models.relationship import Relationship if target_user.private: new_r = Relationship(following_id=self.id, followed_id=target_user.id, pending=True) else: new_r = Relationship(following_id=self.id, followed_id=target_user.id, pending=False) return new_r.save()
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 create(id): idol = User.get_by_id(id) if idol.private == True: relationship = Relationship(fan=current_user.id, idol=idol.id, approved=False) if relationship.save(): flash( f"Request is sent to {idol.username}. Please wait for approval.", "primary") return redirect(url_for('users.show', username=idol.username)) else: relationship = Relationship(fan=current_user.id, idol=idol.id, approved=True) if relationship.save(): flash(f"Successfully followed {idol.username}.", "primary") return redirect(url_for('users.show', username=idol.username))
def follow(id): user = User.select().where(User.id == id).get() fan_id = current_user.id idol_id = request.form.get('idol_id') idol = User.get_by_id(idol_id) follower = Relationship(fan=fan_id, idol=idol_id) follower.save() flash("Successfully followed", 'success') return render_template('users/profile.html', user=user)
def block(to_username): ref = request.referrer logged_user = User.objects.filter(username=session.get('username')).first() fetchNotifications(logged_user) toUser = User.objects.filter(username=to_username).first() if toUser: rel = Relationship.get_relationship(logged_user, toUser) if rel == "FRIENDS_PENDING" or rel == "FRIENDS_APPROVED" or rel == "REVERSE_FRIENDS_PENDING": rel = Relationship.objects.filter(fromUser=logged_user, toUser=toUser).delete() reverse_rel = Relationship.objects.filter( fromUser=toUser, toUser=logged_user).delete() Relationship(fromUser=logged_user, toUser=toUser, rel_type=Relationship.BLOCKED, status=Relationship.APPROVED).save() if ref: return redirect(ref) else: return redirect( url_for('user_app.profile', username=toUser.username)) else: abort(404)
def follow(idol_id): if current_user.is_authenticated: fan = User.get_or_none(User.id == current_user.id) idol = User.get_or_none(User.id == idol_id) if fan and idol: relationship = Relationship(fan=fan, idol=idol) if not relationship.is_exist(): if idol.private: # send_email(f"{fan.username} has followed you on nextagram! Approve follow request to allow profile view.") pass else: relationship.status = "approve" if relationship.save(): flash('Follow profile successful', 'alert alert-success') return redirect( url_for('users.show', id_or_username=idol.username)) else: flash('Profile has been followed', 'alert alert-danger') else: flash('Follow profile failed', 'alert alert-danger') return redirect(url_for('users.show', id_or_username=idol.username)) else: flash('Log in required', 'alert alert-danger') return redirect(url_for('home'))
def add_friend(to_username): ref = request.referrer logged_user = User.objects.filter(username=session.get('username')).first() fetchNotifications(logged_user) toUser = User.objects.filter(username=to_username).first() if toUser: rel = Relationship.get_relationship(logged_user, toUser) to_username = toUser.username if rel == "REVERSE_FRIENDS_PENDING": Relationship(fromUser=logged_user, toUser=toUser, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED).save() reverse_rel = Relationship.objects.get(fromUser=toUser, toUser=logged_user) reverse_rel.status = Relationship.APPROVED reverse_rel.save() notification = Notification.objects.filter( fromUser=toUser.username, toUser=logged_user.username).first() if notification != None: nlist = session.get('notifications') nlist.remove(notification) session['notifications'] = nlist notification.delete() elif rel == None and rel != "REVERSE_BLOCKED": Relationship(fromUser=logged_user, toUser=toUser, rel_type=Relationship.FRIENDS, status=Relationship.PENDING).save() body_html = render_template('mail/relationship/added_friend.html', fromUser=logged_user, toUser=toUser, host=WEBSITE_ADDRESS) body_text = render_template('mail/relationship/added_friend.txt', fromUser=logged_user, toUser=toUser, host=WEBSITE_ADDRESS) email(toUser.email, ("%s has requested to be friends") % logged_user.first_name, body_html, body_text) notification = Notification(fromUser=logged_user.username, toUser=toUser.username, notificationType="request") notification.save() if ref: return redirect(ref) else: return redirect( url_for('user_app.profile', username=toUser.username)) else: abort(404)