from functools import partial

from django.core.urlresolvers import reverse
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.db.models.signals import post_save

from cms.models.fields import PlaceholderField
from cms.models.pluginmodel import CMSPlugin
from aldryn_people.models import Person
import reversion
from reversion.revisions import VersionAdapter
from parler.models import TranslatableModel, TranslatedFields
from parler import cache


class TranslatableVersionAdapter(VersionAdapter):
    revision_manager = None

    def __init__(self, model):
        super(TranslatableVersionAdapter, self).__init__(model)

        # Register the translation model to be tracked as well
        root_model = model._parler_meta.root_model
        self.revision_manager.register(root_model)
        self.follow += (model._parler_meta.root_rel_name, )
        post_save.connect(self._clear_cache, sender=root_model)

        # Exclude all translated fields, as those are tracked separately
        self.exclude += tuple(model._parler_meta.get_translated_fields())

    def _clear_cache(self, sender, instance, raw, **kwargs):
        if raw:
            cache._cache_translation(instance)


register_translatable = partial(reversion.register,
                                adapter_cls=TranslatableVersionAdapter,
                                revision_manager=reversion)


@register_translatable
class Article(TranslatableModel):
    translations = TranslatedFields(
        title = models.CharField(_('Title'), max_length=234),

        content = PlaceholderField(
            'aldryn_newsblog_article_content',
            related_name='aldryn_newsblog_articles'),
    )

    slug = models.SlugField(
        verbose_name=_('Slug'),
        max_length=255,
        unique=True,
        blank=True,
        help_text=_(
            'Used in the URL. If changed, the URL will change. '
            'Clean it to have it re-created.'),
    )

    author = models.ForeignKey(Person)
    owner = models.ForeignKey(User)
    namespace = models.CharField(max_length=123, blank=True, default='')
    category = models.CharField(max_length=123, blank=True, default='')
    publishing_date = models.DateTimeField()

    def get_absolute_url(self):
        return reverse(
            'aldryn_newsblog:article-detail', kwargs={'slug': self.slug})


class LatestEntriesPlugin(CMSPlugin):

    latest_entries = models.IntegerField(
        default=5,
        help_text=_('The number of latests entries to be displayed.')
    )

    def __unicode__(self):
        return str(self.latest_entries).decode('utf8')

    def copy_relations(self, oldinstance):
        self.tags = oldinstance.tags.all()

    def get_articles(self):
        articles = Article.objects.filter_by_language(self.language)
        return articles[:self.latest_entries]

@@ -870,16 +870,77 @@
 t_model)
+%0A%0A        # Also add the translations to the models to follow
 %0A       
@@ -956,12 +956,31 @@
 low 
-+
 = 
-(
+list(self.follow) + %5B
 mode
@@ -1011,11 +1011,10 @@
 name
-, )
+%5D%0A
 %0A   
@@ -1022,210 +1022,273 @@
     
-post_save.connect(self._clear_cac
+# And make sure that when we revert t
 he
+m
 , 
-sender=root_model)%0A%0A        # Exclude all translated fields, as those are tracked separately%0A        self.exclude += tuple(mod
+we update the translations%0A        # cache (this is normally done in the translation %60save_base%60 method,%0A        # but it is not caled when reverting changes).%0A        post_save.connect(s
 el
+f
 ._
-parler_meta.get_translated_fields()
+update_cache, sender=root_model
 )%0A%0A 
@@ -1299,13 +1299,14 @@
 ef _
-clear
+update
 _cac
@@ -1364,16 +1364,83 @@
 if raw:%0A
+            # Raw is set to true only when restoring from fixtures%0A
         

Add more comments.