Ejemplo n.º 1
0
    def render(self, name, value, attrs=None):
        new_attrs = create_attributes(self, UNIVERSAL)
        # build a list of possible elements
        elements = single_attributes(self)

        substitutions = {
            "initial_text": self.initial_text,
            "input_text": self.input_text,
            "clear_template": "",
            "clear_checkbox_label": self.clear_checkbox_label,
        }
        template = u"%(input)s"
        substitutions["input"] = super(ClearableFileInput, self).render(name, value, attrs, new_attrs, elements)

        if value and hasattr(value, "url"):
            template = self.template_with_initial
            substitutions["initial"] = u'<a href="%s">%s</a>' % (escape(value.url), escape(force_unicode(value)))
            if not self.is_required:
                checkbox_name = self.clear_checkbox_name(name)
                checkbox_id = self.clear_checkbox_id(checkbox_name)
                substitutions["clear_checkbox_name"] = conditional_escape(checkbox_name)
                substitutions["clear_checkbox_id"] = conditional_escape(checkbox_id)
                substitutions["clear"] = CheckboxInput().render(checkbox_name, False, attrs={"id": checkbox_id})
                substitutions["clear_template"] = self.template_with_clear % substitutions

        return mark_safe(template % substitutions)
Ejemplo n.º 2
0
 def render(self, name, value, attrs=None):
     new_attrs = create_attributes(self, UNIVERSAL, TEXT_SEARCH)
     attrs.update(**new_attrs)
     elements = single_attributes(self)
     if value is None:
         value = ""
     final_attrs = self.build_attrs(attrs, name=name)
     return mark_safe(
         u"<textarea%s %s>%s</textarea>" % (flatatt(final_attrs), elements, conditional_escape(force_unicode(value)))
     )
Ejemplo n.º 3
0
    def render(self, name, value, attrs=None, choices=()):
        new_attrs = create_attributes(self, UNIVERSAL)
        # build a list of possible elements
        elements = single_attributes(self)
        attrs.update(**new_attrs)

        output = list()
        for i, choice in enumerate(self.choices):
            output.append(RadioInput(name, value, attrs.copy(), choice, i, elements))

        return mark_safe(u"<ul>\n%s\n</ul>" % u"\n".join([u"<li>%s</li>" % force_unicode(w) for w in output]))
Ejemplo n.º 4
0
 def render(self, name, value, attrs=None, choices=()):
     new_attrs = create_attributes(self, UNIVERSAL, SELECT)
     # build a list of possible elements
     elements = single_attributes(self)
     if value is None:
         value = ""
     final_attrs = self.build_attrs(attrs, name=name, **new_attrs)
     output = [u'<select multiple="multiple"%s %s>' % (flatatt(final_attrs), elements)]
     options = self.render_options(choices, value)
     if options:
         output.append(options)
     output.append(u"</select>")
     return mark_safe(u"\n".join(output))
Ejemplo n.º 5
0
 def tag(self):
     new_attrs = create_attributes(self, UNIVERSAL)
     # attrs.update(**new_attrs)
     self.attrs.update(**new_attrs)
     if self.elements:
         elements = self.elements
     else:
         elements = single_attributes(self)
     if "id" in self.attrs:
         self.attrs["id"] = "%s_%s" % (self.attrs["id"], self.index)
     final_attrs = dict(self.attrs, type="radio", name=self.name, value=self.choice_value)
     if self.is_checked():
         final_attrs["checked"] = "checked"
     return mark_safe(u"<input%s %s>" % (flatatt(final_attrs), elements))
Ejemplo n.º 6
0
 def render(self, name, value, attrs=None, elements=None, choices=()):
     new_attrs = create_attributes(self, UNIVERSAL, CHOICE)
     elements = single_attributes(self)
     final_attrs = self.build_attrs(attrs, name=name, type="checkbox", **new_attrs)
     try:
         result = self.check_test(value)
     except:  # Silently catch exceptions
         result = False
     if result:
         final_attrs["checked"] = "checked"
     if value not in ("", True, False, None):
         # Only add the 'value' attribute if a value is non-empty.
         final_attrs["value"] = force_unicode(value)
     return mark_safe(u"<input%s %s>" % (flatatt(final_attrs), elements))
Ejemplo n.º 7
0
    def render(self, name, value, attrs=None, new_attrs=None, elements=None):
        # build a list of possible elements
        if not elements:
            elements = single_attributes(self)

        if not new_attrs:
            # below removes empty attributes and also removes dictionary of attributes that are not longer necessary
            new_attrs = create_attributes(self, UNIVERSAL, TEXT_SEARCH)

        if value is None:
            value = ""
        # set default
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name, **new_attrs)

        if value != "":
            # Only add the 'value' attribute if a value is non-empty.
            # This adds non-dictionary items to the input field such as required
            final_attrs["value"] = force_unicode(value)

        return mark_safe(u"<input%s %s>" % (flatatt(final_attrs), elements))