I’ve managed to log overservations from a python script.
But I’m struggling with adding a category with the script.
Could someone point me in the right directon?
This is something I plan on looking at myself shortly, so I’m not much help, but I’d like to see what you have so far If you don’t mind sharing the script. I’m interested in any solutions and pointers in this regard too.
Well, I found the solution. A great thing about problems are that one learn alot from them
I’ll try to explain this in a general matter, so it’s usable for other things as well.
It’s all there in the JSON. Once you get the hang of it, its quite easy.
I found the api browsing quite useful. If you add /api after your FarmOS domain, you get a JSON listing.
Example : myfarm.farmier.net/api
I think it’s only Firefox that presents this url as something readable.
Also keep in mind you need to be logged in to your FarmOS in a tab to see everyrhing.
Once you get this JSON listed, you can navigate to the asset–category key and click the url there
Then you will see a json of all your categories.
Under the data key you will find
0 for the first category
1 for the second category
and so on.
The next level of keys are id
There you find your specific category id. You use this to feed the log.
Next, you could go back to myfarm.farmier.net/api and open the observation url log–observation
If you have a logged an observation earlier in the web interface, you can find this log, and you will find the category part under relationships key
Knowing this made things easier for me.
I’ll give you an examle code now:
activity_log = {
"attributes": {
"name": "I did this",
"status": "done",
"notes": "From python"
},
"relationships": {
"category": {
"data": {
"0": {
"type" : "taxonomy_term--log_category",
"id" : "your 1st category id" #I insert a comment here with category name
},
"1": {
"type" : "taxonomy_term--log_category",
"id" : "your 2nd category id here" #category 2
}
}
}
}
Maybe theres som bracets missing or so, I edited my code abit now. But I think its ok.
For the reference, the complete code:
Should work.
from farmOS import farmOS
farm_client = farmOS(hostname='https://nnn.nnn.nnn')
token = farm_client.authorize(username="...",
password="....")
activity_log = {
"attributes": {
"name": "I did this",
"status": "done",
"notes": "From python"
},
"relationships": {
"category": {
"data": {
"0": {
"type" : "taxonomy_term--log_category",
"id" : "your 1st category id" #I insert a comment here with category name
},
"1": {
"type" : "taxonomy_term--log_category",
"id" : "your 2nd category id here" #category 2
}
}
}
}
activity_log=farm_client.log.send('activity', activity_log)
If you replace
send(‘activity’
with
send(‘observation’
you’ll make an observation, but i guess you know that…
Thanks for that @pat .
I’ve used JSON formatting in the past for API’s of other applications, just haven’t looked too closely at what can be done with farmOS yet, but very much intend to.
Interesting that Firefox formats it nice and readable, I never noticed that before. Every day is a school day!
I’ve been using https://jsonpathfinder.com/ for reading JSON formatting, you can paste in the hard to read “squashed” JSON output and click beautify and it will display it readable like fire fox on one side and with collapsible tree structure on the other.
Good work figuring it out @pat!
For what it’s worth, here is how you can look up the UUID of a category based on its name: /api/taxonomy_term/log_category?filter[name]=Irrigation
This will show all category terms with a name of “Irrigation” (which should only return a single record). You can then find the UUID in the JSON. Here is what it looks like in Firefox:
Worth noting: the UUIDs of categories will be different from one farmOS instance to another - so if you don’t know the UUID be sure to look it up first!
Yea. I bet theres a lot of great tools to help.
Can’t wait till i’m out of kindergarden and into pre-school
You and me both,
I’ve only been looking at farmOS a short while and I’m a little in awe of the community of tech savvy farmers that exists around projects like this. It’s not the stereotype type we’d normally be branded with anyway.