Beispiel #1
0
    def from_field(cls, field):
        if isinstance(field, models.NullBooleanField):
            return cls(verbose_name=title(field.verbose_name), null=True)

        if isinstance(field, models.BooleanField):
            null = getattr(field, 'null', False)
            return cls(verbose_name=title(field.verbose_name), null=null)
Beispiel #2
0
    def verbose_name(self):
        """
        Return the verbose name for this column, or fallback to the titlised
        column name.

        If the table is using queryset data, then use the corresponding model
        field's `~.db.Field.verbose_name`. If it's traversing a relationship,
        then get the last field in the accessor (i.e. stop when the
        relationship turns from ORM relationships to object attributes [e.g.
        person.upper should stop at person]).
        """
        # Favor an explicit defined verbose_name
        if self.column.verbose_name is not None:
            return self.column.verbose_name

        # This is our reasonable fallback, should the next section not result
        # in anything useful.
        name = title(self.name.replace('_', ' '))

        # Try to use a model field's verbose_name
        if hasattr(self.table.data, 'queryset') and hasattr(self.table.data.queryset, 'model'):
            model = self.table.data.queryset.model
            field = Accessor(self.accessor).get_field(model)
            if field:
                if hasattr(field, 'field'):
                    name = field.field.verbose_name
                else:
                    name = getattr(field, 'verbose_name', field.name)
        return name
Beispiel #3
0
    def header(self):
        """
        The value used for the column heading (e.g. inside the ``<th>`` tag).

        By default this titlises the `~.Column.verbose_name`. If
        `~.Column.verbose_name` is an instance of `~.safestring.SafeData`, it's
        used unmodified.

        :returns: `unicode` or `None`

        .. note::

            This property typically isn't accessed directly when a table is
            rendered. Instead, `.BoundColumn.header` is accessed which in turn
            accesses this property. This allows the header to fallback to the
            column name (it's only available on a `.BoundColumn` object hence
            accessing that first) when this property doesn't return something
            useful.
        """
        if self.verbose_name:
            if isinstance(self.verbose_name, SafeData):
                # If the author has used mark_safe, we're going to assume the
                # author wants the value used verbatim.
                return self.verbose_name
            return title(self.verbose_name)
Beispiel #4
0
    def verbose_name(self):
        """
        Return the verbose name for this column, or fallback to the titlised
        column name.

        If the table is using queryset data, then use the corresponding model
        field's `~.db.Field.verbose_name`. If it's traversing a relationship,
        then get the last field in the accessor (i.e. stop when the
        relationship turns from ORM relationships to object attributes [e.g.
        person.upper should stop at person]).
        """
        # Favor an explicit defined verbose_name
        if self.column.verbose_name is not None:
            return self.column.verbose_name

        # This is our reasonable fallback, should the next section not result
        # in anything useful.
        name = title(self.name.replace('_', ' '))

        # Try to use a model field's verbose_name
        if hasattr(self.table.data, 'queryset') and hasattr(
                self.table.data.queryset, 'model'):
            model = self.table.data.queryset.model
            field = Accessor(self.accessor).get_field(model)
            if field:
                if hasattr(field, 'field'):
                    name = field.field.verbose_name
                else:
                    name = getattr(field, 'verbose_name', field.name)
        return name
Beispiel #5
0
    def verbose_name(self):
        """
        Return the verbose name for this column, or fallback to the titlised
        column name.

        If the table is using queryset data, then use the corresponding model
        field's `~.db.Field.verbose_name`. If it's traversing a relationship,
        then get the last field in the accessor (i.e. stop when the
        relationship turns from ORM relationships to object attributes [e.g.
        person.upper should stop at person]).
        """
        # Favor an explicit defined verbose_name
        if self.column.verbose_name is not None:
            return self.column.verbose_name

        name = None

        # Try to use a model field's verbose_name
        table_data = self.table.data
        internal_data = table_data.data
        type_, supported_data_type = table_data.internal_data_type, table_data.supported_data_type

        if type_ is supported_data_type.QUERYSET and hasattr(internal_data, 'model'):
            model = internal_data.model
            field = Accessor(self.accessor).get_field(model)
            if field:
                if hasattr(field, 'field'):
                    name = field.field.verbose_name
                else:
                    name = getattr(field, 'verbose_name', field.name)

        # if above section didn't get a field verbose_name, fallback to
        # titlecase
        return name or title(self.name.replace('_', ' '))
Beispiel #6
0
    def header(self):
        """
        The value used for the column heading (e.g. inside the ``<th>`` tag).

        By default this titlises the `~.Column.verbose_name`. If
        `~.Column.verbose_name` is an instance of `~.safestring.SafeData`, it's
        used unmodified.

        :returns: `unicode` or `None`

        .. note::

            This property typically isn't accessed directly when a table is
            rendered. Instead, `.BoundColumn.header` is accessed which in turn
            accesses this property. This allows the header to fallback to the
            column name (it's only available on a `.BoundColumn` object hence
            accessing that first) when this property doesn't return something
            useful.
        """
        if self.verbose_name:
            if isinstance(self.verbose_name, SafeData):
                # If the author has used mark_safe, we're going to assume the
                # author wants the value used verbatim.
                return self.verbose_name
            return title(self.verbose_name)
