from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

class Forum(models.Model):
    title = models.CharField(max_length=60)
    description = models.TextField(blank=True, default='')
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now=True)
    creator = models.ForeignKey(User, blank=True, null=True)

    def __unicode__(self):
        return self.title

    def num_posts(self):
        return sum([t.num_posts() for t in self.topic_set.all()])

    def last_post(self):
        if self.topic_set.count():
            last = None
            for t in self.topic_set.all():
                l = t.last_post()
                if l:
                    if not last: last = l
                    elif l.created > last.created: last = l
            return last

class Topic(models.Model):
    title = models.CharField(max_length=60)
    description = models.TextField(max_length=10000, blank=True, null=True)
    forum = models.ForeignKey(Forum)
    created = models.DateTimeField(auto_now=True)
    creator = models.ForeignKey(User, blank=True, null=True)
    updated = models.DateTimeField(auto_now=True)
    closed = models.BooleanField(blank=True, default=False)

    def num_posts(self):
        return self.post_set.count()

    def num_replies(self):
        return max(0, self.post_set.count() - 1)

    def last_post(self):
        if self.post_set.count():
            return self.post_set.order_by("created")[0]

    def __unicode__(self):
        return unicode(self.creator) + " - " + self.title

class Post(models.Model):
    title = models.CharField(max_length=60)
    created = models.DateTimeField(auto_now_add=True)
    creator = models.ForeignKey(User, blank=True, null=True)
    updated = models.DateTimeField(auto_now=True)
    topic = models.ForeignKey(Topic)
    body = models.TextField(max_length=10000)
    user_ip = models.GenericIPAddressField(blank=True, null=True)

    def __unicode__(self):
        return u"%s - %s - %s" % (self.creator, self.topic, self.title)

    def short(self):
        return u"%s - %s\n%s" % (self.creator, self.title, self.created.strftime("%b %d, %I:%M %p"))

    short.allow_tags = True


class ProfaneWord(models.Model):
    word = models.CharField(max_length=60)

    def __unicode__(self):
        return self.word

[((202, 233), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(60)'}), '(max_length=60)\n', (218, 233), False, 'from django.db import models\n'), ((252, 292), 'django.db.models.TextField', 'models.TextField', ([], {'blank': '(True)', 'default': '""""""'}), "(blank=True, default='')\n", (268, 292), False, 'from django.db import models\n'), ((307, 342), 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now': '(True)'}), '(auto_now=True)\n', (327, 342), False, 'from django.db import models\n'), ((357, 392), 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now': '(True)'}), '(auto_now=True)\n', (377, 392), False, 'from django.db import models\n'), ((407, 453), 'django.db.models.ForeignKey', 'models.ForeignKey', (['User'], {'blank': '(True)', 'null': '(True)'}), '(User, blank=True, null=True)\n', (424, 453), False, 'from django.db import models\n'), ((950, 981), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(60)'}), '(max_length=60)\n', (966, 981), False, 'from django.db import models\n'), ((1000, 1057), 'django.db.models.TextField', 'models.TextField', ([], {'max_length': '(10000)', 'blank': '(True)', 'null': '(True)'}), '(max_length=10000, blank=True, null=True)\n', (1016, 1057), False, 'from django.db import models\n'), ((1070, 1094), 'django.db.models.ForeignKey', 'models.ForeignKey', (['Forum'], {}), '(Forum)\n', (1087, 1094), False, 'from django.db import models\n'), ((1109, 1144), 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now': '(True)'}), '(auto_now=True)\n', (1129, 1144), False, 'from django.db import models\n'), ((1159, 1205), 'django.db.models.ForeignKey', 'models.ForeignKey', (['User'], {'blank': '(True)', 'null': '(True)'}), '(User, blank=True, null=True)\n', (1176, 1205), False, 'from django.db import models\n'), ((1220, 1255), 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now': '(True)'}), '(auto_now=True)\n', (1240, 1255), False, 'from django.db import models\n'), ((1269, 1315), 'django.db.models.BooleanField', 'models.BooleanField', ([], {'blank': '(True)', 'default': '(False)'}), '(blank=True, default=False)\n', (1288, 1315), False, 'from django.db import models\n'), ((1697, 1728), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(60)'}), '(max_length=60)\n', (1713, 1728), False, 'from django.db import models\n'), ((1743, 1782), 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now_add': '(True)'}), '(auto_now_add=True)\n', (1763, 1782), False, 'from django.db import models\n'), ((1797, 1843), 'django.db.models.ForeignKey', 'models.ForeignKey', (['User'], {'blank': '(True)', 'null': '(True)'}), '(User, blank=True, null=True)\n', (1814, 1843), False, 'from django.db import models\n'), ((1858, 1893), 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now': '(True)'}), '(auto_now=True)\n', (1878, 1893), False, 'from django.db import models\n'), ((1906, 1930), 'django.db.models.ForeignKey', 'models.ForeignKey', (['Topic'], {}), '(Topic)\n', (1923, 1930), False, 'from django.db import models\n'), ((1942, 1976), 'django.db.models.TextField', 'models.TextField', ([], {'max_length': '(10000)'}), '(max_length=10000)\n', (1958, 1976), False, 'from django.db import models\n'), ((1991, 2042), 'django.db.models.GenericIPAddressField', 'models.GenericIPAddressField', ([], {'blank': '(True)', 'null': '(True)'}), '(blank=True, null=True)\n', (2019, 2042), False, 'from django.db import models\n'), ((2341, 2372), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(60)'}), '(max_length=60)\n', (2357, 2372), False, 'from django.db import models\n')]