Fixed bug on rows page, added unit test

Rows page for rows that linked to the same table in more
than one columns were display incorrectly. Fixed that and added a test.

Also introduced /db/table/row-pk.json?_extras=foreign_key_tables

This is used by the new unit test, but is the first example of a new
?_extras=comma-separated-list pattern I am introducing.
This commit is contained in:
Simon Willison 2017-11-23 13:09:45 -08:00
commit a30c5b220c
No known key found for this signature in database
GPG key ID: FBB38AFE227189DB
2 changed files with 88 additions and 9 deletions

View file

@ -685,14 +685,19 @@ class RowView(BaseView):
'foreign_key_tables': await self.foreign_key_tables(name, table, pk_values),
}
return {
data = {
'database': name,
'table': table,
'rows': rows,
'columns': columns,
'primary_keys': pks,
'primary_key_values': pk_values,
}, template_data
}
if 'foreign_key_tables' in (request.raw_args.get('_extras') or '').split(','):
data['foreign_key_tables'] = await self.foreign_key_tables(name, table, pk_values)
return data, template_data
async def foreign_key_tables(self, name, table, pk_values):
if len(pk_values) != 1:
@ -702,7 +707,7 @@ class RowView(BaseView):
return []
foreign_keys = table_info['foreign_keys']['incoming']
sql = 'select ' + ', '.join([
'(select count(*) from {table} where "{column}"= :id)'.format(
'(select count(*) from {table} where "{column}"=:id)'.format(
table=escape_sqlite_table_name(fk['other_table']),
column=fk['other_column'],
)
@ -713,12 +718,16 @@ class RowView(BaseView):
except sqlite3.OperationalError:
# Almost certainly hit the timeout
return []
foreign_table_counts = dict(zip([fk['other_table'] for fk in foreign_keys], rows[0]))
foreign_table_counts = dict(
zip(
[(fk['other_table'], fk['other_column']) for fk in foreign_keys],
list(rows[0]),
)
)
foreign_key_tables = []
for fk in foreign_keys:
count = foreign_table_counts[fk['other_table']]
if count:
foreign_key_tables.append({**fk, **{'count': count}})
count = foreign_table_counts.get((fk['other_table'], fk['other_column'])) or 0
foreign_key_tables.append({**fk, **{'count': count}})
return foreign_key_tables