From 88c6b64b7916a86a1c95005c01758044f3499b9b Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Mon, 13 Feb 2017 16:56:57 -0500 Subject: [PATCH] Clean up the get_or_create_map function. --- xonstat/views/submission.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/xonstat/views/submission.py b/xonstat/views/submission.py index 2e89c15..0cd61be 100644 --- a/xonstat/views/submission.py +++ b/xonstat/views/submission.py @@ -556,32 +556,27 @@ def get_or_create_server(session, name, hashkey, ip_addr, revision, port, impure return server -def get_or_create_map(session=None, name=None): +def get_or_create_map(session, name): """ Find a map by name or create one if not found. Parameters: session - SQLAlchemy database session factory name - map name of the map to be found or created """ - try: - # find one by the name, if it exists - gmap = session.query(Map).filter_by(name=name).one() - log.debug("Found map id {0}: {1}".format(gmap.map_id, - gmap.name)) - except NoResultFound, e: + maps = session.query(Map).filter_by(name=name).order_by(Map.map_id).all() + + if maps is None or len(maps) == 0: gmap = Map(name=name) session.add(gmap) session.flush() - log.debug("Created map id {0}: {1}".format(gmap.map_id, - gmap.name)) - except MultipleResultsFound, e: - # multiple found, so use the first one but warn - log.debug(e) - gmaps = session.query(Map).filter_by(name=name).order_by( - Map.map_id).all() - gmap = gmaps[0] - log.debug("Found map id {0}: {1} but found \ - multiple".format(gmap.map_id, gmap.name)) + log.debug("Created map id {}: {}".format(gmap.map_id, gmap.name)) + elif len(maps) == 1: + gmap = maps[0] + log.debug("Found map id {}: {}".format(gmap.map_id, gmap.name)) + else: + gmap = maps[0] + map_id_list = ", ".join(["{}".format(m.map_id) for m in maps]) + log.warn("Multiple maps found for {} ({})! Using the first one.".format(name, map_id_list)) return gmap -- 2.39.2