Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updating schema and templates

  • Loading branch information...
commit eaaf608e6e3279c47a805452727531e4d10a8952 1 parent 8ca8053
@chrisclark chrisclark authored
Showing with 20 additions and 10 deletions.
  1. +5 −5 explorer/tests/test_utils.py
  2. +15 −5 explorer/utils.py
View
10 explorer/tests/test_utils.py
@@ -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)
View
20 explorer/utils.py
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.