def renew_database(): from werkzeug.security import generate_password_hash, check_password_hash Chat.objects.delete() Message.objects.delete() User.objects.delete() user1 = User(name="user1", email='*****@*****.**', password=generate_password_hash("test", method='sha256')).save() user2 = User(name="user2", email='*****@*****.**', password=generate_password_hash("test", method='sha256')).save() user3 = User(name="user3", email='*****@*****.**', password=generate_password_hash("test", method='sha256')).save() user4 = User(name="user4", email='*****@*****.**', password=generate_password_hash("test", method='sha256')).save() chat1 = ChatCreation.create_new(members=[user1, user2]) chat2 = ChatCreation.create_new(members=[user1, user3]) Message(text="Zprava user1", author=user1, chat=chat1).save() Message(text="Zprava user2", author=user2, chat=chat1).save() Message(text="Zprava user1", author=user1, chat=chat1).save() Message(text="Zprava user1 chat 2", author=user1, chat=chat2).save() Message(text="Zprava user2 chat2", author=user3, chat=chat2).save()
def create_conversations(): user1 = User.objects(name="test").first() user2 = User.objects(name="test2").first() try: ChatCreation.create_new(members=[user1, user2]) conv1 = Chat.objects(members=[user1, user2]).first() Message(text="Testovací zpráva autora test", author=user1, chat=conv1).save() Message(text="Testovací zpráva autora test3", author=user2, chat=conv1).save() Message(text="1. Testovací zpráva autora test", author=user1, chat=conv1).save() m1 = Message(text="3. Testovací zpráva autora test", author=user1, chat=conv1).save() conv1.update(last_message_date=m1.date_created) except Exception as e: print(str(e))
def add_message(): try: data = json.loads(request.data) user_to = data['to'] message_self = data['message_self'] message_to = data['message_to'] encode_self = message_self.encode('utf-8') encode_to = message_to.encode('utf-8') if len(message_self) > 2048 or len(message_to) > 2048: return { "success": False, "message": "Message exceeds 2048 character limit." } user_from = get_username(session["id"]) message_self = Message(original=user_from, username_to=user_to, username_from=user_from, message=encode_self) message_to = Message(original=user_to, username_to=user_to, username_from=user_from, message=encode_to) db.session.add(message_self) db.session.add(message_to) db.session.commit() nMessages = get_messages_history(user_from, user_from, user_to) return {"success": True, "messages": nMessages} except json.decoder.JSONDecoderError: return {"error": "Malformed request"}, 400
def post(self, id): author = request.get_json().get('author') text = request.get_json().get('text') chat = Chat_db.objects(id=id).first() user = User.objects(name=author).first() if user not in chat.members: return { "success": False, "message": "This user is not part of current chat" } message = Message(author=user, chat=chat, text=text).save() if message is not None: return {"success": True, "message": "Message was sent"}
def test_thing(self): User( name="FooBar1", email="*****@*****.**", password=os.urandom(16), ).save() current_user = User.objects().first() users = User.objects() con = Chat(members=users).save() Message(text="Message test text", author=current_user.id, conversation=con).save() message = Message.objects().first() self.assertEqual(message.author.id, current_user.id)
def handle_message(msg, room): user = User.objects(name=msg['author']).first() chat = Chat.objects(id=room).first() Message(author=user, chat=chat, text=msg['text']).save() status = "" if current_user.is_authenticated: # if msg["author"] == current_user["name"]: # status = "sent" # else: # status = "replies" message = [msg["author"], msg["text"], str(1), status] send(message, room=room) else: send(False, room=room)
def save_message(data): Message(author=data['author'], text=data['text'], chat=data['chat']).save()