from django import template
register = template.Library()

from django.conf import settings
from django.template import Context, Template
from django.utils.safestring import mark_safe
from django.utils.functional import SimpleLazyObject

ERROR_NOTE_TEMPLATE = SimpleLazyObject(lambda: Template('''
    <p class="errorindicator">
        <img src="''' + STATIC_URL + '''icons/exclamation.png" alt="exclamation" />
        Please correct the error below.

def error_note(form):
    Display the pre-form note about errors (if any).
    output = ""
    if form.errors:
        c = Context({})
        output = ERROR_NOTE_TEMPLATE.render(c)

    return mark_safe(output)

FIELD_AS_TD_TEMPLATE = SimpleLazyObject(lambda: Template('''<td>
                           {% if field.errors %}
                           <div class="errortext">{{field.errors.0}}</div>
                           {% endif %}
                        {{ field }}
FIELD_AS_TD_TEMPLATE_HIDDEN = SimpleLazyObject(lambda: Template('<td class="hidden">{{ field }}</td>'))

def display_form_as_row(form, arg=None):
    Convert the form to a HTML table row
    set arg to be "deleted_flag" to include the deleted field
    output = ["<tr>"]
    if arg == 'hidden':
        output = ['<tr class="hidden">']
    for field in form.visible_fields():
        if == "deleted" and (arg != "deleted_flag"):
        c = Context({"field":field})
        output.append( FIELD_AS_TD_TEMPLATE.render(c))
    for field in form.hidden_fields():
        c = Context({"field":field})
        output.append( FIELD_AS_TD_TEMPLATE_HIDDEN.render(c))
    return mark_safe('\n'.join(output)) 

# from
from django.template import Variable, VariableDoesNotExist
def hash(object, attr):
    pseudo_context = { 'object' : object }
        value = Variable('object.%s' % attr).resolve(pseudo_context)
    except VariableDoesNotExist:
        value = None
    return value