Skip to content
Open
2 changes: 1 addition & 1 deletion django_comments/views/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion tests/custom_comments/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.urls import reverse

from . import views
from .forms import CustomCommentForm


def get_model():
Expand All @@ -10,6 +9,7 @@ def get_model():


def get_form():
from .forms import CustomCommentForm
return CustomCommentForm


Expand Down
13 changes: 10 additions & 3 deletions tests/custom_comments/forms.py
Original file line number Diff line number Diff line change
@@ -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
118 changes: 118 additions & 0 deletions tests/custom_comments/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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,
},
),
]
Empty file.
7 changes: 4 additions & 3 deletions tests/custom_comments/models.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
20 changes: 13 additions & 7 deletions tests/testapp/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -66,15 +72,15 @@ 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,
user_url="http://example.com/~frank/",
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,
Expand All @@ -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
19 changes: 18 additions & 1 deletion tests/testapp/tests/test_comment_views.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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")