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(): diff --git a/tests/custom_comments/__init__.py b/tests/custom_comments/__init__.py index 277c866..98835e8 100644 --- a/tests/custom_comments/__init__.py +++ b/tests/custom_comments/__init__.py @@ -1,7 +1,6 @@ from django.urls import reverse from . import views -from .forms import CustomCommentForm def get_model(): @@ -10,6 +9,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..b46910f 100644 --- a/tests/custom_comments/forms.py +++ b/tests/custom_comments/forms.py @@ -1,5 +1,12 @@ -from django import forms +from django.forms import FileField +from django_comments.forms import CommentForm -class CustomCommentForm(forms.Form): - pass + +class CustomCommentForm(CommentForm): + file = 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..61cda6e --- /dev/null +++ b/tests/custom_comments/migrations/0001_initial.py @@ -0,0 +1,118 @@ +# 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 + + +class Migration(migrations.Migration): + initial = True + + dependencies = [ + ("contenttypes", "0002_remove_content_type_name"), + ("sites", "0002_alter_domain_unique"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="CustomComment", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + 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", + "verbose_name_plural": "comments", + "ordering": ("submit_date",), + "permissions": [("can_moderate", "Can moderate comments")], + "abstract": False, + }, + ), + ] 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..ef5b1a5 100644 --- a/tests/custom_comments/models.py +++ b/tests/custom_comments/models.py @@ -1,5 +1,6 @@ -from django.db import models +from django.db.models import FileField +from django_comments.abstracts import CommentAbstractModel -class CustomComment(models.Model): - pass +class CustomComment(CommentAbstractModel): + file = FileField() diff --git a/tests/runtests.py b/tests/runtests.py index 37cbeed..a99f9d3 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -27,6 +27,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..f3e867d 100644 --- a/tests/testapp/tests/__init__.py +++ b/tests/testapp/tests/__init__.py @@ -1,11 +1,13 @@ +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 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 @@ -23,6 +25,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() @@ -35,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", @@ -44,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", @@ -66,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, @@ -74,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, @@ -94,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 diff --git a/tests/testapp/tests/test_comment_views.py b/tests/testapp/tests/test_comment_views.py index df36a00..caef38e 100644 --- a/tests/testapp/tests/test_comment_views.py +++ b/tests/testapp/tests/test_comment_views.py @@ -1,7 +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 -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 @@ -359,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")