Skip to content

Commit

Permalink
[FIX] code format and pylint:
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 527e215
Author: Lindsay <[email protected]>
Date:   Tue Jun 2 14:47:52 2020 +0200

    fixing typo

commit dd9c3ec
Author: Lindsay <[email protected]>
Date:   Tue Jun 2 14:42:47 2020 +0200

    fixing pylint travis

commit 7899f9b
Author: Lindsay <[email protected]>
Date:   Tue Jun 2 14:05:25 2020 +0200

    fix travis flake8 linting

commit 039e867
Author: Lindsay <[email protected]>
Date:   Tue Jun 2 13:54:07 2020 +0200

    fix travis pylint

commit fcfb09e
Author: Lindsay <[email protected]>
Date:   Tue Jun 2 13:45:09 2020 +0200

    fixing travis tests

commit ff2b39e
Author: Lindsay <[email protected]>
Date:   Tue Jun 2 12:07:45 2020 +0200

    fix import

commit cdf43dc
Author: Lindsay <[email protected]>
Date:   Tue Jun 2 11:47:21 2020 +0200

    add responses to travis for testing

commit 79da0ee
Author: Lindsay <[email protected]>
Date:   Tue Jun 2 11:40:55 2020 +0200

    disable pylint warning W0622

commit d6eab49
Author: Lindsay <[email protected]>
Date:   Tue Jun 2 10:29:58 2020 +0200

    formatting for linters
  • Loading branch information
lmarion-source authored and lmignon committed Jun 19, 2020
1 parent 2fd46d4 commit 6eea371
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ install:
- git clone https://github.com/OCA/maintainer-quality-tools.git ${HOME}/maintainer-quality-tools
- export PATH=${HOME}/maintainer-quality-tools/travis:${PATH}
- travis_install_nightly
- pip install geojson Shapely sphinx sphinx_bootstrap_theme sphinx-intl odoo-sphinx-autodoc
- pip install geojson Shapely sphinx sphinx_bootstrap_theme sphinx-intl odoo-sphinx-autodoc responses
# This is to solve an issue with stdout file descriptor
# raising a BlockingIOError while executing pylint and returns an exit -1
# seems related to the numerous lint errors on web_view_google_map module
Expand Down
28 changes: 14 additions & 14 deletions base_geoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
logger.warning('Shapely or geojson are not available in the sys path')


# pylint: disable=sql-injection, invalid-commit
class GeoField(Field):
""" The field descriptor contains the field definition common to all
specialized fields for geolocalization. Subclasses must define a type
Expand Down Expand Up @@ -55,8 +56,7 @@ def convert_to_column(self, value, record):
shape_to_write = self.entry_to_shape(value, same_type=True)
if shape_to_write.is_empty:
return None
else:
return shape_to_write.wkt
return shape_to_write.wkt

def convert_to_cache(self, value, record, validate=True):
val = value
Expand Down Expand Up @@ -117,7 +117,6 @@ def create_geo_column(self, cr, col_name, table, model):
raise
finally:
cr.commit()

return True

def entry_to_shape(self, value, same_type=False):
Expand Down Expand Up @@ -190,6 +189,7 @@ def update_geo_column(self, cr, col_name, table, model):

class GeoLine(GeoField):
"""Field for POSTGIS geometry Line type"""

type = 'geo_line'
geo_type = 'LINESTRING'

Expand Down Expand Up @@ -261,28 +261,28 @@ def from_latlon(cls, cr, latitude, longitude):
def to_latlon(cls, cr, geopoint):
""" Convert a UTM coordinate point to (latitude, longitude):
"""
# Line to execute to retrieve longitude, latitude from UTM in postgres command line:
# Line to execute to retrieve longitude, latitude from UTM
# in postgres command line:
# SELECT ST_X(geom), ST_Y(geom) FROM (SELECT ST_TRANSFORM(ST_SetSRID(
# ST_MakePoint(601179.61612, 6399375,681364), 3847), 4326) as geom) g;
# ST_MakePoint(601179.61612, 6399375,681364), 3847), 4326) as geom) g;
if isinstance(geopoint, BaseGeometry):
geo_point_instance = geopoint
else:
geo_point_instance = asShape(geojson.loads(geopoint))
cr.execute("""
SELECT
ST_TRANSFORM(
ST_SetSRID(
ST_MakePoint(
%(coord_x)s, %(coord_y)s
),
%(srid)s
), 4326)""",
{'coord_x': geo_point_instance.x, 'coord_y':geo_point_instance.y, 'srid': cls._slots['srid']})
SELECT
ST_TRANSFORM(
ST_SetSRID(ST_MakePoint(%(coord_x)s, %(coord_y)s),
%(srid)s), 4326)""",
{'coord_x': geo_point_instance.x,
'coord_y': geo_point_instance.y,
'srid': cls._slots['srid']})

