diff --git a/django/contrib/comments/forms.py b/django/contrib/comments/forms.py
index 713269f..456826e 100644
|
a
|
b
|
from django.forms.util import ErrorDict
|
| 7 | 7 | from django.conf import settings |
| 8 | 8 | from django.http import Http404 |
| 9 | 9 | from django.contrib.contenttypes.models import ContentType |
| 10 | | from models import Comment |
| | 10 | from django.contrib import comments |
| 11 | 11 | from django.utils.encoding import force_unicode |
| 12 | 12 | from django.utils.hashcompat import sha_constructor |
| 13 | 13 | from django.utils.text import get_text_list |
| … |
… |
from django.utils.translation import ungettext, ugettext_lazy as _
|
| 15 | 15 | |
| 16 | 16 | COMMENT_MAX_LENGTH = getattr(settings,'COMMENT_MAX_LENGTH', 3000) |
| 17 | 17 | |
| 18 | | class CommentForm(forms.Form): |
| | 18 | class BaseCommentForm(forms.Form): |
| 19 | 19 | name = forms.CharField(label=_("Name"), max_length=50) |
| 20 | 20 | email = forms.EmailField(label=_("Email address")) |
| 21 | 21 | url = forms.URLField(label=_("URL"), required=False) |
| 22 | 22 | comment = forms.CharField(label=_('Comment'), widget=forms.Textarea, |
| 23 | 23 | max_length=COMMENT_MAX_LENGTH) |
| 24 | | honeypot = forms.CharField(required=False, |
| 25 | | label=_('If you enter anything in this field '\ |
| 26 | | 'your comment will be treated as spam')) |
| 27 | 24 | content_type = forms.CharField(widget=forms.HiddenInput) |
| 28 | 25 | object_pk = forms.CharField(widget=forms.HiddenInput) |
| 29 | 26 | timestamp = forms.IntegerField(widget=forms.HiddenInput) |
| … |
… |
class CommentForm(forms.Form):
|
| 34 | 31 | if initial is None: |
| 35 | 32 | initial = {} |
| 36 | 33 | initial.update(self.generate_security_data()) |
| 37 | | super(CommentForm, self).__init__(data=data, initial=initial) |
| | 34 | super(BaseCommentForm, self).__init__(data=data, initial=initial) |
| 38 | 35 | |
| 39 | 36 | def get_comment_object(self): |
| 40 | 37 | """ |
| … |
… |
class CommentForm(forms.Form):
|
| 47 | 44 | """ |
| 48 | 45 | if not self.is_valid(): |
| 49 | 46 | raise ValueError("get_comment_object may only be called on valid forms") |
| 50 | | |
| 51 | | new = Comment( |
| | 47 | |
| | 48 | comment_model = comments.get_model() |
| | 49 | |
| | 50 | new = comment_model( |
| 52 | 51 | content_type = ContentType.objects.get_for_model(self.target_object), |
| 53 | 52 | object_pk = force_unicode(self.target_object._get_pk_val()), |
| 54 | 53 | user_name = self.cleaned_data["name"], |
| … |
… |
class CommentForm(forms.Form):
|
| 63 | 62 | |
| 64 | 63 | # Check that this comment isn't duplicate. (Sometimes people post comments |
| 65 | 64 | # twice by mistake.) If it is, fail silently by returning the old comment. |
| 66 | | possible_duplicates = Comment.objects.filter( |
| | 65 | possible_duplicates = comment_model.objects.filter( |
| 67 | 66 | content_type = new.content_type, |
| 68 | 67 | object_pk = new.object_pk, |
| 69 | 68 | user_name = new.user_name, |
| … |
… |
class CommentForm(forms.Form):
|
| 79 | 78 | def security_errors(self): |
| 80 | 79 | """Return just those errors associated with security""" |
| 81 | 80 | errors = ErrorDict() |
| 82 | | for f in ["honeypot", "timestamp", "security_hash"]: |
| | 81 | for f in ["timestamp", "security_hash"]: |
| 83 | 82 | if f in self.errors: |
| 84 | 83 | errors[f] = self.errors[f] |
| 85 | 84 | return errors |
| 86 | 85 | |
| 87 | | def clean_honeypot(self): |
| 88 | | """Check that nothing's been entered into the honeypot.""" |
| 89 | | value = self.cleaned_data["honeypot"] |
| 90 | | if value: |
| 91 | | raise forms.ValidationError(self.fields["honeypot"].label) |
| 92 | | return value |
| 93 | | |
| 94 | 86 | def clean_security_hash(self): |
| 95 | 87 | """Check the security hash.""" |
| 96 | 88 | security_hash_dict = { |
| … |
… |
class CommentForm(forms.Form):
|
| 155 | 147 | """Generate a (SHA1) security hash from the provided info.""" |
| 156 | 148 | info = (content_type, object_pk, timestamp, settings.SECRET_KEY) |
| 157 | 149 | return sha_constructor("".join(info)).hexdigest() |
| | 150 | |
| | 151 | |
| | 152 | class CommentForm(BaseCommentForm): |
| | 153 | honeypot = forms.CharField(required=False, |
| | 154 | label=_('If you enter anything in this field ' |
| | 155 | 'your comment will be treated as spam')) |
| | 156 | |
| | 157 | def security_errors(self): |
| | 158 | """Return just those errors associated with security""" |
| | 159 | errors = super(CommentForm, self).security_errors() |
| | 160 | if "honeypot" in self.errors: |
| | 161 | errors['honeypot'] = self.errors['honeypot'] |
| | 162 | return errors |
| | 163 | |
| | 164 | def clean_honeypot(self): |
| | 165 | """Check that nothing's been entered into the honeypot.""" |
| | 166 | value = self.cleaned_data["honeypot"] |
| | 167 | if value: |
| | 168 | raise forms.ValidationError(self.fields["honeypot"].label) |
| | 169 | return value |