Skip to content

Commit

Permalink
updating schema and templates
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisclark committed Feb 9, 2014
1 parent 8ca8053 commit eaaf608
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
10 changes: 5 additions & 5 deletions explorer/tests/test_utils.py
Expand Up @@ -98,19 +98,19 @@ def test_transforms_are_identified_in_headers(self):

def test_transform_alters_row(self):
headers = ['foo', 'bar']
transforms = get_transforms(headers, [('bar', 'http://www.%s.com')])
transforms = get_transforms(headers, [('bar', 'http://www.{0}.com')])
row = [1, 2]
got = transform_row(transforms, row)
self.assertEqual([1, 'http://www.2.com'], got)

def test_multiple_transforms(self):
headers = ['foo', 'bar']
transforms = get_transforms(headers, [('foo', '<a href="%s">link</a>'),
('bar', 'x: %s')])
transforms = get_transforms(headers, [('foo', '<a href="{0}">{0}</a>'),
('bar', 'x: {0}')])
rows = [[1, 2], ['a', 'b']]
got = [transform_row(transforms, row) for row in rows]
expected = [
['<a href="1">link</a>', 'x: 2'],
['<a href="a">link</a>', 'x: b']
['<a href="1">1</a>', 'x: 2'],
['<a href="a">a</a>', 'x: b']
]
self.assertEqual(expected, got)
20 changes: 15 additions & 5 deletions explorer/utils.py
Expand Up @@ -55,7 +55,7 @@ def get_transforms(headers, transforms):
def transform_row(transforms, row):
row = [x.encode('utf-8') if type(x) is unicode else x for x in list(row)]
for i, t in transforms:
row[i] = t % str(row[i])
row[i] = t.format(str(row[i]))
return row


Expand All @@ -74,10 +74,20 @@ def schema_info():
for app in apps:
for model in models.get_models(app):
friendly_model = "%s -> %s" % (app.__package__, model._meta.object_name)
cur_app = (friendly_model, str(model._meta.db_table), [])
for f in model._meta.fields:
cur_app[2].append((f.get_attname_column()[1], f.get_internal_type()))
ret.append(cur_app)
ret.append((
friendly_model,
model._meta.db_table,
[(f.get_attname_column()[1], f.get_internal_type()) for f in model._meta.fields]
))

#Do the same thing for many_to_many fields. These don't show up in the field list of the model
#because they are stored as separate "through" relations and have their own tables
ret += [(
friendly_model,
m2m.rel.through._meta.db_table,
[(f.get_attname_column()[1], f.get_internal_type()) for f in m2m.rel.through._meta.fields]
) for m2m in model._meta.many_to_many]

return sorted(ret, key=lambda t: t[1]) # sort by table name


Expand Down

0 comments on commit eaaf608

Please sign in to comment.