Commit 2a9a15ad by Bridger Maxwell

Beginning to port wiki to multicourse. (Unstable)

parent ef3afda2
...@@ -16,9 +16,13 @@ from util.cache import cache ...@@ -16,9 +16,13 @@ from util.cache import cache
class ShouldHaveExactlyOneRootSlug(Exception): class ShouldHaveExactlyOneRootSlug(Exception):
pass pass
class Namespace(models.Model):
name = models.CharField(max_length=30, verbose_name=_('namespace'))
# TODO: We may want to add permissions, etc later
class Article(models.Model): class Article(models.Model):
"""Wiki article referring to Revision model for actual content. """Wiki article referring to Revision model for actual content.
'slug' and 'parent' field should be maintained centrally, since users 'slug' and 'title' field should be maintained centrally, since users
aren't allowed to change them, anyways. aren't allowed to change them, anyways.
""" """
...@@ -27,12 +31,10 @@ class Article(models.Model): ...@@ -27,12 +31,10 @@ class Article(models.Model):
slug = models.SlugField(max_length=100, verbose_name=_('slug'), slug = models.SlugField(max_length=100, verbose_name=_('slug'),
help_text=_('Letters, numbers, underscore and hyphen.'), help_text=_('Letters, numbers, underscore and hyphen.'),
blank=True) blank=True)
namespace = models.ForeignKey(Namespace, verbose_name=_('Namespace'))
created_by = models.ForeignKey(User, verbose_name=_('Created by'), blank=True, null=True) created_by = models.ForeignKey(User, verbose_name=_('Created by'), blank=True, null=True)
created_on = models.DateTimeField(auto_now_add = 1) created_on = models.DateTimeField(auto_now_add = 1)
modified_on = models.DateTimeField(auto_now_add = 1) modified_on = models.DateTimeField(auto_now_add = 1)
parent = models.ForeignKey('self', verbose_name=_('Parent article slug'),
help_text=_('Affects URL structure and possibly inherits permissions'),
null=True, blank=True)
locked = models.BooleanField(default=False, verbose_name=_('Locked for editing')) locked = models.BooleanField(default=False, verbose_name=_('Locked for editing'))
permissions = models.ForeignKey('Permission', verbose_name=_('Permissions'), permissions = models.ForeignKey('Permission', verbose_name=_('Permissions'),
blank=True, null=True, blank=True, null=True,
...@@ -56,42 +58,26 @@ class Article(models.Model): ...@@ -56,42 +58,26 @@ class Article(models.Model):
except: except:
raise ShouldHaveExactlyOneRootSlug() raise ShouldHaveExactlyOneRootSlug()
def get_url(self):
"""Return the Wiki URL for an article"""
url = self.slug + "/"
if self.parent_id:
parent_url = cache.get("wiki_url-" + str(self.parent_id))
if parent_url is None:
parent_url = self.parent.get_url()
url = parent_url + url
cache.set("wiki_url-" + str(self.id), url, 60*60)
return url
def get_abs_url(self):
"""Return the absolute path for an article. This is necessary in cases
where the template system isn't used for generating URLs..."""
# TODO: Remove and create a reverse() lookup.
return WIKI_BASE + self.get_url()
@models.permalink @models.permalink
def get_absolute_url(self): def get_absolute_url(self):
return ('wiki_view', [self.get_url()]) return ('wiki_view', [self.slug])
@classmethod def get_full_slug(self):
def get_url_reverse(cls, path, article, return_list=[]): # TODO: Return namespace : slug
"""Lookup a URL and return the corresponding set of articles return self.slug
in the path."""
if path == []: # @classmethod
return return_list + [article] # def get_url_reverse(cls, path, article, return_list=[]):
# Lookup next child in path # """Lookup a URL and return the corresponding set of articles
try: # in the path."""
a = Article.objects.get(parent__exact = article, slug__exact=str(path[0])) # if path == []:
return cls.get_url_reverse(path[1:], a, return_list+[article]) # return return_list + [article]
except Exception, e: # # Lookup next child in path
return None # try:
# a = Article.objects.get(parent__exact = article, slug__exact=str(path[0]))
# return cls.get_url_reverse(path[1:], a, return_list+[article])
# except Exception, e:
# return None
def can_read(self, user): def can_read(self, user):
""" Check read permissions and return True/False.""" """ Check read permissions and return True/False."""
...@@ -101,7 +87,8 @@ class Article(models.Model): ...@@ -101,7 +87,8 @@ class Article(models.Model):
perms = self.permissions.can_read.all() perms = self.permissions.can_read.all()
return perms.count() == 0 or (user in perms) return perms.count() == 0 or (user in perms)
else: else:
return self.parent.can_read(user) if self.parent else True # TODO: We can inherit namespace permissions here
return True
def can_write(self, user): def can_write(self, user):
""" Check write permissions and return True/False.""" """ Check write permissions and return True/False."""
...@@ -111,7 +98,8 @@ class Article(models.Model): ...@@ -111,7 +98,8 @@ class Article(models.Model):
perms = self.permissions.can_write.all() perms = self.permissions.can_write.all()
return perms.count() == 0 or (user in perms) return perms.count() == 0 or (user in perms)
else: else:
return self.parent.can_write(user) if self.parent else True # TODO: We can inherit namespace permissions here
return True
def can_write_l(self, user): def can_write_l(self, user):
"""Check write permissions and locked status""" """Check write permissions and locked status"""
...@@ -123,13 +111,13 @@ class Article(models.Model): ...@@ -123,13 +111,13 @@ class Article(models.Model):
return self.can_write_l(user) and (WIKI_ALLOW_ANON_ATTACHMENTS or not user.is_anonymous()) return self.can_write_l(user) and (WIKI_ALLOW_ANON_ATTACHMENTS or not user.is_anonymous())
def __unicode__(self): def __unicode__(self):
if self.slug == '' and not self.parent: if self.slug == '':
return unicode(_('Root article')) return unicode(_('Root article'))
else: else:
return self.get_url() return self.slug
class Meta: class Meta:
unique_together = (('slug', 'parent'),) unique_together = (('slug', 'namespace'),)
verbose_name = _('Article') verbose_name = _('Article')
verbose_name_plural = _('Articles') verbose_name_plural = _('Articles')
......
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