Beispiel #7
0
 def header(self):
     """
     The value that should be used in the header cell for this column.
     """
     # favour Column.header
     column_header = self.column.header
     if column_header:
         return column_header
     # fall back to automatic best guess
     verbose_name = self.verbose_name  # avoid calculating multiple times
     if isinstance(verbose_name, SafeData):
         # If the verbose_name has come from a model field, it's possible
         # that the author used mark_safe to include HTML in the value. If
         # this is the case, we leave it verbatim.
         return verbose_name
     return title(verbose_name)
Beispiel #8
0
 def header(self):
     """
     The value that should be used in the header cell for this column.
     """
     # favour Column.header
     column_header = self.column.header
     if column_header:
         return column_header
     # fall back to automatic best guess
     verbose_name = self.verbose_name  # avoid calculating multiple times
     if isinstance(verbose_name, SafeData):
         # If the verbose_name has come from a model field, it's possible
         # that the author used mark_safe to include HTML in the value. If
         # this is the case, we leave it verbatim.
         return verbose_name
     return title(verbose_name)
Beispiel #9
0
    def verbose_name(self):
        """
        Return the verbose name for this column, or fallback to the titlised
        column name.

        If the table is using queryset data, then use the corresponding model
        field's `~.db.Field.verbose_name`. If it's traversing a relationship,
        then get the last field in the accessor (i.e. stop when the
        relationship turns from ORM relationships to object attributes [e.g.
        person.upper should stop at person]).
        """
        # Favor an explicit defined verbose_name
        if self.column.verbose_name is not None:
            return self.column.verbose_name

        # This is our reasonable fallback, should the next section not result
        # in anything useful.
        name = title(self.name.replace('_', ' '))

        # Try to use a model field's verbose_name
        if hasattr(self.table.data, 'queryset') and hasattr(
                self.table.data.queryset, 'model'):
            model = self.table.data.queryset.model
            parts = self.accessor.split('.')
            field = None
            for part in parts:

                try:
                    if django_version < (1, 8, 0):
                        field, _, _, _ = model._meta.get_field_by_name(part)
                    else:
                        field = model._meta.get_field(part)

                except FieldDoesNotExist:
                    break
                if hasattr(field, 'rel') and hasattr(field.rel, 'to'):
                    model = field.rel.to
                    continue
                break
            if field:
                if hasattr(field, 'field'):
                    name = field.field.verbose_name
                else:
                    name = getattr(field, 'verbose_name', field.name)
        return name
Beispiel #10
0
    def verbose_name(self):
        '''
        Return the verbose name for this column.

        In order of preference, this will return:
          1) The column's explicitly defined `verbose_name`
          2) The titlised model's `verbose_name` (if applicable)
          3) Fallback to the titlised column name.

        Any `verbose_name` that was not passed explicitly in the column
        definition is returned titlised in keeping with the Django convention
        of `verbose_name` being defined in lowercase and uppercased/titlised
        as needed by the application.

        If the table is using queryset data, then use the corresponding model
        field's `~.db.Field.verbose_name`. If it's traversing a relationship,
        then get the last field in the accessor (i.e. stop when the
        relationship turns from ORM relationships to object attributes [e.g.
        person.upper should stop at person]).
        '''
        # Favor an explicit defined verbose_name
        if self.column.verbose_name is not None:
            return self.column.verbose_name

        # This is our reasonable fallback, should the next section not result
        # in anything useful.
        name = self.name.replace('_', ' ')

        # Try to use a model field's verbose_name
        if hasattr(self.table.data, 'queryset') and hasattr(
                self.table.data.queryset, 'model'):
            model = self.table.data.queryset.model
            field = Accessor(self.accessor).get_field(model)
            if field:
                if hasattr(field, 'field'):
                    name = field.field.verbose_name
                else:
                    name = getattr(field, 'verbose_name', field.name)

            # If verbose_name was mark_safe()'d, return intact to keep safety
            if isinstance(name, SafeData):
                return name

        return title(name)
