<commit_before>from django import forms
from django.utils.safestring import mark_safe

from django.conf import settings
from django.utils.html import escape
from django.utils.translation import ugettext_lazy as _

from taggit.utils import edit_string_for_tags, parse_tags


class BootstrapChoiceMixin(object):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('attrs', {})
        kwargs['attrs'].update({'class': 'form-check-input'})
        super(BootstrapChoiceMixin, self).__init__(*args, **kwargs)


class BootstrapCheckboxInput(BootstrapChoiceMixin, forms.CheckboxInput):
    pass


class BootstrapRadioSelect(BootstrapChoiceMixin, forms.RadioSelect):
    option_template_name = 'helper/forms/widgets/radio_option.html'


class BootstrapFileInput(forms.FileInput):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('attrs', {})
        kwargs['attrs'].update({'class': 'form-control'})
        super(BootstrapFileInput, self).__init__(*args, **kwargs)


class PriceInput(forms.TextInput):
    template_name = "helper/forms/widgets/price_input.html"

    def get_context(self, name, value, attrs):
        ctx = super(PriceInput, self).get_context(name, value, attrs)
        ctx['widget'].setdefault('attrs', {})
        ctx['widget']['attrs']['class'] = 'form-control col-3'
        ctx['widget']['attrs']['pattern'] = "[\\d\\.,]*"
        ctx['currency'] = settings.FROIDE_CONFIG['currency']
        return ctx


class TagAutocompleteWidget(forms.TextInput):
    class Media:

        js = (
            'js/tagautocomplete.js',
        )

        css_list = [
            'css/tagautocomplete.css'
        ]
        css = {
            'screen': css_list
        }

    def __init__(self, *args, **kwargs):
        self.autocomplete_url = kwargs.pop('autocomplete_url', None)
        super(TagAutocompleteWidget, self).__init__(*args, **kwargs)

    def value_from_datadict(self, data, files, name):
        """ Force comma separation of tags by adding trailing comma """
        val = data.get(name, None)
        if val is None:
            return None
        return val + ','

    def render(self, name, value, attrs=None, renderer=None):
        """ Render HTML code """
        if value is not None and not isinstance(value, str):
            value_list = [o.tag for o in value]
            value = edit_string_for_tags(value_list)

        options = [
            '<option value="{value}" selected>{value}</option>'.format(
                value=escape(str(o))) for o in parse_tags(value)]
        options = '\n'.join(options)

        context = {
            'data-additemtext': _('Press Enter to add <b>${value}</b>'),
            'data-uniqueitemtext': _('This tag is already set.'),
            'data-loading': _('Searching…'),
            'data-noresults': _('No results'),
            'data-nochoices': _('No results'),
            'data-fetchurl': self.autocomplete_url
        }
        context = {k: escape(v) for k, v in context.items()}
        context = ['%s="%s"' % (k, v) for k, v in context.items()]

        html = super(TagAutocompleteWidget, self).render(
            name, value, attrs, renderer=renderer
        )
        html = """<div style="display: none">{input}</div>
        <select class="tagautocomplete"
        {dataitems}
        id="{objectid}_select" multiple>{options}</select>""".format(
                input=html,
                objectid=attrs['id'],
                options=options,<commit_msg>Fix widget for upgraded django taggit<commit_after>from django import forms
from django.utils.safestring import mark_safe

from django.conf import settings
from django.utils.html import escape
from django.utils.translation import ugettext_lazy as _

from taggit.utils import edit_string_for_tags, parse_tags


class BootstrapChoiceMixin(object):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('attrs', {})
        kwargs['attrs'].update({'class': 'form-check-input'})
        super(BootstrapChoiceMixin, self).__init__(*args, **kwargs)


class BootstrapCheckboxInput(BootstrapChoiceMixin, forms.CheckboxInput):
    pass


class BootstrapRadioSelect(BootstrapChoiceMixin, forms.RadioSelect):
    option_template_name = 'helper/forms/widgets/radio_option.html'


class BootstrapFileInput(forms.FileInput):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('attrs', {})
        kwargs['attrs'].update({'class': 'form-control'})
        super(BootstrapFileInput, self).__init__(*args, **kwargs)


class PriceInput(forms.TextInput):
    template_name = "helper/forms/widgets/price_input.html"

    def get_context(self, name, value, attrs):
        ctx = super(PriceInput, self).get_context(name, value, attrs)
        ctx['widget'].setdefault('attrs', {})
        ctx['widget']['attrs']['class'] = 'form-control col-3'
        ctx['widget']['attrs']['pattern'] = "[\\d\\.,]*"
        ctx['currency'] = settings.FROIDE_CONFIG['currency']
        return ctx


class TagAutocompleteWidget(forms.TextInput):
    class Media:

        js = (
            'js/tagautocomplete.js',
        )

        css_list = [
            'css/tagautocomplete.css'
        ]
        css = {
            'screen': css_list
        }

    def __init__(self, *args, **kwargs):
        self.autocomplete_url = kwargs.pop('autocomplete_url', None)
        super(TagAutocompleteWidget, self).__init__(*args, **kwargs)

    def value_from_datadict(self, data, files, name):
        """ Force comma separation of tags by adding trailing comma """
        val = data.get(name, None)
        if val is None:
            return None
        return val + ','

    def render(self, name, value, attrs=None, renderer=None):
        """ Render HTML code """
        if value is not None and not isinstance(value, str):
            value = edit_string_for_tags(value)

        options = [
            '<option value="{value}" selected>{value}</option>'.format(
                value=escape(str(o))) for o in parse_tags(value)]
        options = '\n'.join(options)

        context = {
            'data-additemtext': _('Press Enter to add <b>${value}</b>'),
            'data-uniqueitemtext': _('This tag is already set.'),
            'data-loading': _('Searching…'),
            'data-noresults': _('No results'),
            'data-nochoices': _('No results'),
            'data-fetchurl': self.autocomplete_url
        }
        context = {k: escape(v) for k, v in context.items()}
        context = ['%s="%s"' % (k, v) for k, v in context.items()]

        html = super(TagAutocompleteWidget, self).render(
            name, value, attrs, renderer=renderer
        )
        html = """<div style="display: none">{input}</div>
        <select class="tagautocomplete"
        {dataitems}
        id="{objectid}_select" multiple>{options}</select>""".format(
                input=html,
                objectid=attrs['id'],
                options=options,
{'fraction_non_alphanumeric': 0.14098313391956177, 'fraction_numerical': 0.00028830906732016723, 'mean_word_length': 2.75027027027027, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '6925644', 'n_tokens_mistral': 2053, 'n_tokens_neox': 2001, 'n_words': 448}