Steve Losh avatar Steve Losh committed 77d99d8

Import the base functionality from the LindyHub repo.

Comments (0)

Files changed (11)

Empty file added.

goodfields/templates/goodfields/fields/checkbox.html

+<li>
+    <label>
+        <input id="{{ field.auto_id }}" type="checkbox" class="checkbox"
+               name="{{ field.html_name }}"
+               {% if checked %}checked="checked"{% endif %} />
+        {% firstof label field.label %}
+        {{ field.errors }}
+        <span class="form-help-text">{{ field.help_text }}</span>
+    </label>
+</li>

goodfields/templates/goodfields/fields/password.html

+{% include "goodfields/fields/validation.html" %}
+
+<div class="field">
+    <label for="{{ field.auto_id }}">{% firstof label field.label %} {{ field.errors }}</label>
+    <input type="password" class="password" id="{{ field.auto_id }}" 
+           name="{{ field.html_name }}" />
+    {% if field.help_text %}
+        <p class="form-help-text">{{ field.help_text }}</p>
+    {% endif %}
+</div>

goodfields/templates/goodfields/fields/radio.html

+<label>{{ label }} {{ field.errors }}</label>
+<ul class="fields">
+    {% for choice in field.field.choices %}
+        <li>
+            <label>
+            <input type="radio" class="radio"
+                   name="{{ field.html_name }}"
+                   value="{{ choice.0 }}"
+                   {% ifequal selected choice.0 %}checked="checked"{% endifequal %} />
+            {{ choice.1 }}
+            </label>
+        </li>
+    {% endfor %}
+</ul>
+{% if field.help_text %}
+    <p class="form-help-text">{{ field.help_text }}</p>
+{% endif %}

goodfields/templates/goodfields/fields/text-date.html

+{% include "goodfields/fields/validation.html" %}
+
+<div class="field">
+    <label for="{{ field.auto_id }}">{% firstof label field.label %} {{ field.errors }}</label>
+    <input type="text" class="text" id="{{ field.auto_id }}" 
+           name="{{ field.html_name }}"
+           value="{% if initial %}{{ initial }}{% endif %}" />
+    {% if field.help_text %}
+        <p class="form-help-text">{{ field.help_text }}</p>
+    {% endif %}
+</div>
+
+<script type="text/javascript">
+    $(document).ready(function() {
+        $('#{{ field.auto_id }}').datepicker({
+            dateFormat: 'yy-mm-dd',
+            minDate: '-0d',
+            firstDay: 1
+        });
+    });
+</script>

goodfields/templates/goodfields/fields/text-datetime.html

+<div class="split-datetime-field">
+    {{ field.errors }}
+    
+    <div class="field split-datetime-date-field">
+        <label for="{{ field.auto_id }}_0">{% firstof label field.label %} Date</label>
+        <input type="text" class="text" id="{{ field.auto_id }}_0" 
+               name="{{ field.html_name }}_0"
+               value="{% if initial_date %}{{ initial_date }}{% endif %}" />
+
+    </div>
+
+    <div class="field split-datetime-time-field">
+        <label for="{{ field.auto_id }}_1">{% firstof label field.label %} Time</label>
+        <input type="text" class="text" id="{{ field.auto_id }}_1" 
+               name="{{ field.html_name }}_1"
+               value="{% if initial_time %}{{ initial_time }}{% endif %}" />
+    </div>
+
+    {% if field.help_text %}
+        <p class="form-help-text">{{ field.help_text }}</p>
+    {% endif %}
+</div>
+
+<script type="text/javascript">
+    $(function() {
+        $('#{{ field.auto_id }}_0').datepicker({
+            dateFormat: 'yy-mm-dd',
+            minDate: '-0d',
+            firstDay: 1
+        });
+    });
+</script>

goodfields/templates/goodfields/fields/text.html

+{% include "goodfields/fields/validation.html" %}
+
+<div class="field" id="{{ field.auto_id }}-container">
+    <label for="{{ field.auto_id }}">{% firstof label field.label %}</label>
+    <input type="text" class="text" id="{{ field.auto_id }}" 
+           name="{{ field.html_name }}"
+           value="{% if initial %}{{ initial }}{% endif %}" />
+    {{ field.errors }}
+    {% if field.help_text %}
+        <p class="form-help-text">{{ field.help_text }}</p>
+    {% endif %}
+</div>

