From 3d8865daf23f43aa72d867ad39d7340babe4397a Mon Sep 17 00:00:00 2001 From: Antoine Goutenoir Date: Sun, 16 Feb 2020 11:32:34 +0100 Subject: [PATCH] Improve warnings and backoffice usability. --- flaskr/controllers/main_controller.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++------------------- flaskr/models.py | 20 ++++++++++++++------ 2 files changed, 65 insertions(+), 25 deletions(-) diff --git a/flaskr/controllers/main_controller.py b/flaskr/controllers/main_controller.py index b7804b3..cde195d 100644 --- a/flaskr/controllers/main_controller.py +++ b/flaskr/controllers/main_controller.py @@ -251,9 +251,15 @@ def compute(): # process the queue of estimation requests db.session.commit() def _handle_warning(_estimation, _warning_message): - _estimation.warnings = _warning_message + if not _estimation.warnings: + _estimation.warnings = _warning_message + else: + _estimation.warnings += u"\n" + _warning_message + # _estimation.warnings = u"%s\n%s" % \ + # (_estimation.warnings, _warning_message) db.session.commit() + estimation = None try: response = "" @@ -296,37 +302,48 @@ def compute(): # process the queue of estimation requests origins = [] if origins_addresses_count > maximum_addresses_to_compute: - errmsg = u"Too many origins. (%d > %d) Please contact us for support of that many origins." % (origins_addresses_count, maximum_addresses_to_compute) + errmsg = u"Too many origins. (%d > %d) \n" \ + u"Please contact us " \ + u"for support of more origins." % \ + (origins_addresses_count, maximum_addresses_to_compute) _handle_failure(estimation, errmsg) return _respond(errmsg) for i in range(origins_addresses_count): origin_address = origins_addresses[i].strip() + + if not origin_address: + continue + if origin_address in failed_addresses: continue try: origin = geocoder.geocode(origin_address.encode('utf-8')) except geopy.exc.GeopyError as e: - response += u"Failed to geocode origin `%s`.\n%s\n" % ( - origin_address, e, + warning = u"Ignoring origin `%s` " \ + u"since we failed to geocode it.\n%s\n" % ( + origin_address, e, ) - _handle_warning(estimation, response) + response += warning + _handle_warning(estimation, warning) failed_addresses.append(origin_address) continue if origin is None: - response += u"Failed to geocode origin `%s`.\n" % ( - origin_address, + warning = u"Ignoring origin `%s` " \ + u"since we failed to geocode it.\n" % ( + origin_address, ) - _handle_warning(estimation, response) + response += warning + _handle_warning(estimation, warning) failed_addresses.append(origin_address) continue origins.append(origin) - response += u"Origin: %s == %s (%f, %f)\n" % ( + response += u"Origin `%s` geocoded to `%s` (%f, %f).\n" % ( origin_address, origin.address, origin.latitude, origin.longitude, ) @@ -338,31 +355,45 @@ def compute(): # process the queue of estimation requests destinations = [] if destinations_addresses_count > maximum_addresses_to_compute: - errmsg = u"Too many destinations. (%d > %d) Please contact us for support of that many destinations." % (destinations_addresses_count, maximum_addresses_to_compute) + errmsg = u"Too many destinations. (%d > %d) \n" \ + u"Please contact us " \ + u"for support of that many destinations." \ + % ( + destinations_addresses_count, + maximum_addresses_to_compute, + ) _handle_failure(estimation, errmsg) return _respond(errmsg) for i in range(destinations_addresses_count): destination_address = destinations_addresses[i].strip() + + if not destination_address: + continue + if destination_address in failed_addresses: continue try: destination = geocoder.geocode(destination_address.encode('utf-8')) except geopy.exc.GeopyError as e: - response += u"Failed to geocode destination `%s`.\n%s\n" % ( - destination_address, e, + warning = u"Ignoring destination `%s` " \ + u"since we failed to geocode it.\n%s\n" % ( + destination_address, e, ) - _handle_warning(estimation, response) + response += warning + _handle_warning(estimation, warning) failed_addresses.append(destination_address) continue if destination is None: - response += u"Failed to geocode destination `%s`.\n" % ( - destination_address, + warning = u"Ignoring destination `%s` " \ + u"since we failed to geocode it.\n" % ( + destination_address, ) - _handle_warning(estimation, response) + response += warning + _handle_warning(estimation, warning) failed_addresses.append(destination_address) continue @@ -370,7 +401,7 @@ def compute(): # process the queue of estimation requests destinations.append(destination) - response += u"Destination: %s == %s (%f, %f)\n" % ( + response += u"Destination `%s` geocoded to `%s` (%f, %f).\n" % ( destination_address, destination.address, destination.latitude, destination.longitude, ) @@ -380,11 +411,11 @@ def compute(): # process the queue of estimation requests # GTFO IF NO ORIGINS OR NO DESTINATIONS ################################### if 0 == len(origins): - response += u"Failed to geocode all the origin(s).\n" + response += u"Failed to geocode ALL the origin(s).\n" _handle_failure(estimation, response) return _respond(response) if 0 == len(destinations): - response += u"Failed to geocode all the destination(s).\n" + response += u"Failed to geocode ALL the destination(s).\n" _handle_failure(estimation, response) return _respond(response) @@ -576,6 +607,7 @@ def compute(): # process the queue of estimation requests estimation.status = StatusEnum.success # estimation.output_yaml = u"%s" % yaml_dump(results) + estimation.informations = response estimation.set_output_dict(results) db.session.commit() diff --git a/flaskr/models.py b/flaskr/models.py index f8ea9c5..d936439 100755 --- a/flaskr/models.py +++ b/flaskr/models.py @@ -81,6 +81,18 @@ class Estimation(db.Model): return self.destination_addresses.strip().count("\n") + 1 @property + def errors_tail(self): + return self.get_tail(self.errors) + + @property + def warnings_tail(self): + return self.get_tail(self.warnings) + + def get_tail(self, of_string, of_length=140): + if not of_string: + return "" + return u"...%s" % of_string[-(min(of_length, len(of_string))):] + def has_failed(self): return self.status == StatusEnum.failure @@ -120,19 +132,15 @@ class Estimation(db.Model): self._output_dict = yaml_load(self.output_yaml) return self._output_dict - @property def is_one_to_one(self): return self.scenario == ScenarioEnum.one_to_one - @property def is_one_to_many(self): return self.scenario == ScenarioEnum.one_to_many - @property def is_many_to_one(self): return self.scenario == ScenarioEnum.many_to_one - @property def is_many_to_many(self): return self.scenario == ScenarioEnum.many_to_many @@ -157,8 +165,8 @@ class EstimationView(ModelView): 'scenario', 'origins_count', 'destinations_count', - 'warnings', - 'errors', + 'warnings_tail', + 'errors_tail', ) # Enable search functionality - it will search for terms in -- libgit2 0.21.2