# -*- coding: utf-8 -*- import json from django import forms from django.forms.utils import flatatt from django.utils.html import format_html from django.utils.safestring import mark_safe class NullCharField(forms.CharField): def clean(self, value): cleaned = super(NullCharField, self).clean(value) return cleaned if len(cleaned) else None class FullTextArea(forms.CharField): widget = forms.Textarea(attrs={'class': 'span12'}) class SearchField(forms.CharField): widget = forms.TextInput(attrs={ 'class': 'search-query', 'autocomplete': 'off', 'placeholder': '', }) class ChoiceField(forms.ChoiceField): def __init__(self, *args, **kwargs): super(ChoiceField, self).__init__(*args, **kwargs) self.widget.attrs['class'] = 'span12' class TextInput(forms.TextInput): def __init__(self, *args, **kwargs): super(TextInput, self).__init__(*args, **kwargs) self.attrs['class'] = 'span12' class AutocompleteCharField(forms.CharField): widget = forms.TextInput(attrs={ 'class': "input typeahead", 'data-provide': "typeahead" }) def __init__(self, values, *args, **kwargs): super(AutocompleteCharField, self).__init__(*args, **kwargs) if not type(values) is str: values = json.dumps(list(values)) self.widget.attrs['data-source'] = values class AutocompleteTextarea(forms.Textarea): def __init__(self, rows=8, choices=None): super(AutocompleteTextarea, self).__init__() self.attrs = { 'rows': rows, 'class': "span12 autocomplete", 'data-source': json.dumps(choices) } class BaseForm(forms.Form): required_css_class = "required" class BaseModelForm(forms.ModelForm): required_css_class = "required" class SearchFieldInput(forms.TextInput): def render(self, name, value, attrs=None): field = super(SearchFieldInput, self).render(name, value, attrs) final_attrs = self.build_attrs(attrs) output = format_html(u'''