forked from fdegrave/djangodoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fdegrave#2 from tvanesse/master
Move from auth.User to the more generic settings.AUTH_USER_MODEL
- Loading branch information
Showing
4 changed files
with
88 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,16 +20,79 @@ def set_auth_cache(): | |
|
||
def set_odoo_client(): | ||
config = getattr(settings, "ODOO_HOST", False) | ||
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.error('Unable to connect to a running Odoo server') | ||
raise | ||
|
||
logger.info("Setting up the Odoo client...") | ||
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 = """<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> | ||
PORT: {}<br> | ||
DB: {}<br> | ||
</div> | ||
<p>And here is the traceback of the exception raised during the last attempt:</p> | ||
<pre> | ||
{} | ||
</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...') | ||
send_mail("APPLICATION FAILURE - DJANGODOO", | ||
mail_content, | ||
getattr(settings, "DEFAULT_FROM_EMAIL", "[email protected]"), | ||
mail_config["RECIPIENTS"], | ||
html_message=html_content, | ||
fail_silently=False) | ||
raise | ||
|
||
_connect(0) | ||
|
||
|
||
def add_extra_model_fields(sender, **kwargs): | ||
|
@@ -57,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.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters