From ebb472aa5183e23aa39690d241ae12de63ec2ce4 Mon Sep 17 00:00:00 2001 From: Thomas Vanesse Date: Tue, 3 May 2016 18:45:01 +0200 Subject: [PATCH 1/6] Switched the relation of OdooUser from auth.User to settings.AUTH_USER_MODEL to port djangodoo to django1.8. --- djangodoo/models.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/djangodoo/models.py b/djangodoo/models.py index c083f5f..eaddc10 100644 --- a/djangodoo/models.py +++ b/djangodoo/models.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- from django.conf import settings from django.db import models -from django.contrib.auth.models import User from django.core.cache import caches import erppeek @@ -158,7 +157,7 @@ def odoo_push(self, fieldnames=None, client=None): class OdooUser(models.Model): - user = models.OneToOneField(User, blank=False, related_name='odoo_user') + user = models.OneToOneField(settings.AUTH_USER_MODEL, blank=False, related_name='odoo_user') def __init__(self, *args, **kwargs): config = getattr(settings, "ODOO_HOST", False) From 0f7b5882053ccb913e33b199c8150ccb223b779c Mon Sep 17 00:00:00 2001 From: Thomas Vanesse Date: Tue, 3 May 2016 19:03:43 +0200 Subject: [PATCH 2/6] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 7c49787..de54e5f 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name='djangodoo', - version='0.2.3', + version='0.2.4', packages=['djangodoo'], include_package_data=True, license='MIT License', From 34218414b3942e9d00f3b3ccce65574020353440 Mon Sep 17 00:00:00 2001 From: Thomas Vanesse Date: Mon, 9 May 2016 11:15:21 +0200 Subject: [PATCH 3/6] WIP on fetching all the IDs of the entries related to a specific Odoo model. --- djangodoo/models.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/djangodoo/models.py b/djangodoo/models.py index eaddc10..53156dd 100644 --- a/djangodoo/models.py +++ b/djangodoo/models.py @@ -36,6 +36,13 @@ def _get_odoo_fields(cls): res = cls._odoo_fields or settings.odoo.model(cls._odoo_model).fields() return [f for f in res if not(f in (cls._odoo_ignore_fields or []))] + @classmethod + def odoo_get_all_ids(cls, client=None): + odoo_model = cls._odoo_model + client = client or settings.odoo + ans = client.model(odoo_model).keys() + return ans + @classmethod def odoo_load(cls, odoo_ids, client=None): """Loads records from Odoo From dd248cc4a410296967faeac929e023c981c0923e Mon Sep 17 00:00:00 2001 From: Thomas Vanesse Date: Tue, 5 Jul 2016 09:48:09 +0200 Subject: [PATCH 4/6] Adding some additional logging statements in __init__.py --- djangodoo/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/djangodoo/__init__.py b/djangodoo/__init__.py index 2041226..40283c0 100644 --- a/djangodoo/__init__.py +++ b/djangodoo/__init__.py @@ -16,6 +16,7 @@ def set_auth_cache(): def set_odoo_client(): config = getattr(settings, "ODOO_HOST", False) + logger.info("Setting up the Odoo client...") try: settings.odoo = erppeek.Client("%s:%d" % (config['HOST'], config['PORT']), db=config['DB'], user=config['USER'], password=config['PASSWORD'], verbose=False) @@ -23,6 +24,7 @@ def set_odoo_client(): settings.odoo_models = {} settings.deferred_m2o = {} settings.deferred_o2m = {} + logger.info("...done.") except: logger.error('Unable to connect to a running Odoo server') raise From c91e8c35707f838de40f16d5f667765d9c61d522 Mon Sep 17 00:00:00 2001 From: Thomas Vanesse Date: Tue, 5 Jul 2016 11:21:24 +0200 Subject: [PATCH 5/6] Allow djangodoo to re-try to connect to an unreachable Odoo instance. The maximum number of attempts as well as the period between each of them is configurable and email notifications can be sent in case of confirmed failure. --- README.rst | 2 +- djangodoo/__init__.py | 88 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 12 deletions(-) diff --git a/README.rst b/README.rst index 05c8eaa..0b80a70 100644 --- a/README.rst +++ b/README.rst @@ -11,7 +11,7 @@ Quick start INSTALLED_APPS = ( ... - 'Djangodoo', + 'djangodoo', ) 2. Include the Odoo host configuration in your project settings like this:: diff --git a/djangodoo/__init__.py b/djangodoo/__init__.py index 40283c0..124062f 100644 --- a/djangodoo/__init__.py +++ b/djangodoo/__init__.py @@ -1,10 +1,14 @@ # -*- coding: utf-8 -*- 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 +from time import sleep +import traceback + logger = logging.getLogger(__name__) @@ -16,18 +20,79 @@ def set_auth_cache(): def set_odoo_client(): config = getattr(settings, "ODOO_HOST", False) + logger.info("Setting up the Odoo client...") - try: - settings.odoo = erppeek.Client("%s:%d" % (config['HOST'], config['PORT']), db=config['DB'], - user=config['USER'], password=config['PASSWORD'], verbose=False) - settings.odoo.context = {"lang": settings.LANGUAGE_CODE} - settings.odoo_models = {} - settings.deferred_m2o = {} - settings.deferred_o2m = {} - logger.info("...done.") - except: - logger.error('Unable to connect to a running Odoo server') - raise + max_retry_attempts = getattr(settings, "ODOO_MAX_RETRY_ATTEMPTS", 3) + retry_delay = getattr(settings, "ODOO_RETRY_DELAY", 5) + + def _connect(retry_cnt): + try: + settings.odoo = erppeek.Client("%s:%d" % (config['HOST'], config['PORT']), db=config['DB'], + user=config['USER'], password=config['PASSWORD'], verbose=False) + settings.odoo.context = {"lang": settings.LANGUAGE_CODE} + settings.odoo_models = {} + settings.deferred_m2o = {} + settings.deferred_o2m = {} + except: + logger.warn('Failed to connect to a running Odoo server.') + logger.warn('Waiting {} [s] before the next attempt...'.format(retry_delay)) + logger.warn('{} trials left...'.format(max_retry_attempts-retry_cnt)) + sleep(retry_delay) + if retry_cnt < max_retry_attempts: + _connect(retry_cnt + 1) + else: + 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: {} + DB: {} + + And here is the traceback of the exception raised during the last attempt: + + + {} + + """.format(max_retry_attempts, retry_delay, config['USER'], config['HOST'], config['PORT'], config['DB'], traceback.format_exc()) + html_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: {}
+ DB: {}
+
+ +

And here is the traceback of the exception raised during the last attempt:

+ +
+
+                {}
+
+                
+ + """.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...') + send_mail("APPLICATION FAILURE - DJANGODOO", + mail_content, + getattr(settings, "DEFAULT_FROM_EMAIL", "djangodoo@example.com"), + mail_config["RECIPIENTS"], + html_message=html_content, + fail_silently=False) + raise + + _connect(0) def add_extra_model_fields(sender, **kwargs): @@ -59,3 +124,4 @@ def add_field(django_model, field_details): set_auth_cache() set_odoo_client() class_prepared.connect(add_extra_model_fields, dispatch_uid="FQFEQ#rfq3r") +logger.info("Done initializing Djangodoo.") From bbeb09eba604b2656e1494c1496faaafd92b086e Mon Sep 17 00:00:00 2001 From: Thomas Vanesse Date: Tue, 5 Jul 2016 11:27:37 +0200 Subject: [PATCH 6/6] Increment the version number. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index de54e5f..3dba152 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name='djangodoo', - version='0.2.4', + version='0.2.5', packages=['djangodoo'], include_package_data=True, license='MIT License',