Commit 1ab69cb122418c973e27210558d11bb3fba921f6
Committed by
Claude Paroz
1 parent
f41e32d7
Use get_current_site to look up the site instead of settings.SITE_ID
Showing
7 changed files
with
68 additions
and
9 deletions
| @@ -8,6 +8,7 @@ Pending | @@ -8,6 +8,7 @@ Pending | ||
| 8 | 8 | ||
| 9 | * Fixed a packaging error which caused sub-packages of the tests to be | 9 | * Fixed a packaging error which caused sub-packages of the tests to be |
| 10 | distributed. | 10 | distributed. |
| 11 | +* Use ``get_current_site`` to look up the site instead of ``settings.SITE_ID``. | ||
| 11 | 12 | ||
| 12 | 1.7.3 (2016-09-13) | 13 | 1.7.3 (2016-09-13) |
| 13 | ------------------ | 14 | ------------------ |
| @@ -105,7 +105,7 @@ class CommentDetailsForm(CommentSecurityForm): | @@ -105,7 +105,7 @@ class CommentDetailsForm(CommentSecurityForm): | ||
| 105 | comment = forms.CharField(label=_('Comment'), widget=forms.Textarea, | 105 | comment = forms.CharField(label=_('Comment'), widget=forms.Textarea, |
| 106 | max_length=COMMENT_MAX_LENGTH) | 106 | max_length=COMMENT_MAX_LENGTH) |
| 107 | 107 | ||
| 108 | - def get_comment_object(self): | 108 | + def get_comment_object(self, site_id=None): |
| 109 | """ | 109 | """ |
| 110 | Return a new (unsaved) comment object based on the information in this | 110 | Return a new (unsaved) comment object based on the information in this |
| 111 | form. Assumes that the form is already validated and will throw a | 111 | form. Assumes that the form is already validated and will throw a |
| @@ -118,7 +118,7 @@ class CommentDetailsForm(CommentSecurityForm): | @@ -118,7 +118,7 @@ class CommentDetailsForm(CommentSecurityForm): | ||
| 118 | raise ValueError("get_comment_object may only be called on valid forms") | 118 | raise ValueError("get_comment_object may only be called on valid forms") |
| 119 | 119 | ||
| 120 | CommentModel = self.get_comment_model() | 120 | CommentModel = self.get_comment_model() |
| 121 | - new = CommentModel(**self.get_comment_create_data()) | 121 | + new = CommentModel(**self.get_comment_create_data(site_id=site_id)) |
| 122 | new = self.check_for_duplicate_comment(new) | 122 | new = self.check_for_duplicate_comment(new) |
| 123 | 123 | ||
| 124 | return new | 124 | return new |
| @@ -131,7 +131,7 @@ class CommentDetailsForm(CommentSecurityForm): | @@ -131,7 +131,7 @@ class CommentDetailsForm(CommentSecurityForm): | ||
| 131 | """ | 131 | """ |
| 132 | return get_model() | 132 | return get_model() |
| 133 | 133 | ||
| 134 | - def get_comment_create_data(self): | 134 | + def get_comment_create_data(self, site_id=None): |
| 135 | """ | 135 | """ |
| 136 | Returns the dict of data to be used to create a comment. Subclasses in | 136 | Returns the dict of data to be used to create a comment. Subclasses in |
| 137 | custom comment apps that override get_comment_model can override this | 137 | custom comment apps that override get_comment_model can override this |
| @@ -145,7 +145,7 @@ class CommentDetailsForm(CommentSecurityForm): | @@ -145,7 +145,7 @@ class CommentDetailsForm(CommentSecurityForm): | ||
| 145 | user_url=self.cleaned_data["url"], | 145 | user_url=self.cleaned_data["url"], |
| 146 | comment=self.cleaned_data["comment"], | 146 | comment=self.cleaned_data["comment"], |
| 147 | submit_date=timezone.now(), | 147 | submit_date=timezone.now(), |
| 148 | - site_id=settings.SITE_ID, | 148 | + site_id=site_id or getattr(settings, "SITE_ID", None), |
| 149 | is_public=True, | 149 | is_public=True, |
| 150 | is_removed=False, | 150 | is_removed=False, |
| 151 | ) | 151 | ) |
| @@ -2,6 +2,7 @@ from django import template | @@ -2,6 +2,7 @@ from django import template | ||
| 2 | from django.template.loader import render_to_string | 2 | from django.template.loader import render_to_string |
| 3 | from django.conf import settings | 3 | from django.conf import settings |
| 4 | from django.contrib.contenttypes.models import ContentType | 4 | from django.contrib.contenttypes.models import ContentType |
| 5 | +from django.contrib.sites.shortcuts import get_current_site | ||
| 5 | from django.utils.encoding import smart_text | 6 | from django.utils.encoding import smart_text |
| 6 | 7 | ||
| 7 | import django_comments | 8 | import django_comments |
| @@ -76,10 +77,16 @@ class BaseCommentNode(template.Node): | @@ -76,10 +77,16 @@ class BaseCommentNode(template.Node): | ||
| 76 | if not object_pk: | 77 | if not object_pk: |
| 77 | return self.comment_model.objects.none() | 78 | return self.comment_model.objects.none() |
| 78 | 79 | ||
| 80 | + # Explicit SITE_ID takes precedence over request. This is also how | ||
| 81 | + # get_current_site operates. | ||
| 82 | + site_id = getattr(settings, "SITE_ID", None) | ||
| 83 | + if not site_id and ('request' in context): | ||
| 84 | + site_id = get_current_site(context['request']).pk | ||
| 85 | + | ||
| 79 | qs = self.comment_model.objects.filter( | 86 | qs = self.comment_model.objects.filter( |
| 80 | content_type=ctype, | 87 | content_type=ctype, |
| 81 | object_pk=smart_text(object_pk), | 88 | object_pk=smart_text(object_pk), |
| 82 | - site__pk=settings.SITE_ID, | 89 | + site__pk=site_id, |
| 83 | ) | 90 | ) |
| 84 | 91 | ||
| 85 | # The is_public and is_removed fields are implementation details of the | 92 | # The is_public and is_removed fields are implementation details of the |
| @@ -3,6 +3,7 @@ from __future__ import absolute_import | @@ -3,6 +3,7 @@ from __future__ import absolute_import | ||
| 3 | from django import http | 3 | from django import http |
| 4 | from django.apps import apps | 4 | from django.apps import apps |
| 5 | from django.conf import settings | 5 | from django.conf import settings |
| 6 | +from django.contrib.sites.shortcuts import get_current_site | ||
| 6 | from django.core.exceptions import ObjectDoesNotExist, ValidationError | 7 | from django.core.exceptions import ObjectDoesNotExist, ValidationError |
| 7 | from django.shortcuts import render | 8 | from django.shortcuts import render |
| 8 | from django.template.loader import render_to_string | 9 | from django.template.loader import render_to_string |
| @@ -100,7 +101,7 @@ def post_comment(request, next=None, using=None): | @@ -100,7 +101,7 @@ def post_comment(request, next=None, using=None): | ||
| 100 | ) | 101 | ) |
| 101 | 102 | ||
| 102 | # Otherwise create the comment | 103 | # Otherwise create the comment |
| 103 | - comment = form.get_comment_object() | 104 | + comment = form.get_comment_object(site_id=get_current_site(request).id) |
| 104 | comment.ip_address = request.META.get("REMOTE_ADDR", None) | 105 | comment.ip_address = request.META.get("REMOTE_ADDR", None) |
| 105 | if request.user.is_authenticated(): | 106 | if request.user.is_authenticated(): |
| 106 | comment.user = request.user | 107 | comment.user = request.user |
| @@ -2,6 +2,7 @@ from __future__ import absolute_import | @@ -2,6 +2,7 @@ from __future__ import absolute_import | ||
| 2 | 2 | ||
| 3 | from django.conf import settings | 3 | from django.conf import settings |
| 4 | from django.contrib.auth.decorators import login_required, permission_required | 4 | from django.contrib.auth.decorators import login_required, permission_required |
| 5 | +from django.contrib.sites.shortcuts import get_current_site | ||
| 5 | from django.shortcuts import get_object_or_404, render | 6 | from django.shortcuts import get_object_or_404, render |
| 6 | from django.views.decorators.csrf import csrf_protect | 7 | from django.views.decorators.csrf import csrf_protect |
| 7 | 8 | ||
| @@ -21,7 +22,9 @@ def flag(request, comment_id, next=None): | @@ -21,7 +22,9 @@ def flag(request, comment_id, next=None): | ||
| 21 | comment | 22 | comment |
| 22 | the flagged `comments.comment` object | 23 | the flagged `comments.comment` object |
| 23 | """ | 24 | """ |
| 24 | - comment = get_object_or_404(django_comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID) | 25 | + comment = get_object_or_404(django_comments.get_model(), |
| 26 | + pk=comment_id, | ||
| 27 | + site__pk=get_current_site(request).pk) | ||
| 25 | 28 | ||
| 26 | # Flag on POST | 29 | # Flag on POST |
| 27 | if request.method == 'POST': | 30 | if request.method == 'POST': |
| @@ -46,7 +49,9 @@ def delete(request, comment_id, next=None): | @@ -46,7 +49,9 @@ def delete(request, comment_id, next=None): | ||
| 46 | comment | 49 | comment |
| 47 | the flagged `comments.comment` object | 50 | the flagged `comments.comment` object |
| 48 | """ | 51 | """ |
| 49 | - comment = get_object_or_404(django_comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID) | 52 | + comment = get_object_or_404(django_comments.get_model(), |
| 53 | + pk=comment_id, | ||
| 54 | + site__pk=get_current_site(request).pk) | ||
| 50 | 55 | ||
| 51 | # Delete on POST | 56 | # Delete on POST |
| 52 | if request.method == 'POST': | 57 | if request.method == 'POST': |
| @@ -72,7 +77,9 @@ def approve(request, comment_id, next=None): | @@ -72,7 +77,9 @@ def approve(request, comment_id, next=None): | ||
| 72 | comment | 77 | comment |
| 73 | the `comments.comment` object for approval | 78 | the `comments.comment` object for approval |
| 74 | """ | 79 | """ |
| 75 | - comment = get_object_or_404(django_comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID) | 80 | + comment = get_object_or_404(django_comments.get_model(), |
| 81 | + pk=comment_id, | ||
| 82 | + site__pk=get_current_site(request).pk) | ||
| 76 | 83 | ||
| 77 | # Delete on POST | 84 | # Delete on POST |
| 78 | if request.method == 'POST': | 85 | if request.method == 'POST': |
| @@ -3,6 +3,7 @@ from __future__ import absolute_import | @@ -3,6 +3,7 @@ from __future__ import absolute_import | ||
| 3 | import time | 3 | import time |
| 4 | 4 | ||
| 5 | from django.conf import settings | 5 | from django.conf import settings |
| 6 | +from django.contrib.sites.models import Site | ||
| 6 | 7 | ||
| 7 | from django_comments.forms import CommentForm | 8 | from django_comments.forms import CommentForm |
| 8 | from django_comments.models import Comment | 9 | from django_comments.models import Comment |
| @@ -12,6 +13,12 @@ from testapp.models import Article | @@ -12,6 +13,12 @@ from testapp.models import Article | ||
| 12 | 13 | ||
| 13 | 14 | ||
| 14 | class CommentFormTests(CommentTestCase): | 15 | class CommentFormTests(CommentTestCase): |
| 16 | + | ||
| 17 | + def setUp(self): | ||
| 18 | + super(CommentFormTests, self).setUp() | ||
| 19 | + self.site_2 = Site.objects.create(id=settings.SITE_ID + 1, | ||
| 20 | + domain="testserver", name="testserver") | ||
| 21 | + | ||
| 15 | def testInit(self): | 22 | def testInit(self): |
| 16 | f = CommentForm(Article.objects.get(pk=1)) | 23 | f = CommentForm(Article.objects.get(pk=1)) |
| 17 | self.assertEqual(f.initial['content_type'], str(Article._meta)) | 24 | self.assertEqual(f.initial['content_type'], str(Article._meta)) |
| @@ -61,6 +68,15 @@ class CommentFormTests(CommentTestCase): | @@ -61,6 +68,15 @@ class CommentFormTests(CommentTestCase): | ||
| 61 | c.save() | 68 | c.save() |
| 62 | self.assertEqual(Comment.objects.count(), 1) | 69 | self.assertEqual(Comment.objects.count(), 1) |
| 63 | 70 | ||
| 71 | + # Create a comment for the second site. We only test for site_id, not | ||
| 72 | + # what has already been tested above. | ||
| 73 | + a = Article.objects.get(pk=1) | ||
| 74 | + d = self.getValidData(a) | ||
| 75 | + d["comment"] = "testGetCommentObject with a site" | ||
| 76 | + f = CommentForm(Article.objects.get(pk=1), data=d) | ||
| 77 | + c = f.get_comment_object(site_id=self.site_2.id) | ||
| 78 | + self.assertEqual(c.site_id, self.site_2.id) | ||
| 79 | + | ||
| 64 | def testProfanities(self): | 80 | def testProfanities(self): |
| 65 | """Test COMMENTS_ALLOW_PROFANITIES and PROFANITIES_LIST settings""" | 81 | """Test COMMENTS_ALLOW_PROFANITIES and PROFANITIES_LIST settings""" |
| 66 | a = Article.objects.get(pk=1) | 82 | a = Article.objects.get(pk=1) |
| 1 | from __future__ import absolute_import | 1 | from __future__ import absolute_import |
| 2 | 2 | ||
| 3 | +import unittest | ||
| 4 | + | ||
| 5 | +from django import get_version | ||
| 6 | +from django.conf import settings | ||
| 3 | from django.contrib.contenttypes.models import ContentType | 7 | from django.contrib.contenttypes.models import ContentType |
| 8 | +from django.contrib.sites.models import Site | ||
| 4 | from django.template import Template, Context | 9 | from django.template import Template, Context |
| 10 | +from django.test.client import RequestFactory | ||
| 11 | +from django.test.utils import override_settings | ||
| 5 | 12 | ||
| 6 | from django_comments.forms import CommentForm | 13 | from django_comments.forms import CommentForm |
| 7 | from django_comments.models import Comment | 14 | from django_comments.models import Comment |
| @@ -12,6 +19,11 @@ from . import CommentTestCase | @@ -12,6 +19,11 @@ from . import CommentTestCase | ||
| 12 | 19 | ||
| 13 | class CommentTemplateTagTests(CommentTestCase): | 20 | class CommentTemplateTagTests(CommentTestCase): |
| 14 | 21 | ||
| 22 | + def setUp(self): | ||
| 23 | + super(CommentTemplateTagTests, self).setUp() | ||
| 24 | + self.site_2 = Site.objects.create(id=settings.SITE_ID + 1, | ||
| 25 | + domain="testserver", name="testserver") | ||
| 26 | + | ||
| 15 | def render(self, t, **c): | 27 | def render(self, t, **c): |
| 16 | ctx = Context(c) | 28 | ctx = Context(c) |
| 17 | out = Template(t).render(ctx) | 29 | out = Template(t).render(ctx) |
| @@ -95,6 +107,21 @@ class CommentTemplateTagTests(CommentTestCase): | @@ -95,6 +107,21 @@ class CommentTemplateTagTests(CommentTestCase): | ||
| 95 | self.createSomeComments() | 107 | self.createSomeComments() |
| 96 | self.verifyGetCommentList("{% get_comment_list for a as cl %}") | 108 | self.verifyGetCommentList("{% get_comment_list for a as cl %}") |
| 97 | 109 | ||
| 110 | + @unittest.skipIf(get_version().startswith("1.7"), | ||
| 111 | + "Retrieving a site from the request is not available in Django 1.7") | ||
| 112 | + def testGetCommentListUsingRequest(self, tag=None): | ||
| 113 | + # A request lookup should return site_2 | ||
| 114 | + with override_settings(SITE_ID=self.site_2.id): | ||
| 115 | + c1, c2, c3, c4 = self.createSomeComments() | ||
| 116 | + | ||
| 117 | + # Effectively unset SITE_ID which forces a site lookup from the | ||
| 118 | + # request. Create a new comment for the second site. | ||
| 119 | + with override_settings(SITE_ID=None): | ||
| 120 | + t = "{% load comments %}" + (tag or "{% get_comment_list for testapp.author a.id as cl %}") | ||
| 121 | + request = RequestFactory().get("/") | ||
| 122 | + ctx, out = self.render(t, a=Author.objects.get(pk=1), request=request) | ||
| 123 | + self.assertEqual(list(ctx["cl"]), [c2]) | ||
| 124 | + | ||
| 98 | def testWhitespaceInGetCommentListTag(self): | 125 | def testWhitespaceInGetCommentListTag(self): |
| 99 | self.createSomeComments() | 126 | self.createSomeComments() |
| 100 | self.verifyGetCommentList("{% load comment_testtags %}{% get_comment_list for a|noop:'x y' as cl %}") | 127 | self.verifyGetCommentList("{% load comment_testtags %}{% get_comment_list for a|noop:'x y' as cl %}") |
Please
register
or
login
to post a comment