From 65bd5db98abd7100ee5d7e42ae9526f0dd6362a7 Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Thu, 27 Apr 2017 07:52:16 +0200 Subject: [PATCH] Fix Decimal issue with JSON --- flatisfy/data.py | 1 - flatisfy/fetch.py | 20 +++++++++++++++++++- flatisfy/filters/metadata.py | 1 + flatisfy/web/configplugin.py | 2 +- flatisfy/web/dbplugin.py | 3 +-- flatisfy/web/js_src/views/details.vue | 2 +- 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/flatisfy/data.py b/flatisfy/data.py index 807cce8..7f51efb 100644 --- a/flatisfy/data.py +++ b/flatisfy/data.py @@ -9,7 +9,6 @@ import collections import json import logging import os -import shutil import flatisfy.exceptions diff --git a/flatisfy/fetch.py b/flatisfy/fetch.py index 7fea23b..68e949f 100644 --- a/flatisfy/fetch.py +++ b/flatisfy/fetch.py @@ -8,7 +8,6 @@ import itertools import json import logging -from flatisfy import data from flatisfy import tools LOGGER = logging.getLogger(__name__) @@ -38,6 +37,22 @@ class WeboobProxy(object): """ return WebNip.VERSION + @staticmethod + def restore_decimal_fields(flat): + """ + Parse fields expected to be in Decimal type to float. They were dumped + as str in the JSON dump process. + + :param flat: A flat dict. + :return: A flat dict with Decimal fields converted to float. + """ + for field in ["area", "cost", "rooms", "bedrooms", "price_per_meter"]: + try: + flat[field] = float(flat[field]) + except (TypeError, ValueError): + flat[field] = None + return flat + def __init__(self, config): """ Create a Weboob handle and try to load the modules. @@ -177,6 +192,8 @@ def fetch_flats_list(config): LOGGER.info("Fetched %d flats.", len(housing_posts)) flats_list = [json.loads(flat) for flat in housing_posts] + flats_list = [WeboobProxy.restore_decimal_fields(flat) + for flat in flats_list] return flats_list @@ -193,6 +210,7 @@ def fetch_details(config, flat_id): weboob_output = weboob_proxy.info(flat_id) flat_details = json.loads(weboob_output) + flats_details = WeboobProxy.restore_decimal_fields(flat_details) LOGGER.info("Fetched details for flat %s.", flat_id) return flat_details diff --git a/flatisfy/filters/metadata.py b/flatisfy/filters/metadata.py index e52050e..f0cab7e 100644 --- a/flatisfy/filters/metadata.py +++ b/flatisfy/filters/metadata.py @@ -34,6 +34,7 @@ def init(flats_list): flat["urls"] = [flat["url"]] # Create merged_ids key flat["merged_ids"] = [flat["id"]] + return flats_list diff --git a/flatisfy/web/configplugin.py b/flatisfy/web/configplugin.py index 68d8a04..659bc7c 100644 --- a/flatisfy/web/configplugin.py +++ b/flatisfy/web/configplugin.py @@ -32,7 +32,7 @@ class ConfigPlugin(object): """ self.config = config - def setup(self, app): # pylint: disable=no-self-use + def setup(self, app): # pylint: disable=locally-disabled,no-self-use """ Make sure that other installed plugins don't affect the same keyword argument and check if metadata is available. diff --git a/flatisfy/web/dbplugin.py b/flatisfy/web/dbplugin.py index 1049b9b..1ebc09c 100644 --- a/flatisfy/web/dbplugin.py +++ b/flatisfy/web/dbplugin.py @@ -11,7 +11,6 @@ from __future__ import ( absolute_import, division, print_function, unicode_literals ) -import functools import inspect import bottle @@ -33,7 +32,7 @@ class DatabasePlugin(object): """ self.get_session = get_session - def setup(self, app): # pylint: disable=no-self-use + def setup(self, app): # pylint: disable=locally-disabled,no-self-use """ Make sure that other installed plugins don't affect the same keyword argument and check if metadata is available. diff --git a/flatisfy/web/js_src/views/details.vue b/flatisfy/web/js_src/views/details.vue index 63dd8d0..4cada5d 100644 --- a/flatisfy/web/js_src/views/details.vue +++ b/flatisfy/web/js_src/views/details.vue @@ -197,7 +197,7 @@ export default { }, displayedStations () { if (this.flat.flatisfy_stations.length > 0) { - const stationsNames = this.flat.flatisfy_stations.map(station => station.name) + const stationsNames = this.flat.flatisfy_stations.map(station => capitalize(station.name)) return stationsNames.join(', ') } else { return null