update librarian
[redakcja.git] / apps / dvcs / models.py
index ab5f77d..e82ad19 100644 (file)
@@ -4,13 +4,13 @@ import os.path
 from django.contrib.auth.models import User
 from django.core.files.base import ContentFile
 from django.core.files.storage import FileSystemStorage
-from django.db import models
+from django.db import models, transaction
 from django.db.models.base import ModelBase
-from django.utils.translation import ugettext_lazy as _
-from mercurial import mdiff, simplemerge
+from django.utils.translation import string_concat, ugettext_lazy as _
+from mercurial import simplemerge
 
 from django.conf import settings
-from dvcs.signals import post_commit
+from dvcs.signals import post_commit, post_publishable
 from dvcs.storage import GzipFileSystemStorage
 
 
@@ -26,8 +26,6 @@ class Tag(models.Model):
     class Meta:
         abstract = True
         ordering = ['ordering']
-        verbose_name = _("tag")
-        verbose_name_plural = _("tags")
 
     def __unicode__(self):
         return self.name
@@ -45,13 +43,13 @@ class Tag(models.Model):
     def listener_changed(sender, instance, **kwargs):
         sender._object_cache = {}
 
-    def next(self):
+    def get_next(self):
         """
             Returns the next tag - stage to work on.
             Returns None for the last stage.
         """
         try:
-            return Tag.objects.filter(ordering__gt=self.ordering)[0]
+            return type(self).objects.filter(ordering__gt=self.ordering)[0]
         except IndexError:
             return None
 
@@ -99,8 +97,6 @@ class Change(models.Model):
         abstract = True
         ordering = ('created_at',)
         unique_together = ['tree', 'revision']
-        verbose_name = _("change")
-        verbose_name_plural = _("changes")
 
     def __unicode__(self):
         return u"Id: %r, Tree %r, Parent %r, Data: %s" % (self.id, self.tree_id, self.parent_id, self.data)
@@ -125,7 +121,7 @@ class Change(models.Model):
         if self.revision is None:
             tree_rev = self.tree.revision()
             if tree_rev is None:
-                self.revision = 0
+                self.revision = 1
             else:
                 self.revision = tree_rev + 1
         return super(Change, self).save(*args, **kwargs)
@@ -167,7 +163,7 @@ class Change(models.Model):
     def set_publishable(self, publishable):
         self.publishable = publishable
         self.save()
-        post_publishable(sender=self, publishable=publishable).send()
+        post_publishable.send(sender=self, publishable=publishable)
 
 
 def create_tag_model(model):
@@ -175,6 +171,10 @@ def create_tag_model(model):
 
     class Meta(Tag.Meta):
         app_label = model._meta.app_label
+        verbose_name = string_concat(_("tag"), " ", _("for:"), " ", 
+                model._meta.verbose_name)
+        verbose_name_plural = string_concat(_("tags"), " ", _("for:"), " ",
+                model._meta.verbose_name)
 
     attrs = {
         '__module__': model.__module__,
@@ -189,6 +189,10 @@ def create_change_model(model):
 
     class Meta(Change.Meta):
         app_label = model._meta.app_label
+        verbose_name = string_concat(_("change"), " ", _("for:"), " ",
+                model._meta.verbose_name)
+        verbose_name_plural = string_concat(_("changes"), " ", _("for:"), " ",
+                model._meta.verbose_name)
 
     attrs = {
         '__module__': model.__module__,
@@ -280,7 +284,7 @@ class Document(models.Model):
         tags = kwargs.get('tags', [])
         if tags:
             # set stage to next tag after the commited one
-            self.stage = max(tags, key=lambda t: t.ordering).next()
+            self.stage = max(tags, key=lambda t: t.ordering).get_next()
 
         change = self.change_set.create(author=author,
                     author_name=author_name,
@@ -307,7 +311,7 @@ class Document(models.Model):
         return self.head
 
     def history(self):
-        return self.change_set.filter(revision__gt=-1)
+        return self.change_set.all().order_by('revision')
 
     def revision(self):
         rev = self.change_set.aggregate(
@@ -319,8 +323,21 @@ class Document(models.Model):
         return self.change_set.get(revision=rev)
 
     def publishable(self):
-        changes = self.change_set.filter(publishable=True)
+        changes = self.history().filter(publishable=True)
         if changes.exists():
-            return changes.order_by('-created_at')[0]
+            return changes.order_by('-revision')[0]
         else:
             return None
+
+    @transaction.atomic
+    def prepend_history(self, other):
+        """Takes over the the other document's history and prepends to own."""
+
+        assert self != other
+        other_revs = other.change_set.all().count()
+        # workaround for a non-atomic UPDATE in SQLITE
+        self.change_set.all().update(revision=0-models.F('revision'))
+        self.change_set.all().update(revision=other_revs - models.F('revision'))
+        other.change_set.all().update(tree=self)
+        assert not other.change_set.exists()
+        other.delete()