2014-02-13 17:17:05 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
"""
|
|
|
|
Copyright 2014 Google Inc.
|
|
|
|
|
|
|
|
Use of this source code is governed by a BSD-style license that can be
|
|
|
|
found in the LICENSE file.
|
|
|
|
|
|
|
|
ColumnHeaderFactory class (see class docstring for details)
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Keys used within dictionary representation of each column header.
|
2014-02-26 19:05:20 +00:00
|
|
|
# NOTE: Keep these in sync with static/constants.js
|
2014-05-08 21:15:20 +00:00
|
|
|
KEY__EXTRACOLUMNHEADERS__HEADER_TEXT = 'headerText'
|
|
|
|
KEY__EXTRACOLUMNHEADERS__HEADER_URL = 'headerUrl'
|
|
|
|
KEY__EXTRACOLUMNHEADERS__IS_FILTERABLE = 'isFilterable'
|
|
|
|
KEY__EXTRACOLUMNHEADERS__IS_SORTABLE = 'isSortable'
|
2014-07-09 13:19:20 +00:00
|
|
|
KEY__EXTRACOLUMNHEADERS__USE_FREEFORM_FILTER = 'useFreeformFilter'
|
2014-05-08 21:15:20 +00:00
|
|
|
KEY__EXTRACOLUMNHEADERS__VALUES_AND_COUNTS = 'valuesAndCounts'
|
2014-02-13 17:17:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ColumnHeaderFactory(object):
|
|
|
|
"""Factory which assembles the header for a single column of data."""
|
|
|
|
|
|
|
|
def __init__(self, header_text, header_url=None,
|
|
|
|
is_filterable=True, is_sortable=True,
|
2014-07-09 13:19:20 +00:00
|
|
|
use_freeform_filter=False):
|
2014-02-13 17:17:05 +00:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
header_text: string; text the client should display within column header.
|
|
|
|
header_url: string; target URL if user clicks on column header.
|
|
|
|
If None, nothing to click on.
|
|
|
|
is_filterable: boolean; whether client should allow filtering on this
|
|
|
|
column.
|
|
|
|
is_sortable: boolean; whether client should allow sorting on this column.
|
2014-07-09 13:19:20 +00:00
|
|
|
use_freeform_filter: boolean; *recommendation* to the client indicating
|
|
|
|
whether to allow freeform text matching, as opposed to listing all
|
|
|
|
values alongside checkboxes. If is_filterable==false, this is
|
|
|
|
meaningless.
|
2014-02-13 17:17:05 +00:00
|
|
|
"""
|
|
|
|
self._header_text = header_text
|
|
|
|
self._header_url = header_url
|
|
|
|
self._is_filterable = is_filterable
|
|
|
|
self._is_sortable = is_sortable
|
2014-07-09 13:19:20 +00:00
|
|
|
self._use_freeform_filter = use_freeform_filter
|
2014-02-13 17:17:05 +00:00
|
|
|
|
|
|
|
def create_as_dict(self, values_and_counts_dict=None):
|
|
|
|
"""Creates the header for this column, in dictionary form.
|
|
|
|
|
|
|
|
Creates the header for this column in dictionary form, as needed when
|
2014-05-08 21:15:20 +00:00
|
|
|
constructing the JSON representation. Uses the KEY__EXTRACOLUMNHEADERS__*
|
|
|
|
constants as keys.
|
2014-02-13 17:17:05 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
values_and_counts_dict: dictionary mapping each possible column value
|
|
|
|
to its count (how many entries in the column have this value), or
|
|
|
|
None if this information is not available.
|
|
|
|
"""
|
|
|
|
asdict = {
|
2014-05-08 21:15:20 +00:00
|
|
|
KEY__EXTRACOLUMNHEADERS__HEADER_TEXT: self._header_text,
|
|
|
|
KEY__EXTRACOLUMNHEADERS__IS_FILTERABLE: self._is_filterable,
|
|
|
|
KEY__EXTRACOLUMNHEADERS__IS_SORTABLE: self._is_sortable,
|
2014-07-09 13:19:20 +00:00
|
|
|
KEY__EXTRACOLUMNHEADERS__USE_FREEFORM_FILTER: self._use_freeform_filter,
|
2014-02-13 17:17:05 +00:00
|
|
|
}
|
|
|
|
if self._header_url:
|
2014-05-08 21:15:20 +00:00
|
|
|
asdict[KEY__EXTRACOLUMNHEADERS__HEADER_URL] = self._header_url
|
2014-07-09 13:19:20 +00:00
|
|
|
if values_and_counts_dict:
|
2014-05-08 21:15:20 +00:00
|
|
|
asdict[KEY__EXTRACOLUMNHEADERS__VALUES_AND_COUNTS] = sorted(
|
|
|
|
values_and_counts_dict.items())
|
2014-02-13 17:17:05 +00:00
|
|
|
return asdict
|