from django.core.urlresolvers import reverse
from django.db import models
from django.utils import timezone

from django.contrib.auth.models import User

from .managers import ThreadManager, MessageManager
from .utils import cached_attribute


class Thread(models.Model):

    subject = models.CharField(max_length=150)
    users = models.ManyToManyField(User, through="UserThread")

    objects = ThreadManager()

    def get_absolute_url(self):
        return reverse("messages_thread_detail", kwargs={"thread_id": self.pk})

    @property
    @cached_attribute
    def first_message(self):
        return self.messages.all()[0]

    @property
    @cached_attribute
    def latest_message(self):
        return self.messages.order_by("-sent_at")[0]

    @classmethod
    def ordered(cls, objs):
        """
        Returns the iterable ordered the correct way, this is a class method
        because we don"t know what the type of the iterable will be.
        """
        objs = list(objs)
        objs.sort(key=lambda o: o.latest_message.sent_at, reverse=True)
        return objs


class UserThread(models.Model):

    thread = models.ForeignKey(Thread)
    user = models.ForeignKey(User)

    unread = models.BooleanField()
    deleted = models.BooleanField()


class Message(models.Model):

    thread = models.ForeignKey(Thread, related_name="messages")

    sender = models.ForeignKey(User, related_name="sent_messages")
    sent_at = models.DateTimeField(default=timezone.now)

    content = models.TextField()

    objects = MessageManager()

    class Meta:
        ordering = ("sent_at",)

    def get_absolute_url(self):
        return self.thread.get_absolute_url()

from __future__ import unicode_literals

from django.core.urlresolvers import reverse
from django.db import models
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible

from django.contrib.auth.models import User

from .managers import ThreadManager, MessageManager
from .utils import cached_attribute


@python_2_unicode_compatible
class Thread(models.Model):

    subject = models.CharField(max_length=150)
    users = models.ManyToManyField(User, through="UserThread")

    objects = ThreadManager()

    def __str__(self):
        return "{}: {}".format(
            self.subject,
            ", ".join([user for user in self.users.all()])
        )

    def get_absolute_url(self):
        return reverse("messages_thread_detail", kwargs={"thread_id": self.pk})

    @property
    @cached_attribute
    def first_message(self):
        return self.messages.all()[0]

    @property
    @cached_attribute
    def latest_message(self):
        return self.messages.order_by("-sent_at")[0]

    @classmethod
    def ordered(cls, objs):
        """
        Returns the iterable ordered the correct way, this is a class method
        because we don"t know what the type of the iterable will be.
        """
        objs = list(objs)
        objs.sort(key=lambda o: o.latest_message.sent_at, reverse=True)
        return objs


class UserThread(models.Model):

    thread = models.ForeignKey(Thread)
    user = models.ForeignKey(User)

    unread = models.BooleanField()
    deleted = models.BooleanField()


class Message(models.Model):

    thread = models.ForeignKey(Thread, related_name="messages")

    sender = models.ForeignKey(User, related_name="sent_messages")
    sent_at = models.DateTimeField(default=timezone.now)

    content = models.TextField()

    objects = MessageManager()

    class Meta:
        ordering = ("sent_at",)

    def get_absolute_url(self):
        return self.thread.get_absolute_url()

Add some basic admin