('parent', self.gf('django.db.models.fields.related.ForeignKey')(default=None, related_name='children', null=True, blank=True, to=orm['dvcs.Change'])),
('merge_parent', self.gf('django.db.models.fields.related.ForeignKey')(default=None, related_name='merge_children', null=True, blank=True, to=orm['dvcs.Change'])),
('description', self.gf('django.db.models.fields.TextField')(default='', blank=True)),
- ('created_at', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, db_index=True, blank=True)),
+ ('created_at', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now, db_index=True)),
))
db.send_create_signal('dvcs', ['Change'])
'dvcs.change': {
'Meta': {'ordering': "('created_at',)", 'unique_together': "(['tree', 'revision'],)", 'object_name': 'Change'},
'author': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}),
- 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'db_index': 'True', 'blank': 'True'}),
+ 'created_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'db_index': 'True'}),
'description': ('django.db.models.fields.TextField', [], {'default': "''", 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'merge_parent': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'related_name': "'merge_children'", 'null': 'True', 'blank': 'True', 'to': "orm['dvcs.Change']"}),
+from datetime import datetime
+
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
related_name="merge_children")
description = models.TextField(blank=True, default='')
- created_at = models.DateTimeField(auto_now_add=True, db_index=True)
+ created_at = models.DateTimeField(editable=False, db_index=True,
+ default=datetime.now)
class Meta:
ordering = ('created_at',)
return self.head
def history(self):
- return self.change_set.filter(revision__gt=0)
+ return self.change_set.filter(revision__gt=-1)
def revision(self):
rev = self.change_set.aggregate(
models.Max('revision'))['revision__max']
- return rev if rev is not None else 0
+ return rev if rev is not None else -1
def at_revision(self, rev):
if rev:
@staticmethod
def listener_initial_commit(sender, instance, created, **kwargs):
+ # run for Document and its subclasses
+ if not isinstance(instance, Document):
+ return
if created:
instance.head = Change.objects.create(
- revision=0,
+ revision=-1,
author=instance.creator,
patch=Change.make_patch('', ''),
tree=instance)
instance.save()
-models.signals.post_save.connect(Document.listener_initial_commit, sender=Document)
+models.signals.post_save.connect(Document.listener_initial_commit)
# Adding model 'Chunk'
db.create_table('wiki_chunk', (
- ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('document_ptr', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['dvcs.Document'], unique=True, primary_key=True)),
('book', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['wiki.Book'])),
('number', self.gf('django.db.models.fields.IntegerField')()),
('slug', self.gf('django.db.models.fields.SlugField')(max_length=50, db_index=True)),
('comment', self.gf('django.db.models.fields.CharField')(max_length=255)),
- ('doc', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['dvcs.Document'], unique=True)),
))
db.send_create_signal('wiki', ['Chunk'])
'dvcs.change': {
'Meta': {'ordering': "('created_at',)", 'unique_together': "(['tree', 'revision'],)", 'object_name': 'Change'},
'author': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}),
- 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'db_index': 'True', 'blank': 'True'}),
+ 'created_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'db_index': 'True'}),
'description': ('django.db.models.fields.TextField', [], {'default': "''", 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'merge_parent': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'related_name': "'merge_children'", 'null': 'True', 'blank': 'True', 'to': "orm['dvcs.Change']"}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '255'})
},
'wiki.chunk': {
- 'Meta': {'ordering': "['number']", 'unique_together': "[['book', 'number'], ['book', 'slug']]", 'object_name': 'Chunk'},
+ 'Meta': {'ordering': "['number']", 'unique_together': "[['book', 'number'], ['book', 'slug']]", 'object_name': 'Chunk', '_ormbases': ['dvcs.Document']},
'book': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['wiki.Book']"}),
'comment': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
- 'doc': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['dvcs.Document']", 'unique': 'True'}),
- 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'document_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['dvcs.Document']", 'unique': 'True', 'primary_key': 'True'}),
'number': ('django.db.models.fields.IntegerField', [], {}),
'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50', 'db_index': 'True'})
},
import itertools
import re
+from django.core.urlresolvers import reverse
from django.db import models
from django.utils.translation import ugettext_lazy as _
"""
instance = cls(*args, **kwargs)
instance.save()
- instance.chunk_set.all()[0].doc.commit(author=creator, text=text)
+ instance[0].commit(author=creator, text=text)
return instance
+ def __iter__(self):
+ return iter(self.chunk_set.all())
+
+ def __getitem__(self, chunk):
+ return self.chunk_set.all()[chunk]
+
+ def __len__(self):
+ return self.chunk_set.count()
+
@staticmethod
def trim(text, trim_begin=True, trim_end=True):
"""
texts = []
trim_begin = False
text = ''
- for chunk in self.chunk_set.all():
- next_text = chunk.doc.materialize()
+ for chunk in self:
+ next_text = chunk.materialize()
if not next_text:
continue
if text:
models.signals.post_save.connect(Book.listener_create, sender=Book)
-class Chunk(models.Model):
+class Chunk(dvcs_models.Document):
""" An editable chunk of text. Every Book text is divided into chunks. """
book = models.ForeignKey(Book)
number = models.IntegerField()
slug = models.SlugField()
comment = models.CharField(max_length=255)
- doc = models.ForeignKey(dvcs_models.Document, editable=False, unique=True, null=True)
class Meta:
unique_together = [['book', 'number'], ['book', 'slug']]
def __unicode__(self):
return "%d-%d: %s" % (self.book_id, self.number, self.comment)
- def save(self, *args, **kwargs):
- if self.doc is None:
- self.doc = dvcs_models.Document.objects.create()
- super(Chunk, self).save(*args, **kwargs)
+ def get_absolute_url(self):
+ return reverse("wiki_editor", args=[self.book.slug, self.slug])
@classmethod
def get(cls, slug, chunk=None):
def pretty_name(self):
return "%s, %s (%d/%d)" % (self.book.title, self.comment,
- self.number, self.book.chunk_set.count())
+ self.number, len(self.book))
<h1>{{ book.title }}</h1>
<p>
- {% for chunk in book.chunk_set.all %}
- <a target="_blank" href="{% url wiki_editor book.slug chunk.slug %}">{{ chunk.comment }}</a><br/>
+ {% for chunk in book %}
+ <a target="_blank" href="{{ chunk.get_absolute_url }}">{{ chunk.comment }}</a><br/>
{% endfor %}
</p>
href="{% url wiki_editor book.slug %}">
[{% trans "edit" %}]</a>
{% else %}
- {% for chunk in book.chunk_set.all %}
+ {% for chunk in book %}
<a target="_blank" data-id="{{ book.slug }}"
- href="{% url wiki_editor book.slug chunk.slug %}">
+ href="{{ chunk.get_absolute_url }}">
[<span class='chunkno'>{{ forloop.counter }}.</span>
{{ chunk.comment }}</a>]<br/>
{% endfor %}
</p>
<p>
<label>{% trans "Current version" %}:</label>
- {{ chunk.doc.revision }} ({{ chunk.doc.head.created_at }})
+ {{ chunk.revision }} ({{ chunk.head.created_at }})
<p>
<label>{% trans "Last edited by" %}:</label>
- {{ chunk.doc.head.author }}
+ {{ chunk.head.author }}
</p>
<p>
<label for="gallery">{% trans "Link to gallery" %}:</label>
@decorator_from_middleware(GZipMiddleware)
def text(request, slug, chunk=None):
try:
- doc = Chunk.get(slug, chunk).doc
+ doc = Chunk.get(slug, chunk)
except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
raise Http404
form = DocumentTextRevertForm(request.POST, prefix="textrevert")
if form.is_valid():
try:
- doc = Chunk.get(slug, chunk).doc
+ doc = Chunk.get(slug, chunk)
except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
raise Http404
revB = None
try:
- doc = Chunk.get(slug, chunk).doc
+ doc = Chunk.get(slug, chunk)
except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
raise Http404
docA = doc.at_revision(revA).materialize()
@never_cache
def revision(request, slug, chunk=None):
try:
- doc = Chunk.get(slug, chunk).doc
+ doc = Chunk.get(slug, chunk)
except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
raise Http404
return http.HttpResponse(str(doc.revision()))
def history(request, slug, chunk=None):
# TODO: pagination
try:
- doc = Chunk.get(slug, chunk).doc
+ doc = Chunk.get(slug, chunk)
except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
raise Http404