Skip to content

Commit

Permalink
ClearableFileInput that works for real
Browse files Browse the repository at this point in the history
  • Loading branch information
brutasse committed Dec 8, 2010
1 parent 0ed0f37 commit cfe99a9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions floppyforms/fields.py
Expand Up @@ -33,6 +33,10 @@ class FileField(FloppyField, forms.FileField):
widget = ClearableFileInput


class ImageField(FloppyField, forms.ImageField):
widget = ClearableFileInput


class MultipleChoiceField(forms.MultipleChoiceField):
widget = SelectMultiple

Expand Down
8 changes: 4 additions & 4 deletions floppyforms/templates/floppyforms/clearable_input.html
@@ -1,8 +1,8 @@
{% if value.url %}{{ initial_text }}: <a target="_blank" href="{{ value.url }}">{{ value }}</a>
{% if not required %}
<input type="checkbox" name="{{ name }}-clear" id="{{ name }}-clear_id">
<label for="{{ name }}-clear_id">{{ clear_checkbox_label }}</label><br/>
{% endif %}
<input type="checkbox" name="{{ checkbox_name }}" id="{{ checkbox_id }}">
<label for="{{ checkbox_id }}">{{ clear_checkbox_label }}</label>
{% endif %}<br />
{{ input_text }}:
{% endif %}
<input type="{{ type }}" name="{{ name }}"{% if value %} value="{{ value }}"{% endif %}{% if required %} required{% endif %}{% for attr in attrs.items %} {{ attr.0 }}="{{ attr.1 }}"{% endfor %}>
<input type="{{ type }}" name="{{ name }}"{% if required and not value %} required{% endif %}{% for attr in attrs.items %} {{ attr.0 }}="{{ attr.1 }}"{% endfor %}>
19 changes: 16 additions & 3 deletions floppyforms/widgets.py
Expand Up @@ -12,12 +12,13 @@ class FloppyInput(forms.TextInput):
def get_context_data(self):
return {}

def render(self, name, value, attrs=None, extra_context={}):
def get_context(self, name, value, attrs=None, extra_context={}):
context = {
'type': self.input_type,
'name': name,
'hidden': self.is_hidden,
'required': self.is_required,
'value': '',
}
context.update(extra_context)

Expand All @@ -34,6 +35,11 @@ def render(self, name, value, attrs=None, extra_context={}):
if value == True:
attrs[key] = False
context['attrs'] = attrs
return context

def render(self, name, value, attrs=None, extra_context={}):
context = self.get_context(name, value, attrs=attrs,
extra_context=extra_context)
return loader.render_to_string(self.template_name, context)


Expand Down Expand Up @@ -74,19 +80,26 @@ def render(self, name, value, attrs=None):
return super(FileInput, self).render(name, None, attrs=attrs)


class ClearableFileInput(FileInput):
class ClearableFileInput(FileInput, forms.ClearableFileInput):
template_name = 'floppyforms/clearable_input.html'
initial_text = ugettext_lazy('Currently')
input_text = ugettext_lazy('Change')
clear_checkbox_label = ugettext_lazy('Clear')

def get_context_data(self):
ctx = super(ClearableFileInput, self).get_context_data()
ctx['initial'] = self.initial_text
ctx['initial_text'] = self.initial_text
ctx['input_text'] = self.input_text
ctx['clear_checkbox_label'] = self.clear_checkbox_label
return ctx

def render(self, name, value, attrs=None, extra_context={}):
context = self.get_context(name, value, attrs=attrs,
extra_context=extra_context)
context['checkbox_name'] = self.clear_checkbox_name(name)
context['checkbox_id'] = self.clear_checkbox_id(context['checkbox_name'])
return loader.render_to_string(self.template_name, context)


class DateInput(forms.DateInput, FloppyInput):
input_type = 'date'
Expand Down

0 comments on commit cfe99a9

Please sign in to comment.