def send_message(recipient_id, text): room = None if User.is_correct_id(recipient_id): recipient = User.query.get(recipient_id) dialog_id = Dialog.get_id(current_user, recipient) if dialog_id is None: room = Dialog.create(current_user, recipient) # Send new dialog to all users for user in room.members: emit('get_new_room', room.to_dict(user), namespace='/user', room=str(user.id)) else: room = Dialog.query.get(dialog_id) else: pass # TODO: CHAT if room is not None and format_message(text) != '': current_user.send_message(room, format_message(text)) last_message = room.get_last_message() if last_message['text'] == '': return for user in room.members: emit('get_message', last_message, namespace='/user', room=str(user.id))
def test_create_message(self): msg = self.d.new_message(sender=self.u1, body='text_body') self.assertTrue(Message.query.filter_by(id=1).first_or_404()) self.assertIs(msg, Message.query.filter_by(id=1).first_or_404()) self.assertEqual( Message.query.filter_by(id=1).first().body, 'text_body') self.assertIs(Dialog.get_by_id(1).last_message, msg) self.assertIsNot(Dialog.get_by_id(2).last_message, msg)
def test_get_dialog(self): self.create_users() d = Dialog.get_dialog(self.u1, self.u2) self.assertEqual(1, d.id) self.assertIsNot(d, Dialog.get_dialog(self.u1, self.u3)) self.assertIs(d, Dialog.get_dialog(self.u1, self.u2)) self.assertEqual(d.id, Dialog.get_dialog(self.u1, self.u2).id)
def test_get_info(self): self.create_users() self.create_dialog(self.u1, self.u2, dialog_name='First_dialog') self.assertEqual(Dialog.get_dialog_info(self.u1, self.u2, info='id'), 1) self.assertEqual( Dialog.get_dialog_info(self.u1, self.u2, info='dialog_name'), 'First_dialog')
def dialog(id): if not current_user.dialogs.filter_by(id=id).all(): return redirect(url_for('dialogs.dialogs')) d: Dialog = Dialog.get_by_id(id) other_users = d.get_recipients(current_user) if not d: flash('You have not gor this dialog') return redirect(url_for('dialogs.dialogs')) form = MessageForm() if form.validate_on_submit(): d.new_message(sender=current_user, body=form.message.data) return redirect(url_for('dialogs.dialog', id=id)) # user_dialog = Dialog.query.filter_by(id=id).first_or_404() msg_list = [] if d: # msg_list = Message.query.filter_by(dialog_id=id).order_by(Message.timestamp_send.desc()).paginate(1, 100, False) msg_list = d.messages.order_by(Message.timestamp_send.desc()).paginate( 1, 100, False) return render_template('dialogs/dialog_window.html', messages_list=msg_list.items, form=form, width=50, recipients=other_users)
def select_dialog(sender, recipient): dialog = Dialog.query.filter_by( sender=sender, recipient=recipient ).first() if not dialog: dialog = Dialog(sender=sender, recipient=recipient) db.session.add(dialog) commit_changes() return dialog
def read_messages(recipient_id): room = None if User.is_correct_id(recipient_id): room = Dialog.get_object(current_user, User.query.get(recipient_id)) else: pass # TODO: CHAT if room is not None: room.read_messages(current_user) for user in room.members: emit('update_room', room.to_dict(user), namespace='/user', room=(str(user.id)))
def add_message(): data = request.get_json() or {} try: MessageValidator().validate({'recipient_id': int, 'message': str}, data) except ValueError as exception: return bad_request(exception.args[0]) current_user.send_message( room=Dialog.get_or_create(current_user, User.query.get_or_404(data['recipient_id'])), text=data['message'] ) return jsonify(True)
def get_dialog(): data = request.get_json() or {} # Validation try: Validator.validate_required_fields({'profile_id': int}, data) except ValueError as exception: return bad_request(exception.args[0]) dialog = Dialog.get_object(current_user, User.query.get_or_404(data['profile_id'])) if dialog is None: return jsonify([]) else: return jsonify(dialog.get_messages())
def create_dialog(): try: form = g.create_dialog_form except AttributeError: g.create_dialog_form = ExampleForm.create_form( current_user.query.filter(User.id != current_user.id).order_by( User.username)) form = g.create_dialog_form if request.method == 'POST': print('Submit') print(form.users_check_box.data) d = Dialog.create_dialog_new(ids=form.users_check_box.data) d.users.append(current_user) del g.create_dialog_form return redirect(url_for('dialogs.dialog', id=d.id)) print(request.form) return render_template('dialogs/new_dialog.html', form=form)
def test_dialog_model(self): dialog = Dialog() db.session.add(dialog) db.session.commit() self.assertEqual(Dialog.query.count(), 1)
def test_create(self): self.create_users() d = Dialog.create_dialog(self.u1, self.u2) self.assertIs(Dialog.query.filter_by(id=1).first(), d)
def create_dialog(self, u1, u2, **kwargs): d = Dialog(users=[u1, u2], **kwargs) db.session.add(d) db.session.commit() return d
def send_message(username): user = User.query.filter_by(username=username).first_or_404() if not user: return redirect(url_for('index')) dialog_id = Dialog.get_dialog_info(current_user, user, info='id') return redirect(url_for('dialogs.dialog', id=dialog_id))
def test_create_dialog_ids(self): self.create_users() d = Dialog.create_dialog_new(ids=(1, 2, 3)) self.assertEqual(d, Dialog.get_by_id(1))
def test_check_dialog(self): self.create_users() self.create_dialog(self.u1, self.u2) self.assertTrue(Dialog.has_dialog(self.u1, self.u2)) self.assertFalse(Dialog.has_dialog(self.u1, self.u3))
def test_get_by_id(self): self.create_users() d = self.create_dialog(self.u1, self.u2) d1 = self.create_dialog(self.u1, self.u3) self.assertIs(d, Dialog.get_by_id(1)) self.assertIsNot(d, Dialog.get_by_id(2))