Commit 83fe3d46 by benjaoming

More on models. Not done yet.

parent 30fb1eca
...@@ -85,6 +85,7 @@ INSTALLED_APPS = ( ...@@ -85,6 +85,7 @@ INSTALLED_APPS = (
'django.contrib.admindocs', 'django.contrib.admindocs',
'south', 'south',
'wiki', 'wiki',
'mptt',
) )
# A sample logging configuration. The only tangible logging # A sample logging configuration. The only tangible logging
......
...@@ -2,4 +2,6 @@ ...@@ -2,4 +2,6 @@
from django.conf import settings as django_settings from django.conf import settings as django_settings
# Should urls be case sensitive? # Should urls be case sensitive?
URL_CASE_SENSITIVE = getattr(django_settings, "WIKI_URL_CASE_SENSITIVE", False) URL_CASE_SENSITIVE = getattr(django_settings, "WIKI_URL_CASE_SENSITIVE", False)
\ No newline at end of file
APP_LABEL = 'wiki'
\ No newline at end of file
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.conf import settings
from django.conf import settings as django_settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
import warnings import warnings
from article import Article, ArticleRevision, ObjectForArticle
from urlpath import URLPath from urlpath import URLPath
from article import Article, ArticleRevision
###################### ######################
# Configuration stuff # Configuration stuff
###################### ######################
if not 'mptt' in settings.INSTALLED_APPS: if not 'mptt' in django_settings.INSTALLED_APPS:
raise ImproperlyConfigured('django-wiki: needs mptt in INSTALLED_APPS') raise ImproperlyConfigured('django-wiki: needs mptt in INSTALLED_APPS')
if not 'django.contrib.contenttypes' in django_settings.INSTALLED_APPS:
raise ImproperlyConfigured('django-wiki: needs django.contrib.contenttypes in INSTALLED_APPS')
###################### ######################
# Warnings # Warnings
###################### ######################
if not 'south' in settings.INSTALLED_APPS: if not 'south' in django_settings.INSTALLED_APPS:
warnings.warn("django-wiki: No south in your INSTALLED_APPS. This is highly discouraged.") warnings.warn("django-wiki: No south in your INSTALLED_APPS. This is highly discouraged.")
\ No newline at end of file
\ No newline at end of file
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.contrib.auth.models import User, Group
from wiki.conf import settings
class Article(models.Model): class Article(models.Model):
title = models.CharField(max_length=512, verbose_name=_(u'title')) title = models.CharField(max_length=512, verbose_name=_(u'title'),
current_revision = models.ForeignKey('ArticleRevision') null=False, blank=False)
current_revision = models.ForeignKey('ArticleRevision',
verbose_name=_(u'current revision'),
blank=True, null=True, related_name='current_set')
# Permissions. If nothing is set, the article will inherit from
# some other parent, whatever the semantics dictate. For instance, using
# URLPaths means that the article inherits from its URLPath parent.
# Inheriting permissions requires a "get_parent_articles" method to exist on
# one of the objects related to the article.
# TIP: The related object with a get_parent_articles method should be an
# MPTTModel inheritor for efficiency. See the URLPath model.
owner = models.ForeignKey(User, verbose_name=_('owner'),
blank=True, null=True)
group = models.ForeignKey(Group, verbose_name=_('group'),
blank=True, null=True)
group_read = models.BooleanField(default=True)
group_write = models.BooleanField(default=True)
other_read = models.BooleanField(default=True)
other_write = models.BooleanField(default=True)
def can_read(self, user=None, group=None):
return True
def can_write(self, user=None, group=None):
return True
def add_revision(self, new_revision, save=True): def add_revision(self, new_revision, save=True):
""" """
Sets the properties of a revision and ensures its the current Sets the properties of a revision and ensures its the current
revision. revision.
""" """
assert self.id or save, ('Article.add_revision: Sorry, you cannot add a'
'revision to an article that has not been saved '
'without using save=True')
revisions = self.articlerevision_set.all() revisions = self.articlerevision_set.all()
try: try:
new_revision.revision_number = revisions.latest().revision_number + 1 new_revision.revision_number = revisions.latest().revision_number + 1
except ArticleRevision.DoesNotExist: except ArticleRevision.DoesNotExist:
new_revision.revision_number = 0 new_revision.revision_number = 0
new_revision.article = self new_revision.article = self
new_revision.save() if save: new_revision.save()
self.current_revision = new_revision self.current_revision = new_revision
self.save() if save: self.save()
def add_object_relation(self, obj):
content_type = ContentType.objects.get_for_model(obj)
rel = ObjectForArticle.objects.get_or_create(article=self,
content_type=content_type,
object_pk=obj.pk,)
return rel
class Meta:
app_label = settings.APP_LABEL
class ObjectForArticle(models.Model):
article = models.ForeignKey('Article')
# Same as django.contrib.comments
content_type = models.ForeignKey(ContentType,
verbose_name=_('content type'),
related_name="content_type_set_for_%(class)s")
object_pk = models.TextField(_('object ID'))
content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
class Meta:
app_label = settings.APP_LABEL
class ArticleRevision(models.Model): class ArticleRevision(models.Model):
article = models.ForeignKey('Article', on_delete=models.CASCADE,) article = models.ForeignKey('Article', on_delete=models.CASCADE,)
...@@ -37,4 +96,19 @@ class ArticleRevision(models.Model): ...@@ -37,4 +96,19 @@ class ArticleRevision(models.Model):
# way, we can redirects and still maintain old content. # way, we can redirects and still maintain old content.
redirect = models.ForeignKey('Article', null=True, blank=True, redirect = models.ForeignKey('Article', null=True, blank=True,
verbose_name=_(u'redirect'), verbose_name=_(u'redirect'),
help_text=_(u'If set, the article will redirect to the contents of another article.')) help_text=_(u'If set, the article will redirect to the contents of another article.'),
\ No newline at end of file related_name='redirect_set')
# User details
ip_address = models.IPAddressField(_('IP address'), blank=True, null=True)
user = models.ForeignKey(User, verbose_name=_('user'),
blank=True, null=True)
# Various stuff
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
class Meta:
app_label = settings.APP_LABEL
get_latest_by = ('id',)
\ No newline at end of file
...@@ -13,8 +13,8 @@ class URLPath(MPTTModel): ...@@ -13,8 +13,8 @@ class URLPath(MPTTModel):
Strategy: Very few fields go here, as most has to be managed through an Strategy: Very few fields go here, as most has to be managed through an
article's revision. As a side-effect, the URL resolution remains slim and swift. article's revision. As a side-effect, the URL resolution remains slim and swift.
""" """
article = models.ForeignKey('Article', article = models.ForeignKey('wiki.Article',
verbosen_name=_(u'article'), verbose_name=_(u'article'),
help_text=_(u'Article to be displayed for this path')) help_text=_(u'Article to be displayed for this path'))
slug = models.SlugField(verbose_name=_(u'slug')) slug = models.SlugField(verbose_name=_(u'slug'))
site = models.ForeignKey(Site) site = models.ForeignKey(Site)
...@@ -36,6 +36,7 @@ class URLPath(MPTTModel): ...@@ -36,6 +36,7 @@ class URLPath(MPTTModel):
verbose_name = _(u'URL path') verbose_name = _(u'URL path')
verbose_name_plural = _(u'URL paths') verbose_name_plural = _(u'URL paths')
unique_together = ('site', 'parent', 'slug') unique_together = ('site', 'parent', 'slug')
app_label = settings.APP_LABEL
@classmethod @classmethod
def get_by_path(cls, path): def get_by_path(cls, path):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment