<commit_before>from snowpenguin.django.recaptcha2.fields import ReCaptchaField
from snowpenguin.django.recaptcha2.widgets import ReCaptchaWidget
from registration.forms import RegistrationFormTermsOfService
from django.utils.translation import ugettext_lazy as _
from django import forms
from .models import Profile, Thread, Post


class RecaptchaRegistrationForm(RegistrationFormTermsOfService):
    captcha = ReCaptchaField(widget=ReCaptchaWidget())

form_control = {'class': 'form-control'}
class ProfileForm(forms.ModelForm):
    is_active = forms.BooleanField(required=False)
    class Meta:
        model = Profile
        fields = ('avatar', 'country', 'city', 'about')
        widgets = {
            'country': forms.Select(attrs=form_control),
            'city': forms.TextInput(attrs={'class': 'form-control', 'placeholder': _('Enter your city')}),
            'about': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
        }


class AskappClearableFileInput(forms.widgets.ClearableFileInput):
    template_with_initial = (
        '%(clear_template)s<br />%(input_text)s: %(input)s'
    )


class ThreadForm(forms.ModelForm):
    class Meta:
        model = Thread
        fields = ('thread_type', 'original', 'link', 'title', 'text', 'tags', 'image')
        widgets = {
            'original': forms.TextInput(),
            'image': AskappClearableFileInput()
        }
        error_messages = {
            'original': {
                'invalid_choice': _('This thread is not found'),
            },
        }

    def __init__(self, user, *args, **kwargs):
        super(ThreadForm, self).__init__(*args, **kwargs)
        if self.instance and not self.instance.id:
            self.instance.user = user
        elif not user.is_staff:
            self.fields.pop('thread_type')

    def clean(self):
        cleaned_data = super(ThreadForm, self).clean()
        link = cleaned_data.get("link")
        thread_type = cleaned_data.get("thread_type")

        #if thread_type and self.initial.get('thread_type', thread_type) != thread_type and not self.user.is_staff and not self.has_error('title'):
        #    self.add_error('title', 'You are not allowed to change the thread type')
        if thread_type in Thread.TYPES_WITH_LINK and not self.has_error('link'):
            if not link:
                msg = _("This field is required")
                self.add_error('link', msg)
            else:
                youtube_info = Thread(link=link).parse_youtube_url()
                if youtube_info:
                    exists = Thread.objects.filter(link__contains=youtube_info['id'])
                    if len(exists):
                        msg = _("Sorry, someone has already posted this video")
                        self.add_error('link', msg)
                elif thread_type == Thread.YOUTUBE:
                    msg = _("This is not a Youtube URL")
                    self.add_error('link', msg)
        elif self.has_error('link') and thread_type not in Thread.TYPES_WITH_LINK:
            del self.errors['link']


class ReplyForm(forms.ModelForm):
    is_answer = forms.BooleanField(required=False)
    class Meta:
        model = Post
        fields = ('text', 'is_answer')
        widgets = {
            'text': forms.Textarea(attrs={'class': 'form-control', 'rows': 5}),
        }<commit_msg>[T-67] bugfix: exclude thread itself from duplicates search when saving edits<commit_after>from snowpenguin.django.recaptcha2.fields import ReCaptchaField
from snowpenguin.django.recaptcha2.widgets import ReCaptchaWidget
from registration.forms import RegistrationFormTermsOfService
from django.utils.translation import ugettext_lazy as _
from django import forms
from .models import Profile, Thread, Post


class RecaptchaRegistrationForm(RegistrationFormTermsOfService):
    captcha = ReCaptchaField(widget=ReCaptchaWidget())

form_control = {'class': 'form-control'}
class ProfileForm(forms.ModelForm):
    is_active = forms.BooleanField(required=False)
    class Meta:
        model = Profile
        fields = ('avatar', 'country', 'city', 'about')
        widgets = {
            'country': forms.Select(attrs=form_control),
            'city': forms.TextInput(attrs={'class': 'form-control', 'placeholder': _('Enter your city')}),
            'about': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
        }


class AskappClearableFileInput(forms.widgets.ClearableFileInput):
    template_with_initial = (
        '%(clear_template)s<br />%(input_text)s: %(input)s'
    )


class ThreadForm(forms.ModelForm):
    class Meta:
        model = Thread
        fields = ('thread_type', 'original', 'link', 'title', 'text', 'tags', 'image')
        widgets = {
            'original': forms.TextInput(),
            'image': AskappClearableFileInput()
        }
        error_messages = {
            'original': {
                'invalid_choice': _('This thread is not found'),
            },
        }

    def __init__(self, user, *args, **kwargs):
        super(ThreadForm, self).__init__(*args, **kwargs)
        if self.instance and not self.instance.id:
            self.instance.user = user
        elif not user.is_staff:
            self.fields.pop('thread_type')

    def clean(self):
        cleaned_data = super(ThreadForm, self).clean()
        link = cleaned_data.get("link")
        thread_type = cleaned_data.get("thread_type")

        #if thread_type and self.initial.get('thread_type', thread_type) != thread_type and not self.user.is_staff and not self.has_error('title'):
        #    self.add_error('title', 'You are not allowed to change the thread type')
        if thread_type in Thread.TYPES_WITH_LINK and not self.has_error('link'):
            if not link:
                msg = _("This field is required")
                self.add_error('link', msg)
            else:
                youtube_info = Thread(link=link).parse_youtube_url()
                if youtube_info:
                    exists = Thread.objects.filter(link__contains=youtube_info['id']).exclude(id=self.instance.id)
                    if len(exists):
                        msg = _("Sorry, someone has already posted this video")
                        self.add_error('link', msg)
                elif thread_type == Thread.YOUTUBE:
                    msg = _("This is not a Youtube URL")
                    self.add_error('link', msg)
        elif self.has_error('link') and thread_type not in Thread.TYPES_WITH_LINK:
            del self.errors['link']


class ReplyForm(forms.ModelForm):
    is_answer = forms.BooleanField(required=False)
    class Meta:
        model = Post
        fields = ('text', 'is_answer')
        widgets = {
            'text': forms.Textarea(attrs={'class': 'form-control', 'rows': 5}),
        }
{'fraction_non_alphanumeric': 0.11972348874834535, 'fraction_numerical': 0.0014708045300779527, 'mean_word_length': 2.5509138381201044, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7508051', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1882, 'n_words': 448}