def send_reset_email(user): token = user.get_reset_token() msg = Message('Password Reset Request', sender='*****@*****.**', recipients=[user.email]) msg.body = f'''To reset your password, visit the following link: {url_for('reset_token', token=token, _external=True)} If you did not make this request then simply ignore this email and no changes will be made. ''' mail.send(msg)
def user_favorities(id): page = request.args.get('page', 1, type=int) user = Account.query.filter_by(id=id).first() star_id = [x.id for x in user.stars] star_paginate = Post.query.order_by(Post.id.desc()).filter( Post.id.in_(star_id)).paginate(page=page, per_page=5) form = MessageForm() if form.validate_on_submit(): message = Message(content=form.content.data, timestamp=datetime.today()) user.messages_recieved.append(message) current_user.messages_sent.append(message) db.session.commit() flash('Your messages has been sended successfully', 'success') return redirect(url_for('users.user_favorities', id=user.id)) if current_user.is_authenticated: if user.id == current_user.id: return redirect(url_for('current.account_stars')) posts = user.stars image_file = url_for('static', filename='images/profile_img/' + user.img) return render_template('/user/user_stars.html', user=user, title='User', subtitle='User_Star', img_file=image_file, posts=star_paginate, form=form)
def user_message(user_id): form = MessageForm() user = Account.query.get(user_id) page = request.args.get('page', 1, type=int) image_file = url_for('static', filename='images/profile_img/' + user.img) message_filter = { or_( and_(Message.sender_id == current_user.id, Message.recipient_id == user.id), and_(Message.sender_id == user.id, Message.recipient_id == current_user.id)) } messages = Message.query.order_by( Message.id.desc()).filter(*message_filter).paginate(page=page, per_page=10) if form.validate_on_submit(): message = Message(content=form.content.data, timestamp=datetime.today()) user.messages_recieved.append(message) current_user.messages_sent.append(message) db.session.commit() flash('Your messages has been sended successfully', 'success') return redirect(url_for('users.user_message', user_id=user.id)) return render_template('/user/user_message.html', user=user, title='User', subtitle='User_Message', img_file=image_file, messages=messages, form=form, Account=Account)
def chatting(id): current_user.last_message_read_time = datetime.utcnow() db.session.commit() user = User.query.filter_by(id=id).first_or_404() form = MessageForm() seen = [] if form.validate_on_submit(): msg = Message(author=current_user, recipient=user, body=form.message.data) db.session.add(msg) db.session.commit() messages = db.session.query(Message).filter( and_( or_(Message.sender_id == current_user.id, Message.recipient_id == current_user.id), or_(Message.sender_id == current_user.id, Message.recipient_id == current_user.id))).order_by( Message.timestamp.desc()) if user == current_user: messid = None else: messid = [ message for message in messages if (message.author == current_user and message.recipient == user) or (message.author == user and message.recipient == current_user) ] return render_template('chat_room_v2.html', messages=messages, form=form, seen=seen, messid=messid)
def save_pic(user_id, msg_text, file_type): # user = User.query.get_or_404(user_id) if request.method == "POST": print(request.files['pic_1'], file_type, "check here #1") pic_1 = add_message_pic(request.files['pic_1'], file_type) now_utc = datetime.now(timezone('UTC')) now_asia = now_utc.astimezone(timezone('Asia/Kolkata')) message_text = msg_text if msg_text == "None": if file_type == "image": message_text = '📷 image' elif file_type == "audio": message_text = '🎵 audio' else: message_text = '📁 file' message1 = Message(user_id=user_id, active_user_id=current_user.id, text=message_text, pic_1=pic_1, time_am_pm=now_asia.strftime("%I:%M %p")) db.session.add(message1) db.session.commit() return "DONE HERE1"
def messages(user_id, current_user_id): if current_user.id == current_user_id: form = MessageForm() if form.validate_on_submit(): message = Message(message=form.content.data, receiver_id=user_id, sender_id=current_user_id) db.session.add(message) db.session.commit() return redirect( url_for('messages', user_id=user_id, current_user_id=current_user_id)) messages_id = [] messages1 = Message.query.filter_by(receiver_id=user_id, sender_id=current_user_id).all() for message in messages1: messages_id.append(message.id) messages2 = Message.query.filter_by(receiver_id=current_user_id, sender_id=user_id).all() for message in messages2: messages_id.append(message.id) messages = [] for id in messages_id: messages.append(Message.query.filter_by(id=id).first()) current_receiver = User.query.filter_by(id=user_id).first() return render_template('messages.html', messages=messages, current_receiver=current_receiver, legend='Messages', form=form) else: flash('Dont try to be Oversmart!', 'danger') return redirect(url_for('home'))
def account_messages(): mate = request.args.get('user', '', type=str) page = request.args.get('page', 1, type=int) current_user.last_message_read_time = datetime.today() db.session.commit() if mate == '': image_file = url_for('static', filename='images/profile_img/' + current_user.img) messages = Message.query.filter( or_(Message.sender_id == current_user.id, Message.recipient_id == current_user.id)).all() users = [] for message in messages: if message.sender_id == current_user.id and message.recipient_id not in users: users.append(message.recipient_id) elif message.recipient_id == current_user.id and message.sender_id not in users: users.append(message.sender_id) return render_template('/account/account_messages.html', title='Account', subtitle='Account_Message', img_file=image_file, users=users, Message=Message, Account=Account) else: user = Account.query.filter_by(username=mate).first() form = MessageForm() chat_message_filter = { or_( and_(Message.sender_id == current_user.id, Message.recipient_id == user.id), and_(Message.sender_id == user.id, Message.recipient_id == current_user.id)) } image_file = url_for('static', filename='images/profile_img/' + user.img) messages = Message.query.order_by(Message.id.desc()).filter( *chat_message_filter).paginate(page=page, per_page=5) if form.validate_on_submit(): message = Message(content=form.content.data, timestamp=datetime.today()) current_user.messages_sent.append(message) user.messages_recieved.append(message) db.session.commit() flash('Your messages has been sended successfully', 'success') return redirect( url_for('current.account_messages', user=user.username)) return render_template('/account/account_chat.html', user=user, messages=messages, img_file=image_file, Account=Account, form=form)
def send_message(recipient): user = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): msg = Message(author=current_user, recipient=user, body=form.message.data) user.add_notification('unread_message_count', user.new_messages()) db.session.add(msg) db.session.commit() flash(_('Your message has been sent.')) return redirect(url_for('user', username=recipient)) return render_template('send_message.html', title=_('Send Message'), form=form, recipient=recipient)
def send_message(id): user = User.query.filter_by(id=id).first_or_404() form = MessageForm() if form.validate_on_submit(): msg = Message(author=current_user, recipient=user, body=form.message.data) db.session.add(msg) db.session.commit() flash('Your message has been sent.', 'success') return redirect(url_for('users.user_posts', id=id)) return render_template('send_message.html', title=('Send Message'), form=form, recipient=user)
def notify(): recipient = request.args.get('recipient', 0, type=str) if recipient == "0": return "error" if request.method == 'POST': user = User.query.filter_by(email=recipient).first_or_404() print("Crack Detected") msg = Message(recipient=user, body=request.form['msg_from_rpi']) db.session.add(msg) db.session.commit() return "Message sent successfully" if request.method == 'GET': messg = {"count": current_user.new_messages()} return jsonify(messg)
def handleMessage(msg): message = Message(messageContent=msg, author=current_user) db.session.add(message) db.session.commit() print('Message: ' + msg) send(msg, broadcast=True)
def get_user_chat(user_id): user = User.query.get_or_404(user_id) if request.method == "POST": message_text = request.form['data'] if message_text == "": message_text = "." now_utc = datetime.now(timezone('UTC')) now_asia = now_utc.astimezone(timezone('Asia/Kolkata')) message1 = Message(user_id=user_id, active_user_id=current_user.id, text=request.form['data'], time_am_pm=now_asia.strftime("%I:%M %p")) db.session.add(message1) db.session.commit() user = User.query.get_or_404(user_id) all_my_messages = Message.query.order_by( Message.timestamp.asc()).filter_by( user_id=user_id, active_user_id=current_user.id).all() all_recieved_messages = Message.query.order_by( Message.timestamp.asc()).filter_by(user_id=current_user.id, active_user_id=user_id).all() received_msg_len = len(all_recieved_messages) # print(all_recieved_messages,"chat_box", user_id, current_user.id, received_msg_len) all_messages = [] for chat in all_my_messages: all_messages.append({ 'active_user_id': chat.active_user_id, 'text': chat.text, 'timestamp': chat.timestamp, 'id': chat.id, 'time_am_pm': chat.time_am_pm, 'user_id': chat.user_id, 'seen': chat.seen, 'pic_1': chat.pic_1 }) for chat in all_recieved_messages: chat.seen = "seen" all_messages.append({ 'active_user_id': chat.active_user_id, 'text': chat.text, 'timestamp': chat.timestamp, 'id': chat.id, 'time_am_pm': chat.time_am_pm, 'user_id': chat.user_id, 'pic_1': chat.pic_1 }) db.session.commit() all_messages.sort(reverse=False, key=lambda x: x.get('timestamp')) all_seen_messages_len = Message.query.order_by( Message.timestamp.asc()).filter_by(user_id=user_id, active_user_id=current_user.id, seen="seen").count() return { 'page': render_template('messages2.html', all_messages=all_messages, user=user, time_now=datetime.utcnow(), received_msg_len=received_msg_len), 'count': received_msg_len, 'tw': user.twitter_link, 'fb': user.facebook_link, 'insta': user.instagram_link, 'seen_message_count': all_seen_messages_len }
def get_all_messages(user_id): user = User.query.get_or_404(user_id) print(request.args, request.form) if request.method == "POST": message_text = anchorUrl(request.form['data']) if message_text == "": message_text = "None" now_utc = datetime.now(timezone('UTC')) now_asia = now_utc.astimezone(timezone('Asia/Kolkata')) message1 = Message(user_id=user_id, active_user_id=current_user.id, text=message_text, time_am_pm=now_asia.strftime("%I:%M %p")) db.session.add(message1) db.session.commit() user = User.query.get_or_404(user_id) all_my_messages = Message.query.order_by( Message.timestamp.asc()).filter_by( user_id=user_id, active_user_id=current_user.id).all() all_recieved_messages = Message.query.order_by( Message.timestamp.asc()).filter_by(user_id=current_user.id, active_user_id=user_id).all() all_messages = [] # print(len(all_recieved_messages)) for chat in all_my_messages: all_messages.append({ 'active_user_id': chat.active_user_id, 'text': chat.text, 'timestamp': chat.timestamp, 'id': chat.id, 'time_am_pm': chat.time_am_pm, 'user_id': chat.user_id, 'seen': chat.seen, 'pic_1': chat.pic_1 }) for chat in all_recieved_messages: chat.seen = "seen" all_messages.append({ 'active_user_id': chat.active_user_id, 'text': chat.text, 'timestamp': chat.timestamp, 'id': chat.id, 'time_am_pm': chat.time_am_pm, 'user_id': chat.user_id, 'pic_1': chat.pic_1 }) db.session.commit() all_messages.sort(reverse=False, key=lambda x: x.get('timestamp')) received_msg_len = len(all_recieved_messages) all_seen_messages_len = Message.query.order_by( Message.timestamp.asc()).filter_by(user_id=user_id, active_user_id=current_user.id, seen="seen").count() return render_template('messages2.html', all_messages=all_messages, user=user, time_now=datetime.utcnow(), received_msg_len=received_msg_len, seen_message_len=all_seen_messages_len)