web/lib/django/contrib/comments/forms.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
    26         self.target_object = target_object
    26         self.target_object = target_object
    27         if initial is None:
    27         if initial is None:
    28             initial = {}
    28             initial = {}
    29         initial.update(self.generate_security_data())
    29         initial.update(self.generate_security_data())
    30         super(CommentSecurityForm, self).__init__(data=data, initial=initial)
    30         super(CommentSecurityForm, self).__init__(data=data, initial=initial)
    31         
    31 
    32     def security_errors(self):
    32     def security_errors(self):
    33         """Return just those errors associated with security"""
    33         """Return just those errors associated with security"""
    34         errors = ErrorDict()
    34         errors = ErrorDict()
    35         for f in ["honeypot", "timestamp", "security_hash"]:
    35         for f in ["honeypot", "timestamp", "security_hash"]:
    36             if f in self.errors:
    36             if f in self.errors:
   105         Does not set any of the fields that would come from a Request object
   105         Does not set any of the fields that would come from a Request object
   106         (i.e. ``user`` or ``ip_address``).
   106         (i.e. ``user`` or ``ip_address``).
   107         """
   107         """
   108         if not self.is_valid():
   108         if not self.is_valid():
   109             raise ValueError("get_comment_object may only be called on valid forms")
   109             raise ValueError("get_comment_object may only be called on valid forms")
   110         
   110 
   111         CommentModel = self.get_comment_model()
   111         CommentModel = self.get_comment_model()
   112         new = CommentModel(**self.get_comment_create_data())
   112         new = CommentModel(**self.get_comment_create_data())
   113         new = self.check_for_duplicate_comment(new)
   113         new = self.check_for_duplicate_comment(new)
   114         
   114 
   115         return new
   115         return new
   116         
   116 
   117     def get_comment_model(self):
   117     def get_comment_model(self):
   118         """
   118         """
   119         Get the comment model to create with this form. Subclasses in custom
   119         Get the comment model to create with this form. Subclasses in custom
   120         comment apps should override this, get_comment_create_data, and perhaps
   120         comment apps should override this, get_comment_create_data, and perhaps
   121         check_for_duplicate_comment to provide custom comment models.
   121         check_for_duplicate_comment to provide custom comment models.
   122         """
   122         """
   123         return Comment
   123         return Comment
   124         
   124 
   125     def get_comment_create_data(self):
   125     def get_comment_create_data(self):
   126         """
   126         """
   127         Returns the dict of data to be used to create a comment. Subclasses in
   127         Returns the dict of data to be used to create a comment. Subclasses in
   128         custom comment apps that override get_comment_model can override this
   128         custom comment apps that override get_comment_model can override this
   129         method to add extra fields onto a custom comment model.
   129         method to add extra fields onto a custom comment model.
   138             submit_date  = datetime.datetime.now(),
   138             submit_date  = datetime.datetime.now(),
   139             site_id      = settings.SITE_ID,
   139             site_id      = settings.SITE_ID,
   140             is_public    = True,
   140             is_public    = True,
   141             is_removed   = False,
   141             is_removed   = False,
   142         )
   142         )
   143         
   143 
   144     def check_for_duplicate_comment(self, new):
   144     def check_for_duplicate_comment(self, new):
   145         """
   145         """
   146         Check that a submitted comment isn't a duplicate. This might be caused
   146         Check that a submitted comment isn't a duplicate. This might be caused
   147         by someone posting a comment twice. If it is a dup, silently return the *previous* comment.
   147         by someone posting a comment twice. If it is a dup, silently return the *previous* comment.
   148         """
   148         """
   149         possible_duplicates = self.get_comment_model()._default_manager.filter(
   149         possible_duplicates = self.get_comment_model()._default_manager.using(
       
   150             self.target_object._state.db
       
   151         ).filter(
   150             content_type = new.content_type,
   152             content_type = new.content_type,
   151             object_pk = new.object_pk,
   153             object_pk = new.object_pk,
   152             user_name = new.user_name,
   154             user_name = new.user_name,
   153             user_email = new.user_email,
   155             user_email = new.user_email,
   154             user_url = new.user_url,
   156             user_url = new.user_url,
   155         )
   157         )
   156         for old in possible_duplicates:
   158         for old in possible_duplicates:
   157             if old.submit_date.date() == new.submit_date.date() and old.comment == new.comment:
   159             if old.submit_date.date() == new.submit_date.date() and old.comment == new.comment:
   158                 return old
   160                 return old
   159                 
   161 
   160         return new
   162         return new
   161 
   163 
   162     def clean_comment(self):
   164     def clean_comment(self):
   163         """
   165         """
   164         If COMMENTS_ALLOW_PROFANITIES is False, check that the comment doesn't
   166         If COMMENTS_ALLOW_PROFANITIES is False, check that the comment doesn't