~ubuntu-branches/ubuntu/utopic/horizon/utopic-updates

« back to all changes in this revision

Viewing changes to horizon/tables/base.py

  • Committer: Package Import Robot
  • Author(s): James Page, Chris Johnston, James Page
  • Date: 2014-10-03 17:54:18 UTC
  • mfrom: (0.4.1) (1.1.44) (70.1.2 utopic)
  • Revision ID: package-import@ubuntu.com-20141003175418-1jomx0azdvnl5fxz
Tags: 1:2014.2~rc1-0ubuntu1
[ Chris Johnston ]
* d/theme/css/ubuntu.css: Fix Ubuntu theme for Instances "more" dropdown
  (LP: #1308651).

[ James Page ]
* New upstream release candidate:
  - d/p/*: Refresh.
* d/watch: Use tarballs.openstack.org for upstream releases. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
from django import forms
25
25
from django.http import HttpResponse  # noqa
26
26
from django import template
 
27
from django.template.defaultfilters import slugify  # noqa
27
28
from django.template.defaultfilters import truncatechars  # noqa
28
29
from django.template.loader import render_to_string
29
30
from django.utils.datastructures import SortedDict
157
158
        A dict of HTML attribute strings which should be added to this column.
158
159
        Example: ``attrs={"data-foo": "bar"}``.
159
160
 
 
161
    .. attribute:: cell_attributes_getter
 
162
 
 
163
       A callable to get the HTML attributes of a column cell depending
 
164
       on the data. For example, to add additional description or help
 
165
       information for data in a column cell (e.g. in Images panel, for the
 
166
       column 'format'):
 
167
 
 
168
            helpText = {
 
169
              'ARI':'Amazon Ramdisk Image'
 
170
              'QCOW2':'QEMU' Emulator'
 
171
              }
 
172
 
 
173
            getHoverHelp(data):
 
174
              text = helpText.get(data, None)
 
175
              if text:
 
176
                  return {'title': text}
 
177
              else:
 
178
                  return {}
 
179
            ...
 
180
            ...
 
181
            cell_attributes_getter = getHoverHelp
 
182
 
160
183
    .. attribute:: truncate
161
184
 
162
185
        An integer for the maximum length of the string in this column. If the
245
268
                 empty_value=None, filters=None, classes=None, summation=None,
246
269
                 auto=None, truncate=None, link_classes=None, wrap_list=False,
247
270
                 form_field=None, form_field_attributes=None,
248
 
                 update_action=None, link_attrs=None):
 
271
                 update_action=None, link_attrs=None,
 
272
                 cell_attributes_getter=None):
249
273
 
250
274
        self.classes = list(classes or getattr(self, "classes", []))
251
275
        super(Column, self).__init__()
283
307
        self.link_attrs = link_attrs or {}
284
308
        if link_classes:
285
309
            self.link_attrs['class'] = ' '.join(link_classes)
 
310
        self.cell_attributes_getter = cell_attributes_getter
286
311
 
287
312
        if status_choices:
288
313
            self.status_choices = status_choices
413
438
        data = filter(lambda datum: datum is not None, data)
414
439
 
415
440
        if len(data):
416
 
            summation = summation_function(data)
417
 
            for filter_func in self.filters:
418
 
                summation = filter_func(summation)
419
 
            return summation
420
 
        else:
421
 
            return None
 
441
            try:
 
442
                summation = summation_function(data)
 
443
                for filter_func in self.filters:
 
444
                    summation = filter_func(summation)
 
445
                return summation
 
446
            except TypeError:
 
447
                pass
 
448
        return None
422
449
 
423
450
 
424
451
class Row(html.HTMLElement):
592
619
        """Fetches the updated data for the row based on the object id
593
620
        passed in. Must be implemented by a subclass to allow AJAX updating.
594
621
        """
595
 
        raise NotImplementedError("You must define a get_data method on %s"
596
 
                                  % self.__class__.__name__)
 
622
        return {}
597
623
 
598
624
 
599
625
class Cell(html.HTMLElement):
654
680
            table._data_cache[column][table.get_object_id(datum)] = data
655
681
        else:
656
682
            data = column.get_data(datum)
 
683
            if column.cell_attributes_getter:
 
684
                cell_attributes = column.cell_attributes_getter(data) or {}
 
685
                self.attrs.update(cell_attributes)
657
686
        return data
658
687
 
659
688
    def __repr__(self):
739
768
        """Returns a flattened string of the cell's CSS classes."""
740
769
        if not self.url:
741
770
            self.column.classes = [cls for cls in self.column.classes
742
 
                                    if cls != "anchor"]
 
771
                                   if cls != "anchor"]
743
772
        column_class_string = self.column.get_final_attrs().get('class', "")
744
773
        classes = set(column_class_string.split(" "))
745
774
        if self.column.status:
881
910
        The class which should be used for handling the columns of this table.
882
911
        Optional. Default: :class:`~horizon.tables.Column`.
883
912
 
 
913
    .. attribute:: css_classes
 
914
 
 
915
        A custom CSS class or classes to add to the ``<table>`` tag of the
 
916
        rendered table, for when the particular table requires special styling.
 
917
        Default: ``""``.
 
918
 
884
919
    .. attribute:: mixed_data_type
885
920
 
886
921
        A toggle to indicate if the table accepts two or more types of data.
887
 
        Optional. Default: :``False``
 
922
        Optional. Default: ``False``
888
923
 
889
924
    .. attribute:: data_types
890
925
 
909
944
    """
910
945
    def __init__(self, options):
911
946
        self.name = getattr(options, 'name', self.__class__.__name__)
912
 
        verbose_name = getattr(options, 'verbose_name', None) \
913
 
                                    or self.name.title()
 
947
        verbose_name = (getattr(options, 'verbose_name', None)
 
948
                        or self.name.title())
914
949
        self.verbose_name = verbose_name
915
950
        self.columns = getattr(options, 'columns', None)
916
951
        self.status_columns = getattr(options, 'status_columns', [])
920
955
        self.cell_class = getattr(options, 'cell_class', Cell)
921
956
        self.row_class = getattr(options, 'row_class', Row)
922
957
        self.column_class = getattr(options, 'column_class', Column)
 
958
        self.css_classes = getattr(options, 'css_classes', '')
923
959
        self.prev_pagination_param = getattr(options,
924
960
                                             'prev_pagination_param',
925
961
                                             'prev_marker')
947
983
                                'template',
948
984
                                'horizon/common/_data_table.html')
949
985
        self.row_actions_template = \
950
 
                        'horizon/common/_data_table_row_actions.html'
 
986
            'horizon/common/_data_table_row_actions.html'
951
987
        self.table_actions_template = \
952
 
                        'horizon/common/_data_table_table_actions.html'
 
988
            'horizon/common/_data_table_table_actions.html'
953
989
        self.context_var_name = unicode(getattr(options,
954
990
                                                'context_var_name',
955
991
                                                'table'))
1157
1193
                                                            filter_string)
1158
1194
        return self._filtered_data
1159
1195
 
 
1196
    def slugify_name(self):
 
1197
        return str(slugify(self._meta.name))
 
1198
 
1160
1199
    def get_filter_string(self):
1161
1200
        """Get the filter string value. For 'server' type filters this is
1162
1201
        saved in the session so that it gets persisted across table loads.
1261
1300
        if not matches:
1262
1301
            raise exceptions.Http302(self.get_absolute_url(),
1263
1302
                                     _('No match returned for the id "%s".')
1264
 
                                       % lookup)
 
1303
                                     % lookup)
1265
1304
        return matches[0]
1266
1305
 
1267
1306
    @property
1703
1742
                              exc_info[2])
1704
1743
 
1705
1744
        return rows
 
1745
 
 
1746
    def css_classes(self):
 
1747
        """Returns the additional CSS class to be added to <table> tag."""
 
1748
        return self._meta.css_classes