def msg_consumer(message): # Save to model #room = message.content['room'] data = json.loads(message.content['message']) username = data.get('username') msg = data.get('message') user = User.objects.filter(username=username).first() print('msg=> ', message.content) message_to_return = json.loads(message.content['message']) if data.get('action') == 'create': msg_obj = Message() msg_obj.sender_user = user msg_obj.message = msg msg_obj.save() message_to_return["id"] = msg_obj.id message_to_return["creation_date"] = datetime.datetime.strftime( msg_obj.creation_date, '%Y-%m-%d %H:%M:%S') if data.get('action') == 'delete': msg_obj = Message.objects.filter(id=data.get('id')).first() msg_obj.delete() # Broadcast to listening sockets message_to_return["action"] = data.get('action') Group('chat').send({'text': json.dumps(message_to_return)})
def msgbox(request): if request.method=='POST': message=Message() message.message=request.POST.get('message') message.user_name= request.user.username message.image=request.user.profile.image message.save() return HttpResponseRedirect("http://127.0.0.1:8000/chat/index/")
def post(request): message = request.POST.get('message',None) if message is None: return HttpResponseBadRequest new_msg = Message() new_msg.message = message new_msg.user = request.user new_msg.save() return HttpResponse(json.dumps({'success': True, 'child': new_msg.chatbox_element}), content_type='application/json')
def save(request): if request.is_ajax(): if 'multipart/form-data' not in str( request.META.get('CONTENT_TYPE', '')): request.POST = json.loads(request.body.decode('utf-8')) message = Message() posted_files = None if request.FILES: posted_files = request.FILES message.message = request.POST.get('message') message.author = request.user if posted_files: message.image_file = posted_files.get('image') message.save() res = {'success': True, 'id': message.id} return HttpResponse(json.dumps(res))
async def receive(self, text_data): import datetime from .tasks import execute text_data_json = json.loads(text_data) print(text_data) message = text_data_json['message'] if len(message) == 0: return False if "from_bot" in text_data_json: username = "******" else: username = self.username if message[0] == '/' and '=' in message: command = message.split('=')[0] parameter = message.split('=')[1] execute.s(command, parameter, self.room_group_name).apply_async(serializer="pickle") currentDT = datetime.datetime.now() datetime = currentDT.strftime("%Y-%m-%d %H:%M:%S") # Send message to room group msg = Message() msg.message = message if not "from_bot" in text_data_json: msg.user = self.user msg.username = self.username msg.room_name = self.room_name msg.room_group_name = self.room_group_name msg.save() await self.channel_layer.group_send( self.room_group_name, { 'type': 'chat_message', 'message': message, 'username': username, 'datetime': datetime })
def receive(self, text_data): text_data_json = json.loads(text_data) message_type = text_data_json['type'] user_json = text_data_json['user'] user = User.objects.get(username=user_json['username'], password=user_json['password'], first_name=user_json['first_name'], last_name=user_json['last_name'], email=user_json['email']) channel_name = self.room_name channel = Channel.objects.get(channel_name=channel_name) if message_type == 'message_channel': message = text_data_json['message'] print("User: {}".format(user)) print("Message: {}".format(message)) print("Channel: {}".format(channel_name)) message_obj = Message() message_obj.channel = channel message_obj.user = user message_obj.message = message message_obj.save() serializer = MessageSerializer(message_obj) # Send message to room group async_to_sync(self.channel_layer.group_send)( self.room_group_name, { 'type': 'broadcast', 'event': message_type, 'message': serializer.data, 'user': user_json, 'channel_name': channel_name }) elif message_type == 'join_channel': if not user in channel.users.all(): channel.users.add(user) channel.save() async_to_sync(self.channel_layer.group_send)( self.room_group_name, { 'type': 'broadcast', 'event': message_type, 'channel_name': channel_name, 'user': user_json }) elif message_type == 'change_topic': async_to_sync(self.channel_layer.group_send)( self.room_group_name, { 'type': 'broadcast', 'event': message_type, 'channel_name': channel_name, 'user': user_json }) elif message_type == 'leave_channel': if user in channel.users.all(): channel.users.remove(user) channel.save() async_to_sync(self.channel_layer.group_send)( self.room_group_name, { 'type': 'broadcast', 'event': message_type, 'channel_name': channel_name, 'user': user_json }) elif message_type == 'delete_channel': if channel.creator == user: for group in self.groups: async_to_sync(self.channel_layer.group_send)( group, { 'type': 'broadcast', 'event': message_type, 'channel_name': channel_name, 'user': user_json }) self.groups.remove('chat_%s' % channel.channel_name.replace(" ", "_")) channel.delete() elif message_type == 'add_channel': channel_n = text_data_json['channel_name'] print("Channel: {}".format(channel_n)) print("Groups: {}".format(self.groups)) for group in self.groups: async_to_sync(self.channel_layer.group_send)(group, { 'type': 'broadcast', 'event': message_type, 'channel_name': channel_name, 'user': user_json }) if not 'chat_%s' % channel_n.replace(" ", "_") in self.groups: self.groups.append('chat_%s' % channel_n.replace(" ", "_")) print(self.groups)