From 0a70f8916fa2ba6e3de8abe549cf73edf3ed7fae Mon Sep 17 00:00:00 2001
From: David Chamont <chamont@in2p3.fr>
Date: Wed, 22 Jul 2020 22:58:56 +0200
Subject: [PATCH] mise au point de l'intialistion.

---
 docker/Dockerfile_latest  |  2 +-
 docker/README.md          |  2 +-
 docker/flask.sh           |  5 +++++
 docker/run_externals.sh   |  2 +-
 docker/run_latest.sh      |  2 +-
 labelstower/getter.py     |  2 +-
 labelstower/request_db.py |  2 +-
 labelstower/routes.py     |  4 ++--
 test/demo/__init__.py     |  3 +--
 test/demo/config.py       |  4 +++-
 test/demo/models.py       |  6 +-----
 test/instance/README.md   | 16 ++++++++++++++++
 test/instance/init_db.sh  |  5 ++++-
 test/scripts/doctests.sh  |  2 ++
 14 files changed, 40 insertions(+), 17 deletions(-)
 create mode 100755 docker/flask.sh
 create mode 100644 test/instance/README.md
 mode change 100644 => 100755 test/instance/init_db.sh

diff --git a/docker/Dockerfile_latest b/docker/Dockerfile_latest
index 7557650..048b0bc 100644
--- a/docker/Dockerfile_latest
+++ b/docker/Dockerfile_latest
@@ -15,7 +15,7 @@ RUN pip3 install labelstower-latest-py3-none-any.whl
 
 # Run flask
 
-EXPOSE 80
+EXPOSE 5000
 
 ENV FLASK_APP=demo
 ENV FLASK_ENV=production
