-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path02-patch-metadata-per-category.py
More file actions
executable file
·58 lines (52 loc) · 1.59 KB
/
02-patch-metadata-per-category.py
File metadata and controls
executable file
·58 lines (52 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
import json
from json import JSONDecodeError
import elabapi_python
from client import api_client
# define which category's items are patched
RESOURCE_CATEGORY_ID = 3
# Synchronize metadata of the item_type with existing items
# See: https://github.com/elabftw/elabftw/issues/3524
# create an instance of Items api
itemsApi = elabapi_python.ItemsApi(api_client)
metadata = {
"elabftw": {
"extra_fields_groups": [
{"id": 1, "name": "Drug settings"},
{"id": 2, "name": "Mice info"},
]
},
"extra_fields": {
"Drug addition": {
"type": "datetime-local",
"value": "",
"group_id": 1,
"required": True,
"description": "Time when drug is added",
},
"Drug concentration": {
"type": "number",
"unit": "mM",
"units": ["mM", "μM", "nM"],
"value": "",
"group_id": 1,
"required": True,
},
"Mouse sex": {
"type": "select",
"value": "Male",
"options": ["Male", "Female"],
"group_id": 2,
},
},
}
for item in itemsApi.read_items(cat=RESOURCE_CATEGORY_ID, limit=9999):
# skip items with metadata already
if not item.metadata:
print(f"Patching item {item.id}")
try:
itemsApi.patch_item(item.id, body={"metadata": json.dumps(metadata)})
except JSONDecodeError as e:
raise ValueError(
"'metadata' passed to API request body contains invalid JSON."
) from e