Skip to content

Commit 8af06f1

Browse files
committed
add example script for metadata to column
1 parent a1e6d5e commit 8af06f1

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
###################################################################################################################
4+
# In this example script, we want to grab information from metadata and add it to the Resource as a proper column #
5+
###################################################################################################################
6+
7+
from pprint import pprint
8+
import elabapi_python
9+
import json
10+
11+
# use the locally defined client.py module to get the api_client object, fully configured and ready to be used to instantiate api objects
12+
from client import api_client
13+
14+
# Start by creating our api object to interact with /items endpoint
15+
items_client = elabapi_python.ItemsApi(api_client)
16+
17+
# In this example, this corresponds to "Chemicals". Adapt to your instance.
18+
RESOURCE_CATEGORY_ID = 5
19+
# this value is the same for all eLabFTW instances
20+
CURRENCY_EUROS = 4
21+
22+
# we need to use _preload_content=False here so we can access the metadata properly
23+
response = items_client.read_items(cat=1, limit=9999, _preload_content=False)
24+
items = json.loads(response.data.decode('utf-8'))
25+
for item in items:
26+
metadata = item.get('metadata', None)
27+
# they are supposed to all have metadata
28+
if metadata is None:
29+
print(f"WARNING: Skipping item with id {item.get('id')} because no metadata is present")
30+
continue
31+
meta = json.loads(metadata)
32+
extra_fields = meta.get('extra_fields', None)
33+
if extra_fields is not None:
34+
proc_pack_qty = extra_fields.get("Quantity", 1).get("value")
35+
proc_price_notax = extra_fields.get("Price", 0.00).get("value")
36+
proc_price_tax = float(proc_price_notax) * 1.2
37+
# no patch the item with these values
38+
print(f"INFO: Patching item wtih id {item.get('id')}")
39+
items_client.patch_item(item.get('id'), body={"proc_pack_qty": proc_pack_qty, "proc_price_notax": proc_price_notax, "proc_price_tax": proc_price_tax, "proc_currency": CURRENCY_EUROS})

0 commit comments

Comments
 (0)