示例#1
0
def outputhelper(self,
                 normal_row,
                 error_row,
                 row_ender,
                 help_text_html,
                 errors_on_separate_row,
                 separator_html=SEPARATOR_HTML):
    "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
    top_errors = self.non_field_errors(
    )  # Errors that should be displayed above all fields.
    output, hidden_fields = [], []

    for name, field in self.fields.items():
        html_class_attr = ''
        bf = BoundField(self, field, name)
        bf_errors = self.error_class([
            conditional_escape(error) for error in bf.errors
        ])  # Escape and cache in local variable.

        # Check that we can get a unicode output of the field (in particular
        # if someone tries to pass bad data to the form
        try:
            unicode(bf)
        except TypeError:
            raise Http404

        if bf.is_hidden:
            if bf_errors:
                top_errors.extend([
                    u'(Hidden field %s) %s' % (name, force_unicode(e))
                    for e in bf_errors
                ])
            hidden_fields.append(unicode(bf))

        elif bf.field.is_separator():
            output.append(separator_html % {
                'label': conditional_escape(force_unicode(bf.label)),
            })

        else:
            # Create a 'class="..."' atribute if the row should have any
            # CSS classes applied.
            css_classes = bf.css_classes()
            if css_classes:
                html_class_attr = ' class="%s"' % css_classes

            if errors_on_separate_row and bf_errors:
                output.append(error_row % force_unicode(bf_errors))

            if bf.label:
                label = conditional_escape(force_unicode(bf.label))
                # Only add the suffix if the label does not end in
                # punctuation.
                if self.label_suffix:
                    if label[-1] not in ':?.!':
                        label += self.label_suffix
                label = bf.label_tag(label) or ''
            else:
                label = ''

            if field.help_text:
                help_text = help_text_html % force_unicode(field.help_text)
            else:
                help_text = u''

            output.append(
                normal_row % {
                    'errors': force_unicode(bf_errors),
                    'label': force_unicode(label),
                    'field': unicode(bf),
                    'help_text': help_text,
                    'html_class_attr': html_class_attr
                })

    if top_errors:
        output.insert(0, error_row % force_unicode(top_errors))

    if hidden_fields:  # Insert any hidden fields in the last row.
        str_hidden = u''.join(hidden_fields)
        if output:
            last_row = output[-1]
            # Chop off the trailing row_ender (e.g. '</td></tr>') and
            # insert the hidden fields.
            if not last_row.endswith(row_ender):
                # This can happen in the as_p() case (and possibly others
                # that users write): if there are only top errors, we may
                # not be able to conscript the last row for our purposes,
                # so insert a new, empty row.
                last_row = (normal_row % {
                    'errors': '',
                    'label': '',
                    'field': '',
                    'help_text': '',
                    'html_class_attr': html_class_attr
                })
                output.append(last_row)
            output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
        else:
            # If there aren't any rows in the output, just append the
            # hidden fields.
            output.append(str_hidden)
    return mark_safe(u'\n'.join(output))
