class BookTable(tables.MemoryTable): id = tables.Column(sortable=True) name = tables.Column(sortable=False) author = tables.Column() class Meta: sortable = default_sortable
class RuleTable(tables.ModelTable): id = tables.Column(sortable=False, visible=False) sensor = tables.Column() rule = tables.Column() class Meta: model = Rule
class CountryTable(tables.ModelTable): null = tables.Column(default="foo") tld = tables.Column(name="domain") class Meta(object): model = Country exclude = ('id', )
class CountryTable(tables.ModelTable): id = tables.Column(sortable=False, visible=False) code = tables.Column(sortable=True) name = tables.Column(sortable=True) class Meta: model = Country
class CountryTable(tables.ModelTable): null = tables.Column(default="foo") domain = tables.Column(model_rel="tld") class Meta: model = Country # noqa exclude = ('id', )
class WebappQueueTable(tables.ModelTable, ItemStateTable): name = tables.Column(verbose_name=_lazy(u'App')) created = tables.Column(verbose_name=_lazy(u'Waiting Time')) abuse_reports__count = tables.Column(verbose_name=_lazy(u'Abuse Reports')) def render_name(self, row): url = '%s?num=%s' % (reverse('editors.app_review', args=[row.slug]), self.item_number) self.increment_item() return u'<a href="%s">%s</a>' % (url, jinja2.escape(row.name)) def render_abuse_reports__count(self, row): url = reverse('editors.abuse_reports', args=[row.slug]) return u'<a href="%s">%s</a>' % (jinja2.escape(url), row.abuse_reports__count) def render_created(self, row): return timesince(row.created) @classmethod def translate_sort_cols(cls, colname): return colname @classmethod def default_order_by(cls): return 'created' @classmethod def review_url(cls, row): return reverse('editors.app_review', args=[row.slug]) class Meta: sortable = True model = Webapp columns = ['name', 'created', 'abuse_reports__count']
class CountryTable(tables.ModelTable): # add relationship spanning columns (using different approaches) capital_name = tables.Column(data='capital__name') capital__population = tables.Column(name="capital_population") invalid = tables.Column(data="capital__invalid") class Meta(object): model = Country
class PeeringTable(tables.ModelTable): id = tables.Column(sortable=False, visible=False) peer = tables.Column() user = tables.Column() key = tables.Column(sortable=False, visible=False) class Meta: model = Peering
class CountryTable(tables.ModelTable): tld = tables.Column(name="domain") population = tables.Column() system = tables.Column(default="republic") custom1 = tables.Column() custom2 = tables.Column(sortable=True) class Meta(object): model = Country
class CountryTable(tables.ModelTable): domain = tables.Column(model_rel="tld") population = tables.Column() system = tables.Column(default="republic") custom1 = tables.Column() custom2 = tables.Column(sortable=True) class Meta: model = Country # noqa
class ProductTable(SarpamTable): product = tables.Column() supplier = tables.Column() manufacturer = tables.Column() country = tables.Column() def __init__(self, rows): tables.MemoryTable.__init__(self, rows, order_by='product') def get_rows_template(self): return "product_rows.html"
class ProductTable(SarpamTable): product = tables.Column() supplier = tables.Column() manufacturer = tables.Column() country = tables.Column(verbose_name="Registered In") def __init__(self, rows): SarpamTable.__init__(self, rows, order_by='product') def get_rows_template(self): return "product_rows.html"
class TransactionReport(tables.ModelTable): #paid = tables.Column(filter='paid') tpin = tables.Column(sortable=False, visible=False) identifier = tables.Column(sortable=False, visible=False) created = tables.Column(sortable=True, visible=True) @classmethod def get_reports_paid(self, object, req): return TransactionReport(object, order_by=req) class Meta: model = Transaction
class CountryTable(tables.ModelTable): # add relationship spanning columns (using different approaches) capital_name = tables.Column(model_rel='capital__name') capital_name_link = tables.Column(model_rel='capital__name') cap_pop = tables.Column(model_rel="capital__population") invalid = tables.Column(model_rel="capital__invalid") class Meta: model = Country # noqa def render_capital_name_link(self, country): return '<a href="http://en.wikipedia.org/wiki/%s">%s</a>' % ( country.capital.name, country.capital.name)
class EditorAllListTable(SQLTable, ItemStateTable): addon_name = tables.Column(verbose_name=_lazy(u'Add-on')) guid = tables.Column(verbose_name=_lazy(u'GUID')) authors = tables.Column(verbose_name=_lazy(u'Authors'), sortable=False) review_date = tables.Column(verbose_name=_lazy(u'Last Review')) version_date = tables.Column(verbose_name=_lazy(u'Last Update')) show_version_notes = False class Meta: columns = [ 'addon_name', 'guid', 'authors', 'last_review', 'version_date' ] def render_addon_name(self, row): url = reverse( 'editors.review', args=[row.addon_slug if row.addon_slug is not None else row.id]) self.increment_item() return safe_substitute(u'<a href="%s">%s <em>%s</em></a>', url, row.addon_name, row.latest_version) def render_guid(self, row): return safe_substitute(u'%s', row.guid) def render_version_date(self, row): return safe_substitute(u'<span>%s</span>', row.version_date) def render_review_date(self, row): if row.review_version_num is None: return _('No Reviews') return safe_substitute( u'<span class="addon-review-text">' u'<a href="#"><em>%s</em> on %s</a></span>', row.review_version_num, row.review_date) def render_authors(self, row): authors = row.authors if not len(authors): return '' more = '\n'.join( safe_substitute(u'%s', uname) for (_, uname) in authors) author_links = ''.join( safe_substitute(u'<a href="%s">%s</a>', UserProfile.create_user_url(id_, username=uname), uname) for (id_, uname) in authors[0:3]) return u'<span title="%s">%s%s</span>' % (more, author_links, '...' if len(authors) > 3 else '') @classmethod def default_order_by(cls): return '-version_date'
class RIRTable(tables.ModelTable): id = tables.Column(sortable=False, visible=False) name = tables.Column() whois = tables.Column() listed = tables.Column() whitelisted = tables.Column() num_providers = tables.Column(sortable=False, visible=False) num_subnets = tables.Column(sortable=False, visible=False) num_listed = tables.Column(sortable=False, visible=False) num_whitelisted = tables.Column(sortable=False, visible=False) num_history = tables.Column(sortable=False, visible=False) class Meta: model = RIR
class FormulationTable(SarpamTable): country = tables.Column() fob_price = tables.Column(verbose_name="FOB Price (%s)" % settings.SARPAM_CURRENCY_CODE) landed_price = tables.Column(verbose_name="Landed Price (%s)" % settings.SARPAM_CURRENCY_CODE) def __init__(self, rows): for row in rows: self.round_to_set_decimal_places(row, 'fob_price') self.round_to_set_decimal_places(row, 'landed_price') SarpamTable.__init__(self, rows, order_by='landed_price') def get_rows_template(self): return "formulation_rows.html"
class CountryTable(tables.ModelTable): capital = tables.TextColumn(verbose_name='Name of capital') projected = tables.Column(verbose_name="Projected Population") class Meta: model = Country # noqa columns = ['capital']
class TestTable(tables.MemoryTable): private_name = tables.Column(name='public_name') def render_public_name(self, data): # We are given the actual data dict and have direct access # to additional values for which no field is defined. return "%s:%s" % (data['private_name'], data['additional'])
class CountryTable(tables.ModelTable): capital = tables.TextColumn(verbose_name='Name of capital') projected = tables.Column(verbose_name="Projected Population") class Meta(object): model = Country exclude = ['tld']
class CountryTable(tables.ModelTable): name = tables.Column() population = tables.Column() capital = tables.Column() system = tables.Column() null = tables.Column(default="foo") null2 = tables.Column() domain = tables.Column(model_rel="tld") tld = tables.Column()
def test_basic(): class StuffTable(tables.MemoryTable): name = tables.Column() answer = tables.Column(default=42) c = tables.Column(name="count", default=1) email = tables.Column(data="@") stuff = StuffTable([ {'id': 1, 'name': 'Foo Bar', '@': '*****@*****.**'}, ]) # access without order_by works stuff.data stuff.rows # make sure BoundColumnn.name always gives us the right thing, whether # the column explicitely defines a name or not. stuff.columns['count'].name == 'count' stuff.columns['answer'].name == 'answer' for r in stuff.rows: # unknown fields are removed/not-accessible assert 'name' in r assert not 'id' in r # missing data is available as default assert 'answer' in r assert r['answer'] == 42 # note: different from prev. line! # all that still works when name overrides are used assert not 'c' in r assert 'count' in r assert r['count'] == 1 # columns with data= option work fine assert r['email'] == '*****@*****.**' # try to splice rows by index assert 'name' in stuff.rows[0] assert isinstance(stuff.rows[0:], list) # [bug] splicing the table gives us valid, working rows assert list(stuff[0]) == list(stuff.rows[0]) assert stuff[0]['name'] == 'Foo Bar' # changing an instance's base_columns does not change the class assert id(stuff.base_columns) != id(StuffTable.base_columns) stuff.base_columns['test'] = tables.Column() assert not 'test' in StuffTable.base_columns # optionally, exceptions can be raised when input is invalid tables.options.IGNORE_INVALID_OPTIONS = False try: assert_raises(ValueError, setattr, stuff, 'order_by', '-name,made-up-column') assert_raises(ValueError, setattr, stuff, 'order_by', ('made-up-column',)) # when a column name is overwritten, the original won't work anymore assert_raises(ValueError, setattr, stuff, 'order_by', 'c') # reset for future tests finally: tables.options.IGNORE_INVALID_OPTIONS = True
class RequestTable(tables.MemoryTable): check = tables.Column(data='id') id = tables.Column() user = tables.Column() time = tables.Column() request_method = tables.Column() server_protocol = tables.Column() lang = tables.Column() priority = tables.Column()
class ListingTable(tables.ModelTable): id = tables.Column(sortable=False, visible=False) ip = tables.Column() reason = tables.Column() sensor = tables.Column() sensor_host = tables.Column() timestamp = tables.Column() duration = tables.Column() reporter = tables.Column() class Meta: model = Listing
class CountryTable(tables.ModelTable): foo = tables.Column() class Meta(object): model = Country columns = ( 'system', 'population', 'foo', 'tld', )
class SubnetTable(tables.ModelTable): id = tables.Column(sortable=False, visible=False) subnet = tables.Column() mask = tables.Column(sortable=False, visible=False) af = tables.Column(sortable=False, visible=False) asnum = tables.Column(name="Provider", data="asnum__name") listed = tables.Column() whitelisted = tables.Column() class Meta: model = Subnet exclude = ["last", "num_listed", "num_whitelisted"]
class ResultsTable(SarpamTable): formulation = tables.Column() fob_price = tables.Column(verbose_name="Median FOB Price (%s)"%settings.SARPAM_CURRENCY_CODE) landed_price = tables.Column(verbose_name="Median Landed Price (%s)"%settings.SARPAM_CURRENCY_CODE) msh_price = tables.Column(verbose_name="MSH International Median (%s)"%settings.SARPAM_CURRENCY_CODE) rows_template = "drug_price_rows.html" def __init__(self, rows, search_string): for row in rows: self.round_to_set_decimal_places(row, 'fob_price') self.round_to_set_decimal_places(row, 'landed_price') self.round_to_set_decimal_places(row, 'msh_price') SarpamTable.__init__(self, rows) self.search_string = search_string def as_html(self): return render_to_string('results.html', \ {'table':self, \ 'search_string':self.search_string}) def get_rows_template(self): return "drug_price_rows.html"
class HostTable(django_tables.MemoryTable): id = django_tables.Column(visible=False) host_name = django_tables.Column(verbose_name="Hostname") alias = django_tables.Column() register = django_tables.Column()
class FormulationGraph(SarpamTable): country = tables.Column() fob_price = tables.Column(verbose_name="FOB Price") landed_price = tables.Column(verbose_name="Landed Price") max_price = 0.001 NO_DATA = "" median_fob_price = 0.0 median_landed_price = 0.0 def __init__(self, rows, msh_price=None): # Don't like this, but rows is being modified below, causing side effects rows = deepcopy(rows) if msh_price is not None: # msh_price might be a Decimal, which max() thinks is larger than # any float, so in order to compare properly with floats we need # to convert msh_price to float as well self.max_price = max(self.max_price, float(msh_price)) # print "input data = %s" % rows (self.median_fob_price, self.median_landed_price) = \ utils.get_median_prices(rows) # print "median prices = %s, %s" % (self.median_fob_price, self.median_landed_price) for row in rows: new_max_price = max(self.max_price, row['fob_price'], row['landed_price']) # logging.warning("max of %s, %s and %s is %s" % # (self.max_price, row['fob_price'], row['landed_price'], # new_max_price)) self.max_price = new_max_price self.round_to_set_decimal_places(row, 'fob_price') self.round_to_set_decimal_places(row, 'landed_price') # scale up the value to between 1 and 10, then round up the first # digit to 1, 2 or 5 to make nicer graph scales rounded_up = self.max_price scale_factor = 1 while rounded_up < 1: rounded_up = rounded_up * 10 scale_factor = scale_factor * 10 while rounded_up > 10: rounded_up = rounded_up / 10.0 scale_factor = scale_factor / 10.0 if rounded_up > 5: rounded_up = 10.0 elif rounded_up > 2: rounded_up = 5.0 elif rounded_up > 1: rounded_up = 2.0 # else it's 1, which is fine # scale back down self.max_price = rounded_up / scale_factor # generate the intervals on the graph scale self.scale = [] for k in range(1, 11): self.scale.append(k * (self.max_price / 10)) self.msh_price = msh_price SarpamTable.__init__(self, rows) def as_html(self): extra_context = { 'sarpam_number_format': settings.SARPAM_NUMBER_FORMAT, 'sarpam_number_rounding': settings.SARPAM_NUMBER_ROUNDING, 'sarpam_currency_code': settings.SARPAM_CURRENCY_CODE, 'table': self, 'msh_price': self.msh_price, 'median_fob_price': self.median_fob_price, 'median_landed_price': self.median_landed_price } return render_to_string('formulation/graph.html', extra_context)
class BookTable(tables.MemoryTable): id = tables.Column() name = tables.Column()