<commit_before>from django import forms
from django.contrib.comments.forms import CommentForm
from threaded_comments.models import ThreadedComment
from django.conf import settings
from django.utils.hashcompat import sha_constructor

class ThreadedCommentForm(CommentForm):
    parent = forms.IntegerField(required=False, widget=forms.HiddenInput)

    def __init__(self, target_object, parent=None, data=None, initial=None):
        self.parent = parent
        if initial is None:
            initial = {}
        initial.update({'parent': self.parent})
        super(ThreadedCommentForm, self).__init__(target_object, data=data, initial=initial)

    def get_comment_object(self):
        new_comment = super(ThreadedCommentForm, self).get_comment_object()
        new_threaded_comment = ThreadedComment(content_type = new_comment.content_type,
                                               object_pk    = new_comment.object_pk,
                                               user_name    = new_comment.user_name,
                                               user_email   = new_comment.user_email,
                                               user_url     = new_comment.user_url,
                                               comment      = new_comment.comment,
                                               submit_date  = new_comment.submit_date,
                                               site_id      = new_comment.site_id,
                                               is_public    = new_comment.is_public,
                                               is_removed   = new_comment.is_removed,
                                               parent_id    = self.cleaned_data['parent'])
        return new_threaded_comment

    def clean_security_hash(self):
        """Check the security hash."""
        security_hash_dict = {
            'content_type' : self.data.get("content_type", ""),
            'object_pk'    : self.data.get("object_pk", ""),
            'timestamp'    : self.data.get("timestamp", ""),
            'parent'       : self.data.get("parent", "")
        }
        expected_hash = self.generate_security_hash(**security_hash_dict)
        actual_hash = self.cleaned_data["security_hash"]
        if expected_hash != actual_hash:
            raise forms.ValidationError("Security hash check failed.")
        return actual_hash

    def initial_security_hash(self, timestamp):
        """
        Generate the initial security hash from self.content_object
        and a (unix) timestamp.
        """

        if self.parent:
            parent = str(parent)
        else:
            parent = ""

        initial_security_dict = {
            'content_type' : str(self.target_object._meta),
            'object_pk'    : str(self.target_object._get_pk_val()),
            'timestamp'    : str(timestamp),
            'parent'       : parent,
          }
        return self.generate_security_hash(**initial_security_dict)

    def generate_security_hash(self, content_type, object_pk, timestamp, parent):
        """Generate a (SHA1) security hash from the provided info."""
        info = (content_type, object_pk, timestamp, parent, settings.SECRET_KEY)
        return sha_constructor("".join(info)).hexdigest()
<commit_msg>Fixed a referenced before assignment error. Thanks BartTC<commit_after>from django import forms
from django.contrib.comments.forms import CommentForm
from threaded_comments.models import ThreadedComment
from django.conf import settings
from django.utils.hashcompat import sha_constructor

class ThreadedCommentForm(CommentForm):
    parent = forms.IntegerField(required=False, widget=forms.HiddenInput)

    def __init__(self, target_object, parent=None, data=None, initial=None):
        self.parent = parent
        if initial is None:
            initial = {}
        initial.update({'parent': self.parent})
        super(ThreadedCommentForm, self).__init__(target_object, data=data, initial=initial)

    def get_comment_object(self):
        new_comment = super(ThreadedCommentForm, self).get_comment_object()
        new_threaded_comment = ThreadedComment(content_type = new_comment.content_type,
                                               object_pk    = new_comment.object_pk,
                                               user_name    = new_comment.user_name,
                                               user_email   = new_comment.user_email,
                                               user_url     = new_comment.user_url,
                                               comment      = new_comment.comment,
                                               submit_date  = new_comment.submit_date,
                                               site_id      = new_comment.site_id,
                                               is_public    = new_comment.is_public,
                                               is_removed   = new_comment.is_removed,
                                               parent_id    = self.cleaned_data['parent'])
        return new_threaded_comment

    def clean_security_hash(self):
        """Check the security hash."""
        security_hash_dict = {
            'content_type' : self.data.get("content_type", ""),
            'object_pk'    : self.data.get("object_pk", ""),
            'timestamp'    : self.data.get("timestamp", ""),
            'parent'       : self.data.get("parent", "")
        }
        expected_hash = self.generate_security_hash(**security_hash_dict)
        actual_hash = self.cleaned_data["security_hash"]
        if expected_hash != actual_hash:
            raise forms.ValidationError("Security hash check failed.")
        return actual_hash

    def initial_security_hash(self, timestamp):
        """
        Generate the initial security hash from self.content_object
        and a (unix) timestamp.
        """

        if self.parent:
            parent = str(self.parent)
        else:
            parent = ""

        initial_security_dict = {
            'content_type' : str(self.target_object._meta),
            'object_pk'    : str(self.target_object._get_pk_val()),
            'timestamp'    : str(timestamp),
            'parent'       : parent,
          }
        return self.generate_security_hash(**initial_security_dict)

    def generate_security_hash(self, content_type, object_pk, timestamp, parent):
        """Generate a (SHA1) security hash from the provided info."""
        info = (content_type, object_pk, timestamp, parent, settings.SECRET_KEY)
        return sha_constructor("".join(info)).hexdigest()

{'fraction_non_alphanumeric': 0.08633530931752546, 'fraction_numerical': 0.0003039975680194558, 'mean_word_length': 1.8191945158526135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7430922', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1575, 'n_words': 321}