<commit_before>from datetime import datetime

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

from django.contrib.auth.models import User

from user_messages.managers import ThreadManager, MessageManager
from user_messages.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=datetime.utcnow)
    
    content = models.TextField()
    
    objects = MessageManager()
    
    class Meta:
        ordering = ("sent_at",)
    <commit_msg>Using the now function provided by django.utils.timezone<commit_after>from datetime import datetime

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

from django.utils import timezone
from django.contrib.auth.models import User

from user_messages.managers import ThreadManager, MessageManager
from user_messages.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",)
    
{'fraction_non_alphanumeric': 0.09119496855345911, 'fraction_numerical': 0.002858776443682104, 'mean_word_length': 3.106807511737089, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5685118', 'n_tokens_mistral': 1007, 'n_tokens_neox': 1000, 'n_words': 242}