From e4d2b27a0fed243f360db7fbc87d8e86efa6911e Mon Sep 17 00:00:00 2001 From: David Moreau Simard Date: Wed, 12 Sep 2018 09:14:38 -0400 Subject: [PATCH] Move django import and initialization inside the offline client class This allows to import the class without triggering the import and initialization of django and instead deferring it to when (and if) the class is instanciated. Change-Id: I79be90eb877f21b5254f116e51cc78b310c8674c --- ara/clients/offline.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/ara/clients/offline.py b/ara/clients/offline.py index f2876ed..c84528d 100644 --- a/ara/clients/offline.py +++ b/ara/clients/offline.py @@ -22,26 +22,27 @@ import json import logging import os -try: - from django import setup as django_setup - from django.core.management import execute_from_command_line - from django.test import Client - - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ara.server.settings') - - # Automatically create the database and run migrations (is there a better way?) - execute_from_command_line(['django', 'migrate']) - - # Set up the things Django needs - django_setup() -except ImportError as e: - print('ERROR: The offline client requires ara-server to be installed') - raise e - class AraOfflineClient(object): def __init__(self): self.log = logging.getLogger(__name__) + + try: + from django import setup as django_setup + from django.core.management import execute_from_command_line + from django.test import Client + + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ara.server.settings') + + # Automatically create the database and run migrations (is there a better way?) + execute_from_command_line(['django', 'migrate']) + + # Set up the things Django needs + django_setup() + except ImportError as e: + self.log.error('The offline client requires ara-server to be installed') + raise e + self.client = Client() def _request(self, method, endpoint, **kwargs):