# Path: della/inbox/forms.py
# class MessageCreateForm(ModelForm):
# 
#     class Meta:
#         model = Message
#         fields = ['text']
# 
# Path: della/inbox/models.py
# class Message(TimeStampMixin):
#     text = models.TextField()
# 
#     sent_by = models.ForeignKey(User, on_delete=models.CASCADE)
#     thread = models.ForeignKey(Thread, on_delete=models.CASCADE,  related_name='messages')
# 
# class Thread(TimeStampMixin):
#     """
#     `is_sneaky` flag will be used when the message thread is between santa and
#     santee. If `is_sneaky` is set to `True` then `santa` shouldn't be null.
# 
#     `participant_1.id` will be always less than `participant_2.id`
#     """
#     is_sneaky = models.BooleanField(default=False)
# 
#     participant_1 = models.ForeignKey(
#         User, on_delete=models.CASCADE,  related_name='participant_1_threads')
#     participant_2 = models.ForeignKey(
#         User, on_delete=models.CASCADE,  related_name='participant_2_threads')
#     santa = models.ForeignKey(User, on_delete=models.CASCADE,  null=True, related_name='santa_threads')


from django.views.generic.edit import CreateView
from django.views.generic import DetailView, ListView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.shortcuts import reverse, get_object_or_404
from django.db.models import Q, Max
from django.http import Http404, JsonResponse
from django.contrib.auth.models import User
from django.utils.formats import date_format
from .forms import MessageCreateForm
from .models import Message, Thread
from . import inbox_service
from . import tasks

    def _get_response(self):
        timestamp = date_format(self.object.created_on, 'DATETIME_FORMAT')
        data = {
            'text': self.object.text,
            'signature': "{} | {}".format(self.object.sent_by.username,
                                          timestamp),
        }
        return {'status': True, 'data': data}


@method_decorator(login_required, name='dispatch')
class ThreadListView(ListView):
    model = Thread

    def get_queryset(self):
        user = self.request.user
        return Thread.objects.prefetch_related('messages').filter(
            Q(participant_1=user) | Q(participant_2=user)).annotate(
            last_message_time=Max('messages__created_on')).order_by(
                '-last_message_time')

    def get_context_data(self, **kwargs):
        sender = self.request.user
        context = super().get_context_data(**kwargs)
        object_list = []
        sneaky_list = []
        for obj in context['object_list']:
            try:
                obj.latest_message = obj.messages.latest('created_on')

# Path: della/inbox/forms.py
# class MessageCreateForm(ModelForm):
# 
#     class Meta:
#         model = Message
#         fields = ['text']
# 
# Path: della/inbox/models.py
# class Message(TimeStampMixin):
#     text = models.TextField()
# 
#     sent_by = models.ForeignKey(User, on_delete=models.CASCADE)
#     thread = models.ForeignKey(Thread, on_delete=models.CASCADE,  related_name='messages')
# 
# class Thread(TimeStampMixin):
#     """
#     `is_sneaky` flag will be used when the message thread is between santa and
#     santee. If `is_sneaky` is set to `True` then `santa` shouldn't be null.
# 
#     `participant_1.id` will be always less than `participant_2.id`
#     """
#     is_sneaky = models.BooleanField(default=False)
# 
#     participant_1 = models.ForeignKey(
#         User, on_delete=models.CASCADE,  related_name='participant_1_threads')
#     participant_2 = models.ForeignKey(
#         User, on_delete=models.CASCADE,  related_name='participant_2_threads')
#     santa = models.ForeignKey(User, on_delete=models.CASCADE,  null=True, related_name='santa_threads')


# Path: della/inbox/views.py
from django.views.generic.edit import CreateView
from django.views.generic import DetailView, ListView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.shortcuts import reverse, get_object_or_404
from django.db.models import Q, Max
from django.http import Http404, JsonResponse
from django.contrib.auth.models import User
from django.utils.formats import date_format
from .forms import MessageCreateForm
from .models import Message, Thread
from . import inbox_service
from . import tasks


    def _get_response(self):
        timestamp = date_format(self.object.created_on, 'DATETIME_FORMAT')
        data = {
            'text': self.object.text,
            'signature': "{} | {}".format(self.object.sent_by.username,
                                          timestamp),
        }
        return {'status': True, 'data': data}


@method_decorator(login_required, name='dispatch')
class ThreadListView(ListView):
    model = Thread

    def get_queryset(self):
        user = self.request.user
        return Thread.objects.prefetch_related('messages').filter(
            Q(participant_1=user) | Q(participant_2=user)).annotate(
            last_message_time=Max('messages__created_on')).order_by(
                '-last_message_time')

    def get_context_data(self, **kwargs):
        sender = self.request.user
        context = super().get_context_data(**kwargs)
        object_list = []
        sneaky_list = []
        for obj in context['object_list']:
            try:
                obj.latest_message = obj.messages.latest('created_on')

            except Message.DoesNotExist: