Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
Dropped support for Django < 1.11
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed May 28, 2018
1 parent b93836d commit e94b557
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 112 deletions.
12 changes: 0 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
language: python
env:
- TOXENV=py27-django18
- TOXENV=py34-django18
- TOXENV=py27-django19
- TOXENV=py34-django19
- TOXENV=py27-django110
- TOXENV=py34-django110
- TOXENV=py27-django111
- TOXENV=py34-django111
- TOXENV=py34-django20
matrix:
include:
- python: 3.5
env: TOXENV=py35-django-18
- python: 3.5
env: TOXENV=py35-django-19
- python: 3.5
env: TOXENV=py35-django-110
- python: 3.5
env: TOXENV=py35-django-111
- python: 3.5
Expand Down
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ X.Y.Z (YYYY-MM-DD)

* Added testing for Python 3.6.
* Confirmed support for Django 2.0.
* Dropped support for Django < 1.11.

1.8.0 (2017-02-03)
------------------
Expand Down
5 changes: 1 addition & 4 deletions django_comments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
from django.apps import apps
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse # Django < 1.10
from django.urls import reverse


DEFAULT_COMMENTS_APP = 'django_comments'
Expand Down
9 changes: 2 additions & 7 deletions django_comments/abstracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse # Django < 1.10

from .managers import CommentManager

Expand Down Expand Up @@ -61,9 +58,7 @@ class CommentAbstractModel(BaseCommentAbstractModel):
blank=True, null=True, related_name="%(class)s_comments",
on_delete=models.SET_NULL)
user_name = models.CharField(_("user's name"), max_length=50, blank=True)
# Explicit `max_length` to apply both to Django 1.7 and 1.8+.
user_email = models.EmailField(_("user's email address"), max_length=254,
blank=True)
user_email = models.EmailField(_("user's email address"), blank=True)
user_url = models.URLField(_("user's URL"), blank=True)

comment = models.TextField(_('comment'), max_length=COMMENT_MAX_LENGTH)
Expand Down
8 changes: 2 additions & 6 deletions django_comments/views/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ def post_comment(request, next=None, using=None):
"""
# Fill out some initial data fields from an authenticated user, if present
data = request.POST.copy()
try:
user_is_authenticated = request.user.is_authenticated()
except TypeError: # Django >= 1.11
user_is_authenticated = request.user.is_authenticated
if user_is_authenticated:
if request.user.is_authenticated:
if not data.get('name', ''):
data["name"] = request.user.get_full_name() or request.user.get_username()
if not data.get('email', ''):
Expand Down Expand Up @@ -107,7 +103,7 @@ def post_comment(request, next=None, using=None):
# Otherwise create the comment
comment = form.get_comment_object(site_id=get_current_site(request).id)
comment.ip_address = request.META.get("REMOTE_ADDR", None) or None
if user_is_authenticated:
if request.user.is_authenticated:
comment.user = request.user

# Signal that the comment is about to be saved
Expand Down
2 changes: 1 addition & 1 deletion django_comments/views/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def next_redirect(request, fallback, **get_kwargs):
Returns an ``HttpResponseRedirect``.
"""
next = request.POST.get('next')
if not is_safe_url(url=next, host=request.get_host()):
if not is_safe_url(url=next, allowed_hosts={request.get_host()}):
next = resolve_url(fallback)

if get_kwargs:
Expand Down
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
'Development Status :: 5 - Production/Stable',
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.8',
'Framework :: Django :: 1.9',
'Framework :: Django :: 1.10',
'Framework :: Django :: 1.11',
'Framework :: Django :: 2.0',
'Intended Audience :: Developers',
Expand All @@ -42,5 +39,5 @@
packages=find_packages(exclude=['tests', 'tests.*']),
include_package_data=True,
test_suite='tests.runtests.main',
install_requires=['Django>=1.8']
install_requires=['Django>=1.11']
)
5 changes: 1 addition & 4 deletions tests/custom_comments/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse # Django < 1.10
from django.urls import reverse

from . import views
from .forms import CustomCommentForm
Expand Down
5 changes: 0 additions & 5 deletions tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
),
MIDDLEWARE_CLASSES=(
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
), # Django < 1.10
ROOT_URLCONF='testapp.urls',
TEMPLATES=[
{
Expand Down
10 changes: 0 additions & 10 deletions tests/testapp/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import absolute_import

from django import VERSION
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
Expand Down Expand Up @@ -91,12 +90,3 @@ def getValidData(self, obj):
d = self.getData()
d.update(f.initial)
return d

def assertRedirects(self, response, expected_url, **kwargs):
"""
Wrapper for assertRedirects to handle Django pre-1.9.
"""
if VERSION >= (1, 9) and expected_url.startswith('http://testserver'):
expected_url = expected_url[len('http://testserver'):]
return super(CommentTestCase, self).assertRedirects(
response, expected_url, **kwargs)
65 changes: 34 additions & 31 deletions tests/testapp/tests/test_comment_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import absolute_import, unicode_literals

import re

from django.conf import settings
from django.contrib.auth.models import User

Expand All @@ -12,10 +10,6 @@
from testapp.models import Article, Book


# Before Django 1.9, the response contained the scheme/netloc parts
post_redirect_re = re.compile(r'^(http://testserver)?/posted/\?c=(?P<pk>\d+$)')


class CommentViewTests(CommentTestCase):

def testPostCommentHTTPMethods(self):
Expand Down Expand Up @@ -263,35 +257,38 @@ def testCommentNext(self):
a = Article.objects.get(pk=1)
data = self.getValidData(a)
response = self.client.post("/post/", data)
location = response["Location"]
match = post_redirect_re.match(location)
self.assertIsNotNone(match, "Unexpected redirect location: %s" % location)

self.assertRedirects(
response,
'/posted/?c=%s' % Comment.objects.latest('id').pk,
fetch_redirect_response=False,
)
data["next"] = "/somewhere/else/"
data["comment"] = "This is another comment"
response = self.client.post("/post/", data)
location = response["Location"]
match = re.search(r"^(http://testserver)?/somewhere/else/\?c=\d+$", location)
self.assertIsNotNone(match, "Unexpected redirect location: %s" % location)

self.assertRedirects(
response,
'/somewhere/else/?c=%s' % Comment.objects.latest('id').pk,
fetch_redirect_response=False,
)
data["next"] = "http://badserver/somewhere/else/"
data["comment"] = "This is another comment with an unsafe next url"
response = self.client.post("/post/", data)
location = response["Location"]
match = post_redirect_re.match(location)
self.assertIsNotNone(match, "Unsafe redirection to: %s" % location)
self.assertRedirects(
response,
'/posted/?c=%s' % Comment.objects.latest('id').pk,
fetch_redirect_response=False,
)

def testCommentDoneView(self):
a = Article.objects.get(pk=1)
data = self.getValidData(a)
response = self.client.post("/post/", data)
location = response["Location"]
match = post_redirect_re.match(location)
self.assertIsNotNone(match, "Unexpected redirect location: %s" % location)
pk = int(match.group('pk'))
comment = Comment.objects.latest('id')
location = '/posted/?c=%s' % comment.pk
self.assertRedirects(response, location, fetch_redirect_response=False)
response = self.client.get(location)
self.assertTemplateUsed(response, "comments/posted.html")
self.assertEqual(response.context["comment"], Comment.objects.get(pk=pk))
self.assertEqual(response.context["comment"], comment)

def testCommentNextWithQueryString(self):
"""
Expand All @@ -302,9 +299,11 @@ def testCommentNextWithQueryString(self):
data["next"] = "/somewhere/else/?foo=bar"
data["comment"] = "This is another comment"
response = self.client.post("/post/", data)
location = response["Location"]
match = re.search(r"^(http://testserver)?/somewhere/else/\?foo=bar&c=\d+$", location)
self.assertIsNotNone(match, "Unexpected redirect location: %s" % location)
self.assertRedirects(
response,
'/somewhere/else/?foo=bar&c=%s' % Comment.objects.latest('id').pk,
fetch_redirect_response=False,
)

def testCommentPostRedirectWithInvalidIntegerPK(self):
"""
Expand All @@ -331,16 +330,20 @@ def testCommentNextWithQueryStringAndAnchor(self):
data["next"] = "/somewhere/else/?foo=bar#baz"
data["comment"] = "This is another comment"
response = self.client.post("/post/", data)
location = response["Location"]
match = re.search(r"^(http://testserver)?/somewhere/else/\?foo=bar&c=\d+#baz$", location)
self.assertIsNotNone(match, "Unexpected redirect location: %s" % location)
self.assertRedirects(
response,
'/somewhere/else/?foo=bar&c=%s#baz' % Comment.objects.latest('id').pk,
fetch_redirect_response=False,
)

# Without a query string
a = Article.objects.get(pk=1)
data = self.getValidData(a)
data["next"] = "/somewhere/else/#baz"
data["comment"] = "This is another comment"
response = self.client.post("/post/", data)
location = response["Location"]
match = re.search(r"^(http://testserver)?/somewhere/else/\?c=\d+#baz$", location)
self.assertIsNotNone(match, "Unexpected redirect location: %s" % location)
self.assertRedirects(
response,
'/somewhere/else/?c=%s#baz' % Comment.objects.latest('id').pk,
fetch_redirect_response=False,
)
33 changes: 13 additions & 20 deletions tests/testapp/tests/test_moderation_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def testFlagPost(self):
pk = comments[0].pk
self.client.login(username="normaluser", password="normaluser")
response = self.client.post("/flag/%d/" % pk)
self.assertRedirects(response, "http://testserver/flagged/?c=%d" % pk)
self.assertRedirects(response, "/flagged/?c=%d" % pk)
c = Comment.objects.get(pk=pk)
self.assertEqual(c.flags.filter(flag=CommentFlag.SUGGEST_REMOVAL).count(), 1)
return c
Expand All @@ -40,8 +40,7 @@ def testFlagPostNext(self):
pk = comments[0].pk
self.client.login(username="normaluser", password="normaluser")
response = self.client.post("/flag/%d/" % pk, {'next': "/go/here/"})
self.assertRedirects(response,
"http://testserver/go/here/?c=%d" % pk, fetch_redirect_response=False)
self.assertRedirects(response, "/go/here/?c=%d" % pk, fetch_redirect_response=False)

def testFlagPostUnsafeNext(self):
"""
Expand All @@ -53,8 +52,7 @@ def testFlagPostUnsafeNext(self):
self.client.login(username="normaluser", password="normaluser")
response = self.client.post("/flag/%d/" % pk,
{'next': "http://elsewhere/bad"})
self.assertRedirects(response,
"http://testserver/flagged/?c=%d" % pk)
self.assertRedirects(response, "/flagged/?c=%d" % pk)

def testFlagPostTwice(self):
"""Users don't get to flag comments more than once."""
Expand All @@ -69,11 +67,11 @@ def testFlagAnon(self):
pk = comments[0].pk
response = self.client.get("/flag/%d/" % pk)
self.assertRedirects(response,
"http://testserver/accounts/login/?next=/flag/%d/" % pk,
"/accounts/login/?next=/flag/%d/" % pk,
fetch_redirect_response=False)
response = self.client.post("/flag/%d/" % pk)
self.assertRedirects(response,
"http://testserver/accounts/login/?next=/flag/%d/" % pk,
"/accounts/login/?next=/flag/%d/" % pk,
fetch_redirect_response=False)

def testFlaggedView(self):
Expand Down Expand Up @@ -118,7 +116,7 @@ def testDeletePermissions(self):
self.client.login(username="normaluser", password="normaluser")
response = self.client.get("/delete/%d/" % pk)
self.assertRedirects(response,
"http://testserver/accounts/login/?next=/delete/%d/" % pk,
"/accounts/login/?next=/delete/%d/" % pk,
fetch_redirect_response=False)

makeModerator("normaluser")
Expand All @@ -132,7 +130,7 @@ def testDeletePost(self):
makeModerator("normaluser")
self.client.login(username="normaluser", password="normaluser")
response = self.client.post("/delete/%d/" % pk)
self.assertRedirects(response, "http://testserver/deleted/?c=%d" % pk)
self.assertRedirects(response, "/deleted/?c=%d" % pk)
c = Comment.objects.get(pk=pk)
self.assertTrue(c.is_removed)
self.assertEqual(c.flags.filter(flag=CommentFlag.MODERATOR_DELETION, user__username="normaluser").count(), 1)
Expand All @@ -147,8 +145,7 @@ def testDeletePostNext(self):
makeModerator("normaluser")
self.client.login(username="normaluser", password="normaluser")
response = self.client.post("/delete/%d/" % pk, {'next': "/go/here/"})
self.assertRedirects(response,
"http://testserver/go/here/?c=%d" % pk, fetch_redirect_response=False)
self.assertRedirects(response, "/go/here/?c=%d" % pk, fetch_redirect_response=False)

def testDeletePostUnsafeNext(self):
"""
Expand All @@ -161,8 +158,7 @@ def testDeletePostUnsafeNext(self):
self.client.login(username="normaluser", password="normaluser")
response = self.client.post("/delete/%d/" % pk,
{'next': "http://elsewhere/bad"})
self.assertRedirects(response,
"http://testserver/deleted/?c=%d" % pk)
self.assertRedirects(response, "/deleted/?c=%d" % pk)

def testDeleteSignals(self):
def receive(sender, **kwargs):
Expand Down Expand Up @@ -195,7 +191,7 @@ def testApprovePermissions(self):
response = self.client.get("/approve/%d/" % pk)
self.assertRedirects(
response,
"http://testserver/accounts/login/?next=/approve/%d/" % pk,
"/accounts/login/?next=/approve/%d/" % pk,
fetch_redirect_response=False
)

Expand All @@ -212,7 +208,7 @@ def testApprovePost(self):
makeModerator("normaluser")
self.client.login(username="normaluser", password="normaluser")
response = self.client.post("/approve/%d/" % c1.pk)
self.assertRedirects(response, "http://testserver/approved/?c=%d" % c1.pk)
self.assertRedirects(response, "/approved/?c=%d" % c1.pk)
c = Comment.objects.get(pk=c1.pk)
self.assertTrue(c.is_public)
self.assertEqual(c.flags.filter(flag=CommentFlag.MODERATOR_APPROVAL, user__username="normaluser").count(), 1)
Expand All @@ -230,9 +226,7 @@ def testApprovePostNext(self):
self.client.login(username="normaluser", password="normaluser")
response = self.client.post("/approve/%d/" % c1.pk,
{'next': "/go/here/"})
self.assertRedirects(response,
"http://testserver/go/here/?c=%d" % c1.pk,
fetch_redirect_response=False)
self.assertRedirects(response, "/go/here/?c=%d" % c1.pk, fetch_redirect_response=False)

def testApprovePostUnsafeNext(self):
"""
Expand All @@ -247,8 +241,7 @@ def testApprovePostUnsafeNext(self):
self.client.login(username="normaluser", password="normaluser")
response = self.client.post("/approve/%d/" % c1.pk,
{'next': "http://elsewhere/bad"})
self.assertRedirects(response,
"http://testserver/approved/?c=%d" % c1.pk)
self.assertRedirects(response, "/approved/?c=%d" % c1.pk)

def testApproveSignals(self):
def receive(sender, **kwargs):
Expand Down
Loading

0 comments on commit e94b557

Please sign in to comment.