django-markupwiki/markupwiki/models.py

71 lines
2.2 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
2010-03-30 20:17:42 +00:00
DEFAULT_MARKUP_TYPE = getattr(settings, 'MARKUPWIKI_DEFAULT_MARKUP_TYPE', 'plain')
2010-03-30 21:26:43 +00:00
WRITE_LOCK_SECONDS = getattr(settings, 'MARKUPWIKI_WRITE_LOCK_SECONDS', 60)
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):
title = models.CharField(max_length=50)
creator = models.ForeignKey(User, related_name='wiki_articles')
status = models.IntegerField(choices=ARTICLE_STATUSES, default=PUBLIC)
def __unicode__(self):
return self.title
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-30 21:26:43 +00:00
def get_write_lock(self, user):
cache_key = 'markupwiki_articlelock_%s' % self.id
lock = cache.get(cache_key)
if lock:
return lock == user.id
cache.set(cache_key, user.id, WRITE_LOCK_SECONDS)
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()
timestamp = models.DateTimeField(auto_now_add=True)
removed = models.BooleanField(default=False)
2010-03-30 20:17:42 +00:00
body = MarkupField(default_markup_type=DEFAULT_MARKUP_TYPE)
2010-03-30 14:43:30 +00:00
class Meta:
ordering = ['timestamp']
get_latest_by = 'timestamp'
def __unicode__(self):
return unicode(self.article)
def get_absolute_url(self):
return reverse('article_version', args=[self.article.title, self.number])