Fix Decimal issue with JSON

This commit is contained in:
Lucas Verney 2017-04-27 07:52:16 +02:00
parent 847a5954d5
commit 65bd5db98a
6 changed files with 23 additions and 6 deletions

View File

@ -9,7 +9,6 @@ import collections
import json
import logging
import os
import shutil
import flatisfy.exceptions

View File

@ -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

View File

@ -34,6 +34,7 @@ def init(flats_list):
flat["urls"] = [flat["url"]]
# Create merged_ids key
flat["merged_ids"] = [flat["id"]]
return flats_list

View File

@ -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.

View File

@ -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.

View File

@ -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