Skip to content

Commit

Permalink
[IMP] base_external_dbsource: black, isort
Browse files Browse the repository at this point in the history
  • Loading branch information
sergio-teruel committed Jan 9, 2020
1 parent c37fd14 commit 2aa799c
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 190 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ line_length=88
known_odoo=odoo
known_odoo_addons=odoo.addons
sections=FUTURE,STDLIB,THIRDPARTY,ODOO,ODOO_ADDONS,FIRSTPARTY,LOCALFOLDER
known_third_party=
known_third_party=mock,psycopg2
10 changes: 5 additions & 5 deletions base_external_dbsource/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ External Database Sources
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github
:target: https://github.com/OCA/server-backend/tree/12.0/base_external_dbsource
:target: https://github.com/OCA/server-backend/tree/13.0/base_external_dbsource
:alt: OCA/server-backend
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-backend-12-0/server-backend-12-0-base_external_dbsource
:target: https://translation.odoo-community.org/projects/server-backend-13-0/server-backend-13-0-base_external_dbsource
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/253/12.0
:target: https://runbot.odoo-community.org/runbot/253/13.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -75,7 +75,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-backend/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-backend/issues/new?body=module:%20base_external_dbsource%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/server-backend/issues/new?body=module:%20base_external_dbsource%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand Down Expand Up @@ -111,6 +111,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/server-backend <https://github.com/OCA/server-backend/tree/12.0/base_external_dbsource>`_ project on GitHub.
This module is part of the `OCA/server-backend <https://github.com/OCA/server-backend/tree/13.0/base_external_dbsource>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
33 changes: 11 additions & 22 deletions base_external_dbsource/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@
# Copyright <2016> <Henry Zhou MAXodoo>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'External Database Sources',
'version': '12.0.1.0.1',
'category': 'Tools',
'author': "Daniel Reis, "
"LasLabs, "
"Odoo Community Association (OCA)",
'website': 'https://github.com/OCA/server-backend',
'license': 'LGPL-3',
'images': [
'images/screenshot01.png',
],
'depends': [
'base',
],
'data': [
'views/base_external_dbsource.xml',
'security/ir.model.access.csv',
],
'demo': [
'demo/base_external_dbsource.xml',
],
'installable': True,
"name": "External Database Sources",
"version": "13.0.1.0.0",
"category": "Tools",
"author": "Daniel Reis, " "LasLabs, " "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/server-backend",
"license": "LGPL-3",
"images": ["images/screenshot01.png"],
"depends": ["base"],
"data": ["views/base_external_dbsource.xml", "security/ir.model.access.csv"],
"demo": ["demo/base_external_dbsource.xml"],
"installable": True,
}
119 changes: 51 additions & 68 deletions base_external_dbsource/models/base_external_dbsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

import logging
import psycopg2

from contextlib import contextmanager

import psycopg2

from odoo import _, api, fields, models, tools

from ..exceptions import ConnectionFailedError, ConnectionSuccessError
Expand All @@ -33,18 +33,18 @@ class BaseExternalDbsource(models.Model):
"""

_name = "base.external.dbsource"
_description = 'External Database Sources'
_description = "External Database Sources"

CONNECTORS = [
('postgresql', 'PostgreSQL'),
]
CONNECTORS = [("postgresql", "PostgreSQL")]
# This is appended to the conn string if pass declared but not detected.
# Children should declare PWD_STRING_CONNECTOR (such as PWD_STRING_FBD)
# to allow for override.
PWD_STRING = 'PWD=%s;'
PWD_STRING = "PWD=%s;"

name = fields.Char('Datasource name', required=True, size=64)
conn_string = fields.Text('Connection string', help="""
name = fields.Char("Datasource name", required=True, size=64)
conn_string = fields.Text(
"Connection string",
help="""
Sample connection strings:
- Microsoft SQL Server:
mssql+pymssql://username:%s@server:port/dbname?charset=utf8
Expand All @@ -56,34 +56,31 @@ class BaseExternalDbsource(models.Model):
password=%s
- SQLite: sqlite:///test.db
- Elasticsearch: https://user:%s@localhost:9200
""")
conn_string_full = fields.Text(
readonly=True,
compute='_compute_conn_string_full',
""",
)
password = fields.Char('Password', size=40)
conn_string_full = fields.Text(readonly=True, compute="_compute_conn_string_full")
password = fields.Char("Password", size=40)
client_cert = fields.Text()
client_key = fields.Text()
ca_certs = fields.Char(
help='Path to CA Certs file on server.',
)
ca_certs = fields.Char(help="Path to CA Certs file on server.")
connector = fields.Selection(
CONNECTORS, 'Connector', required=True,
CONNECTORS,
"Connector",
required=True,
help="If a connector is missing from the list, check the server "
"log to confirm that the required components were detected.",
"log to confirm that the required components were detected.",
)

current_table = None

@api.multi
@api.depends('conn_string', 'password')
@api.depends("conn_string", "password")
def _compute_conn_string_full(self):
for record in self:
if record.password:
if '%s' not in record.conn_string:
if "%s" not in record.conn_string:
pwd_string = getattr(
record,
'PWD_STRING_%s' % record.connector.upper(),
"PWD_STRING_%s" % record.connector.upper(),
record.PWD_STRING,
)
record.conn_string += pwd_string
Expand All @@ -93,23 +90,20 @@ def _compute_conn_string_full(self):

# Interface

@api.multi
def change_table(self, name):
""" Change the table that is used for CRUD operations """
self.current_table = name

@api.multi
def connection_close(self, connection):
""" It closes the connection to the data source.
This method calls adapter method of this same name, suffixed with
the adapter type.
"""

method = self._get_adapter_method('connection_close')
method = self._get_adapter_method("connection_close")
return method(connection)

@api.multi
@contextmanager
def connection_open(self):
""" It provides a context manager for the data source.
Expand All @@ -118,20 +112,17 @@ def connection_open(self):
the adapter type.
"""

method = self._get_adapter_method('connection_open')
method = self._get_adapter_method("connection_open")
try:
connection = method()
yield connection
finally:
try:
self.connection_close(connection)
except:
_logger.exception('Connection close failure.')
except Exception:
_logger.exception("Connection close failure.")

@api.multi
def execute(
self, query=None, execute_params=None, metadata=False, **kwargs
):
def execute(self, query=None, execute_params=None, metadata=False, **kwargs):
""" Executes a query and returns a list of rows.
"execute_params" can be a dict of values, that can be referenced
Expand All @@ -154,24 +145,23 @@ def execute(
# Old API compatibility
if not query:
try:
query = kwargs['sqlquery']
query = kwargs["sqlquery"]
except KeyError:
raise TypeError(_('query is a required argument'))
raise TypeError(_("query is a required argument"))
if not execute_params:
try:
execute_params = kwargs['sqlparams']
execute_params = kwargs["sqlparams"]
except KeyError:
pass

method = self._get_adapter_method('execute')
method = self._get_adapter_method("execute")
rows, cols = method(query, execute_params, metadata)

if metadata:
return {'cols': cols, 'rows': rows}
return {"cols": cols, "rows": rows}
else:
return rows

@api.multi
def connection_test(self):
""" It tests the connection
Expand All @@ -182,19 +172,17 @@ def connection_test(self):

for obj in self:
try:
with self.connection_open():
with obj.connection_open():
pass
except Exception as e:
raise ConnectionFailedError(_(
"Connection test failed:\n"
"Here is what we got instead:\n%s"
) % tools.ustr(e))
raise ConnectionSuccessError(_(
"Connection test succeeded:\n"
"Everything seems properly set up!",
))

@api.multi
raise ConnectionFailedError(
_("Connection test failed:\n" "Here is what we got instead:\n%s")
% tools.ustr(e)
)
raise ConnectionSuccessError(
_("Connection test succeeded:\n" "Everything seems properly set up!")
)

def remote_browse(self, record_ids, *args, **kwargs):
""" It browses for and returns the records from remote by ID
Expand All @@ -210,10 +198,9 @@ def remote_browse(self, record_ids, *args, **kwargs):
"""

assert self.current_table
method = self._get_adapter_method('remote_browse')
method = self._get_adapter_method("remote_browse")
return method(record_ids, *args, **kwargs)

@api.multi
def remote_create(self, vals, *args, **kwargs):
""" It creates a record on the remote data source.
Expand All @@ -229,10 +216,9 @@ def remote_create(self, vals, *args, **kwargs):
"""

assert self.current_table
method = self._get_adapter_method('remote_create')
method = self._get_adapter_method("remote_create")
return method(vals, *args, **kwargs)

@api.multi
def remote_delete(self, record_ids, *args, **kwargs):
""" It deletes records by ID on remote
Expand All @@ -248,10 +234,9 @@ def remote_delete(self, record_ids, *args, **kwargs):
"""

assert self.current_table
method = self._get_adapter_method('remote_delete')
method = self._get_adapter_method("remote_delete")
return method(record_ids, *args, **kwargs)

@api.multi
def remote_search(self, query, *args, **kwargs):
""" It searches the remote for the query.
Expand All @@ -267,10 +252,9 @@ def remote_search(self, query, *args, **kwargs):
"""

assert self.current_table
method = self._get_adapter_method('remote_search')
method = self._get_adapter_method("remote_search")
return method(query, *args, **kwargs)

@api.multi
def remote_update(self, record_ids, vals, *args, **kwargs):
""" It updates the remote records with the vals
Expand All @@ -286,7 +270,7 @@ def remote_update(self, record_ids, vals, *args, **kwargs):
"""

assert self.current_table
method = self._get_adapter_method('remote_update')
method = self._get_adapter_method("remote_update")
return method(record_ids, vals, *args, **kwargs)

# Adapters
Expand All @@ -312,7 +296,6 @@ def _execute_generic(self, query, params, metadata):

# Compatibility & Private

@api.multi
def conn_open(self):
""" It opens and returns a connection to the remote data source.
Expand All @@ -339,14 +322,14 @@ def _get_adapter_method(self, method_prefix):
"""

self.ensure_one()
method = '%s_%s' % (method_prefix, self.connector)
method = "{}_{}".format(method_prefix, self.connector)

try:
return getattr(self, method)
except AttributeError:
raise NotImplementedError(_(
'"%s" method not found, check that all assets are installed '
'for the %s connector type.'
)) % (
method, self.connector,
)
raise NotImplementedError(
_(
'"%s" method not found, check that all assets are installed '
"for the %s connector type."
)
) % (method, self.connector)
6 changes: 3 additions & 3 deletions base_external_dbsource/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">External Database Sources</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/server-backend/tree/12.0/base_external_dbsource"><img alt="OCA/server-backend" src="https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/server-backend-12-0/server-backend-12-0-base_external_dbsource"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/253/12.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/server-backend/tree/13.0/base_external_dbsource"><img alt="OCA/server-backend" src="https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/server-backend-13-0/server-backend-13-0-base_external_dbsource"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/253/13.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p>This module allows you to define connections to foreign databases using ODBC,
Firebird, Oracle Client or SQLAlchemy.</p>
<p><strong>Table of contents</strong></p>
Expand Down Expand Up @@ -427,7 +427,7 @@ <h1><a class="toc-backref" href="#id5">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-backend/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/server-backend/issues/new?body=module:%20base_external_dbsource%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/server-backend/issues/new?body=module:%20base_external_dbsource%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand Down Expand Up @@ -457,7 +457,7 @@ <h2><a class="toc-backref" href="#id9">Maintainers</a></h2>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/server-backend/tree/12.0/base_external_dbsource">OCA/server-backend</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/server-backend/tree/13.0/base_external_dbsource">OCA/server-backend</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
Loading

0 comments on commit 2aa799c

Please sign in to comment.