diff --git a/docker/README.md b/docker/README.md
index b7f8f85..352568c 100644
--- a/docker/README.md
+++ b/docker/README.md
@@ -1,7 +1,7 @@
 
 LAST MINUTE : if you want to run such a container with no command,
 that is with the default flask command, then one must define `LT_CFG``
-environment variable before, so that docker defines `LABELSTOWER_ENV=demo.config.Config${LF_CFG}`
+environment variable before, so that docker defines `LABELSTOWER_ENV=demo.config.Config${LT_CFG}`
 for the need of the demo web application.
 
 # Directory for Docker images and utility scripts
diff --git a/docker/flask.sh b/docker/flask.sh
new file mode 100755
index 0000000..1c74c86
--- /dev/null
+++ b/docker/flask.sh
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+
+export PYTHONPATH=`pwd`
+export LABELSTOWER_ENV="demo.config.Config${LT_CFG}"
+flask run --reload --host 0.0.0.0
diff --git a/docker/run_externals.sh b/docker/run_externals.sh
index 32cb7f5..f355ced 100755
--- a/docker/run_externals.sh
+++ b/docker/run_externals.sh
@@ -6,4 +6,4 @@ SCRIPT_NAME=${BASH_SOURCE[0]}
 cd `dirname ${SCRIPT_NAME}`
 
 # Flask opère sur le port 5000
-docker run -it --rm -p 5000:5000 -v $PWD/..:/work -w /work --env LF_CFG="${LF_CFG}" labelstower:externals $*
+docker run -it --rm -p 5000:5000 -v $PWD/..:/work -w /work --env LT_CFG="${LT_CFG}" labelstower:externals $*
diff --git a/docker/run_latest.sh b/docker/run_latest.sh
index 9cbc2e8..4652da0 100755
--- a/docker/run_latest.sh
+++ b/docker/run_latest.sh
@@ -6,4 +6,4 @@ SCRIPT_NAME=${BASH_SOURCE[0]}
 cd `dirname ${SCRIPT_NAME}`
 
 # Flask opère sur le port 5000
-docker run -it --rm -p 5000:5000 -v $PWD/..:/work -w /work/test --env LF_CFG="${LF_CFG}" labelstower:latest $*
\ No newline at end of file
+docker run -it --rm -p 5000:5000 -v $PWD/../test:/work -w /work --env LT_CFG="${LT_CFG}" labelstower:latest $*
\ No newline at end of file
diff --git a/labelstower/getter.py b/labelstower/getter.py
index 9b62647..7a73e48 100644
--- a/labelstower/getter.py
+++ b/labelstower/getter.py
@@ -1,6 +1,6 @@
 from typing import Dict, List
 from .request_db import send_query_to_db
-from ..labelstower import bp
+from . import bp
 
 
 def get_selected_elements(id_mandatory_labels: int, id_forbiden_labels: str, number_of_mandatory_labels: str) -> List[Dict[int, str]] :
diff --git a/labelstower/request_db.py b/labelstower/request_db.py
index 8c092ef..0af3790 100644
--- a/labelstower/request_db.py
+++ b/labelstower/request_db.py
@@ -1,5 +1,5 @@
 from typing import Dict, List
-from ..labelstower import bp
+from . import bp
 
 def send_query_to_db(query: str) -> List[Dict[int, str]] :
     '''
diff --git a/labelstower/routes.py b/labelstower/routes.py
index 5328ced..cdf8324 100644
--- a/labelstower/routes.py
+++ b/labelstower/routes.py
@@ -1,7 +1,7 @@
 from typing import Dict, List
 import json
-from ..labelstower.getter import get_selected_elements, get_discriminating_labels, get_high_discriminating_labels, get_user_selection
-from ..labelstower import bp
+from .getter import get_selected_elements, get_discriminating_labels, get_high_discriminating_labels, get_user_selection
+from . import bp
 
 #URLs call to update labels and elements along researchs
 
diff --git a/test/demo/__init__.py b/test/demo/__init__.py
index 7d8f638..915afb4 100644
--- a/test/demo/__init__.py
+++ b/test/demo/__init__.py
@@ -20,9 +20,8 @@ def create_app():
                       }
     app = CustomFlask(__name__)
     CORS(app)
-    db.init_app(app)
-
     app.config.from_object(os.environ['LABELSTOWER_ENV'])
+    db.init_app(app)
 
     from labelstower import bp as bp_labels
     bp_labels.db = db
diff --git a/test/demo/config.py b/test/demo/config.py
index a8c1f30..1cb798a 100644
--- a/test/demo/config.py
+++ b/test/demo/config.py
@@ -1,6 +1,8 @@
 
 class ConfigCodingPool():
     SQLALCHEMY_DATABASE_URI = 'sqlite:///../instance/CodingPool.sqlite'
+    SQLALCHEMY_TRACK_MODIFICATIONS = False
 
 class ConfigVoiture():
-    SQLALCHEMY_DATABASE_URI = 'sqlite:///../instance/Voiture.sqlite'
\ No newline at end of file
+    SQLALCHEMY_DATABASE_URI = 'sqlite:///../instance/Voiture.sqlite'
+    SQLALCHEMY_TRACK_MODIFICATIONS = False
diff --git a/test/demo/models.py b/test/demo/models.py
index 3f24d5d..815fba1 100644
--- a/test/demo/models.py
+++ b/test/demo/models.py
@@ -1,10 +1,6 @@
 from flask import Flask
 from flask_sqlalchemy import SQLAlchemy
-import os
-
-app = Flask(__name__)
-app.config.from_object(os.environ['LABELSTOWER_ENV'])
-db = SQLAlchemy(app)
+from . import db
 
 element_label = db.Table('Element_Label', db.Model.metadata,
      db.Column('id_element', db.Integer, db.ForeignKey('element.id')),
diff --git a/test/instance/README.md b/test/instance/README.md
new file mode 100644
index 0000000..dc7cf88
--- /dev/null
+++ b/test/instance/README.md
@@ -0,0 +1,16 @@
+
+#  Directory for the test database instanciation
+
+This is the directory where we can create a database for the tests.
+
+## Requirements
+
+We use SQLite for the tests.
+
+If one wants also to take profit of the utility scripts provided in the current directory, the command `/usr/bin/env bash` (used as a shebang in the scripts) must return a valid bash shell.
+
+## Scripts
+
+The script `init_db.sh` requires a python3 environment where the wheel from the `../../dist` directory has been already installed with `pip3`.
+
+If the wheel is not already installed, rather use `pip_init_db.sh`. This is typically useful if you create your database with a docker command.
diff --git a/test/instance/init_db.sh b/test/instance/init_db.sh
old mode 100644
new mode 100755
index 7332fe1..75e0d42
--- a/test/instance/init_db.sh
+++ b/test/instance/init_db.sh
@@ -5,8 +5,11 @@
 SCRIPT_NAME=${BASH_SOURCE[0]}
 cd `dirname ${SCRIPT_NAME}`
 
+export LABELSTOWER_ENV="demo.config.Config${LT_CFG}"
+echo LABELSTOWER_ENV "demo.config.Config${LT_CFG}"
+
 rm -f ${LT_CFG}.sqlite
 cd .. # so to see demo_blog
 python3 -c 'from demo.models import db; from demo import create_app; db.create_all(app=create_app());'
 cd instance
-sqlite3 ${LT_CFG}.sqlite '.read Data${LT_CFG}.sql'
+sqlite3 ${LT_CFG}.sqlite ".read Data${LT_CFG}.sql"
diff --git a/test/scripts/doctests.sh b/test/scripts/doctests.sh
index ee1859f..067a1bf 100755
--- a/test/scripts/doctests.sh
+++ b/test/scripts/doctests.sh
@@ -5,5 +5,7 @@
 SCRIPT_NAME=${BASH_SOURCE[0]}
 cd `dirname ${SCRIPT_NAME}`
 
+export LABELSTOWER_ENV="demo.config.Config${LT_CFG}"
+
 cd ../..
 python3 labelstower/getter.py
-- 
GitLab