V
V
vikholodov2018-05-22 14:04:13
Django
vikholodov, 2018-05-22 14:04:13

Why is signal not firing in Django?

Hello colleagues!
There is a simple signal that is triggered when an object is created through the admin panel, but absolutely nothing happens when the object is saved through the view. I tried with the decorator, and as you can see, without. I don't understand what the problem is.

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:
Sorry, I didn't describe the problem in enough detail. I seem to have figured out what's wrong. I'm downloading one of the github repositories for my project. Apparently, the problem is in asynchrony.
here, in fact, is the problem area (specifically, 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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
un1t, 2018-05-22
@un1t

Maybe I posted it in the wrong file. You can also try specifying weak=False (this is a parameter of the connect function).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question