def user_search(): form = UserSearchForm(request.form) connect_form = ConnectForm() session['url'] = 'app.user_search' users = None user_name = request.args.get('username', None, type=str) next_url = prev_url = None if user_name: page = request.args.get('page', 1, type=int) posts = User.search_by_name(user_name, page) next_url = url_for('app.user_search', page=posts.next_num, username=user_name) if posts.has_next else None prev_url = url_for('app.user_search', page=posts.prev_num, username=user_name) if posts.has_prev else None users = posts.items # 検索結果のユーザを取ってくる。UserテーブルとUserConnectテーブルを紐づけて、 # UserConnectテーブルのstatusを見ます # from_user_id = 自分のID, to_user_id = 相手のID、status=1の場合は自分から友達申請中 # to_user_id = 自分のID, from_user_id = 相手のID、status=1の場合は、相手から友達申請されている # status = 2の場合、友達になっている # レコードが存在しない場合、申請していないし、されていない return render_template('user_search.html', form=form, connect_form=connect_form, users=users, next_url=next_url, prev_url=prev_url)
def connect_user(): form = ConnectForm(request.form) if request.method == 'POST' and form.validate(): if form.connect_condition.data == 'connect': new_connect = UserConnect(current_user.get_id(), form.to_user_id.data) with db.session.begin(subtransactions=True): new_connect.create_new_connect() db.session.commit() elif form.connect_condition.data == 'accept': connect = UserConnect.select_by_from_user_id(form.to_user_id.data) if connect: with db.session.begin(subtransactions=True): connect.update_status() db.session.commit() next_url = session.pop('url', 'app:home') return redirect(url_for(next_url))
def home(): friends = requested_friends = requesting_friends = None connect_form = ConnectForm() session['url'] = 'app.home' if current_user.is_authenticated: friends = User.select_friends() requested_friends = User.select_requested_friends() requesting_friends = User.select_requesting_friends() return render_template('home.html', friends=friends, requested_friends=requested_friends, requesting_friends=requesting_friends, connect_form=connect_form)
def user_search(): form = UserSearchForm(request.form) connect_form = ConnectForm() session['url'] = 'app.user_search' users = None if request.method == 'POST' and form.validate(): username = form.username.data users = User.search_by_name(username) # 検索結果のユーザを取ってくる。UserテーブルとUserConnectテーブルを紐づけて、 # UserConnectテーブルのstatusを見ます # from_user_id = 自分のID, to_user_id = 相手のID、status=1の場合は自分から友達申請中 # to_user_id = 自分のID, from_user_id = 相手のID、status=1の場合は、相手から友達申請されている # status = 2の場合、友達になっている # レコードが存在しない場合、申請していないし、されていない return render_template('user_search.html', form=form, connect_form=connect_form, users=users)
def user_search(): form = UserSearchForm(request.form) connect_form = ConnectForm() session['url'] = 'app.user_search' users = None user_name = request.args.get('username', None, type=str) next_url = prev_url = None if user_name: page = request.args.get('page', 1, type=int) posts = User.search_by_name(user_name, page) next_url = url_for('app.user_search', page=posts.next_num, username=user_name) if posts.has_next else None prev_url = url_for('app.user_search', page=posts.prev_num, username=user_name) if posts.has_prev else None users = posts.items return render_template('user_search.html', form=form, connect_form=connect_form, users=users, next_url=next_url, prev_url=prev_url)