django-markupwiki/markupwiki/models.py

95 lines
3.1 KiB
Python
Raw Normal View History

2010-03-30 21:26:43 +00:00
import datetime
2010-03-30 14:43:30 +00:00
from django.db import models
2010-03-30 20:17:42 +00:00
from django.conf import settings
2010-03-30 14:43:30 +00:00
from django.contrib.auth.models import User
2010-03-30 21:26:43 +00:00
from django.core.cache import cache
2010-03-30 14:43:30 +00:00
from django.core.urlresolvers import reverse
from markupfield.fields import MarkupField
from markupfield import markup
from markupwiki.utils import wikify_markup_wrapper
2010-03-30 14:43:30 +00:00
2010-04-06 18:23:26 +00:00
DEFAULT_MARKUP_TYPE = getattr(settings, 'MARKUPWIKI_DEFAULT_MARKUP_TYPE', 'markdown')
WRITE_LOCK_SECONDS = getattr(settings, 'MARKUPWIKI_WRITE_LOCK_SECONDS', 300)
MARKUP_TYPES = getattr(settings, 'MARKUPWIKI_MARKUP_TYPES', markup.DEFAULT_MARKUP_TYPES)
# add make_wiki_links to MARKUP_TYPES
WIKI_MARKUP_TYPES = []
for name, func in MARKUP_TYPES:
WIKI_MARKUP_TYPES.append((name, wikify_markup_wrapper(func)))
2010-03-30 20:17:42 +00:00
PUBLIC, LOCKED, DELETED = range(3)
2010-03-30 14:43:30 +00:00
ARTICLE_STATUSES = (
(PUBLIC, 'Public'), # public - no restrictions on viewing/editing
(LOCKED, 'Locked'), # locked - only admins can edit
(DELETED, 'Deleted'), # deleted - display deleted page
)
class Article(models.Model):
2010-04-05 16:00:57 +00:00
title = models.CharField(max_length=200)
2010-03-30 14:43:30 +00:00
creator = models.ForeignKey(User, related_name='wiki_articles')
status = models.IntegerField(choices=ARTICLE_STATUSES, default=PUBLIC)
2010-04-02 17:50:49 +00:00
redirect_to = models.ForeignKey('self', null=True)
2010-03-30 14:43:30 +00:00
def __unicode__(self):
return self.title
2010-04-05 17:45:05 +00:00
@property
def display_title(self):
return self.title.rsplit('/',1)[-1].replace('_', ' ')
2010-04-05 18:23:26 +00:00
@property
def section_name(self):
if '/' in self.title:
return self.title.rsplit('/',1)[0]
2010-03-30 14:43:30 +00:00
def get_absolute_url(self):
return reverse('view_article', args=[self.title])
2010-03-30 16:12:10 +00:00
def is_public(self):
return self.status == PUBLIC
def is_locked(self):
return self.status == LOCKED
def is_deleted(self):
return self.status == DELETED
def is_editable_by_user(self, user):
if self.status in (LOCKED, DELETED):
return user.is_staff
else:
return user.is_authenticated()
2010-03-31 14:33:40 +00:00
def get_write_lock(self, user, release=False):
2010-03-30 21:26:43 +00:00
cache_key = 'markupwiki_articlelock_%s' % self.id
lock = cache.get(cache_key)
if lock:
2010-03-31 14:33:40 +00:00
if release:
cache.delete(cache_key)
2010-03-30 21:26:43 +00:00
return lock == user.id
if not release:
cache.set(cache_key, user.id, WRITE_LOCK_SECONDS)
2010-03-30 21:26:43 +00:00
return True
2010-03-30 16:12:10 +00:00
2010-03-30 14:43:30 +00:00
class ArticleVersion(models.Model):
article = models.ForeignKey(Article, related_name='versions')
author = models.ForeignKey(User, related_name='article_versions')
number = models.PositiveIntegerField()
body = MarkupField(default_markup_type=DEFAULT_MARKUP_TYPE,
markup_choices=WIKI_MARKUP_TYPES)
2010-03-31 16:02:07 +00:00
comment = models.CharField(max_length=200, blank=True)
2010-03-30 14:43:30 +00:00
timestamp = models.DateTimeField(auto_now_add=True)
removed = models.BooleanField(default=False)
class Meta:
ordering = ['timestamp']
get_latest_by = 'timestamp'
def __unicode__(self):
2010-04-01 18:30:33 +00:00
return '%s rev #%s' % (self.article, self.number)
2010-03-30 14:43:30 +00:00
def get_absolute_url(self):
return reverse('article_version', args=[self.article.title, self.number])