def post(self, request): if request.user.is_authenticated: user = get_object(Account, request.user.id) team = user.team_set.all() channel = Channel.objects.get(id=request.POST.get('channel_id', '')) thread = Thread() thread.name = request.POST.get('name', '') thread.save() thread.channel.add(channel) thread.save() message = Message() message.sender = user message.thread = thread message.text = request.POST.get('text', '') message.save() afres = {} afres['tread_id'] = thread.id afres['tread_name'] = thread.name afres['channel_id'] = channel.id afres['partner'] = message.sender.fio() afres['text'] = message.text return JsonResponse(afres)
def receive(self, text_data): data = json.loads(text_data) chat_obj = Chat.objects.get(pk=data['chat']) sender = chat_obj.group.users.all().filter(username=self.user_name) if chat_obj and sender: sender = sender[0] datetimeObj = datetime.now() created_time = datetimeObj.strftime("%b %d %Y %I:%M%p") # set last chat used by user. sender.selected_chat = chat_obj sender.save() # Save message to database message_obj = Message() # sender is in list format, get first sender message_obj.sender = sender message_obj.chat = chat_obj message_obj.text = data['message'] if 'image' in data: # Generate a unique file name chars = string.ascii_letters + string.digits + '!@#$%^&*()' random.seed = (os.urandom(1024)) filename = ''.join(random.choice(chars) for i in range(23)) # Save to message object message_obj.image = base64_file(data=data['image'], name=filename) message_obj.save() # Emit message to online users for user in chat_obj.group.users.all(): async_to_sync(self.send_user_message)(user.username, message_obj, created_time)
def send_message(thread_id, sender_id, message_text, sender_name=None): message = Message() message.text = message_text message.thread_id = thread_id message.sender_id = sender_id message.save()