X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/db50b75e23a5cd6b29d1315f3a8ed2b7befeb81f..e515627122bf9dc3f21c428f4e48eea835dadf8e:/apps/dvcs/models.py
diff --git a/apps/dvcs/models.py b/apps/dvcs/models.py
index 47e7c26d..6ecb97ce 100644
--- a/apps/dvcs/models.py
+++ b/apps/dvcs/models.py
@@ -1,151 +1,261 @@
+# -*- 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
+import re
+from subprocess import PIPE, Popen
+from tempfile import NamedTemporaryFile
+
+from django.conf import settings
+from django.core.files.base import ContentFile
from django.db import models
-from django.contrib.auth.models import User
+from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
-from mercurial import mdiff, simplemerge
-import pickle
-class Change(models.Model):
+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)
+
+
+@python_2_unicode_compatible
+class Revision(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 contains a pickled diff needed to reproduce the initial document.
+ A document revision. The "parent"
+ argument points to the version against which this change has been
+ recorded. Initial text will have a null parent.
+
+ Gzipped text of the document is stored in a file.
"""
- author = models.ForeignKey(User)
- patch = models.TextField(blank=True)
- tree = models.ForeignKey('Document')
+ 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,
- related_name="children")
+ 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,
- 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(blank=True, default='')
- created_at = models.DateTimeField(auto_now_add=True)
+ description = models.TextField(_('description'), blank=True, default='')
+ created_at = models.DateTimeField(editable=False, db_index=True, default=datetime.now)
class Meta:
ordering = ('created_at',)
+ verbose_name = _("revision")
+ verbose_name_plural = _("revisions")
- def __unicode__(self):
- return u"Id: %r, Tree %r, Parent %r, Patch '''\n%s'''" % (self.id, self.tree_id, self.parent_id, self.patch)
+ def __str__(self):
+ return "Id: %r, Parent %r, Data: %s" % (self.id, self.parent_id, self.get_text_path())
- @staticmethod
- def make_patch(src, dst):
- return pickle.dumps(mdiff.textdiff(src, dst))
+ 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 materialize(self):
- changes = Change.objects.exclude(parent=None).filter(
- tree=self.tree,
- created_at__lte=self.created_at).order_by('created_at')
- text = u''
- for change in changes:
- text = change.apply_to(text)
- return text
+ def save_text(self, content):
+ return repo.save(self.get_text_path(), ContentFile(content.encode('utf-8')))
- def make_child(self, patch, author, description):
- return self.children.create(patch=patch,
- tree=self.tree, author=author,
- description=description)
+ def author_str(self):
+ if self.author:
+ return "%s %s <%s>" % (
+ self.author.first_name,
+ self.author.last_name,
+ self.author.email)
+ else:
+ return "%s <%s>" % (
+ self.author_name,
+ self.author_email
+ )
- def make_merge_child(self, patch, author, description):
- return self.merge_children.create(patch=patch,
- tree=self.tree, author=author,
- description=description)
+ @classmethod
+ def create(cls, text, parent=None, merge_parent=None, author=None, author_name=None, author_email=None,
+ description=''):
- def apply_to(self, text):
- return mdiff.patch(text, pickle.loads(self.patch.encode('ascii')))
+ if text:
+ text = text.replace(
+ '