def send_message(recipient): user = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): from Crypto.Cipher import AES from Crypto.Random import get_random_bytes from base64 import b64encode, b64decode key = get_random_bytes(16) AES_encryption_cipher = AES.new(key, AES.MODE_CFB) ciphertext = AES_encryption_cipher.encrypt( form.message.data.encode("utf-8")) iv = b64encode(AES_encryption_cipher.iv).decode("utf-8") ct = b64encode(ciphertext).decode('utf-8') iv = b64decode(iv) ct = b64decode(ct) AES_decryption_cipher = AES.new(key, AES.MODE_CFB, iv=iv) plaintext = AES_decryption_cipher.decrypt(ct) msg = Message(sender=current_user, recipient=user, body=plaintext.decode("utf-8"), encrypted=b64encode(ct).decode("utf-8"), key=b64encode(key).decode("utf-8")) db.session.add(msg) db.session.commit() flash('Your message has been sent.') return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title='Send Message', form=form, recipient=recipient)
def conversation(other): user = User.query.filter_by(username=other).first_or_404() form = MessageForm() if form.validate_on_submit(): language = guess_language(form.message.data) if language == 'UNKNOWN' or len(language) > 5: language = '' msg = Message(author=current_user, recipient=user, body=form.message.data, language=language) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash('Your message has been sent.') return redirect(url_for('main.conversation', other=other)) page = request.args.get('page', 1, type=int) conversation = current_user.all_messages_with_other(user).paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('main.conversation', page=conversation.next_num, other=other) if conversation.has_next else None prev_url = url_for('main.conversation', page=conversation.prev_num, other=other) if conversation.has_prev else None if conversation.total % current_app.config['POSTS_PER_PAGE'] == 0: last_page = conversation.total // current_app.config['POSTS_PER_PAGE'] else: last_page = conversation.total // current_app.config['POSTS_PER_PAGE'] + 1 last_url = url_for('main.conversation', page=last_page, other=other) return render_template('messages_with_other.html', conversation=conversation.items, next_url=next_url, prev_url=prev_url, user=user, title=user.username + ' - Conversation', form=form, last_url=last_url)
def dialog_by(user_id): current_user.last_message_read_time = datetime.utcnow() db.session.commit() page = request.args.get('page', 1, type=int) user = User.query.filter_by(id=user_id).first_or_404() form = MessageForm() if form.validate_on_submit(): msg = PrivateMessages(sender=current_user, recipient=user, body=form.message.data) db.session.add(msg) db.session.commit() flash(_('Your message has been sent.')) return redirect(url_for('main.dialog_by', user_id=user_id)) page = request.args.get('page', 1, type=int) messages = PrivateMessages.select_messages_to_dialog(user).paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('main.messages_outbox', page=messages.next_num) if messages.has_next else None prev_url = url_for('main.messages_outbox', page=messages.prev_num) if messages.has_prev else None return render_template('dialog.html', title=_(f'Dialog by {user.username}:'), form=form, render_html=render_html, User=User, user=user, messages=messages.items, next_url=next_url, prev_url=prev_url)
def contact(): form = MessageForm() if form.validate_on_submit(): send_message(form) flash('Thanks! For showing your interest we will try to get back to you as soon as possible. ', 'success') return redirect(url_for('main.home')) return render_template('main/contact.html', form=form, title='Contact us')
def user(username): form = MessageForm() user = User.query.filter_by(username=username).first_or_404() page = request.args.get('page', 1, type=int) posts = user.posts.order_by(Post.timestamp.desc()).paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('main.user', username=username, page=posts.next_num) if posts.has_next else None prev_url = url_for('main.user', username=username, page=posts.prev_num) if posts.has_prev else None 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 to {}.'.format(user.username))) return render_template('user.html', user=user, posts=posts.items, next_url=next_url, prev_url=prev_url, form=form, recipient=username)
def send_message(recipient): user = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): if not user.get_room(current_user): room = Room(author=current_user, recipient=user) db.session.add(room) db.session.commit() session['name'] = current_user.username session['room'] = current_user.get_room(user).id 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('main.dialog', recipient=recipient)) return render_template('send_message.html', title=_('Send Message'), form=form, recipient=recipient)
def send_message(recipient): user = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): msg = Message(sender=current_user, recipient=user, body=form.message.data) user.add_notification('unread_message_count', user.new_message()) db.session.commit() flash('Your message has been sent.', "success") return redirect(url_for('main.message', username=recipient)) return render_template('main/send_messages.html', title='Send Message', form=form, recipient=recipient)
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) db.session.add(msg) db.session.commit() flash(_('Your message has been sent')) return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title=_('Send Message'), formm=form, recipient=recipient)
def send_message(recipient): user = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): msg = Message(body=form.message.data, author=current_user, recipient=user) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash(_('Sua mensagem foi enviada.')) return redirect(url_for('main.user', username=recipient)) return render_template('send_messagem.html', title=_('Enviar Mensagem'), form=form, recipient=recipient)
def dispatch_request(self, 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) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash(_('您的消息已被发送')) return redirect(url_for('main.user_info', username=recipient)) return render_template('send_message.html', title=_('发送消息'), form=form, recipient=recipient)
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) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash(_('Ваше сообщение было отправлено пользователю {}'.format(recipient))) return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title=_('Отправить сообщение'), form=form, recipient=recipient)
def send_message(recipient_name): form = MessageForm() if form.validate_on_submit(): recipient = User.query.filter_by(username=recipient_name).first_or_404() new_msg = Message(author=current_user, recipient=recipient, body=form.message.data) recipient.add_notification('unread_message_count', recipient.new_messages()) db.session.add(new_msg) db.session.commit() flash('Message sent successfully') return redirect(url_for('main.user', username=recipient_name)) return render_template('send_message.html', title='Send message', form=form, recipient=recipient_name)
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, receiver=_user, body=form.message.data) _user.add_notification('unread_message_count', _user.new_messages()) db.session.add(msg) db.session.commit() flash('Your message have been sent.') return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', form=form, recipient=recipient, title='Send Message')
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) db.session.add(msg) user.add_notification("unread_message_count", user.new_messages()) db.session.commit() flash(_("Your message has been sent.")) return redirect(url_for("main.user", username=recipient)) return render_template("send_message.html", title=_("Send Message"), form=form, recipient=recipient)
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) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash(_('Your message has been sent.')) return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title=_('Send Message'), form=form, recipient=recipient)
def send_message(user): user = Users.query.filter_by(username=user).first_or_404() form = MessageForm() if form.validate_on_submit(): message = Messages(author=current_user, recipient=user, body=form.message.data) db.session.add(message) db.session.commit() flash('Your message was sent.') return redirect(url_for('main.profile', username=user.username)) return render_template('main/send_message.html', title='Send a Message', form=form, user=user)
def send_message(recipient): user = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): language = guess_language(form.message.data) if language == 'UNKNOWN' or len(language) > 5: language = '' msg = Message(author=current_user, recipient=user, body=form.message.data, language=language) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash('Your message has been sent.') return redirect(url_for('main.user', username=recipient)) # redirect (when and) only when form is successfully submitted return render_template('send_message.html', title='Send Message', form=form, user=user)
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) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash('Il tuo messaggio è stato inviato.') return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title='Invia messaggio', form=form, recipient=recipient)
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) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash(_('Ditt meddelande har skickats.')) return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title=_('Skicka meddelande'), form=form, recipient=recipient)
def send_message(recipient): receive_user = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): message = Message(author=current_user, recipient=receive_user, body=form.message.data) receive_user.add_notification("未读的消息数", receive_user.new_messages()) db.session.add(message) db.session.commit() flash("你的消息已经发送给了{}".format(receive_user.username)) return render_template("send_messages.html", title="发送消息", form=form, recipient=recipient)
def room(room): """ Chat room templated accordingly to the room name. """ form = MessageForm() return render_template('room.html', form=form, room=room)
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) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash("Your message was successfully delivered.") return redirect(url_for('main.user', username=recipient)) return render_temlpate('send_message.html', title="Private Message", form=form, recipient=recipient)
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) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() send_email('Test', '*****@*****.**', ['*****@*****.**'], 'Test email', '<h1>Test email</h1>') flash(_('Your message has been sent.')) return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title=_('Send Message'), form=form, recipient=recipient)
def send_message(recipient): form = MessageForm() if form.validate_on_submit(): recipient_user = User.query.filter_by( username=recipient).first_or_404() message = Message(sender_id=current_user.id, receiver_id=recipient_user.id, body=form.message.data) db.session.add(message) recipient_user.add_notification('unread_message_count', recipient_user.new_messages()) db.session.commit() flash('Your message has been sent') return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title='Send message', form=form, recipient=recipient)
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) db.session.add(msg) # 增加接收人的未读消息的数量 n = user.add_notification('unread_message_count', user.new_messages()) # print("------------->", n.timestamp, user.new_messages()) db.session.commit() # print("==============", n.timestamp, user.new_messages()) flash(_('Your message has been sent.')) return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title=_('Send Message'), form=form, recipient=recipient)
def cooperation(): selected_posts = db.session.query(Post).filter( Post.selected_posts == True).all() recipient = User.query.filter_by(username="******").first_or_404() form = MessageForm() user = current_user if form.validate_on_submit(): msg = PrivateMessages(sender=current_user, recipient=recipient, body=form.message.data, title="coperation---" + form.title.data) db.session.add(msg) db.session.commit() flash(_('Your message has been sent.')) return redirect(url_for('main.cooperation')) return render_template('coperation.html', user=user, form=form, selected_posts=selected_posts)
def send_message(receiver): user = User.query.filter_by(username=receiver).first_or_404() form = MessageForm() if form.validate_on_submit(): message = Message(author=current_user, receiver=user, body=form.message.data) db.session.add(message) # 添加用户通知 user.add_notification(current_app.config['UNREAD_MESSAGE_COUNT'], user.unread_messages_count()) db.session.commit() flash(_('消息发送成功')) return redirect(url_for('main.user', username=receiver)) return render_template('send_message.html', receiver=receiver, title=_('发送信息'), form=form)
def send_message(recipient): user = User.query.filter_by(username=recipient).first_or_404() if current_user.hindi != True: form = MessageForm() else: form = MessageFormHINDI() if form.validate_on_submit(): msg = Message(author=current_user, recipient=user, body=form.message.data) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash(_('Your message has been sent.')) return redirect(url_for('main.user', username=recipient)) if current_user.hindi != True: return render_template('send_message.html', title=_('Send Message'), form=form, recipient=recipient) else: return render_template('Hindi/send_message.html', title=_('Send Message'), form=form, recipient=recipient)
def messages(): log_page_access(request, current_user) current_user.last_message_read_time = datetime.utcnow() current_user.add_notification('unread_message_count', 0) db.session.commit() users = [(u.id, u.username) for u in User.query.order_by(User.username.asc()) if u != current_user] form = MessageForm() form.recipient_id.choices = users recipient_guid = request.args.get('recipient') if recipient_guid: recipient = User.get_by_guid_or_404(recipient_guid) form.recipient_id.data = recipient.id if form.validate_on_submit(): recipient = User.query.get(form.recipient_id.data) msg = Message(author=current_user, recipient=recipient, body=form.message.data) db.session.add(msg) recipient.add_notification('unread_message_count', recipient.new_messages()) db.session.commit() flash(_('Your message has been sent.')) return redirect(url_for('main.messages')) page = request.args.get('page', 1, type=int) messages = current_user.messages_sent.union( current_user.messages_received).order_by( Message.timestamp.desc()).paginate( page, current_app.config['MESSAGES_PER_PAGE'], False) next_url = url_for('main.messages', page=messages.next_num) \ if messages.has_next else None prev_url = url_for('main.messages', page=messages.prev_num) \ if messages.has_prev else None return render_template('messages.html', title=_('Messages'), form=form, messages=messages.items, next_url=next_url, prev_url=prev_url)
def compose(id): recipient = User.query.filter_by(id=id).first_or_404() form = MessageForm() if current_user == recipient: flash('You can\'t send yourself a message') return redirect(url_for('user.user', userid=current_user.id)) if form.validate_on_submit(): msg = Message(author=current_user, recipient=recipient, body=form.message.data) db.session.add(msg) db.session.commit() flash('Your message has been sent') return redirect(url_for('user.user', userid=id)) print("no send") return render_template('compose.html', title='Send Message', form=form, recipient=recipient)
def send_message(recipient): """ Wysyła wiadomość do okreslonego użytkownika i zapisuje w bazie danych :param recipient: użytkownik docelowy :return: przekierowanie na stronę z formularzem """ user_msg = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): msg = Message(author=current_user, recipient=user_msg, body=form.message.data) user_msg.add_notification('unread_message_count', user_msg.new_messages()) db.session.add(msg) db.session.commit() flash(_('Your message has been sent.')) return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title=_('Send Message'), form=form, recipient=recipient)