Skip to content

Commit

Permalink
Merge pull request #474 from spookylukey/fix_deprecation_warnings
Browse files Browse the repository at this point in the history
Fix all the deprecation warnings on Django 1.7/1.8
  • Loading branch information
benjaoming committed Oct 22, 2015
2 parents 4622bcd + 92a9e88 commit b1fba36
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 66 deletions.
50 changes: 37 additions & 13 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import django
from django.conf import settings

settings.configure(

settings_dict = dict(
DEBUG=True,
AUTH_USER_MODEL='testdata.CustomUser',
DATABASES={
Expand Down Expand Up @@ -40,22 +41,45 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
],
TEMPLATE_CONTEXT_PROCESSORS=(
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"sekizai.context_processors.sekizai",
),
USE_TZ=True,
SOUTH_TESTS_MIGRATE=True,
SECRET_KEY = 'b^fv_)t39h%9p40)fnkfblo##jkr!$0)lkp6bpy!fi*f$4*92!',
SECRET_KEY='b^fv_)t39h%9p40)fnkfblo##jkr!$0)lkp6bpy!fi*f$4*92!',
)

TEMPLATE_CONTEXT_PROCESSORS = [
"django.contrib.auth.context_processors.auth",
"django.template.context_processors.debug",
"django.template.context_processors.i18n",
"django.template.context_processors.media",
"django.template.context_processors.request",
"django.template.context_processors.static",
"django.template.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"sekizai.context_processors.sekizai",
]

if django.VERSION < (1, 8):
settings_dict.update(dict(
TEMPLATE_CONTEXT_PROCESSORS=[p.replace('django.template.context_processors',
'django.core.context_processors')
for p in TEMPLATE_CONTEXT_PROCESSORS]
))
else:
settings_dict.update(dict(
TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'OPTIONS': {
'context_processors': TEMPLATE_CONTEXT_PROCESSORS
},
},
]
))

settings.configure(**settings_dict)


# If you use South for migrations, uncomment this to monkeypatch
# syncdb to get migrations to run.
if django.VERSION < (1, 7):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ deps =
django-mptt==0.7.2
git+https://github.com/mariocesar/sorl-thumbnail@007156ba3428ce02a92dcd9cce03a4803225ea66#egg=sorl.thumbnail
six==1.6.1
django_nyt==0.9.7.2
django_nyt==0.9.8
https://github.com/ojii/django-sekizai/archive/master.zip#egg=django-sekizai-0.8.1
django15: Django==1.5.12
django16: Django==1.6.11
Expand Down
17 changes: 17 additions & 0 deletions wiki/core/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,20 @@ def nop_decorator(func):
except AttributeError:
atomic = nop_decorator
transaction_commit_on_success = transaction.commit_on_success


if DJANGO_VERSION < (1, 8):
from django.template.loader import render_to_string as django_render_to_string
from django.template import Context, RequestContext

# Similar to 1.8 signature, but with things we can support
# under 1.7 and less.
def render_to_string(template_name, context=None, request=None):
if request is not None:
context_instance = RequestContext(request, context)
else:
context_instance = Context(context)
return django_render_to_string(template_name,
context_instance=context_instance)
else:
from django.template.loader import render_to_string
8 changes: 7 additions & 1 deletion wiki/core/plugins/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals

try:
from importlib import import_module
except ImportError:
# Python 2.6 fallback
from django.utils.importlib import import_module

from django.conf import settings
from django.utils.importlib import import_module


def get_module(app, modname, verbose, failfast):
Expand Down
10 changes: 8 additions & 2 deletions wiki/core/plugins/registry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
# -*- coding: utf-8 -*-
from django.utils.importlib import import_module

try:
from importlib import import_module
except ImportError:
# Python 2.6 fallback
from django.utils.importlib import import_module

from six import string_types

_cache = {}
Expand Down
9 changes: 7 additions & 2 deletions wiki/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
# -*- coding: utf-8 -*-
from django.utils.importlib import import_module

try:
from importlib import import_module
except ImportError:
# Python 2.6 fallback
from django.utils.importlib import import_module


def get_class_from_str(class_path):
Expand Down
16 changes: 8 additions & 8 deletions wiki/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

from django.shortcuts import redirect, get_object_or_404
from django.template.context import RequestContext
from django.template.loader import render_to_string
try:
import json
except ImportError:
from django.utils import simplejson as json