Beispiel #11
0
    def verbose_name(self):
        """
        Return the verbose name for this column, or fallback to the titlised
        column name.

        If the table is using queryset data, then use the corresponding model
        field's `~.db.Field.verbose_name`. If it's traversing a relationship,
        then get the last field in the accessor (i.e. stop when the
        relationship turns from ORM relationships to object attributes [e.g.
        person.upper should stop at person]).
        """
        # Favor an explicit defined verbose_name
        if self.column.verbose_name is not None:
            return self.column.verbose_name

        # This is our reasonable fallback, should the next section not result
        # in anything useful.
        name = title(self.name.replace('_', ' '))

        # Try to use a model field's verbose_name
        if hasattr(self.table.data, 'queryset') and hasattr(self.table.data.queryset, 'model'):
            model = self.table.data.queryset.model
            parts = self.accessor.split('.')
            field = None
            for part in parts:

                try:
                    if django_version < (1, 8, 0):
                        field, _, _, _ = model._meta.get_field_by_name(part)
                    else:
                        field = model._meta.get_field(part)

                except FieldDoesNotExist:
                    break
                if hasattr(field, 'rel') and hasattr(field.rel, 'to'):
                    model = field.rel.to
                    continue
                break
            if field:
                if hasattr(field, 'field'):
                    name = field.field.verbose_name
                else:
                    name = getattr(field, 'verbose_name', field.name)
        return name
Beispiel #12
0
    def verbose_name(self):
        '''
        Return the verbose name for this column.

        In order of preference, this will return:
          1) The column's explicitly defined `verbose_name`
          2) The titlised model's `verbose_name` (if applicable)
          3) Fallback to the titlised column name.

        Any `verbose_name` that was not passed explicitly in the column
        definition is returned titlised in keeping with the Django convention
        of `verbose_name` being defined in lowercase and uppercased/titlised
        as needed by the application.

        If the table is using queryset data, then use the corresponding model
        field's `~.db.Field.verbose_name`. If it's traversing a relationship,
        then get the last field in the accessor (i.e. stop when the
        relationship turns from ORM relationships to object attributes [e.g.
        person.upper should stop at person]).
        '''
        # Favor an explicit defined verbose_name
        if self.column.verbose_name is not None:
            return self.column.verbose_name

        # This is our reasonable fallback, should the next section not result
        # in anything useful.
        name = self.name.replace('_', ' ')

        # Try to use a model field's verbose_name
        model = self.table.data.get_model()
        if model:
            field = Accessor(self.accessor).get_field(model)
            if field:
                if hasattr(field, 'field'):
                    name = field.field.verbose_name
                else:
                    name = getattr(field, 'verbose_name', field.name)

            # If verbose_name was mark_safe()'d, return intact to keep safety
            if isinstance(name, SafeData):
                return name

        return title(name)
Beispiel #13
0
    def from_field(cls, field):
        '''
        Return a specialised column for the model field or `None`.

        :param field: the field that needs a suitable column
        :type  field: model field instance
        :returns: `.Column` object or `None`

        If the column isn't specialised for the given model field, it should
        return `None`. This gives other columns the opportunity to do better.

        If the column is specialised, it should return an instance of itself
        that's configured appropriately for the field.
        '''
        # Since this method is inherited by every subclass, only provide a
        # column if this class was asked directly.
        if cls is Column:
            if hasattr(field, 'get_related_field'):
                verbose_name = field.get_related_field().verbose_name
            else:
                verbose_name = getattr(field, 'verbose_name', field.name)
            return cls(verbose_name=title(verbose_name))
Beispiel #14
0
    def from_field(cls, field):
        '''
        Return a specialised column for the model field or `None`.

        :param field: the field that needs a suitable column
        :type  field: model field instance
        :returns: `.Column` object or `None`

        If the column isn't specialised for the given model field, it should
        return `None`. This gives other columns the opportunity to do better.

        If the column is specialised, it should return an instance of itself
        that's configured appropriately for the field.
        '''
        # Since this method is inherited by every subclass, only provide a
        # column if this class was asked directly.
        if cls is Column:
            if hasattr(field, 'get_related_field'):
                verbose_name = field.get_related_field().verbose_name
            else:
                verbose_name = getattr(field, 'verbose_name', field.name)
            return cls(verbose_name=title(verbose_name))
Beispiel #15
0
 def from_field(cls, field):
     if isinstance(field, models.BooleanField):
         return cls(verbose_name=title(field.verbose_name), null=False)
     if isinstance(field, models.NullBooleanField):
         return cls(verbose_name=title(field.verbose_name), null=True)
Beispiel #16
0
 def from_field(cls, field):
     if isinstance(field, models.TimeField):
         return cls(verbose_name=title(field.verbose_name))
Beispiel #17
0
 def from_field(cls, field):
     if POSTGRES_AVAILABLE:
         if isinstance(field, JSONField) or isinstance(field, HStoreField):
             return cls(verbose_name=title(field.verbose_name))
Beispiel #18
0
 def from_field(cls, field):
     if isinstance(field, models.URLField):
         return cls(verbose_name=title(field.verbose_name))
Beispiel #19
0
 def header(self):
     if self.verbose_name:
         if isinstance(self.verbose_name, SafeData):
             return self.verbose_name
         return title(self.verbose_name)