Source

superwiki / superwiki / models.py

Full commit
# -*- coding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext as _
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.conf import settings
import reversion

from common import markup_processors


class WikiPage(models.Model):
    
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    bind_to = generic.GenericForeignKey('content_type', 'object_id')
    url = models.CharField(_('url'), max_length=100)
    title = models.CharField(_('title'), max_length=100, null=True, blank=True)
    author = models.ForeignKey(User, verbose_name=_('author'))
    body = models.TextField(_('body'))
    rendered_body = models.TextField(_('rendered body'))
    created = models.DateTimeField(_('created'), auto_now_add=True)
    rev = models.PositiveIntegerField(_('revision'), default=0)
    linked_pages = models.ManyToManyField('self', null=True, blank=True, symmetrical=False)
    
    def save(self, *args, **kwargs):
        self.rendered_body = self.body
        if not self.rev:
            self.rev = 0
        self.rev += 1
        markup_type = getattr(self.bind_to, 'markup_type', settings.MARKUP_TYPES[0][0])
        for markup_processor in markup_processors[markup_type]:
            markup_processor(self)
        super(WikiPage, self).save(*args, **kwargs)
    
    def __unicode__(self):
        return u'%s - %s' % (self.bind_to, self.url)
    
    class Meta:
        db_table = 'wiki'
        app_label = 'wiki'
        unique_together = ('object_id', 'content_type', 'url')

        
reversion.register(WikiPage)