| 36 | | |
|---|
| 37 | | class FieldWidgetNode(template.Node): |
|---|
| 38 | | nodelists = {} |
|---|
| 39 | | default = None |
|---|
| 40 | | |
|---|
| 41 | | def __init__(self, bound_field_var): |
|---|
| 42 | | self.bound_field_var = template.Variable(bound_field_var) |
|---|
| 43 | | |
|---|
| 44 | | def get_nodelist(cls, klass): |
|---|
| 45 | | if klass not in cls.nodelists: |
|---|
| 46 | | try: |
|---|
| 47 | | field_class_name = klass.__name__ |
|---|
| 48 | | template_name = u"widget/%s.html" % class_name_to_underscored(field_class_name) |
|---|
| 49 | | nodelist = loader.get_template(template_name).nodelist |
|---|
| 50 | | except template.TemplateDoesNotExist: |
|---|
| 51 | | super_klass = bool(klass.__bases__) and klass.__bases__[0] or None |
|---|
| 52 | | if super_klass and super_klass != Field: |
|---|
| 53 | | nodelist = cls.get_nodelist(super_klass) |
|---|
| 54 | | else: |
|---|
| 55 | | if not cls.default: |
|---|
| 56 | | cls.default = loader.get_template("widget/default.html").nodelist |
|---|
| 57 | | nodelist = cls.default |
|---|
| 58 | | |
|---|
| 59 | | cls.nodelists[klass] = nodelist |
|---|
| 60 | | return nodelist |
|---|
| 61 | | else: |
|---|
| 62 | | return cls.nodelists[klass] |
|---|
| 63 | | get_nodelist = classmethod(get_nodelist) |
|---|
| 64 | | |
|---|
| 65 | | def render(self, context): |
|---|
| 66 | | bound_field = self.bound_field_var.resolve(context) |
|---|
| 67 | | |
|---|
| 68 | | context.push() |
|---|
| 69 | | context['bound_field'] = bound_field |
|---|
| 70 | | |
|---|
| 71 | | output = self.get_nodelist(bound_field.field.__class__).render(context) |
|---|
| 72 | | context.pop() |
|---|
| 73 | | return output |
|---|
| 74 | | |
|---|
| 75 | | class FieldWrapper(object): |
|---|
| 76 | | def __init__(self, field ): |
|---|
| 77 | | self.field = field |
|---|
| 78 | | |
|---|
| 79 | | def needs_header(self): |
|---|
| 80 | | return not isinstance(self.field, models.AutoField) |
|---|
| 81 | | |
|---|
| 82 | | def header_class_attribute(self): |
|---|
| 83 | | return self.field.blank and mark_safe(' class="optional"') or '' |
|---|
| 84 | | |
|---|
| 85 | | def use_raw_id_admin(self): |
|---|
| 86 | | return isinstance(self.field.rel, (models.ManyToOneRel, models.ManyToManyRel)) \ |
|---|
| 87 | | and self.field.rel.raw_id_admin |
|---|
| 88 | | |
|---|
| 89 | | class FormFieldCollectionWrapper(object): |
|---|
| 90 | | def __init__(self, field_mapping, fields, index): |
|---|
| 91 | | self.field_mapping = field_mapping |
|---|
| 92 | | self.fields = fields |
|---|
| 93 | | self.bound_fields = [AdminBoundField(field, self.field_mapping, field_mapping['original']) |
|---|
| 94 | | for field in self.fields] |
|---|
| 95 | | self.index = index |
|---|
| 96 | | |
|---|
| 97 | | def output_all(form_fields): |
|---|
| 98 | | return u''.join([force_unicode(f) for f in form_fields]) |
|---|
| 99 | | output_all = register.simple_tag(output_all) |
|---|
| 100 | | |
|---|
| 101 | | def field_widget(parser, token): |
|---|
| 102 | | bits = token.contents.split() |
|---|
| 103 | | if len(bits) != 2: |
|---|
| 104 | | raise template.TemplateSyntaxError, "%s takes 1 argument" % bits[0] |
|---|
| 105 | | return FieldWidgetNode(bits[1]) |
|---|
| 106 | | field_widget = register.tag(field_widget) |
|---|