Browse Source

Fix Decimal issue with JSON

responsive
Lucas Verney 6 years ago
parent
commit
65bd5db98a
  1. 1
      flatisfy/data.py
  2. 20
      flatisfy/fetch.py
  3. 1
      flatisfy/filters/metadata.py
  4. 2
      flatisfy/web/configplugin.py
  5. 3
      flatisfy/web/dbplugin.py
  6. 2
      flatisfy/web/js_src/views/details.vue

1
flatisfy/data.py

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

20
flatisfy/fetch.py

@ -8,7 +8,6 @@ import itertools @@ -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): @@ -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): @@ -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): @@ -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

1
flatisfy/filters/metadata.py

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

2
flatisfy/web/configplugin.py

@ -32,7 +32,7 @@ class ConfigPlugin(object): @@ -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.

3
flatisfy/web/dbplugin.py

@ -11,7 +11,6 @@ from __future__ import ( @@ -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): @@ -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.

2
flatisfy/web/js_src/views/details.vue

@ -197,7 +197,7 @@ export default { @@ -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

Loading…
Cancel
Save