Commit 30fb1eca by benjaoming

Begging to implement models

parent 330c772d
......@@ -44,4 +44,11 @@ So far the dependencies are:
* [django=>1.4](http://www.djangoproject.com)
* [django-south](http://south.aeracode.org/)
* [Markdown>=2.2.0](https://github.com/waylan/Python-Markdown)
* [django-mptt>=0.5](https://github.com/django-mptt/django-mptt)
Acknowledgements
----------------
* The people at [edX](http://www.edxonline.org/) & MIT for finding and supporting the project both financially and with ideas.
* [django-cms](https://github.com/divio/django-cms) for venturing where no django app has gone before in terms of well-planned features and high standards. It's a very big inspiration.
......@@ -8,6 +8,5 @@ from wiki.urls import get_pattern as wiki_pattern
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^wiki1', include(wiki_pattern())),
url(r'^wiki2', include(wiki_pattern())),
url(r'', include(wiki_pattern())),
)
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from django.conf import settings as django_settings
# Should urls be case sensitive?
URL_CASE_SENSITIVE = getattr(django_settings, "WIKI_URL_CASE_SENSITIVE", False)
\ No newline at end of file
# If no root URL is found, we raise this...
class NoRootURL(Exception):
pass
# If there is more than one...
class MultipleRootURLs(Exception):
pass
\ No newline at end of file
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
import warnings
from urlpath import URLPath
from article import Article, ArticleRevision
######################
# Configuration stuff
######################
if not 'mptt' in settings.INSTALLED_APPS:
raise ImproperlyConfigured('django-wiki: needs mptt in INSTALLED_APPS')
######################
# Warnings
######################
if not 'south' in settings.INSTALLED_APPS:
warnings.warn("You do not have south in your INSTALLED_APPS. This is highly discouraged.")
\ No newline at end of file
warnings.warn("django-wiki: No south in your INSTALLED_APPS. This is highly discouraged.")
\ No newline at end of file
# -*- coding: utf-8 -*-
from django.db import models
from django.utils.translation import ugettext_lazy as _
class Article(models.Model):
title = models.CharField(max_length=512, verbose_name=_(u'title'))
current_revision = models.ForeignKey('ArticleRevision')
def add_revision(self, new_revision, save=True):
"""
Sets the properties of a revision and ensures its the current
revision.
"""
revisions = self.articlerevision_set.all()
try:
new_revision.revision_number = revisions.latest().revision_number + 1
except ArticleRevision.DoesNotExist:
new_revision.revision_number = 0
new_revision.article = self
new_revision.save()
self.current_revision = new_revision
self.save()
class ArticleRevision(models.Model):
article = models.ForeignKey('Article', on_delete=models.CASCADE,)
revision_number = models.IntegerField()
# This is where the content goes, with whatever markup language is used
content = models.TextField(blank=True)
# Simple properties
deleted = models.BooleanField(verbose_name=_(u'Article has been deleted'))
# Allow a revision to redirect to another *article*. This
# way, we can redirects and still maintain old content.
redirect = models.ForeignKey('Article', null=True, blank=True,
verbose_name=_(u'redirect'),
help_text=_(u'If set, the article will redirect to the contents of another article.'))
\ No newline at end of file
# -*- coding: utf-8 -*-
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.sites.models import Site
from mptt.models import MPTTModel
from mptt.fields import TreeForeignKey
from wiki.core.exceptions import NoRootURL, MultipleRootURLs
from wiki.conf import settings
class URLPath(MPTTModel):
"""
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 = models.ForeignKey('Article',
verbosen_name=_(u'article'),
help_text=_(u'Article to be displayed for this path'))
slug = models.SlugField(verbose_name=_(u'slug'))
site = models.ForeignKey(Site)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
def get_path(self):
"/".join([obj.slug for obj in self.get_ancestors(include_self=True)])
class MPTTMeta:
order_insertion_by = ['slug']
def __unicode__(self):
return self.path
def save(self, *args, **kwargs):
super(URLPath, self).save(*args, **kwargs)
class Meta:
verbose_name = _(u'URL path')
verbose_name_plural = _(u'URL paths')
unique_together = ('site', 'parent', 'slug')
@classmethod
def get_by_path(cls, path):
"""
Strategy: Don't handle all kinds of weird cases. Be strict.
Accepts paths both starting with and without '/'
"""
site = Site.objects.get_current()
paths = cls.objects.filter(site=site)
root_nodes = paths.filter(parent=None)
path = path.lstrip("/")
# Root page requested
if not path:
no_paths = root_nodes.count()
if no_paths == 0:
raise NoRootURL
if no_paths > 1:
raise MultipleRootURLs
return root_nodes[0]
slugs = path.split('/')
for slug in slugs:
if settings.URL_CASE_SENSITIVE:
paths = paths.filter(parent__slug=slug)
else:
paths = paths.filter(parent__slug__iexact=slug)
if paths.count() == 0:
raise cls.DoesNotExist
return paths[0]
\ No newline at end of file
# -*- coding: utf-8 -*-
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('',
......
# Create your views here.
# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template.context import RequestContext
......
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