res = cr.fetchone()
point_latlon = cls.load_geo(res[0])
return point_latlon.x, point_latlon.y


class GeoPolygon(GeoField):
"""Field for POSTGIS geometry Polygon type"""
type = 'geo_polygon'
Expand Down
19 changes: 11 additions & 8 deletions base_geoengine/geo_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""Helper to setup Postgis"""
import logging

from odoo import _
from odoo.exceptions import MissingError

logger = logging.getLogger('geoengine.sql')
Expand Down Expand Up @@ -33,12 +34,14 @@ def init_postgis(cr):
""")
except Exception:
raise MissingError(
"Error, can not automatically initialize spatial postgis support. "
"Database user may have to be superuser and postgres/postgis "
"extentions with their devel header have to be installed. "
"If you do not want Odoo to connect with a super user "
"you can manually prepare your database. To do this"
"open a client to your database using a super user and run: \n"
"CREATE EXTENSION postgis;\n"
"CREATE EXTENSION postgis_topology;\n"
_("Error,"
"can not automatically initialize spatial postgis support."
"Database user may have to be superuser and postgres/postgis "
"extentions with their devel header have to be installed. "
"If you do not want Odoo to connect with a super user "
"you can manually prepare your database. To do this"
"open a client to your database using a super user and run: \n"
"CREATE EXTENSION postgis;\n"
"CREATE EXTENSION postgis_topology;\n")

)
4 changes: 4 additions & 0 deletions base_geoengine/geo_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def geo_search(model, domain=None, geo_domain=None, offset=0,
* geo_contains
* geo_intersect
"""

cr = model._cr
domain = domain or []
geo_domain = geo_domain or []
Expand Down Expand Up @@ -114,9 +115,12 @@ def geo_search(model, domain=None, geo_domain=None, offset=0,
where_statement = " WHERE %s" % (u' '.join(where_clause_arr))
else:
where_statement = u''

# pylint: disable=sql-injection
sql = 'SELECT "%s".id FROM ' % model._table + from_clause + \
where_statement + order_by + limit_str + offset_str
# logger.debug(cursor.mogrify(sql, where_clause_params))

cr.execute(sql, where_clause_params)
res = cr.fetchall()
if res:
Expand Down
16 changes: 6 additions & 10 deletions base_geoengine/tests/test_geoengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
from odoo import fields
from odoo.exceptions import MissingError

from odoo.addons.base_geoengine.geo_model import GeoModel
from odoo.addons.base_geoengine import fields as geo_fields
from odoo.addons.base_geoengine.fields import GeoPoint
from ..geo_model import GeoModel
from .. import fields as geo_fields
from ..fields import GeoPoint
from .data import MULTIPOLYGON_1, GEO_VIEW, FORM_VIEW
from .data import EXPECTED_GEO_COLUMN_MULTIPOLYGON

Expand Down Expand Up @@ -274,10 +274,9 @@ def test_create_line_from_points(self):
self.env.cr, geo_point_1, geo_point_2)
self.assertEqual(geo_line, expected_line)


def test_from_lat_lon(self):
latitude = 49.72842315886126
longitude = 5.400488376617026
longitude = 5.400488376617026

# This is computed with postgis in postgres:

Expand All @@ -287,13 +286,10 @@ def test_from_lat_lon(self):

self.assertAlmostEqual(geo_point.x, expected_coordinates[0], 4)
self.assertAlmostEqual(geo_point.y, expected_coordinates[1], 4)

def test_to_lat_lon(self):
x = 613393.2849222135
y = 5587677.847237722

geo_point = Point(601179.61612, 6399375,681364)
expected_lat_lon = [49.72842315886126, 5.400488376617026]
geo_point = Point(601179.61612, 6399375.681364)

longitude, latitude = GeoPoint.to_latlon(self.env.cr, geo_point)

Expand Down
4 changes: 2 additions & 2 deletions base_geoengine_demo/models/geo_npa.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class NPA(geo_model.GeoModel):
city = fields.Char('City', size=64, index=True, required=True)
the_geom = geo_fields.GeoMultiPolygon('NPA Shape')
total_sales = fields.Float(
compute='_get_ZIP_total_sales',
compute='_compute_ZIP_total_sales',
string='Spatial! Total Sales',
)

@api.multi
def _get_ZIP_total_sales(self):
def _compute_ZIP_total_sales(self):
"""Return the total of the invoiced sales for this npa"""
mach_obj = self.env['geoengine.demo.automatic.retailing.machine']
for rec in self:
Expand Down
2 changes: 1 addition & 1 deletion base_geoengine_demo/models/retail_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from odoo.addons.base_geoengine import geo_model
from odoo.addons.base_geoengine import fields as geo_fields


logger = logging.getLogger(__name__)

try:
import geojson
except ImportError:
Expand Down
28 changes: 17 additions & 11 deletions base_geolocalize_openstreetmap/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import requests

from odoo import api, exceptions, models, _
from odoo import exceptions, models, _

_logger = logging.getLogger(__name__)

Expand All @@ -16,8 +16,8 @@ class ResPartner(models.AbstractModel):
_inherit = 'res.partner'

@classmethod
def _geocode_address(
cls, street=None, zip_code=None, city=None, state=None, country=None):
def _geocode_address(cls, street=None,
zip_code=None, city=None, state=None, country=None):
"""Get the latitude and longitude by requesting Openstreetmap"
"""
pay_load = {
Expand All @@ -30,22 +30,28 @@ def _geocode_address(
'country': country or '',
}

request_result = requests.get(cls._url, params=pay_load)
request_result = requests.get(cls._url, params=pay_load)
try:
request_result.raise_for_status()
except Exception as e:
_logger.exception('Geocoding error')
raise exceptions.Warning(
_('Geocoding error. \n %s') % e.message)
values = request_result.json()
values = values and values[0] or {}
values = values[0] if values else {}
return values

@classmethod
def _geo_localize(cls, apikey, street='', zip='', city='', state='', country=''):
result = cls._geocode_address(street=street, zip_code=zip, city=city, state=state, country=country)

def _geo_localize(cls,
apikey, street='', zip='',
city='', state='', country=''):
# pylint: disable=W0622
result = cls._geocode_address(street=street,
zip_code=zip, city=city,
state=state, country=country)

if not result:
result = cls._geocode_address(city=city, state=state, country=country)

return result.get('lat'), result.get('lon')
result = cls._geocode_address(city=city,
state=state, country=country)

return result.get('lat'), result.get('lon')
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# Copyright 2015-2017 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import responses
import requests
import odoo.tests.common as common


class TestGeolocalizeOpenstreetmap(common.TransactionCase):

def setUp(self):
common.TransactionCase.setUp(self)
super(TestGeolocalizeOpenstreetmap, self).setUp()
self.expected_latitude = 50.4311411
self.expected_longitude = 4.6132813

Expand All @@ -22,10 +22,10 @@ def setUp(self):
'state': 'Namur'
}


self.partner_id_known = self.env['res.partner'].create(vals)

# Second, test a fake street to force the code to go to the second iteration
# Second, test a fake street to force the code to go to
# the second iteration
vals2 = {
'name': 'Partner Project',
'street': 'Rue test',
Expand All @@ -34,7 +34,8 @@ def setUp(self):
}
self.partner_id_known_second = self.env['res.partner'].create(vals2)

# Third, test a fake street to force the code to go to the second iteration
# Third, test a fake street to force the code to go
# to the second iteration
vals3 = {
'name': 'Partner Project',
'street': 'Rue test',
Expand All @@ -43,50 +44,56 @@ def setUp(self):
}
self.partner_id_not_known = self.env['res.partner'].create(vals3)


@responses.activate
def test_osm_found_lat_long_with_mock(self):
responses.add(
responses.Response(
method='GET',
url ='https://nominatim.openstreetmap.org/search?city=Tamines&format=json&country=Belgium&state=&street=Rue+bois+des+noix&limit=1&postalCode=5060',
url='https://nominatim.openstreetmap.org/search?' +
'city=Tamines&format=json&country=Belgium&' +
'state=&street=Rue+bois+des+noix&limit=1&postalCode=5060',
match_querystring=True,
json=[{'lat': self.expected_latitude, 'lon': self.expected_longitude}],
json=[{'lat': self.expected_latitude,
'lon': self.expected_longitude}],
status=200
))

self.partner_id_known.geo_localize()

self.assertEqual(len(responses.calls), 1, 'call does not exist')
self.assertAlmostEqual(
self.partner_id_known.partner_latitude, self.expected_latitude, 3,
self.partner_id_known.partner_latitude, self.expected_latitude, 3,
'Latitude Should be equals')
self.assertAlmostEqual(
self.partner_id_known.partner_longitude, self.expected_longitude, 3,
self.partner_id_known.partner_longitude,
self.expected_longitude, 3,
'Longitude Should be equals')


@responses.activate
def test_osm_found_lat_long_second_time_with_mock(self):
responses.add(
responses.Response(
method='GET',
url ='https://nominatim.openstreetmap.org/search?city=Tamines&format=json&country=Belgium&state=&street=Rue+test&limit=1&postalCode=',
url='https://nominatim.openstreetmap.org/search?city=Tamines' +
'&format=json&country=Belgium&state=&street=Rue+test&' +
'limit=1&postalCode=',
match_querystring=True,
json=[{}],
status=200
))
responses.add(
responses.Response(
method='GET',
url ='https://nominatim.openstreetmap.org/search?city=Tamines&format=json&country=Belgium&state=&street=&limit=1&postalCode=',
url='https://nominatim.openstreetmap.org/search?city=Tamines' +
'&format=json&country=Belgium&state=&street=&' +
'limit=1&postalCode=',
match_querystring=True,
json=[{'lat': 50.825833, 'lon': 4.3475227}],
status=200
))

self.partner_id_known_second.geo_localize()

self.assertAlmostEqual(
self.partner_id_known_second.partner_latitude, 50.825833, 3,
'Latitude Should be equals')
Expand All @@ -100,23 +107,26 @@ def test_osm_loc_not_found_with_mock(self):
responses.add(
responses.Response(
method='GET',
url='https://nominatim.openstreetmap.org/search?city=Tmnss&format=json&country=Belgium&state=&street=Rue+test&limit=1&postalCode=',
url='https://nominatim.openstreetmap.org/search?city=Tmnss&' +
'format=json&country=Belgium&state=&street=Rue+test&' +
'limit=1&postalCode=',
match_querystring=True,
json=[{}]
))

responses.add(
responses.Response(
method='GET',
url='https://nominatim.openstreetmap.org/search?city=Tmnss&format=json&country=Belgium&state=&street=&limit=1&postalCode=',
url='https://nominatim.openstreetmap.org/search?city=Tmnss&' +
'format=json&country=Belgium&state=&street=&' +
'limit=1&postalCode=',
match_querystring=True,
json=[{}]
))

self.partner_id_not_known.geo_localize()


self.assertFalse(
self.partner_id_not_known.partner_longitude)
self.assertFalse(
self.partner_id_not_known.partner_latitude)
self.partner_id_not_known.partner_latitude)
Loading

0 comments on commit 6eea371

Please sign in to comment.