from wiki.core.compat import render_to_string
from wiki.core.exceptions import NoRootURL

from wiki.conf import settings
Expand Down Expand Up @@ -46,12 +46,12 @@ def response_forbidden(request, article, urlpath):
qs = ''
return redirect(settings.LOGIN_URL + "?next=" + request.path + qs)
else:
c = RequestContext(request, {'article': article,
'urlpath': urlpath})
return HttpResponseForbidden(
render_to_string(
"wiki/permission_denied.html",
context_instance=c))
context={'article': article,
'urlpath': urlpath},
request=request))


def get_article(func=None, can_read=True, can_write=False,
Expand Down Expand Up @@ -111,13 +111,13 @@ def wrapper(request, *args, **kwargs):
"wiki:create", kwargs={'path': parent.path, }) +
"?slug=%s" % pathlist[-1])
except models.URLPath.DoesNotExist:
c = RequestContext(
request, {
'error_type': 'ancestors_missing'})
return HttpResponseNotFound(
render_to_string(
"wiki/error.html",
context_instance=c))
context={
'error_type': 'ancestors_missing'
},
request=request))
if urlpath.article:
# urlpath is already smart about prefetching items on article
# (like current_revision), so we don't have to
Expand Down
8 changes: 4 additions & 4 deletions wiki/plugins/attachments/markdown_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import re

from django.core.urlresolvers import reverse
from django.template.context import Context
from django.template.loader import render_to_string
from django.contrib.auth.models import AnonymousUser

from wiki.core.compat import render_to_string
from wiki.core.permissions import can_read

ATTACHMENT_RE = re.compile(
Expand Down Expand Up @@ -66,11 +66,11 @@ def run(self, lines):
self.markdown.article, article_owner)
html = render_to_string(
"wiki/plugins/attachments/render.html",
Context({
context={
'url': url,
'filename': attachment.original_filename,
'attachment_can_read': attachment_can_read,
}))
})
line = self.markdown.htmlStash.store(html, safe=True)
except models.Attachment.DoesNotExist:
html = """<span class="attachment attachment-deleted">Attachment with ID #%s is deleted.</span>""" % attachment_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h4 style="margin-top:0;">
{% if attachment|can_write:user %}
{% if not attachment.current_revision.deleted %}
<a href="{% url 'wiki:attachments_replace' path=urlpath.path article_id=article.id attachment_id=attachment.id %}" class="btn btn-default btn-sm">{% trans "Replace" %}</a>
{% if attachment.article = article %}
{% if attachment.article == article %}
<a href="{% url 'wiki:attachments_delete' path=urlpath.path article_id=article.id attachment_id=attachment.id %}" class="btn btn-default btn-sm">{% trans "Delete" %}</a>
{% else %}
<a href="{% url 'wiki:attachments_delete' path=urlpath.path article_id=article.id attachment_id=attachment.id %}" class="btn btn-default btn-sm">{% trans "Detach" %}</a>
Expand Down
6 changes: 3 additions & 3 deletions wiki/plugins/attachments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def form_valid(self, form):
article_id=self.article.id)

def get_form_kwargs(self):
kwargs = FormView.get_form_kwargs(self)
kwargs = super(AttachmentView, self).get_form_kwargs()
kwargs['article'] = self.article
kwargs['request'] = self.request
return kwargs
Expand Down Expand Up @@ -204,8 +204,8 @@ def form_valid(self, form):
path=self.urlpath.path,
article_id=self.article.id)

def get_form(self, form_class):
form = FormView.get_form(self, form_class)
def get_form(self, form_class=None):
form = super(AttachmentReplaceView, self).get_form(form_class=form_class)
form.fields['file'].help_text = _(
'Your new file will automatically be renamed to match the file already present. Files with different extensions are not allowed.')
return form
Expand Down
14 changes: 8 additions & 6 deletions wiki/plugins/images/markdown_extensions.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
# -*- coding: utf-8 -*-

import markdown
import re

from django.template.loader import render_to_string
from django.template import Context

IMAGE_RE = re.compile(
r'.*(\[image\:(?P<id>\d+)(\s+align\:(?P<align>right|left))?\s*\]).*',
re.IGNORECASE)

from wiki.core.compat import render_to_string
from wiki.plugins.images import models


