<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import os
import hashlib

from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_bytes

from ..core import utils
from ..core.utils.markdown import Markdown
from ..topic.models import Topic
from .poll.models import CommentPoll, CommentPollChoice
from .models import Comment


class CommentForm(forms.ModelForm):

    comment = forms.CharField(
        max_length=settings.ST_COMMENT_MAX_LEN,
        widget=forms.Textarea)
    comment_hash = forms.CharField(
        max_length=128,
        widget=forms.HiddenInput,
        required=False)

    class Meta:
        model = Comment
        fields = ['comment', ]

    def __init__(self, user=None, topic=None, *args, **kwargs):
        super(CommentForm, self).__init__(*args, **kwargs)
        self.user = user
        self.topic = topic
        self.mentions = None  # {username: User, }
        self.polls = None  # {polls: [], choices: []}
        self.fields['comment'].widget.attrs['placeholder'] = _("Write comment...")

    def get_comment_hash(self):
        assert not self.instance.pk

        # This gets saved into
        # User.last_post_hash,
        # it does not matter whether
        # is a safe string or not

        # todo: add topic.pk if there is a self.topic
        return (self.cleaned_data.get('comment_hash', None) or
                hashlib
                .md5(smart_bytes(self.cleaned_data['comment']))
                .hexdigest())

    def _get_comment_html(self):
        user = self.user or self.instance.user
        markdown = Markdown(no_follow=not user.st.is_moderator)
        comment_html = markdown.render(self.cleaned_data['comment'])
        self.mentions = markdown.get_mentions()
        self.polls = markdown.get_polls()
        return comment_html

    def _save_polls(self):
        assert self.instance.pk
        assert self.polls is not None

        polls = self.polls['polls']
        choices = self.polls['choices']
<commit_msg>comment.forms add thread id to hash<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import os
import hashlib

from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_bytes

from ..core import utils
from ..core.utils.markdown import Markdown
from ..topic.models import Topic
from .poll.models import CommentPoll, CommentPollChoice
from .models import Comment


class CommentForm(forms.ModelForm):

    comment = forms.CharField(
        max_length=settings.ST_COMMENT_MAX_LEN,
        widget=forms.Textarea)
    comment_hash = forms.CharField(
        max_length=128,
        widget=forms.HiddenInput,
        required=False)

    class Meta:
        model = Comment
        fields = ['comment', ]

    def __init__(self, user=None, topic=None, *args, **kwargs):
        super(CommentForm, self).__init__(*args, **kwargs)
        self.user = user
        self.topic = topic
        self.mentions = None  # {username: User, }
        self.polls = None  # {polls: [], choices: []}
        self.fields['comment'].widget.attrs['placeholder'] = _("Write comment...")

    def get_comment_hash(self):
        assert not self.instance.pk
        assert self.topic

        # This gets saved into
        # User.last_post_hash,
        # it does not matter whether
        # is a safe string or not
        comment_hash = self.cleaned_data.get('comment_hash', None)

        if comment_hash:
            return comment_hash

        md5 = hashlib.md5()
        md5.update(smart_bytes(self.cleaned_data['comment']))
        md5.update(smart_bytes(
            'thread-{}'.format(self.topic.pk)))
        return md5.hexdigest()

    def _get_comment_html(self):
        user = self.user or self.instance.user
        markdown = Markdown(no_follow=not user.st.is_moderator)
        comment_html = markdown.render(self.cleaned_data['comment'])
        self.mentions = markdown.get_mentions()
        self.polls = markdown.get_polls()
        return comment_html

    def _save_polls(self):
        assert self.instance.pk
        assert self.polls is not None

        polls = self.polls['polls']
        choices = self.polls['choices']

{'fraction_non_alphanumeric': 0.10098465765972063, 'fraction_numerical': 0.003205862147927639, 'mean_word_length': 2.6430358632193496, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1425016', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1271, 'n_words': 318}