Skip to content

Commit 652149d

Browse files
committed
Using Flask-SQLAlchemy instead of SQLAlchemy for the database
1 parent 2974791 commit 652149d

7 files changed

Lines changed: 45 additions & 16 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ __pycache__
99
.vscode
1010
uploads
1111
node_modules
12-
schemas.json
12+
schemas.json
13+
*.db

app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from src.opengeodeweb_back.routes.models import blueprint_models
1111
from src.opengeodeweb_back.utils_functions import handle_exception
1212
from src.opengeodeweb_back import app_config
13+
from src.opengeodeweb_back.database import init_db
1314

1415

1516
""" Global config """
@@ -58,4 +59,5 @@ def return_error():
5859
# ''' Main '''
5960
if __name__ == "__main__":
6061
print(f"Python is running in {FLASK_DEBUG} mode")
62+
init_db(app)
6163
app.run(debug=FLASK_DEBUG, host=DEFAULT_HOST, port=PORT, ssl_context=SSL)

src/opengeodeweb_back/app_config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Config(object):
1515
REQUEST_COUNTER = 0
1616
LAST_REQUEST_TIME = time.time()
1717
LAST_PING_TIME = time.time()
18+
SQLALCHEMY_TRACK_MODIFICATIONS = False
1819

1920

2021
class ProdConfig(Config):
@@ -23,6 +24,7 @@ class ProdConfig(Config):
2324
MINUTES_BEFORE_TIMEOUT = "1"
2425
SECONDS_BETWEEN_SHUTDOWNS = "10"
2526
DATA_FOLDER_PATH = "/data/"
27+
SQLALCHEMY_DATABASE_URI = f"sqlite:///{os.path.join(DATA_FOLDER_PATH, 'db.sqlite3')}"
2628

2729

2830
class DevConfig(Config):
@@ -31,3 +33,4 @@ class DevConfig(Config):
3133
MINUTES_BEFORE_TIMEOUT = "1"
3234
SECONDS_BETWEEN_SHUTDOWNS = "10"
3335
DATA_FOLDER_PATH = "./data/"
36+
SQLALCHEMY_DATABASE_URI = f"sqlite:///{os.path.join(DATA_FOLDER_PATH, 'db.sqlite3')}"

src/opengeodeweb_back/database.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
db = SQLAlchemy()
44

5+
initialized = False
56

67
def init_db(app):
8+
global initialized
9+
if initialized:
10+
return db
11+
print('DB', app.config.get("SQLALCHEMY_DATABASE_URI"))
712
db.init_app(app)
813
with app.app_context():
914
db.create_all()
15+
initialized = True
1016
return db

src/opengeodeweb_back/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ class Data(db.Model):
1515
name = db.Column(String, nullable=False)
1616
native_file_name = db.Column(String, nullable=False)
1717
viewable_file_name = db.Column(String, nullable=False)
18-
light_viewable = db.Column(
19-
String, nullable=True
20-
) # Renommé pour correspondre au code
18+
light_viewable = db.Column(String, nullable=True)
2119
geode_object = db.Column(String, nullable=False)
2220
input_files = db.Column(JSON, nullable=True)
2321
created_at = db.Column(DateTime, default=datetime.now)

tests/conftest.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Standard library imports
22
import time
33
import shutil
4-
5-
# Third party imports
4+
import os
65
import pytest
76

8-
# Local application imports
7+
98
from app import app
9+
from src.opengeodeweb_back.database import init_db
1010

1111
TEST_ID = "1"
1212

@@ -25,6 +25,14 @@ def client():
2525
app.config["UPLOAD_FOLDER"] = "./tests/data/"
2626
app.config["REQUEST_COUNTER"] = 0
2727
app.config["LAST_REQUEST_TIME"] = time.time()
28+
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
29+
db_path = os.path.join(BASE_DIR, "data", "project.db")
30+
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{db_path}"
31+
32+
print("Current working directory:", os.getcwd())
33+
print("Directory contents:", os.listdir("."))
34+
35+
init_db(app)
2836
client = app.test_client()
2937
client.headers = {"Content-type": "application/json", "Accept": "application/json"}
3038
yield client

tests/test_utils_functions.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import shutil
88

99
# Local application imports
10+
from src.opengeodeweb_back.database import db
11+
from src.opengeodeweb_back.models import Data
1012
from src.opengeodeweb_back import geode_functions, utils_functions
1113

1214

@@ -97,15 +99,24 @@ def test_save_all_viewables_and_return_info(client):
9799
geode_object, data, generated_id, data_path, additional_files
98100
)
99101

100-
assert isinstance(result, dict)
101-
assert result["name"] == data.name()
102-
assert result["native_file_name"].startswith("native.")
103-
assert result["viewable_file_name"].endswith(".vtm")
104-
assert re.match(r"[0-9a-f]{32}", result["id"])
105-
assert isinstance(result["object_type"], str)
106-
assert isinstance(result["binary_light_viewable"], str)
107-
assert result["geode_object"] == geode_object
108-
assert result["input_files"] == additional_files
102+
assert isinstance(result, dict)
103+
assert result["name"] == data.name()
104+
assert result["native_file_name"].startswith("native.")
105+
assert result["viewable_file_name"].endswith(".vtm")
106+
assert re.match(r"[0-9a-f]{32}", result["id"])
107+
assert isinstance(result["object_type"], str)
108+
assert isinstance(result["binary_light_viewable"], str)
109+
assert result["geode_object"] == geode_object
110+
assert result["input_files"] == additional_files
111+
112+
db_entry = Data.query.get(generated_id)
113+
assert db_entry is not None
114+
assert db_entry.name == data.name()
115+
assert db_entry.native_file_name == os.path.basename(result["native_file_name"])
116+
assert db_entry.viewable_file_name == os.path.basename(result["viewable_file_name"])
117+
assert db_entry.light_viewable == os.path.basename(db_entry.light_viewable)
118+
assert db_entry.geode_object == geode_object
119+
assert db_entry.input_files == additional_files
109120

110121

111122
def test_generate_native_viewable_and_light_viewable_from_object(client):

0 commit comments

Comments
 (0)