Expand Down Expand Up @@ -61,9 +61,11 @@ def run(self, lines):
caption_placeholder = "{{{IMAGECAPTION}}}"
html = render_to_string(
"wiki/plugins/images/render.html",
Context(
{'image': image, 'caption': caption_placeholder,
'align': alignment}))
context={
'image': image,
'caption': caption_placeholder,
'align': alignment,
})
html_before, html_after = html.split(caption_placeholder)
placeholder_before = self.markdown.htmlStash.store(
html_before,
Expand Down
10 changes: 5 additions & 5 deletions wiki/plugins/macros/mdx/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from six import string_types

from django.utils.translation import ugettext as _
from django.template.loader import render_to_string
from django.template import Context

# See:
# http://stackoverflow.com/questions/430759/regex-for-managing-escaped-characters-for-items-like-string-literals
Expand All @@ -21,6 +19,7 @@
re_sq_short,
re.IGNORECASE)

from wiki.core.compat import render_to_string
from wiki.plugins.macros import settings


Expand Down Expand Up @@ -75,10 +74,11 @@ def run(self, lines):
def article_list(self, depth="2"):
html = render_to_string(
"wiki/plugins/macros/article_list.html",
Context(
{'article_children': self.markdown.article.get_children(
context={
'article_children': self.markdown.article.get_children(
article__current_revision__deleted=False),
'depth': int(depth) + 1, }))
'depth': int(depth) + 1,
})
return self.markdown.htmlStash.store(html, safe=True)
article_list.meta = dict(
short_description=_('Article list'),
Expand Down
15 changes: 12 additions & 3 deletions wiki/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from django.test import TestCase
from django.test.client import Client
from django.template import Context, Template
from django.test.utils import override_settings
try:
from django.test import override_settings
except ImportError:
from django.test.utils import override_settings

from wiki.models import URLPath

Expand Down Expand Up @@ -80,9 +83,15 @@ def render(self, context):
# https://github.com/django-wiki/django-wiki/pull/382
class wiki_override_settings(override_settings):

def __enter__(self):
super(wiki_override_settings, self).__enter__()
def enable(self):
super(wiki_override_settings, self).enable()
self.reload_wiki_settings()

def disable(self):
super(wiki_override_settings, self).disable()
self.reload_wiki_settings()

def reload_wiki_settings(self):
from imp import reload
from wiki.conf import settings
reload(settings)
14 changes: 7 additions & 7 deletions wiki/tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ def get_article_path_urls(self):

if DJANGO_VERSION < (1, 8):
urlpatterns = patterns('', *urlpatterns)



@wiki_override_settings(WIKI_URL_CONFIG_CLASS='wiki.tests.test_models.WikiCustomUrlPatterns',
ROOT_URLCONF='wiki.tests.test_urls')
class ArticleModelReverseMethodTest(TestCase):
urls = 'wiki.tests.test_urls'

@wiki_override_settings(WIKI_URL_CONFIG_CLASS='wiki.tests.test_models.WikiCustomUrlPatterns')

if DJANGO_VERSION < (1, 7):
urls = 'wiki.tests.test_urls'

def test_get_absolute_url_if_urlpath_set_is_not_exists__no_root_urlconf(self):

a = Article.objects.create()

url = a.get_absolute_url()
Expand All @@ -64,7 +65,6 @@ def test_get_absolute_url_if_urlpath_set_is_not_exists__no_root_urlconf(self):

self.assertEqual(url, expected)

@wiki_override_settings(WIKI_URL_CONFIG_CLASS='wiki.tests.test_models.WikiCustomUrlPatterns')
def test_get_absolute_url_if_urlpath_set_is_exists__no_root_urlconf(self):

a1 = Article.objects.create()
Expand Down
4 changes: 2 additions & 2 deletions wiki/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class WikiURLPatterns(object):
article_source_view_class = article.Source
article_plugin_view_class = article.Plugin
revision_change_view = article.ChangeRevisionView
revision_merge_view = 'wiki.views.article.merge'
revision_merge_view = article.merge

search_view_class = settings.SEARCH_VIEW
article_diff_view = 'wiki.views.article.diff'
article_diff_view = article.diff

# account views
signup_view_class = accounts.Signup
Expand Down

0 comments on commit b1fba36

Please sign in to comment.