Skip to content

Commit

Permalink
Feat/setting up demo instance (naturalsolutions#52)
Browse files Browse the repository at this point in the history
* feat: bdd initialization set-up if demo instance

* fix: database initialization modification

* fix: use current datetime as media creation date

* style: apply isort and black

* fix: use of a boolean variable from env

* fix: delete some useless fields

---------

Co-authored-by: Mathilde Leclerc <[email protected]>
  • Loading branch information
ophdlv and MathildeNS authored Dec 8, 2023
1 parent 2f14ac6 commit 40a3988
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 61 deletions.
120 changes: 61 additions & 59 deletions api/src/connectors/database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import hashlib
import os
from datetime import datetime as dt
from pathlib import Path

import magic
from dateutil.relativedelta import relativedelta
from sqlmodel import Session, create_engine

from src.config import settings
Expand All @@ -12,107 +15,106 @@
from src.models.site import SiteBase
from src.schemas.user import UserCreate
from src.services import deployment, device, files, project, site, user
from src.utils import file_as_bytes

DATABASE_URL = settings.DB_URL

engine = create_engine(DATABASE_URL, echo=True)

date = dt.today()


def init_db():
# TODO: make it conditionnal not to create at each startup
with Session(engine) as session:
role = Roles(role="user", description="default_user")
session.add(role)
session.commit()
owner = user.create_user(
db=session,
user=UserCreate(name="user", email="[email protected]", password="password"),
)

for curent_file in files.get_files(db=session):
files.delete_file(db=session, id=curent_file.id)

for curent_deployment in deployment.get_deployments(db=session):
deployment.delete_deployment(db=session, id=curent_deployment.id)

for curent_device in device.get_devices(db=session):
device.delete_device(db=session, id=curent_device.id)

for curent_site in site.get_sites(db=session):
site.delete_site(db=session, id=curent_site.id)

for curent_project in project.get_projects(db=session):
project.delete_project(db=session, id=curent_project.id)

for curent_user in user.get_users(db=session):
user.delete_user(db=session, id=curent_user.id)

project_example = project.create_project(
db=session,
project=ProjectBase(
name="Projet 1",
description="Description premier projet",
protocole="Protocole A",
creation_date=dt.fromisoformat("2022-04-12"),
start_date=dt.fromisoformat("2022-04-12"),
end_date=dt.fromisoformat("2023-04-12"),
protocol="Protocole A",
acquisition_framework="Acq 1",
targeted_species="Loup",
referential="ref 1",
timezone="CET",
image="",
onwner_id=owner.id,
contact_id=owner.id,
),
)
project.create_project(
db=session,
project=ProjectBase(
name="Projet 2",
creation_date=dt.fromisoformat("2022-04-12"),
start_date=dt.fromisoformat("2022-09-12"),
end_date=dt.fromisoformat("2022-12-12"),
description="Description deuxième projet",
owner_id=owner.id,
contact_id=owner.id,
name="Demonstration project",
protocol="The sampling protocol can be indicated in this field, but is not required.",
creation_date=date,
start_date=date,
end_date=date + relativedelta(years=+1),
),
)

site_example = site.create_site(
db=session,
site=SiteBase(
name="Site 1",
latitude=43.29,
longitude=5.37,
habitat="Prairie",
description="Description",
name="Natural Solutions Site",
longitude=5.371927397059713,
latitude=43.29208734554677,
description="This site has been created to demonstrate the tool. Feel free to modify it or create your own!",
),
)
device_example = device.create_device(
db=session,
device=DeviceBase(
name="Dispositif 1",
model="Modèle A",
purchase_date="2022-07-19",
price=120,
description="Description",
detection_area=1163,
status="En stock",
name="Demonstration device",
model="This field is used to indicate the model of the device used for the field survey.",
purchase_date=date + relativedelta(months=-1),
operating_life=2000,
price=150,
detection_area=1163,
description="This site has been created to demonstrate the tool. Feel free to modify it or create your own!",
),
)

deployment_example = deployment.create_deployment(
db=session,
deployment=NewDeploymentWithTemplateSequence(
name="Déploiement 1",
name="Demonstration deployment",
site_id=site_example.id,
device_id=device_example.id,
start_date=dt.fromisoformat("2022-04-12"),
end_date=dt.fromisoformat("2022-04-12"),
description="Description premier déploiement",
bait="None",
feature="Arbre fruitier",
start_date=date,
end_date=date + relativedelta(months=+3),
support="This field indicates device support.",
feature="This field indicates deployment feature.",
height=50,
bait="This field indicates bait used.",
project_id=project_example.id,
template_sequences=[],
description="This deployment has been created to demonstrate the tool. Feel free to modify it or create your own!",
),
)

path = Path(__file__).parent.parent.parent
if not os.path.isdir(os.path.join(path, "img")):
os.mkdir(os.path.join(path, "img"))
for fileName in os.listdir(os.path.join(path, "img")):
fileNameSplit = fileName.split(".")
print(os.path.join(path, "img", fileName))
with open(os.path.join(path, "img", fileName), "rb") as file:
img_dir = os.path.join(path, "img")

if not os.path.isdir(img_dir):
os.mkdir(img_dir)

for filename_example in os.listdir(img_dir):
with open(os.path.join(img_dir, filename_example), "rb") as f:
file_content = f.read()
f.seek(0)
files.upload_file(
db=session,
hash=fileNameSplit[0],
ext=fileNameSplit[1],
filename=fileName,
new_file=file,
hash=hashlib.sha256(file_content).hexdigest(),
ext=magic.from_buffer(file_content, mime=True),
filename=filename_example,
new_file=f,
deployment_id=deployment_example.id,
)

Expand Down
9 changes: 8 additions & 1 deletion api/src/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os

from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware

from src.config import settings
from src.connectors.s3 import init_bucket
from src.connectors.database import init_db
from src.connectors.s3 import get_bucket_name, init_bucket, remove_bucket
from src.keycloak.idp import idp
from src.routers import deployments, devices, files, home, projects, sites, templateSequences, users

Expand Down Expand Up @@ -36,3 +39,7 @@
@app.on_event("startup")
def on_startup():
init_bucket()

is_demo_instance = (os.environ.get("DEMO_INSTANCE", None) == "True")
if is_demo_instance:
init_db()
2 changes: 1 addition & 1 deletion api/src/services/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def upload_file(
name=filename,
extension=ext,
bucket=settings.MINIO_BUCKET_NAME,
date=datetime.fromisoformat("2022-01-22"),
date=datetime.now(),
deployment_id=deployment_id,
)
try:
Expand Down
4 changes: 4 additions & 0 deletions docker/.env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# To activate if production mode has to be activated
#ENV=production

# To activate if demo instance
#DEMO_INSTANCE=True

PROJECT_NAME=geonature-annotation
DOMAIN=localhost
PROTOCOL=http # either http or https
Expand Down

0 comments on commit 40a3988

Please sign in to comment.