Advertisement
Guest User

Untitled

a guest
Feb 10th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. def get_dialog_between_users(user1_id, user2_id):
  2.     qs = ParticipantChat.objects.filter(chat__is_group=False)
  3.  
  4.     chats_user1 = qs.filter(user__id=user1_id).values_list('chat_id', flat=True)
  5.     chats_user2 = qs.filter(user__id=user2_id).values_list('chat_id', flat=True)
  6.  
  7.     dialog_ids = set(chats_user1) & set(chats_user2)
  8.  
  9.     if len(dialog_ids) > 1:
  10.         raise exceptions.ValidationError(detail='There is more than one chat between users')
  11.     if not dialog_ids:
  12.         return None
  13.  
  14.     dialog_id = dialog_ids.pop()
  15.     return Chat.objects.filter(id=dialog_id).first()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement