Ticket #9958: 9958.diff

File 9958.diff, 4.6 KB (added by Thejaswi Puthraya, 15 years ago)

git-patch against the latest checkout

  • django/contrib/comments/forms.py

    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  
    77from django.conf import settings
    88from django.http import Http404
    99from django.contrib.contenttypes.models import ContentType
    10 from models import Comment
     10from django.contrib import comments
    1111from django.utils.encoding import force_unicode
    1212from django.utils.hashcompat import sha_constructor
    1313from django.utils.text import get_text_list
    from django.utils.translation import ungettext, ugettext_lazy as _  
    1515
    1616COMMENT_MAX_LENGTH = getattr(settings,'COMMENT_MAX_LENGTH', 3000)
    1717
    18 class CommentForm(forms.Form):
     18class BaseCommentForm(forms.Form):
    1919    name          = forms.CharField(label=_("Name"), max_length=50)
    2020    email         = forms.EmailField(label=_("Email address"))
    2121    url           = forms.URLField(label=_("URL"), required=False)
    2222    comment       = forms.CharField(label=_('Comment'), widget=forms.Textarea,
    2323                                    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'))
    2724    content_type  = forms.CharField(widget=forms.HiddenInput)
    2825    object_pk     = forms.CharField(widget=forms.HiddenInput)
    2926    timestamp     = forms.IntegerField(widget=forms.HiddenInput)
    class CommentForm(forms.Form):  
    3431        if initial is None:
    3532            initial = {}
    3633        initial.update(self.generate_security_data())
    37         super(CommentForm, self).__init__(data=data, initial=initial)
     34        super(BaseCommentForm, self).__init__(data=data, initial=initial)
    3835
    3936    def get_comment_object(self):
    4037        """
    class CommentForm(forms.Form):  
    4744        """
    4845        if not self.is_valid():
    4946            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(
    5251            content_type = ContentType.objects.get_for_model(self.target_object),
    5352            object_pk    = force_unicode(self.target_object._get_pk_val()),
    5453            user_name    = self.cleaned_data["name"],
    class CommentForm(forms.Form):  
    6362
    6463        # Check that this comment isn't duplicate. (Sometimes people post comments
    6564        # 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(
    6766            content_type = new.content_type,
    6867            object_pk = new.object_pk,
    6968            user_name = new.user_name,
    class CommentForm(forms.Form):  
    7978    def security_errors(self):
    8079        """Return just those errors associated with security"""
    8180        errors = ErrorDict()
    82         for f in ["honeypot", "timestamp", "security_hash"]:
     81        for f in ["timestamp", "security_hash"]:
    8382            if f in self.errors:
    8483                errors[f] = self.errors[f]
    8584        return errors
    8685
    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 
    9486    def clean_security_hash(self):
    9587        """Check the security hash."""
    9688        security_hash_dict = {
    class CommentForm(forms.Form):  
    155147        """Generate a (SHA1) security hash from the provided info."""
    156148        info = (content_type, object_pk, timestamp, settings.SECRET_KEY)
    157149        return sha_constructor("".join(info)).hexdigest()
     150
     151
     152class 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
Back to Top