<filename>app/message/views.py<gh_stars>0
from django.shortcuts import render
from django.urls import reverse
from django.contrib.auth import get_user_model
from django.views.generic import TemplateView, RedirectView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import TalkRoom, Message, UserOwnTalkRoom


class TalkRoomView(LoginRequiredMixin, TemplateView):
    template_name = 'message.html'

    def get(self, request, *args, **kwargs):
        login_user_id = self.request.user.id
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            participant=login_user_id).values('talk_room')
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            talk_room__in=talk_rooms).exclude(participant=login_user_id)
        context = super(TalkRoomView, self).get_context_data(**kwargs)
        context['talk_rooms'] = talk_rooms
        context['login_user_id'] = login_user_id
        return render(self.request, self.template_name, context)


class MessageView(LoginRequiredMixin, TemplateView):
    template_name = 'message.html'

    def get(self, request, *args, **kwargs):
        talk_room_id = kwargs['talkroom']
        login_user_id = self.request.user.id
        messages = Message.objects.all().filter(talk_room=talk_room_id)
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            participant=login_user_id).values('talk_room')
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            talk_room__in=talk_rooms).exclude(participant=login_user_id)
        context = super(MessageView, self).get_context_data(**kwargs)
        context['talk_rooms'] = talk_rooms
        context['login_user_id'] = login_user_id
        context['messages'] = messages
        context['talkroom'] = talk_room_id
        return render(self.request, self.template_name, context)


class UserOwnTalkRoomView(LoginRequiredMixin, RedirectView):

    def get_redirect_url(self, *args, **kwargs):
        login_user = self.request.user
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            participant=login_user.id).values('talk_room')
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            talk_room__in=talk_rooms, participant=kwargs['sender'])
        if not talk_rooms:
            talk_room = TalkRoom.objects.create(last_message='')
            UserOwnTalkRoom.objects.create(
                participant=login_user, talk_room=talk_room)
            sender = get_user_model().objects.get(pk=kwargs['sender'])
            UserOwnTalkRoom.objects.create(
                participant=sender, talk_room=talk_room)
        else:
            talk_room = talk_rooms[0]
        return reverse('messages:message', args=(talk_room.id,))

<filename>app/message/views.py<gh_stars>0
from django.shortcuts import render
from django.urls import reverse
from django.contrib.auth import get_user_model
from django.views.generic import TemplateView, RedirectView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import TalkRoom, Message, UserOwnTalkRoom


class TalkRoomView(LoginRequiredMixin, TemplateView):
    template_name = 'message.html'

    def get(self, request, *args, **kwargs):
        login_user_id = self.request.user.id
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            participant=login_user_id).values('talk_room')
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            talk_room__in=talk_rooms).exclude(participant=login_user_id)
        context = super(TalkRoomView, self).get_context_data(**kwargs)
        context['talk_rooms'] = talk_rooms
        context['login_user_id'] = login_user_id
        return render(self.request, self.template_name, context)


class MessageView(LoginRequiredMixin, TemplateView):
    template_name = 'message.html'

    def get(self, request, *args, **kwargs):
        talk_room_id = kwargs['talkroom']
        login_user_id = self.request.user.id
        messages = Message.objects.all().filter(talk_room=talk_room_id)
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            participant=login_user_id).values('talk_room')
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            talk_room__in=talk_rooms).exclude(participant=login_user_id)
        context = super(MessageView, self).get_context_data(**kwargs)
        context['talk_rooms'] = talk_rooms
        context['login_user_id'] = login_user_id
        context['messages'] = messages
        context['talkroom'] = talk_room_id
        return render(self.request, self.template_name, context)


class UserOwnTalkRoomView(LoginRequiredMixin, RedirectView):

    def get_redirect_url(self, *args, **kwargs):
        login_user = self.request.user
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            participant=login_user.id).values('talk_room')
        talk_rooms = UserOwnTalkRoom.objects.all().filter(
            talk_room__in=talk_rooms, participant=kwargs['sender'])
        if not talk_rooms:
            talk_room = TalkRoom.objects.create(last_message='')
            UserOwnTalkRoom.objects.create(
                participant=login_user, talk_room=talk_room)
            sender = get_user_model().objects.get(pk=kwargs['sender'])
            UserOwnTalkRoom.objects.create(
                participant=sender, talk_room=talk_room)
        else:
            talk_room = talk_rooms[0]
        return reverse('messages:message', args=(talk_room.id,))
