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

[(7, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(60)'}), False, 'from django.db import models\n'), (8, 'django.db.models.TextField', 'models.TextField', ([], {'blank': '(True)', 'default': '""""""'}), False, 'from django.db import models\n'), (9, 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now': '(True)'}), False, 'from django.db import models\n'), (10, 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now': '(True)'}), False, 'from django.db import models\n'), (11, 'django.db.models.ForeignKey', 'models.ForeignKey', (['User'], {'blank': '(True)', 'null': '(True)'}), False, 'from django.db import models\n'), (30, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(60)'}), False, 'from django.db import models\n'), (31, 'django.db.models.TextField', 'models.TextField', ([], {'max_length': '(10000)', 'blank': '(True)', 'null': '(True)'}), False, 'from django.db import models\n'), (32, 'django.db.models.ForeignKey', 'models.ForeignKey', (['Forum'], {}), False, 'from django.db import models\n'), (33, 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now': '(True)'}), False, 'from django.db import models\n'), (34, 'django.db.models.ForeignKey', 'models.ForeignKey', (['User'], {'blank': '(True)', 'null': '(True)'}), False, 'from django.db import models\n'), (35, 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now': '(True)'}), False, 'from django.db import models\n'), (36, 'django.db.models.BooleanField', 'models.BooleanField', ([], {'blank': '(True)', 'default': '(False)'}), False, 'from django.db import models\n'), (52, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(60)'}), False, 'from django.db import models\n'), (53, 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now_add': '(True)'}), False, 'from django.db import models\n'), (54, 'django.db.models.ForeignKey', 'models.ForeignKey', (['User'], {'blank': '(True)', 'null': '(True)'}), False, 'from django.db import models\n'), (55, 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now': '(True)'}), False, 'from django.db import models\n'), (56, 'django.db.models.ForeignKey', 'models.ForeignKey', (['Topic'], {}), False, 'from django.db import models\n'), (57, 'django.db.models.TextField', 'models.TextField', ([], {'max_length': '(10000)'}), False, 'from django.db import models\n'), (58, 'django.db.models.GenericIPAddressField', 'models.GenericIPAddressField', ([], {'blank': '(True)', 'null': '(True)'}), False, 'from django.db import models\n'), (70, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(60)'}), False, 'from django.db import models\n')]