From 4c786e92c8d72391737d7e3468313b13b538d63c Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Thu, 11 May 2023 17:03:50 +0100 Subject: [PATCH 01/12] pass request.FILES to form --- django_comments/views/comments.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_comments/views/comments.py b/django_comments/views/comments.py index 9a35c19..b34c191 100644 --- a/django_comments/views/comments.py +++ b/django_comments/views/comments.py @@ -71,7 +71,7 @@ def post_comment(request, next=None, using=None): preview = "preview" in data # Construct the comment form - form = django_comments.get_form()(target, data=data) + form = django_comments.get_form()(target, data=data, files=request.FILES) # Check security information if form.security_errors(): From b4c7c8d65714ef663eda2ba4d37058d1a37a520a Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Wed, 17 May 2023 22:34:15 +0100 Subject: [PATCH 02/12] add test for Add request.FILES to post_comment form --- tests/custom_comments/__init__.py | 3 +- tests/custom_comments/forms.py | 11 ++++- .../migrations/0001_initial.py | 40 +++++++++++++++++++ tests/custom_comments/migrations/__init__.py | 0 tests/custom_comments/models.py | 6 ++- tests/runtests.py | 2 + tests/testapp/tests/__init__.py | 7 ++++ tests/testapp/tests/test_comment_views.py | 18 +++++++++ 8 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 tests/custom_comments/migrations/0001_initial.py create mode 100644 tests/custom_comments/migrations/__init__.py diff --git a/tests/custom_comments/__init__.py b/tests/custom_comments/__init__.py index 277c866..6d4f4cd 100644 --- a/tests/custom_comments/__init__.py +++ b/tests/custom_comments/__init__.py @@ -1,7 +1,7 @@ from django.urls import reverse from . import views -from .forms import CustomCommentForm + def get_model(): @@ -10,6 +10,7 @@ def get_model(): def get_form(): + from .forms import CustomCommentForm return CustomCommentForm diff --git a/tests/custom_comments/forms.py b/tests/custom_comments/forms.py index 07918dd..ddc9c7f 100644 --- a/tests/custom_comments/forms.py +++ b/tests/custom_comments/forms.py @@ -1,5 +1,12 @@ from django import forms +from django_comments.forms import CommentForm -class CustomCommentForm(forms.Form): - pass + +class CustomCommentForm(CommentForm): + file = forms.FileField() + + def get_comment_create_data(self, site_id=None): + data = super().get_comment_create_data(site_id=site_id) + data["file"] = self.cleaned_data["file"] + return data diff --git a/tests/custom_comments/migrations/0001_initial.py b/tests/custom_comments/migrations/0001_initial.py new file mode 100644 index 0000000..b9693e5 --- /dev/null +++ b/tests/custom_comments/migrations/0001_initial.py @@ -0,0 +1,40 @@ +# Generated by Django 4.2.1 on 2023-05-17 22:32 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + initial = True + + dependencies = [ + ("django_comments", "0004_add_object_pk_is_removed_index"), + ] + + operations = [ + migrations.CreateModel( + name="CustomComment", + fields=[ + ( + "comment_ptr", + models.OneToOneField( + auto_created=True, + on_delete=django.db.models.deletion.CASCADE, + parent_link=True, + primary_key=True, + serialize=False, + to="django_comments.comment", + ), + ), + ("file", models.FileField(upload_to="")), + ], + options={ + "verbose_name": "comment", + "verbose_name_plural": "comments", + "ordering": ("submit_date",), + "permissions": [("can_moderate", "Can moderate comments")], + "abstract": False, + }, + bases=("django_comments.comment",), + ), + ] diff --git a/tests/custom_comments/migrations/__init__.py b/tests/custom_comments/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/custom_comments/models.py b/tests/custom_comments/models.py index 646f625..19404a4 100644 --- a/tests/custom_comments/models.py +++ b/tests/custom_comments/models.py @@ -1,5 +1,7 @@ from django.db import models +from django_comments.models import Comment -class CustomComment(models.Model): - pass + +class CustomComment(Comment): + file = models.FileField() diff --git a/tests/runtests.py b/tests/runtests.py index 37cbeed..f5acbd7 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -7,6 +7,7 @@ import os import sys import django +from django.core.management import call_command here = os.path.dirname(os.path.abspath(__file__)) parent = os.path.dirname(here) @@ -27,6 +28,7 @@ "testapp", "custom_comments", ], + MEDIA_ROOT=os.path.join(here, 'media'), MIDDLEWARE=( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', diff --git a/tests/testapp/tests/__init__.py b/tests/testapp/tests/__init__.py index 1ca21f7..2c4b3ba 100644 --- a/tests/testapp/tests/__init__.py +++ b/tests/testapp/tests/__init__.py @@ -1,3 +1,6 @@ +import shutil + +from django.conf import settings from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site @@ -23,6 +26,10 @@ class CommentTestCase(TestCase): """ fixtures = ["comment_tests"] + def tearDown(self): + super().tearDown() + shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True) + @classmethod def setUpTestData(cls): super().setUpTestData() diff --git a/tests/testapp/tests/test_comment_views.py b/tests/testapp/tests/test_comment_views.py index df36a00..d31c1ae 100644 --- a/tests/testapp/tests/test_comment_views.py +++ b/tests/testapp/tests/test_comment_views.py @@ -1,6 +1,10 @@ from django.conf import settings + from django.contrib.auth.models import User +from django.core.files.uploadedfile import SimpleUploadedFile +from django.test.utils import override_settings +import django_comments from django_comments import signals from django_comments.abstracts import COMMENT_MAX_LENGTH from django_comments.models import Comment @@ -85,6 +89,20 @@ def testPostTooLongComment(self): response, "Ensure this value has at most %d characters" % COMMENT_MAX_LENGTH ) + @override_settings( + COMMENTS_APP='custom_comments', + ) + def testPostCommentWithFile(self): + a = Article.objects.get(pk=1) + data = self.getValidData(a) + test_file = SimpleUploadedFile("test_file.txt", b"file_content") + data["file"] = test_file + response = self.client.post("/post/", data) + self.assertEqual(response.status_code, 302) + custom_comment = django_comments.get_model().objects.first() + self.assertTrue(custom_comment.file) + self.assertEqual(custom_comment.file.read(), b"file_content") + def testCommentPreview(self): a = Article.objects.get(pk=1) data = self.getValidData(a) From 51c168fe1970241cffbb853a3c238211a131a000 Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Wed, 17 May 2023 22:51:18 +0100 Subject: [PATCH 03/12] remove call_command --- tests/runtests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/runtests.py b/tests/runtests.py index f5acbd7..a99f9d3 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -7,7 +7,6 @@ import os import sys import django -from django.core.management import call_command here = os.path.dirname(os.path.abspath(__file__)) parent = os.path.dirname(here) From 4aa4f975db9ff11d51a7be1cf5111910f1cd02d8 Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Sun, 28 May 2023 11:55:44 +0100 Subject: [PATCH 04/12] inherit from CommentAbstractModel --- .../migrations/0001_initial.py | 94 +++++++++++++++++-- tests/custom_comments/models.py | 9 +- 2 files changed, 90 insertions(+), 13 deletions(-) diff --git a/tests/custom_comments/migrations/0001_initial.py b/tests/custom_comments/migrations/0001_initial.py index b9693e5..61cda6e 100644 --- a/tests/custom_comments/migrations/0001_initial.py +++ b/tests/custom_comments/migrations/0001_initial.py @@ -1,5 +1,6 @@ -# Generated by Django 4.2.1 on 2023-05-17 22:32 +# Generated by Django 4.2.1 on 2023-05-28 11:38 +from django.conf import settings from django.db import migrations, models import django.db.models.deletion @@ -8,7 +9,9 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ("django_comments", "0004_add_object_pk_is_removed_index"), + ("contenttypes", "0002_remove_content_type_name"), + ("sites", "0002_alter_domain_unique"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ @@ -16,17 +19,93 @@ class Migration(migrations.Migration): name="CustomComment", fields=[ ( - "comment_ptr", - models.OneToOneField( + "id", + models.BigAutoField( auto_created=True, - on_delete=django.db.models.deletion.CASCADE, - parent_link=True, primary_key=True, serialize=False, - to="django_comments.comment", + verbose_name="ID", + ), + ), + ( + "object_pk", + models.CharField( + db_index=True, max_length=64, verbose_name="object ID" + ), + ), + ( + "user_name", + models.CharField( + blank=True, max_length=50, verbose_name="user's name" + ), + ), + ( + "user_email", + models.EmailField( + blank=True, max_length=254, verbose_name="user's email address" + ), + ), + ("user_url", models.URLField(blank=True, verbose_name="user's URL")), + ("comment", models.TextField(max_length=3000, verbose_name="comment")), + ( + "submit_date", + models.DateTimeField( + db_index=True, default=None, verbose_name="date/time submitted" + ), + ), + ( + "ip_address", + models.GenericIPAddressField( + blank=True, + null=True, + unpack_ipv4=True, + verbose_name="IP address", + ), + ), + ( + "is_public", + models.BooleanField( + default=True, + help_text="Uncheck this box to make the comment effectively disappear from the site.", + verbose_name="is public", + ), + ), + ( + "is_removed", + models.BooleanField( + db_index=True, + default=False, + help_text='Check this box if the comment is inappropriate. A "This comment has been removed" message will be displayed instead.', + verbose_name="is removed", ), ), ("file", models.FileField(upload_to="")), + ( + "content_type", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="content_type_set_for_%(class)s", + to="contenttypes.contenttype", + verbose_name="content type", + ), + ), + ( + "site", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="sites.site" + ), + ), + ( + "user", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + related_name="%(class)s_comments", + to=settings.AUTH_USER_MODEL, + verbose_name="user", + ), + ), ], options={ "verbose_name": "comment", @@ -35,6 +114,5 @@ class Migration(migrations.Migration): "permissions": [("can_moderate", "Can moderate comments")], "abstract": False, }, - bases=("django_comments.comment",), ), ] diff --git a/tests/custom_comments/models.py b/tests/custom_comments/models.py index 19404a4..ef5b1a5 100644 --- a/tests/custom_comments/models.py +++ b/tests/custom_comments/models.py @@ -1,7 +1,6 @@ -from django.db import models +from django.db.models import FileField +from django_comments.abstracts import CommentAbstractModel -from django_comments.models import Comment - -class CustomComment(Comment): - file = models.FileField() +class CustomComment(CommentAbstractModel): + file = FileField() From c5a6792551a9c6b63adb1187cd8b34cae7249d08 Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Sun, 28 May 2023 11:56:30 +0100 Subject: [PATCH 05/12] replace hardcoded Comment and CommentForm with get_model and get_form for use in custom comment app tests --- tests/testapp/tests/__init__.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/testapp/tests/__init__.py b/tests/testapp/tests/__init__.py index 2c4b3ba..f3e867d 100644 --- a/tests/testapp/tests/__init__.py +++ b/tests/testapp/tests/__init__.py @@ -7,8 +7,7 @@ from django.test import TestCase from django.test.utils import override_settings -from django_comments.forms import CommentForm -from django_comments.models import Comment +from django_comments import get_model, get_form from testapp.models import Article, Author @@ -42,7 +41,7 @@ def setUpTestData(cls): def createSomeComments(self): # Two anonymous comments on two different objects - c1 = Comment.objects.create( + c1 = get_model().objects.create( content_type=CT(Article), object_pk="1", user_name="Joe Somebody", @@ -51,7 +50,7 @@ def createSomeComments(self): comment="First!", site=Site.objects.get_current(), ) - c2 = Comment.objects.create( + c2 = get_model().objects.create( content_type=CT(Author), object_pk="1", user_name="Joe Somebody", @@ -73,7 +72,7 @@ def createSomeComments(self): is_active=True, is_superuser=False, ) - c3 = Comment.objects.create( + c3 = get_model().objects.create( content_type=CT(Article), object_pk="1", user=user, @@ -81,7 +80,7 @@ def createSomeComments(self): comment="Damn, I wanted to be first.", site=Site.objects.get_current(), ) - c4 = Comment.objects.create( + c4 = get_model().objects.create( content_type=CT(Author), object_pk="2", user=user, @@ -101,7 +100,7 @@ def getData(self): } def getValidData(self, obj): - f = CommentForm(obj) + f = get_form()(obj) d = self.getData() d.update(f.initial) return d From 7906e47ce39c522929ffddfd382322958681b155 Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Sun, 28 May 2023 11:56:51 +0100 Subject: [PATCH 06/12] explicit import of FileField --- tests/custom_comments/forms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/custom_comments/forms.py b/tests/custom_comments/forms.py index ddc9c7f..b46910f 100644 --- a/tests/custom_comments/forms.py +++ b/tests/custom_comments/forms.py @@ -1,10 +1,10 @@ -from django import forms +from django.forms import FileField from django_comments.forms import CommentForm class CustomCommentForm(CommentForm): - file = forms.FileField() + file = FileField() def get_comment_create_data(self, site_id=None): data = super().get_comment_create_data(site_id=site_id) From 19336c5b23add217535a29ff470d47c85787bb5b Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Sun, 28 May 2023 11:57:12 +0100 Subject: [PATCH 07/12] separate custom comment app tests into new class --- tests/testapp/tests/test_comment_views.py | 33 +++++++++++------------ 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/tests/testapp/tests/test_comment_views.py b/tests/testapp/tests/test_comment_views.py index d31c1ae..caef38e 100644 --- a/tests/testapp/tests/test_comment_views.py +++ b/tests/testapp/tests/test_comment_views.py @@ -1,11 +1,9 @@ from django.conf import settings - from django.contrib.auth.models import User from django.core.files.uploadedfile import SimpleUploadedFile from django.test.utils import override_settings -import django_comments -from django_comments import signals +from django_comments import signals, get_model from django_comments.abstracts import COMMENT_MAX_LENGTH from django_comments.models import Comment @@ -89,20 +87,6 @@ def testPostTooLongComment(self): response, "Ensure this value has at most %d characters" % COMMENT_MAX_LENGTH ) - @override_settings( - COMMENTS_APP='custom_comments', - ) - def testPostCommentWithFile(self): - a = Article.objects.get(pk=1) - data = self.getValidData(a) - test_file = SimpleUploadedFile("test_file.txt", b"file_content") - data["file"] = test_file - response = self.client.post("/post/", data) - self.assertEqual(response.status_code, 302) - custom_comment = django_comments.get_model().objects.first() - self.assertTrue(custom_comment.file) - self.assertEqual(custom_comment.file.read(), b"file_content") - def testCommentPreview(self): a = Article.objects.get(pk=1) data = self.getValidData(a) @@ -377,3 +361,18 @@ def testCommentNextWithQueryStringAndAnchor(self): '/somewhere/else/?c=%s#baz' % Comment.objects.latest('id').pk, fetch_redirect_response=False, ) + +@override_settings( + COMMENTS_APP='custom_comments', + ) +class CustomCommentViewTests(CommentTestCase): + def testPostCommentWithFile(self): + a = Article.objects.get(pk=1) + data = self.getValidData(a) + test_file = SimpleUploadedFile("test_file.txt", b"file_content") + data["file"] = test_file + response = self.client.post("/post/", data) + self.assertEqual(response.status_code, 302) + custom_comment = get_model().objects.first() + self.assertTrue(custom_comment.file) + self.assertEqual(custom_comment.file.read(), b"file_content") From 33e2244587e637e50e5ce358bbd36e6cb9cd8506 Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Sun, 28 May 2023 12:04:01 +0100 Subject: [PATCH 08/12] removed added new line --- tests/custom_comments/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/custom_comments/__init__.py b/tests/custom_comments/__init__.py index 6d4f4cd..98835e8 100644 --- a/tests/custom_comments/__init__.py +++ b/tests/custom_comments/__init__.py @@ -3,7 +3,6 @@ from . import views - def get_model(): from .models import CustomComment return CustomComment From bff5a7cd926d24fa2e3dc0d4df27a22ecc39fdb6 Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Wed, 17 May 2023 22:34:15 +0100 Subject: [PATCH 09/12] add test for Add request.FILES to post_comment form --- tests/runtests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/runtests.py b/tests/runtests.py index a99f9d3..f5acbd7 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -7,6 +7,7 @@ import os import sys import django +from django.core.management import call_command here = os.path.dirname(os.path.abspath(__file__)) parent = os.path.dirname(here) From d5e79c6657a3138b5aabd41702a5761b5eb448c8 Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Wed, 17 May 2023 22:51:18 +0100 Subject: [PATCH 10/12] remove call_command --- tests/runtests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/runtests.py b/tests/runtests.py index f5acbd7..a99f9d3 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -7,7 +7,6 @@ import os import sys import django -from django.core.management import call_command here = os.path.dirname(os.path.abspath(__file__)) parent = os.path.dirname(here) From 69b6d2caa80728e994248e6fe4fc28c9115ad30c Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Wed, 17 May 2023 22:34:15 +0100 Subject: [PATCH 11/12] add test for Add request.FILES to post_comment form --- tests/runtests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/runtests.py b/tests/runtests.py index a99f9d3..f5acbd7 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -7,6 +7,7 @@ import os import sys import django +from django.core.management import call_command here = os.path.dirname(os.path.abspath(__file__)) parent = os.path.dirname(here) From 1866bdf3b520ed4ed519f15e0cfec0f083940b6e Mon Sep 17 00:00:00 2001 From: DominicLGit Date: Wed, 17 May 2023 22:51:18 +0100 Subject: [PATCH 12/12] remove call_command --- tests/runtests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/runtests.py b/tests/runtests.py index f5acbd7..a99f9d3 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -7,7 +7,6 @@ import os import sys import django -from django.core.management import call_command here = os.path.dirname(os.path.abspath(__file__)) parent = os.path.dirname(here)