Commit 6af3dcb8 by benjaoming

Merge branch 'master' of github.com:benjaoming/django-wiki

parents 5fe706cf 8cd2bb7b
...@@ -78,14 +78,14 @@ def get_article(func=None, can_read=True, can_write=False, deleted_contents=Fals ...@@ -78,14 +78,14 @@ def get_article(func=None, can_read=True, can_write=False, deleted_contents=Fals
return redirect(return_url) return redirect(return_url)
if can_read and not article.can_read(request.user): if can_read and not article.can_read(request.user):
if request.user.is_anonymous: if request.user.is_anonymous():
return redirect(django_settings.LOGIN_URL) return redirect(django_settings.LOGIN_URL)
else: else:
pass pass
# TODO: Return a permission denied page # TODO: Return a permission denied page
if can_write and not article.can_write(request.user): if can_write and not article.can_write(request.user):
if request.user.is_anonymous: if request.user.is_anonymous():
return redirect(django_settings.LOGIN_URL) return redirect(django_settings.LOGIN_URL)
else: else:
pass pass
......
...@@ -56,7 +56,13 @@ original_django_reverse = urlresolvers.reverse ...@@ -56,7 +56,13 @@ original_django_reverse = urlresolvers.reverse
def reverse(*args, **kwargs): def reverse(*args, **kwargs):
"""Now this is a crazy and silly hack, but it is basically here to """Now this is a crazy and silly hack, but it is basically here to
enforce that an empty path always takes precedence over an article_id enforce that an empty path always takes precedence over an article_id
such that the root article doesn't get resolved to /ID/ but /.""" such that the root article doesn't get resolved to /ID/ but /.
Another crazy hack that this supports is transforming every wiki url
by a function. If _transform_url is set on this function, it will
return the result of calling reverse._transform_url(reversed_url)
for every url in the wiki namespace.
"""
if args[0].startswith('wiki:'): if args[0].startswith('wiki:'):
url_kwargs = kwargs.get('kwargs', {}) url_kwargs = kwargs.get('kwargs', {})
path = url_kwargs.get('path', False) path = url_kwargs.get('path', False)
...@@ -65,8 +71,14 @@ def reverse(*args, **kwargs): ...@@ -65,8 +71,14 @@ def reverse(*args, **kwargs):
url_kwargs.pop('article_id', None) url_kwargs.pop('article_id', None)
url_kwargs['path'] = path url_kwargs['path'] = path
kwargs['kwargs'] = url_kwargs kwargs['kwargs'] = url_kwargs
url = original_django_reverse(*args, **kwargs)
if hasattr(reverse, '_transform_url'):
url = reverse._transform_url(url)
else:
url = original_django_reverse(*args, **kwargs)
return original_django_reverse(*args, **kwargs) return url
# Now we redefine reverse method # Now we redefine reverse method
urlresolvers.reverse = reverse urlresolvers.reverse = reverse
\ No newline at end of file
...@@ -39,7 +39,8 @@ class Article(models.Model): ...@@ -39,7 +39,8 @@ class Article(models.Model):
other_write = models.BooleanField(default=True, verbose_name=_(u'others write access')) other_write = models.BooleanField(default=True, verbose_name=_(u'others write access'))
def can_read(self, user=None, group=None): def can_read(self, user=None, group=None):
if self.other_read: is_other = (user and not user.is_anonymous() ) or settings.ANONYMOUS
if is_other and self.other_read:
return True return True
if user == self.owner: if user == self.owner:
return True return True
...@@ -53,7 +54,8 @@ class Article(models.Model): ...@@ -53,7 +54,8 @@ class Article(models.Model):
return False return False
def can_write(self, user=None, group=None): def can_write(self, user=None, group=None):
if self.other_write: is_other = (user and not user.is_anonymous() ) or settings.ANONYMOUS
if is_other and self.other_write:
return True return True
if user == self.owner: if user == self.owner:
return True return True
......
...@@ -32,13 +32,17 @@ class URLPath(MPTTModel): ...@@ -32,13 +32,17 @@ class URLPath(MPTTModel):
@property @property
def path(self): def path(self):
if not self.parent: return "" if not self.parent: return ""
return "/".join([obj.slug if obj.slug else "" for obj in self.get_ancestors(include_self=True).exclude(parent=None)]) + "/" if not hasattr(self, '_cachedpath'):
self._cachedpath = "/".join([obj.slug if obj.slug else "" for obj in self.get_ancestors(include_self=True).exclude(parent=None)]) + "/"
return self._cachedpath
@classmethod @classmethod
def root(cls): def root(cls):
site = Site.objects.get_current() site = Site.objects.get_current()
root_nodes = cls.objects.root_nodes().filter(site=site) root_nodes = list(cls.objects.root_nodes().filter(site=site))
no_paths = root_nodes.count() # We fetch the nodes as a list and use len(), not count() because we need
# to get the result out anyway. This only takes one sql query
no_paths = len(root_nodes)
if no_paths == 0: if no_paths == 0:
raise NoRootURL("You need to create a root article on site '%s'" % site) raise NoRootURL("You need to create a root article on site '%s'" % site)
if no_paths > 1: if no_paths > 1:
......
...@@ -35,7 +35,7 @@ class AttachmentView(ArticleMixin, FormView): ...@@ -35,7 +35,7 @@ class AttachmentView(ArticleMixin, FormView):
# WARNING! The below decorator silences other exceptions that may occur! # WARNING! The below decorator silences other exceptions that may occur!
#@transaction.commit_manually #@transaction.commit_manually
def form_valid(self, form): def form_valid(self, form):
if self.request.user.is_anonymous and not settings.ANONYMOUS: if self.request.user.is_anonymous() and not settings.ANONYMOUS:
return redirect(django_settings.LOGIN_URL) return redirect(django_settings.LOGIN_URL)
try: try:
...@@ -63,7 +63,7 @@ class AttachmentView(ArticleMixin, FormView): ...@@ -63,7 +63,7 @@ class AttachmentView(ArticleMixin, FormView):
kwargs['attachments'] = self.attachments kwargs['attachments'] = self.attachments
kwargs['search_form'] = forms.SearchForm() kwargs['search_form'] = forms.SearchForm()
kwargs['selected_tab'] = 'attachments' kwargs['selected_tab'] = 'attachments'
kwargs['anonymous_disallowed'] = self.request.user.is_anonymous and not settings.ANONYMOUS kwargs['anonymous_disallowed'] = self.request.user.is_anonymous() and not settings.ANONYMOUS
return super(AttachmentView, self).get_context_data(**kwargs) return super(AttachmentView, self).get_context_data(**kwargs)
......
...@@ -59,7 +59,7 @@ class Create(FormView, ArticleMixin): ...@@ -59,7 +59,7 @@ class Create(FormView, ArticleMixin):
def form_valid(self, form): def form_valid(self, form):
user=None user=None
ip_address = None ip_address = None
if not self.request.user.is_anonymous: if not self.request.user.is_anonymous():
user = self.request.user user = self.request.user
if settings.LOG_IPS_USERS: if settings.LOG_IPS_USERS:
ip_address = self.request.META.get('REMOTE_ADDR', None) ip_address = self.request.META.get('REMOTE_ADDR', None)
......
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