X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/ce8d791a5298e0cb2569034aec4c8b57afac97b2..18b53f03ffd1620e2f3f5b1c17e7dd12c6a6afa6:/apps/dvcs/models.py?ds=sidebyside
diff --git a/apps/dvcs/models.py b/apps/dvcs/models.py
index ab5f77d9..6ecb97ce 100644
--- a/apps/dvcs/models.py
+++ b/apps/dvcs/models.py
@@ -1,109 +1,72 @@
+# -*- coding: utf-8 -*-
+#
+# This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+from __future__ import unicode_literals, print_function
+
from datetime import datetime
-import os.path
+import os
+import re
+from subprocess import PIPE, Popen
+from tempfile import NamedTemporaryFile
-from django.contrib.auth.models import User
+from django.conf import settings
from django.core.files.base import ContentFile
-from django.core.files.storage import FileSystemStorage
from django.db import models
-from django.db.models.base import ModelBase
+from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
-from mercurial import mdiff, simplemerge
-from django.conf import settings
-from dvcs.signals import post_commit
+from dvcs.signals import post_commit, post_merge
from dvcs.storage import GzipFileSystemStorage
+# default repository path; make a setting for it
+REPO_PATH = os.path.join(settings.MEDIA_ROOT, 'dvcs')
+repo = GzipFileSystemStorage(location=REPO_PATH)
-class Tag(models.Model):
- """A tag (e.g. document stage) which can be applied to a Change."""
- name = models.CharField(_('name'), max_length=64)
- slug = models.SlugField(_('slug'), unique=True, max_length=64,
- null=True, blank=True)
- ordering = models.IntegerField(_('ordering'))
-
- _object_cache = {}
-
- class Meta:
- abstract = True
- ordering = ['ordering']
- verbose_name = _("tag")
- verbose_name_plural = _("tags")
-
- def __unicode__(self):
- return self.name
-
- @classmethod
- def get(cls, slug):
- if slug in cls._object_cache:
- return cls._object_cache[slug]
- else:
- obj = cls.objects.get(slug=slug)
- cls._object_cache[slug] = obj
- return obj
-
- @staticmethod
- 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
+@python_2_unicode_compatible
+class Revision(models.Model):
+ """
+ A document revision. The "parent"
+ argument points to the version against which this change has been
+ recorded. Initial text will have a null parent.
-models.signals.pre_save.connect(Tag.listener_changed, sender=Tag)
+ Gzipped text of the document is stored in a file.
+ """
+ author = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, verbose_name=_('author'))
+ author_name = models.CharField(
+ _('author name'), max_length=128, null=True, blank=True, help_text=_("Used if author is not set."))
+ author_email = models.CharField(
+ _('author email'), max_length=128, null=True, blank=True, help_text=_("Used if author is not set."))
+ # Any other author data?
+ # How do we identify an author?
+ parent = models.ForeignKey(
+ 'self', null=True, blank=True, default=None, verbose_name=_('parent'), related_name="children")
-def data_upload_to(instance, filename):
- return "%d/%d" % (instance.tree.pk, instance.pk)
-
-class Change(models.Model):
- """
- Single document change related to previous change. The "parent"
- argument points to the version against which this change has been
- recorded. Initial text will have a null parent.
-
- Data file contains a gzipped text of the document.
- """
- author = models.ForeignKey(User, null=True, blank=True, verbose_name=_('author'))
- author_name = models.CharField(_('author name'), max_length=128,
- null=True, blank=True,
- help_text=_("Used if author is not set.")
- )
- author_email = models.CharField(_('author email'), max_length=128,
- null=True, blank=True,
- help_text=_("Used if author is not set.")
- )
- revision = models.IntegerField(_('revision'), db_index=True)
-
- parent = models.ForeignKey('self',
- null=True, blank=True, default=None,
- verbose_name=_('parent'),
- related_name="children")
-
- merge_parent = models.ForeignKey('self',
- null=True, blank=True, default=None,
- verbose_name=_('merge parent'),
- related_name="merge_children")
+ merge_parent = models.ForeignKey(
+ 'self', null=True, blank=True, default=None, verbose_name=_('merge parent'), related_name="merge_children")
description = models.TextField(_('description'), blank=True, default='')
- created_at = models.DateTimeField(editable=False, db_index=True,
- default=datetime.now)
- publishable = models.BooleanField(_('publishable'), default=False)
+ created_at = models.DateTimeField(editable=False, db_index=True, default=datetime.now)
class Meta:
- abstract = True
ordering = ('created_at',)
- unique_together = ['tree', 'revision']
- verbose_name = _("change")
- verbose_name_plural = _("changes")
+ verbose_name = _("revision")
+ verbose_name_plural = _("revisions")
+
+ def __str__(self):
+ return "Id: %r, Parent %r, Data: %s" % (self.id, self.parent_id, self.get_text_path())
+
+ def get_text_path(self):
+ if self.pk:
+ return re.sub(r'([0-9a-f]{2})([^.])', r'\1/\2', '%x.gz' % self.pk)
+ else:
+ return None
- def __unicode__(self):
- return u"Id: %r, Tree %r, Parent %r, Data: %s" % (self.id, self.tree_id, self.parent_id, self.data)
+ def save_text(self, content):
+ return repo.save(self.get_text_path(), ContentFile(content.encode('utf-8')))
def author_str(self):
if self.author:
@@ -117,210 +80,182 @@ class Change(models.Model):
self.author_email
)
-
- def save(self, *args, **kwargs):
- """
- take the next available revision number if none yet
- """
- if self.revision is None:
- tree_rev = self.tree.revision()
- if tree_rev is None:
- self.revision = 0
- else:
- self.revision = tree_rev + 1
- return super(Change, self).save(*args, **kwargs)
+ @classmethod
+ def create(cls, text, parent=None, merge_parent=None, author=None, author_name=None, author_email=None,
+ description=''):
+
+ if text:
+ text = text.replace(
+ '