def validate(self): if not super(FlaskForm, self).validate(): return False is_friend = UserConnect.is_friend(self.to_user_id.data) if not is_friend: return False return True
def message(id): if not UserConnect.is_friend(id): return redirect(url_for('app.home')) form = MessageForm(request.form) # 自分と相手のやり取りのメッセージを取得 messages = Message.get_friend_messages(current_user.get_id(), id) user = User.select_user_by_id(id) # まだ読まれていないが、新たに読まれるメッセージ read_message_ids = [message.id for message in messages if (not message.is_read) and (message.from_user_id == int(id))] # すでに読まれていて、かつまだチェックしていない自分のメッセージをチェック not_checked_message_ids = [message.id for message in messages if message.is_read and (not message.is_checked) and (message.from_user_id == int(current_user.get_id()))] if not_checked_message_ids: with db.session.begin(subtransactions=True): Message.update_is_checked_by_ids(not_checked_message_ids) db.session.commit() # read_message_idsのis_readをTrueに変更 if read_message_ids: with db.session.begin(subtransactions=True): Message.update_is_read_by_ids(read_message_ids) db.session.commit() if request.method == 'POST' and form.validate(): new_message = Message(current_user.get_id(), id, form.message.data) with db.session.begin(subtransactions=True): new_message.create_message() db.session.commit() return redirect(url_for('app.message', id=id)) return render_template( 'message.html', form=form, messages=messages, to_user_id=id, user=user )
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 message(id): if not UserConnect.is_friend(id): return redirect(url_for('app.home')) form = MessageForm(request.form) messages = Message.get_friend_messages(current_user.get_id(), id) if request.method == 'POST' and form.validate(): new_message = Message(current_user.get_id(), id, form.message.data) with db.session.begin(subtransactions=True): new_message.create_message() db.session.commit() return redirect(url_for('app.message', id=id)) return render_template('message.html', form=form, messages=messages, to_user_id=id)