def post(self, request): form = MessageForm(data=request.POST) chat = Chat.objects.get(members=request.user) print(form) if form.is_valid(): message = form.save(commit=False) message.chat_id = chat.id message.author = request.user message.save() return redirect(reverse('contact_with_centre'))
def get(self, request): try: chat = Chat.objects.get(members=request.user) if request.user in chat.members.all(): chat.message_set.filter(is_read=False).exclude(author=request.user).update(is_read=True) else: chat = None except Chat.DoesNotExist: chat = None unread_messages = len(Message.objects.filter(chat=chat, is_read=False).exclude(author=request.user)) context = {'user_profile': request.user, 'unread_messages': unread_messages, 'chat': chat, 'form': MessageForm()} client_instance = client_check(request.user) context['client_img'] = load_client_img(client_instance) return render(request, 'client/client_chat.html', context)