Привет, коллеги!
Есть простейший сигнал, который срабатывает при создания объекта через админку, но совершенно ничего не происходит при сохранении объекта через вьюху. Пробовал и с декоратором, и как видите, без. Не пойму в чем проблема.
def save_dialog(sender, instance, **kwargs):
instance.dialog.last_message_date = instance.created
instance.dialog.save()
print('Все нормуль')
post_save.connect(save_dialog, sender=Message)
upd:
Сори, я недостаточно подробно описал проблему. Я похоже понял в чем загвоздка. Я пилю один из репозиториев с гитхаба под свой проект . Судя по всему, проблема в ассинхронности.
вот, собственно, проблемный участок (конкретно models.Message.objects.create()):
@asyncio.coroutine
def new_messages_handler(stream):
"""
Saves a new chat message to db and distributes msg to connected users
"""
# TODO: handle no user found exception
while True:
packet = yield from stream.get()
session_id = packet.get('session_key')
msg = packet.get('message')
username_opponent = packet.get('username')
if session_id and msg and username_opponent:
user_owner = get_user_from_session(session_id)
if user_owner:
user_opponent = get_user_model().objects.get(username=username_opponent)
dialog = get_dialogs_with_user(user_owner, user_opponent, packet.get('id'))
if len(dialog) > 0:
# Save the message
msg = models.Message.objects.create(
dialog=dialog[0],
sender=user_owner,
text=packet['message'],
is_system=False,
read=False
)
#packet['created'] = msg.get_formatted_create_datetime()
packet['created'] = msg.get_formatted_create_datetime()
packet['sender_name'] = msg.sender.username
packet['message_id'] = msg.id
# мои доработки
if msg.sender.userprofile.avatar:
packet['avatar'] = msg.sender.userprofile.avatar_mini.url
else:
packet['avatar'] = '/static/img/avatar_default.jpg'
print(packet['avatar'] )
# Send the message
connections = []
# Find socket of the user which sent the message
if (user_owner.username, user_opponent.username) in ws_connections:
connections.append(ws_connections[(user_owner.username, user_opponent.username)])
# Find socket of the opponent
if (user_opponent.username, user_owner.username) in ws_connections:
connections.append(ws_connections[(user_opponent.username, user_owner.username)])
else:
# Find sockets of people who the opponent is talking with
opponent_connections = list(filter(lambda x: x[0] == user_opponent.username, ws_connections))
opponent_connections_sockets = [ws_connections[i] for i in opponent_connections]
connections.extend(opponent_connections_sockets)
yield from fanout_message(connections, packet)
else:
pass # no dialog found
else:
pass # no user_owner
else:
pass # missing one of params