forked from naturalsolutions/ecoSecrets
-
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.
Feat/setting up demo instance (naturalsolutions#52)
* 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
1 parent
2f14ac6
commit 40a3988
Showing
4 changed files
with
74 additions
and
61 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
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 | ||
|
@@ -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, | ||
) | ||
|
||
|
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
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