Skip to content

Commit

Permalink
end of day push
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardbruelhart committed Nov 20, 2024
1 parent c339cb0 commit f47610b
Show file tree
Hide file tree
Showing 4 changed files with 457 additions and 393 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.8
67 changes: 37 additions & 30 deletions ms_sample_list_creator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, parent: tk.Tk, *args: Any, **kwargs: Any):
data = response.json()["tag_name"]
tag = float(str.replace(data, "v.", ""))

if tag <= 0.7:
if tag <= 1.0:
self.label = tk.Label(self, text="Connect to directus and adjust the parameters")
self.label.pack()

Expand Down Expand Up @@ -377,47 +377,53 @@ def testConnection(self) -> None:

# Test if the method is already present in directus
access_token = os.environ.get("ACCESS_TOKEN")
base_url = "https://emi-collection.unifr.ch/directus"
collection_url = base_url + f"/items/Injection_Methods/{self.file}"
collection_url = base_url + "/items/Injection_Methods/"
params = {"filter[method_name][_eq]": self.file}
session = requests.Session()
session.headers.update({"Authorization": f"Bearer {access_token}"})
# collection_url = base_url + '/items/samples'
response = session.get(collection_url)
response = session.get(collection_url, params=params)
value = response.status_code
# if already present, launches the sample list window
if value == 200:
# Hide the main page and open Window 2
self.manage_choice()
# else adds the new method to directus
else:
# Send data to directus
base_url = "https://emi-collection.unifr.ch/directus"
collection_url = base_url + "/items/Injection_Methods"
session = requests.Session()
session.headers.update({"Authorization": f"Bearer {access_token}"})

# Add headers
headers = {"Content-Type": "application/json"}

data = {"method_name": self.file}

response = session.post(url=collection_url, headers=headers, json=data)

# if method is successfully added to directus, launchtes the sample list window
if response.status_code == 200:
data = str(response.json()["data"])
if data == "[]":
self.add_method(access_token, collection_url)
else:
# Hide the main page and open Window 2
self.manage_choice()
else:
self.label.config(text="Connexion to directus failed, please try again", foreground="red")

# If connection to directus failed, informs the user that connection failed.
else:
self.label.config(
text="Connexion to directus failed, verify your credentials/vpn connection", foreground="red"
)
self.label.config(text="Connexion to directus failed, verify your credentials", foreground="red")

else:
# If user didn't enter all necessary values, shows this message
self.label.config(text="Please provide all asked values", foreground="red")

def add_method(self, access_token: str, collection_url: str) -> None:
"""
Adds an injection method to directus
"""

# Send data to directus
session = requests.Session()
session.headers.update({"Authorization": f"Bearer {access_token}"})

# Add headers
headers = {"Content-Type": "application/json"}

data = {"method_name": self.file}

response = session.post(url=collection_url, headers=headers, json=data)

# if method is successfully added to directus, launchtes the sample list window
if response.status_code == 200:
# Hide the main page and open Window 2
self.manage_choice()

def manage_choice(self) -> None:
"""
Returns to main script which option did the user choose.
Expand Down Expand Up @@ -591,18 +597,19 @@ def add_row(self, event: Optional[tk.Event] = None) -> None:

# Send data to directus
base_url = "https://emi-collection.unifr.ch/directus"
collection_url = base_url + "/items/Mass_Spectrometry_Analysis"
collection_url = base_url + "/items/MS_Data"
session = requests.Session()
session.headers.update({"Authorization": f"Bearer {self.access_token}"})

# Add headers
headers = {"Content-Type": "application/json"}

data = {
"aliquot_id": aliquot_id,
"mass_spec_id": filename,
"ms_id": self.ms_id,
"parent_sample_container": aliquot_id,
"filename": filename,
"instrument_used": self.ms_id,
"injection_volume": inj_volume,
"injection_volume_unit": 18,
"injection_method": file,
}

Expand Down
Loading

0 comments on commit f47610b

Please sign in to comment.