document stages on import,
[redakcja.git] / apps / dvcs / models.py
index 262472a..f81f2ca 100644 (file)
@@ -38,6 +38,16 @@ class Tag(models.Model):
     def listener_changed(sender, instance, **kwargs):
         sender._object_cache = {}
 
     def listener_changed(sender, instance, **kwargs):
         sender._object_cache = {}
 
+    def 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]
+        except IndexError:
+            return None
+
 models.signals.pre_save.connect(Tag.listener_changed, sender=Tag)
 
 
 models.signals.pre_save.connect(Tag.listener_changed, sender=Tag)
 
 
@@ -168,12 +178,16 @@ class Document(models.Model):
     """
         File in repository.        
     """
     """
         File in repository.        
     """
-    creator = models.ForeignKey(User, null=True, blank=True, editable=False)
+    creator = models.ForeignKey(User, null=True, blank=True, editable=False,
+                related_name="created_documents")
     head = models.ForeignKey(Change,
                     null=True, blank=True, default=None,
                     help_text=_("This document's current head."),
                     editable=False)
 
     head = models.ForeignKey(Change,
                     null=True, blank=True, default=None,
                     help_text=_("This document's current head."),
                     editable=False)
 
+    user = models.ForeignKey(User, null=True, blank=True)
+    stage = models.ForeignKey(Tag, null=True, blank=True)
+
     def __unicode__(self):
         return u"{0}, HEAD: {1}".format(self.id, self.head_id)
 
     def __unicode__(self):
         return u"{0}, HEAD: {1}".format(self.id, self.head_id)
 
@@ -213,6 +227,9 @@ class Document(models.Model):
         author = kwargs.get('author', None)
         author_desc = kwargs.get('author_desc', None)
         tags = kwargs.get('tags', [])
         author = kwargs.get('author', None)
         author_desc = kwargs.get('author_desc', None)
         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()
 
         old_head = self.head
         if parent != old_head:
 
         old_head = self.head
         if parent != old_head: