Skip to content

Commit

Permalink
Text input widget render
Browse files Browse the repository at this point in the history
  • Loading branch information
kmmbvnr committed Jun 25, 2014
1 parent e3c2b1f commit 1922a3f
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 39 deletions.
6 changes: 6 additions & 0 deletions tests/unit/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django import forms


class AllElementsForm(forms.Form):
text_input = forms.CharField(help_text="Sample text input")
another_input = forms.CharField(show_hidden_initial=True)
38 changes: 38 additions & 0 deletions tests/unit/templates/unit/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% load viewflow %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Form Sample Template</title>

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Form example</h1>
</div>

<form method="POST">
{% viewform 'unit/form_layout.html' %}
{% endviewform %}
</form>

<div class="page-header">
<h1>Standard django</h1>
</div>

<form method="POST">
<table>
{{ form.as_table }}
</table>
</form>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
</body>
</html>

7 changes: 7 additions & 0 deletions tests/unit/templates/unit/form_layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% load viewflow %}

{% for field in form.visible_fields %}
{% viewpart field field.html_name %}
{% viewfield field %}
{% endviewpart %}
{% endfor %}
10 changes: 0 additions & 10 deletions tests/unit/templates/unit/test_render_form.html

This file was deleted.

5 changes: 5 additions & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic.base import TemplateView
from django.views.generic.edit import FormView
from unit.forms import AllElementsForm


admin.autodiscover()
Expand All @@ -11,5 +13,8 @@
url(r'^admin/', include(admin.site.urls)),
url(r'^examples/shipment/', include('examples.shipment.urls')),
url(r'^examples/helloworld/', include('examples.helloworld.urls')),
url(r'^examples/form/$', FormView.as_view(
form_class=AllElementsForm,
template_name='unit/form.html')),
url(r'^', include('examples.website')),
)
12 changes: 7 additions & 5 deletions viewflow/templates/viewflow/form/widgets/text_input.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{% include 'viewflow/form/field_errors.html' %}
<label class="control-label">
{{ field.label }}
<input type="text" class="form-control" name="{{ field.name }}" id="id_{{ field.name }}" value="{{ field.value }}" >
<div class="form-group">
{% include 'viewflow/form/field_errors.html' %}
<label for="{{ field.id_for_label }}" class="control-label"> {{ field.label }} </label>
<input type="text" class="form-control" name="{{ field.name }}" id="id_{{ field.name }}" {% if field.value %}value="{{ field.value }}"{% endif %}>
{% if field.help_text %}
<p class="help-text">{{ field.help_text }}</p>
{% endif %}
</label>
{{ hidden_initial }}
</div>
<br/>
6 changes: 3 additions & 3 deletions viewflow/templatetags/viewflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def __init__(self, field):
self.field = field

def render(self, context):
field = self.field.resolve(context)
return render_widget(field.widget, field.name)
bound_field = self.field.resolve(context)
return render_widget(bound_field.field.widget, bound_field)


@register.tag
Expand Down Expand Up @@ -166,6 +166,6 @@ def viewfield(parser, token):
bits = token.split_contents()
if len(bits) < 2:
raise TemplateSyntaxError("'{}' takes at least one argument <field>".format(bits[0]))

field = parser.compile_filter(bits[1])
return ViewFieldNode(field)
74 changes: 53 additions & 21 deletions viewflow/viewform.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,71 @@
from singledispatch import singledispatch
from django.forms import widgets
from django.template import Context
from django.template.loader import get_template


@singledispatch
def render_widget(widget, name, value, attrs=None):
return widdget.render(name, value, attrs=attrs)
def render_widget_template(bound_field, template_name, widget=None, attrs=None):
"""
Renders field depends on widget template
"""
if widget is None:
widget = bound_field.field.widget

if bound_field.field.localize:
widget.is_localized = True

@render_widget.register(widgets.TextInput)
def _(widget, name, value, attrs=None):
pass
attrs = attrs or {}
auto_id = bound_field.auto_id

if auto_id and 'id' not in attrs and 'id' not in widget.attrs:
attrs['id'] = auto_id

hidden_initial = ''
if bound_field.field.show_hidden_initial:
hidden_initial = bound_field.as_hidden(only_initial=True)

template = get_template(template_name)
return template.render(Context({
'field': bound_field,
'widget': widget,
'id': auto_id,
'hidden_initial': hidden_initial
}))

@render_widget.register(widgets.Textarea)
def _(widget, name, value, attrs=None):
pass

@singledispatch
def render_widget(widget, bound_field, attrs=None):
result = bound_field.as_widget(attrs=attrs)
if bound_field.field.show_hidden_initial:
result += bound_field.as_hidden(only_initial=True, attrs=attrs)
return result


@render_widget.register(widgets.TextInput)
def _(widget, bound_field, template_name='viewflow/form/widgets/text_input.html', attrs=None):
return render_widget_template(bound_field, template_name=template_name, attrs=attrs)

@render_widget.register(widgets.CheckboxInput)
def _(widget, name, value, attrs=None):
pass

@render_widget.register(widgets.Textarea) # NOQA
def _(widget, bound_field, template_name='viewflow/form/widgets/text_input.html', attrs=None):
return render_widget_template(bound_field, template_name=template_name, attrs=attrs)

@render_widget.register(widgets.Select)
def _(widget, name, value, attrs=None):
pass

@render_widget.register(widgets.CheckboxInput) # NOQA
def _(widget, bound_field, template_name='viewflow/form/widgets/text_input.html', attrs=None):
return render_widget_template(bound_field, template_name=template_name, attrs=attrs)

@render_widget.register(widgets.RadioSelect)
def _(widget, name, value, attrs=None):
pass

@render_widget.register(widgets.Select) # NOQA
def _(widget, bound_field, template_name='viewflow/form/widgets/text_input.html', attrs=None):
return render_widget_template(bound_field, template_name=template_name, attrs=attrs)

@render_widget.register(widgets.SelectMultiple)
def _(widget, name, value, attrs=None):
pass

@render_widget.register(widgets.RadioSelect) # NOQA
def _(widget, bound_field, template_name='viewflow/form/widgets/text_input.html', attrs=None):
return render_widget_template(bound_field, template_name=template_name, attrs=attrs)


@render_widget.register(widgets.SelectMultiple) # NOQA
def _(widget, bound_field, template_name='viewflow/form/widgets/text_input.html', attrs=None):
return render_widget_template(bound_field, template_name=template_name, attrs=attrs)

0 comments on commit 1922a3f

Please sign in to comment.