from django.db import models
from django.utils import translation
from django.conf import settings
from cms.markupfield.fields import MarkupField
from django.utils.safestring import mark_safe
from django.utils.translation import get_language

class Category(models.Model):
    name = models.CharField(max_length=80)

    def __unicode__(self):
        return self.name

class ItemManager(models.Manager):
    def retrieve(self, category):
        category = Category.objects.get(name=category)

        return self.get(category=category)

    def retrieve_content(self, category, lang):
        return mark_safe(self.retrieve(category=category).get_latest(lang))

class Item(models.Model):
    category = models.ForeignKey(Category)

    objects = ItemManager()

    def get_latest(self, lang=get_language()):
        return self.content_set.get(language=lang).revision_set.all()[0]

    @property
    def content(self):
        return self.get_latest()

    def __unicode__(self):
        try:
            content = self.content_set.get(language=get_language())
        except Content.DoesNotExist:
            content = self.content_set.all()[0]

        return "Content in '%s': %s" % (content.language, str(content))

class NewsitemManager(models.Manager):
    def newest(self, amount=None, lang=get_language()):
        newsitems = self.filter(content__language=lang)

        if amount == None:
            return newsitems
        else:
            return newsitems[:amount]

class Newsitem(Item):
    date = models.DateField(auto_now_add=True)

    objects = NewsitemManager()

    class Meta:
        ordering = ['-date']

class Content(models.Model):
    item = models.ForeignKey(Item)
    language = models.CharField(max_length=10)

    # FIXME: Sanity check language

    def __unicode__(self):
        revision = self.revision_set.order_by('-date')
        if len(revision) > 0:
            return str(self.item.category) + " / " + str(revision[0])
        else:
            return str(self.item.category) + " / no revisions exist"

class Revision(models.Model):
    date = models.DateTimeField(auto_now_add=True)
    content = models.ForeignKey(Content)
    subject = models.CharField(max_length=200, null=True, blank=True)
    data = MarkupField()

    class Meta:
        ordering = ['-date']

    def __unicode__(self):
        if not self.subject:
            subject = "[No subject]"
        else:
            subject = self.subject
        return "%s / %s" % (str(self.date), subject)

# This exists to make things just a bit snappier (not used for now)
class Cache(models.Model):
    item = models.ForeignKey(Item)
    language = models.CharField(max_length=10)
    content = models.ForeignKey(Content)
    revision = models.ForeignKey(Revision)

@@ -143,54 +143,8 @@
 eld%0A
-from django.utils.safestring import mark_safe%0A
 from
@@ -531,16 +531,21 @@
 ry, lang
+=None
 ):%0A     
@@ -551,25 +551,17 @@
     
-return mark_safe(
+latest = 
 self
@@ -605,17 +605,38 @@
 st(lang)
-)
+%0A        return latest
 %0A%0Aclass 

Remove mark_safe from ItemManager.retrieve_content