示例#2
0
def html_output_alt(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row,
        extra_css_class_attr = "manual_css_classes"):
    "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
    top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
    output, hidden_fields = [], []

    for name, field in list(self.fields.items()):
        html_class_attr = ''
        bf = BoundField(self, field, name)
        bf_errors = self.error_class([conditional_escape(error) for error in bf.errors]) # Escape and cache in local variable.
        if bf.is_hidden:
            if bf_errors:
                top_errors.extend(['(Hidden field %s) extra_css_class_attr%s' % (name, force_text(e)) for e in bf_errors])
            hidden_fields.append(str(bf))
        else:
            # Create a 'class="..."' atribute if the row should have any
            # CSS classes applied.
            css_classes = bf.css_classes(getattr(field, extra_css_class_attr, None))
            if css_classes:
                html_class_attr = ' class="%s"' % css_classes

            if errors_on_separate_row and bf_errors:
                output.append(error_row % force_text(bf_errors))

            if bf.label:
                label = conditional_escape(force_text(bf.label))
                # Only add the suffix if the label does not end in
                # punctuation.
                if self.label_suffix:
                    if label[-1] not in ':?.!':
                        label += self.label_suffix
                label = bf.label_tag(label) or ''
            else:
                label = ''

            if field.help_text:
                help_text = help_text_html % force_text(field.help_text)
            else:
                help_text = ''

            output.append(normal_row % {
                'errors': force_text(bf_errors),
                'label': force_text(label),
                'field': str(bf),
                'help_text': help_text,
                'html_class_attr': html_class_attr
            })

    #if top_errors:
    #    output.insert(0, error_row % force_text(top_errors))

    if hidden_fields: # Insert any hidden fields in the last row.
        str_hidden = ''.join(hidden_fields)
        if output:
            last_row = output[-1]
            # Chop off the trailing row_ender (e.g. '</td></tr>') and
            # insert the hidden fields.
            if not last_row.endswith(row_ender):
                # This can happen in the as_p() case (and possibly others
                # that users write): if there are only top errors, we may
                # not be able to conscript the last row for our purposes,
                # so insert a new, empty row.
                last_row = (normal_row % {'errors': '', 'label': '',
                                          'field': '', 'help_text':'',
                                          'html_class_attr': html_class_attr})
                output.append(last_row)
            output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
        else:
            # If there aren't any rows in the output, just append the
            # hidden fields.
            output.append(str_hidden)
    return mark_safe('\n'.join(output))
