fix: add some more info when viewing animal

This commit is contained in:
Lance Edgar 2026-02-04 08:53:22 -06:00
parent 07b6fa7c22
commit bd972e30db

View file

@ -25,6 +25,8 @@ Master view for Farm Animals
import datetime import datetime
import colander
from farmOS import farmOS from farmOS import farmOS
from wuttafarm.web.views.farmos import FarmOSMasterView from wuttafarm.web.views.farmos import FarmOSMasterView
@ -46,7 +48,8 @@ class AnimalView(FarmOSMasterView):
farmos_refurl_path = "/assets/animal" farmos_refurl_path = "/assets/animal"
labels = { labels = {
"species_breed": "Species / Breed", "is_castrated": "Castrated",
"location_name": "Current Location",
"raw_image_url": "Raw Image URL", "raw_image_url": "Raw Image URL",
"large_image_url": "Large Image URL", "large_image_url": "Large Image URL",
"thumbnail_image_url": "Thumbnail Image URL", "thumbnail_image_url": "Thumbnail Image URL",
@ -54,20 +57,24 @@ class AnimalView(FarmOSMasterView):
grid_columns = [ grid_columns = [
"name", "name",
"species_breed",
"birthdate", "birthdate",
"sex", "sex",
"location", "is_castrated",
"status",
] ]
sort_defaults = "name" sort_defaults = "name"
form_fields = [ form_fields = [
"name", "name",
"species_breed", "animal_type_name",
"birthdate", "birthdate",
"sex", "sex",
"location", "is_castrated",
"status",
"owners",
"location_name",
"notes",
"raw_image_url", "raw_image_url",
"large_image_url", "large_image_url",
"thumbnail_image_url", "thumbnail_image_url",
@ -98,8 +105,38 @@ class AnimalView(FarmOSMasterView):
# instance data # instance data
data = self.normalize_animal(animal["data"]) data = self.normalize_animal(animal["data"])
# add image_url
if relationships := animal["data"].get("relationships"): if relationships := animal["data"].get("relationships"):
# add animal type
if animal_type := relationships.get("animal_type"):
if animal_type["data"]:
animal_type = self.farmos_client.resource.get_id(
"taxonomy_term", "animal_type", animal_type["data"]["id"]
)
data["animal_type_name"] = animal_type["data"]["attributes"]["name"]
# add location
if location := relationships.get("location"):
if location["data"]:
location = self.farmos_client.resource.get_id(
"asset", "structure", location["data"][0]["id"]
)
data["location_name"] = location["data"]["attributes"]["name"]
# add owners
if owner := relationships.get("owner"):
owners = []
for owner_data in owner["data"]:
owners.append(
self.farmos_client.resource.get_id(
"user", "user", owner_data["id"]
)
)
data["owners"] = ", ".join(
[o["data"]["attributes"]["display_name"] for o in owners]
)
# add image urls
if image := relationships.get("image"): if image := relationships.get("image"):
if image["data"]: if image["data"]:
image = self.farmos_client.resource.get_id( image = self.farmos_client.resource.get_id(
@ -135,7 +172,10 @@ class AnimalView(FarmOSMasterView):
"species_breed": "", # TODO "species_breed": "", # TODO
"birthdate": birthdate, "birthdate": birthdate,
"sex": animal["attributes"]["sex"], "sex": animal["attributes"]["sex"],
"is_castrated": animal["attributes"]["is_castrated"],
"location": "", # TODO "location": "", # TODO
"status": animal["attributes"]["status"],
"notes": animal["attributes"]["notes"]["value"],
} }
def configure_form(self, form): def configure_form(self, form):
@ -143,6 +183,12 @@ class AnimalView(FarmOSMasterView):
super().configure_form(f) super().configure_form(f)
animal = f.model_instance animal = f.model_instance
# is_castrated
f.set_node("is_castrated", colander.Boolean())
# notes
f.set_widget("notes", "notes")
# image # image
if url := animal.get("large_image_url"): if url := animal.get("large_image_url"):
f.set_widget("image", AnimalImage()) f.set_widget("image", AnimalImage())