<commit_before>from django import forms
from django.forms.models import inlineformset_factory

from .models import Knot, Link


class KnotForm(forms.ModelForm):
    class Meta:
        model = Knot
        fields = ['name', 'photo', 'other_names', 'creator_name', 'notes', 'tags']


class InlineLinkForm(forms.ModelForm):
    class Meta:
        model = Link
        fields = ['name', 'link']

LinkFormset = inlineformset_factory(Knot, Link)<commit_msg>Set up form to use special widget for tags<commit_after>from django import forms
from django.forms.models import inlineformset_factory
from django.utils import six

from .models import Knot, Link


def edit_string_for_tags(tags):
    """
    Convert list of Tags into comma-separated list.

    We can't use the one built into django-taggit, because select2 freaks out
    about the space after the comma.
    """
    names = []
    for tag in tags:
        name = tag.name
        if ',' in name or ' ' in name:
            names.append('"%s"' % name)
        else:
            names.append(name)
    return ','.join(sorted(names))


class HiddenTagWidget(forms.HiddenInput):
    """ For use with select2 and ajax tagging """
    def render(self, name, value, attrs=None):
        if value is not None and not isinstance(value, six.string_types):
            value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
        return super(HiddenTagWidget, self).render(name, value, attrs)


class KnotForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(KnotForm, self).__init__(*args, **kwargs)
        print self.fields.keys()
        self.fields['tags'].widget = HiddenTagWidget()

    class Meta:
        model = Knot
        fields = ['name', 'photo', 'other_names', 'creator_name', 'notes', 'tags']


class InlineLinkForm(forms.ModelForm):
    class Meta:
        model = Link
        fields = ['name', 'link']

LinkFormset = inlineformset_factory(Knot, Link)
{'fraction_non_alphanumeric': 0.11377551020408164, 'fraction_numerical': 0.0010204081632653062, 'mean_word_length': 3.1546610169491527, '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': '1447921', 'n_tokens_mistral': 594, 'n_tokens_neox': 578, 'n_words': 178}