goodfields/templates/goodfields/fields/textarea.html

+{% include "goodfields/fields/validation.html" %}
+
+<div class="field" id="{{ field.auto_id }}-container">
+    <label for="{{ field.auto_id }}">{% firstof label field.label %} {{ field.errors }}</label>
+    <textarea id="{{ field.auto_id }}" rows="8"
+              name="{{ field.html_name }}">{% if initial %}{{ initial }}{% endif %}</textarea>
+    {% if field.help_text %}
+        <p class="form-help-text">{{ field.help_text }}</p>
+    {% endif %}
+</div>

goodfields/templates/goodfields/fields/validation.html

+{% if field.field.required or field.field.min_length or field.field.max_length or validation %}
+
+<script type="text/javascript" charset="utf-8">
+    $(function() {
+        var lv = new LiveValidation(
+            "{{ field.auto_id }}", { validMessage: " ", onlyOnBlur: true,
+                onValid: function() {
+                    this.insertMessage( this.createMessageSpan() );
+                    this.addFieldClass();
+                    $("#{{ field.auto_id }}-container ul.errorlist").hide();
+                }
+            });
+        
+        {% if field.field.required %}
+            lv.add(
+                Validate.Presence,
+                { failureMessage: "This field is required." }
+            );
+        {% endif %}
+        {% if field.field.min_length or field.field.max_length %}
+            lv.add(
+                Validate.Length,
+                {
+                    minimum: {% firstof field.field.min_length "0" %},
+                    maximum: {% firstof field.field.max_length "1000000000000000" %}
+                }
+            );
+        {% endif %}
+        {% if validation.email %}
+            lv.add(Validate.Email);
+        {% endif %}
+        {% if validation.pattern %}
+            lv.add(
+                Validate.Format,
+                {
+                    pattern: {{ validation.pattern.re }},
+                    failureMessage: "{{ validation.pattern.message }}"
+                }
+            );
+        {% endif %}
+    });
+</script>
+
+{% endif %}
Add a comment to this file

goodfields/templatetags/__init__.py

Empty file added.

goodfields/templatetags/goodfields.py

+from django import template
+from django.conf import settings
+from django.template.loader import render_to_string
+
+validate = (hasattr(settings, 'GOODFIELD_VALIDATION') and not settings.GOODFIELD_VALIDATION)
+register = template.Library()
+
+class GoodfieldNode(template.Node):
+    def __init__(self, field, field_type, label=None, validation=None):
+        self.field = template.Variable(field)
+        self.field_type = field_type.strip('"')
+        self.label = label.strip('"') if label else None
+        self.validation = validation
+    
+    def render(self, context):
+        field = self.field.resolve(context)
+        f = field.form
+        initial = field.data if f.data else f.initial.get(field.name, '')
+        
+        template = "goodfields/fields/%s.html" % self.field_type
+        
+        return render_to_string(template, {
+            'field': field,
+            'initial': initial,
+            'label': self.label,
+            'validation': self.validation,
+            'validate': validate,
+        })
+    
+
+
+@register.tag
+def goodfield(parser, token):
+    """Render good form fields."""
+    
+    bits = token.split_contents()
+    tag_name = bits.pop(0)
+    
+    if len(bits) < 2:
+        raise template.TemplateSyntaxError(
+            u"'%s' tag requires at least a field and field type." % tag_name
+        )
+    
+    field = bits.pop(0)
+    field_type = bits.pop(0)
+    
+    bits, kwargs = iter(bits), { 'validation': {} }
+    
+    for bit in bits:
+        if bit == 'label':
+            kwargs['label'] = bits.next()
+        if bit == 'email':
+            kwargs['validation']['email'] = True
+        if bit == 'pattern':
+            kwargs['validation']['pattern'] = {
+                're': bits.next().strip('"'),
+                'message': bits.next().strip('"'),
+            }
+    
+    return GoodfieldNode(field, field_type, **kwargs)
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.