示例#3
0
    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
        "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."

        top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
        output, hidden_fields = [], []
        for name, field in self.fields.items():
            bf = BoundField(self, field, name)
            bf_errors = self.error_class([conditional_escape(error) for error in bf.errors]) # Escape and cache in local variable.
            if bf.is_hidden:
                if bf_errors:
                    top_errors.extend([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
                hidden_fields.append(unicode(bf))
            else:
                if errors_on_separate_row and bf_errors:
                    output.append(error_row % force_unicode(bf_errors))
                if bf.label:
                    label = conditional_escape(force_unicode(bf.label))
                    # Only add the suffix if the label does not end in
                    # punctuation.
                    if self.label_suffix:
                        if label[-1] not in ':?.!':
                            label += self.label_suffix
                    # Sets label with an asterisk if this is a obligatory field according to to validation rules
                    if hasattr(self.Meta,'min_required'):
                        if not hasattr(self.Meta,'count'):
                            self.Meta.count = 0
                        if self.Meta.min_required > 0 and self.Meta.count < self.Meta.min_required:
                            if name != 'DELETE':
                                label = label + ' <span class="required_field">(*)</span>'
                    elif trial_validator.field_is_required(self, name):
                        label = label + ' <span class="required_field">(*)</span>'
                    label = bf.label_tag(label) or ''
                else:
                    label = ''


                # Gets the field status for this field to use it as CSS class
                if self.instance:
                    field_status = trial_validator.get_field_status(self, name, self.instance)
                else:
                    field_status = ''

                if field.help_text:
                    help_text = help_text_html % force_unicode(field.help_text)
                else:
                    help_text = u''
                form_name = self.__class__.__name__
                help_record, new = FieldHelp.objects.get_or_create(form=form_name, field=name)

                # Trying to get the translation for help_record
                try:
                    help_object = FieldHelpTranslation.objects.get_translation_for_object(
                        lang=get_language(), model=FieldHelp, object_id=help_record.pk,
                        )
                    help_text = "<div class='help_text'>%s</div>" % (help_object.text,)
                    if help_object.example:
                        help_text = "%s<div class='help_text_example'>%s:<br />%s</div>" % (help_text, _("Example"), help_object.example)

                    if not help_text.strip():
                        help_text = unicode(help_record)
                except (FieldHelpTranslation.DoesNotExist, AttributeError):
                    help_text = "<div class='help_text'>%s</div>" % (help_record.text,)
                    if help_record.example:
                        help_text = "%s<div class='help_text_example'>%s:<br />%s</div>" % (help_text, _("Example"), help_record.example)

                help_text = u'' + force_unicode(help_text)
                help_text = linebreaksbr(help_text_html % help_text)
                output.append(normal_row % {'errors': force_unicode(bf_errors),
                                            'label': force_unicode(label),
                                            'field': unicode(bf),
                                            'help_text': help_text,
                                            'help_id': 'id_%s-help%s' % ((self.prefix or name),help_record.pk),
                                            'field_class': field_status,
                                            'field_name': name,
                                            })

        # if necessary, updates the count of rendered repetitive forms
        if hasattr(self.Meta,'min_required'):
            if hasattr(self.Meta,'count'):
                self.Meta.count = self.Meta.count + 1

        if top_errors:
            output.insert(0, error_row % force_unicode(top_errors))
        if hidden_fields: # Insert any hidden fields in the last row.
            str_hidden = u''.join(hidden_fields)

            if output:
                last_row = output[-1]
                # Chop off the trailing row_ender (e.g. '</td></tr>') and
                # insert the hidden fields.
                if not last_row.endswith(row_ender):
                    # This can happen in the as_p() case (and possibly others
                    # that users write): if there are only top errors, we may
                    # not be able to conscript the last row for our purposes,
                    # so insert a new, empty row.
                    last_row = normal_row % {'errors': '',
                                             'label': '',
                                             'field': '',
                                             'help_text': '',
                                             'help_id': 'id_%s-help%s' % (self.prefix,help_record.pk),
                                             'issue': '',}
                    output.append(last_row)
                output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
            else:
                # If there aren't any rows in the output, just append the
                # hidden fields.
                output.append(str_hidden)

        return mark_safe(u'\n'.join(output))
示例#4
0
    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
        "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."

        top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
        output, hidden_fields = [], []
        for name, field in self.fields.items():
            bf = BoundField(self, field, name)
            bf_errors = self.error_class([conditional_escape(error) for error in bf.errors]) # Escape and cache in local variable.
            if bf.is_hidden:
                if bf_errors:
                    top_errors.extend([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
                hidden_fields.append(unicode(bf))
            else:
                if errors_on_separate_row and bf_errors:
                    output.append(error_row % force_unicode(bf_errors))
                if bf.label:
                    label = conditional_escape(force_unicode(bf.label))
                    # Only add the suffix if the label does not end in
                    # punctuation.
                    if self.label_suffix:
                        if label[-1] not in ':?.!':
                            label += self.label_suffix
                    # Sets label with an asterisk if this is a obligatory field according to to validation rules
                    if hasattr(self.Meta,'min_required'):
                        if not hasattr(self.Meta,'count'):
                            self.Meta.count = 0
                        if self.Meta.min_required > 0 and self.Meta.count < self.Meta.min_required:
                            if name != 'DELETE':
                                label = label + ' <span class="required_field">(*)</span>'
                    elif trial_validator.field_is_required(self, name):
                        label = label + ' <span class="required_field">(*)</span>'
                    label = bf.label_tag(label) or ''
                else:
                    label = ''


                # Gets the field status for this field to use it as CSS class
                if self.instance:
                    field_status = trial_validator.get_field_status(self, name, self.instance)
                else:
                    field_status = ''

                if field.help_text:
                    help_text = help_text_html % force_unicode(field.help_text)
                else:
                    help_text = u''
                form_name = self.__class__.__name__
                help_record, new = FieldHelp.objects.get_or_create(form=form_name, field=name)

                # Trying to get the translation for help_record
                try:
                    help_object = FieldHelpTranslation.objects.get_translation_for_object(
                        lang=get_language(), model=FieldHelp, object_id=help_record.pk,
                        )
                    help_text = "<div class='help_text'>%s</div>" % (help_object.text,)
                    if help_object.example:
                        help_text = "%s<div class='help_text_example'>%s:<br />%s</div>" % (help_text, _("Example"), help_object.example)

                    if not help_text.strip():
                        help_text = unicode(help_record)
                except (FieldHelpTranslation.DoesNotExist, AttributeError):
                    help_text = "<div class='help_text'>%s</div>" % (help_record.text,)
                    if help_record.example:
                        help_text = "%s<div class='help_text_example'>%s:<br />%s</div>" % (help_text, _("Example"), help_record.example)

                help_text = u'' + force_unicode(help_text)
                help_text = linebreaksbr(help_text_html % help_text)
                output.append(normal_row % {'errors': force_unicode(bf_errors),
                                            'label': force_unicode(label),
                                            'field': unicode(bf),
                                            'help_text': help_text,
                                            'help_id': 'id_%s-help%s' % ((self.prefix or name),help_record.pk),
                                            'field_class': field_status,
                                            'field_name': name,
                                            })

        # if necessary, updates the count of rendered repetitive forms
        if hasattr(self.Meta,'min_required'):
            if hasattr(self.Meta,'count'):
                self.Meta.count = self.Meta.count + 1

        if top_errors:
            output.insert(0, error_row % force_unicode(top_errors))
        if hidden_fields: # Insert any hidden fields in the last row.
            str_hidden = u''.join(hidden_fields)

            if output:
                last_row = output[-1]
                # Chop off the trailing row_ender (e.g. '</td></tr>') and
                # insert the hidden fields.
                if not last_row.endswith(row_ender):
                    # This can happen in the as_p() case (and possibly others
                    # that users write): if there are only top errors, we may
                    # not be able to conscript the last row for our purposes,
                    # so insert a new, empty row.
                    last_row = normal_row % {'errors': '',
                                             'label': '',
                                             'field': '',
                                             'help_text': '',
                                             'help_id': 'id_%s-help%s' % (self.prefix,help_record.pk),
                                             'issue': '',}
                    output.append(last_row)
                output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
            else:
                # If there aren't any rows in the output, just append the
                # hidden fields.
                output.append(str_hidden)

        return mark_safe(u'\n'.join(output))
    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
        "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."

        top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
        output, hidden_fields = [], []
        for name, field in self.fields.items():
            bf = BoundField(self, field, name)
            bf_errors = self.error_class([conditional_escape(error) for error in bf.errors]) # Escape and cache in local variable.
            if bf.is_hidden:
                if bf_errors:
                    top_errors.extend([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
                hidden_fields.append(unicode(bf))
            else:
                if errors_on_separate_row and bf_errors:
                    output.append(error_row % force_unicode(bf_errors))
                if bf.label:
                    label = conditional_escape(force_unicode(bf.label))
                    # Only add the suffix if the label does not end in
                    # punctuation.
                    if self.label_suffix:
                        if label[-1] not in ':?.!':
                            label += self.label_suffix
                    label = bf.label_tag(label) or ''
                else:
                    label = ''
                if field.help_text:
                    help_text = help_text_html % force_unicode(field.help_text)
                else:
                    help_text = u''
                form_name = self.__class__.__name__
                #import pdb; pdb.set_trace()
                help_record, new = FieldHelp.objects.get_or_create(form=form_name, field=name)
                help_text = help_text + u' ' + force_unicode(help_record)
                help_text = help_text_html % help_text
                field_path = '%s.%s' % (form_name, name)
                issue_text = '%s #%s' % (field_path, self.instance.pk)
                output.append(normal_row % {'errors': force_unicode(bf_errors),
                                            'label': force_unicode(label),
                                            'field': unicode(bf),
                                            'help_text': help_text,
                                            'help_id': 'id_%s-help%s' % ((self.prefix or name),help_record.pk),
                                            'issue': issue_text,})
        if top_errors:
            output.insert(0, error_row % force_unicode(top_errors))
        if hidden_fields: # Insert any hidden fields in the last row.
            str_hidden = u''.join(hidden_fields)
            if output:
                last_row = output[-1]
                # Chop off the trailing row_ender (e.g. '</td></tr>') and
                # insert the hidden fields.
                if not last_row.endswith(row_ender):
                    # This can happen in the as_p() case (and possibly others
                    # that users write): if there are only top errors, we may
                    # not be able to conscript the last row for our purposes,
                    # so insert a new, empty row.
                    last_row = normal_row % {'errors': '',
                                             'label': '',
                                             'field': '',
                                             'help_text': '',
                                             'help_id': 'id_%s-help%s' % (self.prefix,help_record.pk),
                                             'issue': '',}
                    output.append(last_row)
                output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
            else:
                # If there aren't any rows in the output, just append the
                # hidden fields.
                output.append(str_hidden)
        return mark_safe(u'\n'.join(output))