def add_friends(): # log usage ulog('add_friends -> page access') users = User.query.all() users = filter(lambda u: u.user_id != current_user.user_id, users) # update friends if form submitted if request.method == 'POST': friend_ids = request.form.getlist('friend_checkbox') for friend_id in friend_ids: friend = User.query.get(int(friend_id)) ulog('add_friends -> user {} added friend {}'.format( current_user, friend)) print('adding friend ', friend_id) current_user.add_friend(friend) # TODO: allow unfriending people? db.session.commit() # redirect to list return redirect(url_for('list')) return render_template( 'add_friends.html', users=users, current_friends=current_user.friends if current_user.friends else [])
def pending_requests(username): if request.method == 'POST': user = Users.query.filter_by(id=request.form.get('accept')).first() if request.form.get('accept') \ else Users.query.filter_by(id=request.form.get('deny')).first() if user is not None and user in current_user.pending_friend_requests: if request.form.get('accept'): flash( f'On your way to accepting friend request from {user.username}!' ) current_user.add_friend(user) current_user.pending_friend_requests.remove(user) db.session.commit() return redirect(url_for('main.index')) elif request.form.get('deny'): flash( f'You are readying to deny a friend request from {user.username}.' ) current_user.pending_friend_requests.remove(user) db.session.commit() return redirect(url_for('main.index')) user = Users.query.filter_by(username=username).first() if user is None: flash(f'Could not find user {username}.') return redirect(url_for('main.index')) if user != current_user: flash('This is not your page!') return redirect(url_for('main.index')) pending_friend_requests = user.pending_friend_requests.all() return render_template('main/pending_requests.html', title='View Your Pending Requests', user=user, requests=pending_friend_requests)
def friends(): form = AddFriend() if form.validate_on_submit(): friend = User.query.filter_by(username=form.username.data).first( ) # find object for given username current_user.add_friend(friend) # add friend in db (from object) db.session.commit() # update db flash('Friend Added!', 'success') return render_template("friendList.html", title="Friend's List", form=form)
def add_to_friends(user_id): user_to_add = User.query.get_or_404(user_id) if current_user.new_requests() == 1: current_user.last_look_at_requests_time = datetime.utcnow() req = RequestToAddToFriends.query.filter_by( sender_id=user_to_add.id, recipient_id=current_user.id).first() current_user.add_friend(user_to_add) db.session.delete(req) db.session.commit() return redirect(url_for('friends', user_id=current_user.id))
def accept_friendship_request(friendship_request_id): friendship_request = Request.query.filter_by( id=int(friendship_request_id)).first() if friendship_request is not None: friendship_request.status = 1 db.session.commit() sender = User.query.filter_by(id=friendship_request.sender).first() if sender is None: return jsonify(response_message='utente non trovato') current_user.add_friend(sender) return jsonify(response_message='richiesta accettata')
def add_friend(id): user = User.query.filter_by(id=id).first() if user is None or current_user == user: flash('申请用户有误!') next_url = request.referrer if next_url is None or not next_url.startswith('/'): next_url = url_for('main.index') return redirect(next_url) elif current_user.request_sent(user) or current_user.is_friend(user): flash('您无需重复申请好友。') return redirect(url_for('main.user', id=user.id)) else: if current_user.is_requested_by(user): form = ConfirmForm() if form.validate_on_submit(): if form.submit_yes.data: current_user.add_friend(user) db.session.commit() attention_body = current_user.nickname + "同意了您的好友请求。" attention_url = url_for('main.webchat', nickname=current_user.nickname) store_attentions([user], attention_body, attention_url) elif form.submit_no.data: r = user.requests.filter_by(to_id=current_user.id).first() db.session.delete(r) db.session.commit() next_url = request.referrer if next_url is None or not next_url.startswith('/'): next_url = url_for('main.index') return redirect(next_url) return render_template('friendrequest.html', user=user, form=form) else: form = RequestForm() form.message = "你好,我是" + current_user.nickname + '。' if form.validate_on_submit(): f = FriendRequest(from_id=current_user.id, to_id=user.id, validation=form.message) db.session.add(f) db.session.commit() attention_body = current_user.nickname + "请求添加您为好友。" attention_url = url_for('main.add_friend', id=current_user.id) store_attentions([user], attention_body, attention_url) next_url = request.referrer if next_url is None or not next_url.startswith('/'): next_url = url_for('main.index') return redirect(next_url) return render_template('friendrequest.html', user=user, form=form)
def agree_friendReqs(): form = {k: request.form[k].strip() for k in request.form} if 'friend_id' not in form: assert 'friend_username' in form if utils.validate_username(form['friend_username']): return Validity( False, 'User ' + form['friend_username'] + ' does not exist.').get_resp() form['friend_id'] = utils.get_userid(form['friend_username']) if not utils.validate_friendreqs(int(form['friend_id']), int(current_user.id)): return Validity(False, 'Request does not exist.').get_resp() friend = User.query.filter_by(id=int(form['friend_id'])).first() friend.agree_friendReq(int(current_user.id)) current_user.add_friend(int(form['friend_id'])) db.session.commit() return Validity(True).get_resp()
def addFriend(): friend = request.form['friend'] answer = request.form['answer'] if answer == "no": sendMessage(friend, "拒绝申请", 0, admin, current_user.nickname + "拒绝了您的好友请求") return jsonify({'code': 0, 'message': '发送成功'}) if friend == current_user.id: return jsonify({'code': 7, 'message': '不能和自己成为好友'}) if current_user.friends.filter_by(other=friend).first(): return jsonify({'code': 8, 'message': '不能重复添加好友'}) user = User.query.filter_by(id=friend).first() if user: current_user.add_friend(friend) sendMessage(friend, "好友申请", 0, current_user.id, "我同意了您的好友请求,我们可以开始聊天了") return jsonify({'code': 0, 'message': '成功添加好友'}) else: return jsonify({'code': 9, 'message': '添加的好友不存在'})