Commit 614c5686 by Saleem Latif

Merge pull request #19 from edx/saleem-latif/WL-322

WL-322: Update django wiki to support multi-tenancy
parents 629fc2f6 36e87060
...@@ -29,7 +29,7 @@ package_data = dict( ...@@ -29,7 +29,7 @@ package_data = dict(
setup( setup(
name = "django-wiki", name = "django-wiki",
version = "0.0.5", version = "0.0.6",
author = "Benjamin Bach", author = "Benjamin Bach",
author_email = "benjamin@overtag.dk", author_email = "benjamin@overtag.dk",
description = ("A wiki system written for the Django framework."), description = ("A wiki system written for the Django framework."),
......
...@@ -69,6 +69,7 @@ MIDDLEWARE_CLASSES = ( ...@@ -69,6 +69,7 @@ MIDDLEWARE_CLASSES = (
'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'wiki.middleware.RequestCache',
# Uncomment the next line for simple clickjacking protection: # Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware', # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
) )
......
from middleware import get_current_request
import threading
import importlib
from django.conf import settings
if getattr(settings, "WIKI_REQUEST_CACHE_MIDDLEWARE_CLASS", None):
class_name = settings.WIKI_REQUEST_CACHE_MIDDLEWARE_CLASS.split(".")[-1]
module = ".".join(settings.WIKI_REQUEST_CACHE_MIDDLEWARE_CLASS.split('.')[:-1])
RequestCache = getattr(importlib.import_module(module), class_name)
else:
class _RequestCache(threading.local):
"""
A thread-local for storing the per-request cache.
"""
def __init__(self):
super(_RequestCache, self).__init__()
self.data = {}
self.request = None
REQUEST_CACHE = _RequestCache()
class RequestCache(object):
@classmethod
def get_request_cache(cls, name=None):
"""
This method is deprecated. Please use :func:`request_cache.get_cache`.
"""
if name is None:
return REQUEST_CACHE
else:
return REQUEST_CACHE.data.setdefault(name, {})
@classmethod
def get_current_request(cls):
"""
This method is deprecated. Please use :func:`request_cache.get_request`.
"""
return REQUEST_CACHE.request
@classmethod
def clear_request_cache(cls):
"""
Empty the request cache.
"""
REQUEST_CACHE.data = {}
REQUEST_CACHE.request = None
def process_request(self, request):
self.clear_request_cache()
REQUEST_CACHE.request = request
return None
def process_response(self, request, response):
self.clear_request_cache()
return response
def get_current_request():
"""Return the request associated with the current thread."""
return RequestCache.get_current_request()
...@@ -4,6 +4,7 @@ import logging ...@@ -4,6 +4,7 @@ import logging
from django.contrib.contenttypes import fields from django.contrib.contenttypes import fields
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.db import models, transaction from django.db import models, transaction
...@@ -14,6 +15,7 @@ from mptt.fields import TreeForeignKey ...@@ -14,6 +15,7 @@ from mptt.fields import TreeForeignKey
from mptt.models import MPTTModel from mptt.models import MPTTModel
from wiki import managers from wiki import managers
from wiki import get_current_request
from wiki.conf import settings from wiki.conf import settings
from wiki.core.exceptions import NoRootURL, MultipleRootURLs from wiki.core.exceptions import NoRootURL, MultipleRootURLs
from wiki.models.article import ArticleRevision, ArticleForObject, Article from wiki.models.article import ArticleRevision, ArticleForObject, Article
...@@ -110,7 +112,7 @@ class URLPath(MPTTModel): ...@@ -110,7 +112,7 @@ class URLPath(MPTTModel):
@classmethod @classmethod
def root(cls): def root(cls):
site = Site.objects.get_current() site = get_current_site(get_current_request())
root_nodes = list( root_nodes = list(
cls.objects.root_nodes().filter(site=site).select_related_common() cls.objects.root_nodes().filter(site=site).select_related_common()
) )
...@@ -192,7 +194,7 @@ class URLPath(MPTTModel): ...@@ -192,7 +194,7 @@ class URLPath(MPTTModel):
@classmethod @classmethod
def create_root(cls, site=None, title="Root", **kwargs): def create_root(cls, site=None, title="Root", **kwargs):
if not site: site = Site.objects.get_current() if not site: site = get_current_site(get_current_request())
root_nodes = cls.objects.root_nodes().filter(site=site) root_nodes = cls.objects.root_nodes().filter(site=site)
if not root_nodes: if not root_nodes:
# (get_or_create does not work for MPTT models??) # (get_or_create does not work for MPTT models??)
...@@ -210,7 +212,7 @@ class URLPath(MPTTModel): ...@@ -210,7 +212,7 @@ class URLPath(MPTTModel):
def create_article(cls, parent, slug, site=None, title="Root", article_kwargs={}, **kwargs): def create_article(cls, parent, slug, site=None, title="Root", article_kwargs={}, **kwargs):
"""Utility function: """Utility function:
Create a new urlpath with an article and a new revision for the article""" Create a new urlpath with an article and a new revision for the article"""
if not site: site = Site.objects.get_current() if not site: site = get_current_site(get_current_request())
article = Article(**article_kwargs) article = Article(**article_kwargs)
article.add_revision(ArticleRevision(title=title, **kwargs), article.add_revision(ArticleRevision(title=title, **kwargs),
save=True) save=True)
...@@ -241,8 +243,8 @@ post_save.connect(on_article_relation_save, ArticleForObject) ...@@ -241,8 +243,8 @@ post_save.connect(on_article_relation_save, ArticleForObject)
def on_article_delete(instance, *args, **kwargs): def on_article_delete(instance, *args, **kwargs):
# If an article is deleted, then throw out its URLPaths # If an article is deleted, then throw out its URLPaths
# But move all descendants to a lost-and-found node. # But move all descendants to a lost-and-found node.
site = Site.objects.get_current() site = get_current_site(get_current_request())
# Get the Lost-and-found path or create a new one # Get the Lost-and-found path or create a new one
try: try:
lost_and_found = URLPath.objects.get(slug=settings.LOST_AND_FOUND_SLUG, lost_and_found = URLPath.objects.get(slug=settings.LOST_AND_FOUND_SLUG,
......
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