Skip to content

Commit

Permalink
Now offering a non-blocking initialization process, avoiding single-p…
Browse files Browse the repository at this point in the history
…oint failures in third-party applications.
  • Loading branch information
tvanesse committed Oct 25, 2016
1 parent bcbbab1 commit f2cfa4d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
37 changes: 28 additions & 9 deletions djangodoo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.conf import settings
from django.db.models.signals import class_prepared
from django.core.mail import send_mail

import erppeek
from .fields import convert_field
import logging
Expand All @@ -10,6 +11,7 @@
import traceback

logger = logging.getLogger(__name__)
VERSION = "0.3.0"


def set_auth_cache():
Expand All @@ -21,7 +23,12 @@ def set_auth_cache():
def set_odoo_client():
config = getattr(settings, "ODOO_HOST", False)

logger.info("Setting up the Odoo client...")
if not config:
raise RuntimeError("You need to define ODOO_HOST in your settings in order to use Djangodoo.")
elif not config.get("HOST", None):
raise RuntimeError("You need to provide a HOST location in ODOO_HOST in order to use Djangodoo.")

logger.info("Setting up the Odoo client (djangodoo version {})...".format(VERSION))
max_retry_attempts = getattr(settings, "ODOO_MAX_RETRY_ATTEMPTS", 3)
retry_delay = getattr(settings, "ODOO_RETRY_DELAY", 5)

Expand All @@ -44,11 +51,11 @@ def _connect(retry_cnt):
logger.error('Unable to connect to a running Odoo server. Aborting.')
mail_config = getattr(settings, "ODOO_EMAIL_NOTIFICATION", False)
mail_content = """Unable to connect to a running Odoo server. Your application may have failed to start up due to a connection problem with an Odoo instance.
Djangodoo tried to reconnect {} times, waiting {} seconds between each attempt. Still, the server could not be reached.
The problem occured with the following host configuration:
USER: {}
HOST: {}
PORT: {}
Expand All @@ -58,14 +65,15 @@ def _connect(retry_cnt):
{}
""".format(max_retry_attempts, retry_delay, config['USER'], config['HOST'], config['PORT'], config['DB'], traceback.format_exc())
html_content = """<p>Unable to connect to a running Odoo server. Your application may have failed to start up due to a connection problem with an Odoo instance.</p>
<p>Djangodoo tried to reconnect <b>{} times</b>, waiting <b>{} seconds</b> between each attempt. Still, the server could not be reached.</p>
<p>The problem occured with the following host configuration:</p>
<div style="border-left: 1px solid gray; padding-left: 10px;">
USER: {}<br>
HOST: {}<br>
Expand All @@ -80,7 +88,7 @@ def _connect(retry_cnt):
{}
</pre>
""".format(max_retry_attempts, retry_delay, config['USER'], config['HOST'], config['PORT'], config['DB'], traceback.format_exc())
if mail_config:
logger.info('Sending an email notification to the administrator...')
Expand All @@ -90,7 +98,6 @@ def _connect(retry_cnt):
mail_config["RECIPIENTS"],
html_message=html_content,
fail_silently=False)
raise

_connect(0)

Expand All @@ -100,14 +107,26 @@ def add_extra_model_fields(sender, **kwargs):
The fields are "translated" by using the definitions in fields
"""
odoo = getattr(settings, "odoo", None)

def add_field(django_model, field_details):
odoo_field = convert_field(field_details)
if odoo_field:
field = odoo_field.to_django()
field.contribute_to_class(django_model, field_details['name'])

odoo = settings.odoo
if getattr(sender, "_odoo_model", False):
# This means we are dealing with an OdooModel
if not odoo:
# This means set_odoo_client() failed to establish the connexion.
# There is no point in proceeding any further as this would raise
# an exception that will block the entire application.
#TODO handle the case properly instead of bypassing the model generation
host = getattr(settings, "ODOO_HOST", False).get("HOST", None)
err_msg = """The model named {} could not be processed by Djangodoo because the Odoo server at {} could not be reached.""".format(sender.__name__, host)
logger.error(err_msg)
return

settings.odoo_models[sender._odoo_model] = sender
_all_fields = odoo.model(sender._odoo_model).fields(sender._get_odoo_fields())
for fname, fdetails in _all_fields.items():
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))


setup(
name='djangodoo',
version='0.2.5',
version="0.3.0",
packages=['djangodoo'],
include_package_data=True,
license='MIT License',
Expand Down

0 comments on commit f2cfa4d

Please sign in to comment.