Simplify server URL parsing & allow a server behind e.g. Varnish.

This commit is contained in:
Paul Makepeace 2011-04-26 15:44:32 -04:00
parent 7bcf52996a
commit df2661448a
2 changed files with 20 additions and 8 deletions

View File

@ -198,14 +198,10 @@ def RowsResponseFactory(column_index):
class RefineProject:
"""A Google Refine project."""
def __init__(self, server, project_id):
def __init__(self, server, project_id=None):
if not isinstance(server, RefineServer):
url = urlparse.urlparse(server)
if url.query:
# Parse out the project ID and create a base server URL
project_id = url.query[8:] # skip project=
server = urlparse.urlunparse((
url.scheme, url.netloc, '', '', '', ''))
if '/project?project=' in server:
server, project_id = server.split('/project?project=')
server = RefineServer(server)
self.server = server
if not project_id:
@ -215,7 +211,7 @@ class RefineProject:
self.sorting = facet.Sorting()
# following filled in by get_models()
self.has_records = False
self.column_order = {} # order of column in UI
self.column_order = {} # order of columns in UI
self.rows_response_factory = None # for parsing get_rows()
self.get_models()

View File

@ -46,5 +46,21 @@ class RefineRowsTest(unittest.TestCase):
self.assertEqual(response.rows[0]['name'], 'Danny Baron')
class RefineProjectTest(unittest.TestCase):
def test_server_init(self):
RP = refine.RefineProject
# Mock out get_models() so it doesn't attempt to connect to a server
RP.get_models = lambda self: self
p = RP('http://127.0.0.1:3333/project?project=1658955153749')
self.assertEqual(p.server.server, 'http://127.0.0.1:3333')
self.assertEqual(p.project_id, '1658955153749')
p = RP('http://127.0.0.1:3333', '1658955153749')
self.assertEqual(p.server.server, 'http://127.0.0.1:3333')
self.assertEqual(p.project_id, '1658955153749')
p = RP('http://server/varnish/project?project=1658955153749')
self.assertEqual(p.server.server, 'http://server/varnish')
self.assertEqual(p.project_id, '1658955153749')
if __name__ == '__main__':
unittest.main()