Refactored CommentForm.get_comment_object into a handful of separete methods to make it easier for subclasses to provide custom models and data. Refs #8630.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9889 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
542709d0d1
commit
7d4a954836
|
@ -33,7 +33,7 @@ class CommentForm(forms.Form):
|
||||||
initial = {}
|
initial = {}
|
||||||
initial.update(self.generate_security_data())
|
initial.update(self.generate_security_data())
|
||||||
super(CommentForm, self).__init__(data=data, initial=initial)
|
super(CommentForm, self).__init__(data=data, initial=initial)
|
||||||
|
|
||||||
def get_comment_object(self):
|
def get_comment_object(self):
|
||||||
"""
|
"""
|
||||||
Return a new (unsaved) comment object based on the information in this
|
Return a new (unsaved) comment object based on the information in this
|
||||||
|
@ -45,8 +45,28 @@ class CommentForm(forms.Form):
|
||||||
"""
|
"""
|
||||||
if not self.is_valid():
|
if not self.is_valid():
|
||||||
raise ValueError("get_comment_object may only be called on valid forms")
|
raise ValueError("get_comment_object may only be called on valid forms")
|
||||||
|
|
||||||
new = Comment(
|
CommentModel = self.get_comment_model()
|
||||||
|
new = CommentModel(**self.get_comment_create_data())
|
||||||
|
new = self.check_for_duplicate_comment(new)
|
||||||
|
|
||||||
|
return new
|
||||||
|
|
||||||
|
def get_comment_model(self):
|
||||||
|
"""
|
||||||
|
Get the comment model to create with this form. Subclasses in custom
|
||||||
|
comment apps should override this, get_comment_create_data, and perhaps
|
||||||
|
check_for_duplicate_comment to provide custom comment models.
|
||||||
|
"""
|
||||||
|
return Comment
|
||||||
|
|
||||||
|
def get_comment_create_data(self):
|
||||||
|
"""
|
||||||
|
Returns the dict of data to be used to create a comment. Subclasses in
|
||||||
|
custom comment apps that override get_comment_model can override this
|
||||||
|
method to add extra fields onto a custom comment model.
|
||||||
|
"""
|
||||||
|
return dict(
|
||||||
content_type = ContentType.objects.get_for_model(self.target_object),
|
content_type = ContentType.objects.get_for_model(self.target_object),
|
||||||
object_pk = force_unicode(self.target_object._get_pk_val()),
|
object_pk = force_unicode(self.target_object._get_pk_val()),
|
||||||
user_name = self.cleaned_data["name"],
|
user_name = self.cleaned_data["name"],
|
||||||
|
@ -58,10 +78,13 @@ class CommentForm(forms.Form):
|
||||||
is_public = True,
|
is_public = True,
|
||||||
is_removed = False,
|
is_removed = False,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check that this comment isn't duplicate. (Sometimes people post comments
|
def check_for_duplicate_comment(self, new):
|
||||||
# twice by mistake.) If it is, fail silently by returning the old comment.
|
"""
|
||||||
possible_duplicates = Comment.objects.filter(
|
Check that a submitted comment isn't a duplicate. This might be caused
|
||||||
|
by someone posting a comment twice. If it is a dup, silently return the *previous* comment.
|
||||||
|
"""
|
||||||
|
possible_duplicates = self.get_comment_model()._default_manager.filter(
|
||||||
content_type = new.content_type,
|
content_type = new.content_type,
|
||||||
object_pk = new.object_pk,
|
object_pk = new.object_pk,
|
||||||
user_name = new.user_name,
|
user_name = new.user_name,
|
||||||
|
@ -71,7 +94,7 @@ class CommentForm(forms.Form):
|
||||||
for old in possible_duplicates:
|
for old in possible_duplicates:
|
||||||
if old.submit_date.date() == new.submit_date.date() and old.comment == new.comment:
|
if old.submit_date.date() == new.submit_date.date() and old.comment == new.comment:
|
||||||
return old
|
return old
|
||||||
|
|
||||||
return new
|
return new
|
||||||
|
|
||||||
def security_errors(self):
|
def security_errors(self):
|
||||||
|
|
Loading…
Reference in New Issue