feat: add schema, edit/sync support for Water Assets

This commit is contained in:
Lance Edgar 2026-03-09 15:36:51 -05:00
parent dfc8dc0de3
commit d9211c1713
13 changed files with 376 additions and 5 deletions

View file

@ -0,0 +1,100 @@
"""add WaterAsset
Revision ID: de1197d24485
Revises: 9c53513f8862
Create Date: 2026-03-09 14:59:12.032318
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
import wuttjamaican.db.util
# revision identifiers, used by Alembic.
revision: str = "de1197d24485"
down_revision: Union[str, None] = "9c53513f8862"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# asset_water
op.create_table(
"asset_water",
sa.Column("uuid", wuttjamaican.db.util.UUID(), nullable=False),
sa.ForeignKeyConstraint(
["uuid"], ["asset.uuid"], name=op.f("fk_asset_water_uuid_asset")
),
sa.PrimaryKeyConstraint("uuid", name=op.f("pk_asset_water")),
)
op.create_table(
"asset_water_version",
sa.Column(
"uuid", wuttjamaican.db.util.UUID(), autoincrement=False, nullable=False
),
sa.Column(
"transaction_id", sa.BigInteger(), autoincrement=False, nullable=False
),
sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
sa.Column("operation_type", sa.SmallInteger(), nullable=False),
sa.PrimaryKeyConstraint(
"uuid", "transaction_id", name=op.f("pk_asset_water_version")
),
)
op.create_index(
op.f("ix_asset_water_version_end_transaction_id"),
"asset_water_version",
["end_transaction_id"],
unique=False,
)
op.create_index(
op.f("ix_asset_water_version_operation_type"),
"asset_water_version",
["operation_type"],
unique=False,
)
op.create_index(
"ix_asset_water_version_pk_transaction_id",
"asset_water_version",
["uuid", sa.literal_column("transaction_id DESC")],
unique=False,
)
op.create_index(
"ix_asset_water_version_pk_validity",
"asset_water_version",
["uuid", "transaction_id", "end_transaction_id"],
unique=False,
)
op.create_index(
op.f("ix_asset_water_version_transaction_id"),
"asset_water_version",
["transaction_id"],
unique=False,
)
def downgrade() -> None:
# asset_water
op.drop_index(
op.f("ix_asset_water_version_transaction_id"), table_name="asset_water_version"
)
op.drop_index(
"ix_asset_water_version_pk_validity", table_name="asset_water_version"
)
op.drop_index(
"ix_asset_water_version_pk_transaction_id", table_name="asset_water_version"
)
op.drop_index(
op.f("ix_asset_water_version_operation_type"), table_name="asset_water_version"
)
op.drop_index(
op.f("ix_asset_water_version_end_transaction_id"),
table_name="asset_water_version",
)
op.drop_table("asset_water_version")
op.drop_table("asset_water")

View file

@ -51,6 +51,7 @@ from .asset_plant import (
PlantAssetPlantType, PlantAssetPlantType,
PlantAssetSeason, PlantAssetSeason,
) )
from .asset_water import WaterAsset
from .log import LogType, Log, LogAsset, LogGroup, LogLocation, LogQuantity, LogOwner from .log import LogType, Log, LogAsset, LogGroup, LogLocation, LogQuantity, LogOwner
from .log_activity import ActivityLog from .log_activity import ActivityLog
from .log_harvest import HarvestLog from .log_harvest import HarvestLog

View file

@ -0,0 +1,45 @@
# -*- coding: utf-8; -*-
################################################################################
#
# WuttaFarm --Web app to integrate with and extend farmOS
# Copyright © 2026 Lance Edgar
#
# This file is part of WuttaFarm.
#
# WuttaFarm is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# WuttaFarm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# WuttaFarm. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Model definition for Water Assets
"""
from wuttjamaican.db import model
from wuttafarm.db.model.asset import AssetMixin, add_asset_proxies
class WaterAsset(AssetMixin, model.Base):
"""
Represents a water asset from farmOS
"""
__tablename__ = "asset_water"
__versioned__ = {}
__wutta_hint__ = {
"model_title": "Water Asset",
"model_title_plural": "Water Assets",
"farmos_asset_type": "water",
}
add_asset_proxies(WaterAsset)

View file

@ -491,6 +491,21 @@ class StructureAssetImporter(ToFarmOSAsset):
return payload return payload
class WaterAssetImporter(ToFarmOSAsset):
model_title = "WaterAsset"
farmos_asset_type = "water"
supported_fields = [
"uuid",
"asset_name",
"is_location",
"is_fixed",
"notes",
"archived",
]
############################## ##############################
# quantity importers # quantity importers
############################## ##############################

View file

@ -98,6 +98,7 @@ class FromWuttaFarmToFarmOS(FromWuttaFarmHandler, ToFarmOSHandler):
importers = super().define_importers() importers = super().define_importers()
importers["LandAsset"] = LandAssetImporter importers["LandAsset"] = LandAssetImporter
importers["StructureAsset"] = StructureAssetImporter importers["StructureAsset"] = StructureAssetImporter
importers["WaterAsset"] = WaterAssetImporter
importers["AnimalType"] = AnimalTypeImporter importers["AnimalType"] = AnimalTypeImporter
importers["AnimalAsset"] = AnimalAssetImporter importers["AnimalAsset"] = AnimalAssetImporter
importers["GroupAsset"] = GroupAssetImporter importers["GroupAsset"] = GroupAssetImporter
@ -417,6 +418,14 @@ class StructureAssetImporter(
return data return data
class WaterAssetImporter(FromWuttaFarmAsset, farmos_importing.model.WaterAssetImporter):
"""
WuttaFarm farmOS API exporter for Water Assets
"""
source_model_class = model.WaterAsset
############################## ##############################
# quantity importers # quantity importers
############################## ##############################

View file

@ -106,6 +106,7 @@ class FromFarmOSToWuttaFarm(FromFarmOSHandler, ToWuttaFarmHandler):
importers["LandAsset"] = LandAssetImporter importers["LandAsset"] = LandAssetImporter
importers["StructureType"] = StructureTypeImporter importers["StructureType"] = StructureTypeImporter
importers["StructureAsset"] = StructureAssetImporter importers["StructureAsset"] = StructureAssetImporter
importers["WaterAsset"] = WaterAssetImporter
importers["AnimalType"] = AnimalTypeImporter importers["AnimalType"] = AnimalTypeImporter
importers["AnimalAsset"] = AnimalAssetImporter importers["AnimalAsset"] = AnimalAssetImporter
importers["GroupAsset"] = GroupAssetImporter importers["GroupAsset"] = GroupAssetImporter
@ -830,6 +831,14 @@ class StructureTypeImporter(FromFarmOS, ToWutta):
} }
class WaterAssetImporter(AssetImporterBase):
"""
farmOS API WuttaFarm importer for Water Assets
"""
model_class = model.WaterAsset
class UserImporter(FromFarmOS, ToWutta): class UserImporter(FromFarmOS, ToWutta):
""" """
farmOS API WuttaFarm importer for Users farmOS API WuttaFarm importer for Users

View file

@ -112,6 +112,11 @@ class WuttaFarmMenuHandler(base.MenuHandler):
"route": "structure_assets", "route": "structure_assets",
"perm": "structure_assets.list", "perm": "structure_assets.list",
}, },
{
"title": "Water",
"route": "water_assets",
"perm": "water_assets.list",
},
{"type": "sep"}, {"type": "sep"},
{ {
"title": "Animal Types", "title": "Animal Types",
@ -259,6 +264,11 @@ class WuttaFarmMenuHandler(base.MenuHandler):
"route": "farmos_structure_assets", "route": "farmos_structure_assets",
"perm": "farmos_structure_assets.list", "perm": "farmos_structure_assets.list",
}, },
{
"title": "Water Assets",
"route": "farmos_water_assets",
"perm": "farmos_water_assets.list",
},
{"type": "sep"}, {"type": "sep"},
{ {
"title": "Activity Logs", "title": "Activity Logs",
@ -383,6 +393,11 @@ class WuttaFarmMenuHandler(base.MenuHandler):
"route": "farmos_structure_assets", "route": "farmos_structure_assets",
"perm": "farmos_structure_assets.list", "perm": "farmos_structure_assets.list",
}, },
{
"title": "Water",
"route": "farmos_water_assets",
"perm": "farmos_water_assets.list",
},
{"type": "sep"}, {"type": "sep"},
{ {
"title": "Animal Types", "title": "Animal Types",

View file

@ -57,6 +57,7 @@ def includeme(config):
config.include("wuttafarm.web.views.animals") config.include("wuttafarm.web.views.animals")
config.include("wuttafarm.web.views.groups") config.include("wuttafarm.web.views.groups")
config.include("wuttafarm.web.views.plants") config.include("wuttafarm.web.views.plants")
config.include("wuttafarm.web.views.water")
config.include("wuttafarm.web.views.logs") config.include("wuttafarm.web.views.logs")
config.include("wuttafarm.web.views.logs_activity") config.include("wuttafarm.web.views.logs_activity")
config.include("wuttafarm.web.views.logs_harvest") config.include("wuttafarm.web.views.logs_harvest")

View file

@ -40,6 +40,7 @@ def includeme(config):
config.include("wuttafarm.web.views.farmos.animals") config.include("wuttafarm.web.views.farmos.animals")
config.include("wuttafarm.web.views.farmos.groups") config.include("wuttafarm.web.views.farmos.groups")
config.include("wuttafarm.web.views.farmos.plants") config.include("wuttafarm.web.views.farmos.plants")
config.include("wuttafarm.web.views.farmos.water")
config.include("wuttafarm.web.views.farmos.log_types") config.include("wuttafarm.web.views.farmos.log_types")
config.include("wuttafarm.web.views.farmos.logs_activity") config.include("wuttafarm.web.views.farmos.logs_activity")
config.include("wuttafarm.web.views.farmos.logs_harvest") config.include("wuttafarm.web.views.farmos.logs_harvest")

View file

@ -24,6 +24,7 @@ Base class for Asset master views
""" """
import colander import colander
import requests
from webhelpers2.html import tags from webhelpers2.html import tags
from wuttafarm.web.views.farmos import FarmOSMasterView from wuttafarm.web.views.farmos import FarmOSMasterView
@ -75,6 +76,22 @@ class AssetMasterView(FarmOSMasterView):
"archived": {"active": True, "verb": "is_false"}, "archived": {"active": True, "verb": "is_false"},
} }
form_fields = [
"name",
"notes",
"asset_type_name",
"is_location",
"is_fixed",
"owners",
"locations",
"groups",
"archived",
"thumbnail_url",
"image_url",
"thumbnail",
"image",
]
def get_grid_data(self, **kwargs): def get_grid_data(self, **kwargs):
return ResourceData( return ResourceData(
self.config, self.config,
@ -142,11 +159,16 @@ class AssetMasterView(FarmOSMasterView):
return {"asset_type", "location", "owner", "image"} return {"asset_type", "location", "owner", "image"}
def get_instance(self): def get_instance(self):
try:
result = self.farmos_client.asset.get_id( result = self.farmos_client.asset.get_id(
self.farmos_asset_type, self.farmos_asset_type,
self.request.matchdict["uuid"], self.request.matchdict["uuid"],
params={"include": ",".join(self.get_farmos_api_includes())}, params={"include": ",".join(self.get_farmos_api_includes())},
) )
except requests.HTTPError as exc:
if exc.response.status_code == 404:
raise self.notfound()
raise
self.raw_json = result self.raw_json = result
included = {obj["id"]: obj for obj in result.get("included", [])} included = {obj["id"]: obj for obj in result.get("included", [])}
return self.normalize_asset(result["data"], included) return self.normalize_asset(result["data"], included)
@ -217,6 +239,8 @@ class AssetMasterView(FarmOSMasterView):
"name": asset["attributes"]["name"], "name": asset["attributes"]["name"],
"asset_type": asset_type_object, "asset_type": asset_type_object,
"asset_type_name": asset_type_name, "asset_type_name": asset_type_name,
"is_location": asset["attributes"]["is_location"],
"is_fixed": asset["attributes"]["is_fixed"],
"notes": notes or colander.null, "notes": notes or colander.null,
"owners": owner_objects, "owners": owner_objects,
"owner_names": owner_names, "owner_names": owner_names,
@ -253,6 +277,16 @@ class AssetMasterView(FarmOSMasterView):
f.set_widget("notes", "notes") f.set_widget("notes", "notes")
f.set_required("notes", False) f.set_required("notes", False)
# is_location
f.set_node("is_location", colander.Boolean())
# is_fixed
f.set_node("is_fixed", colander.Boolean())
# groups
if self.creating or self.editing:
f.remove("groups") # TODO: add support for this?
# archived # archived
f.set_node("archived", colander.Boolean()) f.set_node("archived", colander.Boolean())

View file

@ -217,6 +217,7 @@ class QuantityMasterView(FarmOSMasterView):
except requests.HTTPError as exc: except requests.HTTPError as exc:
if exc.response.status_code == 404: if exc.response.status_code == 404:
raise self.notfound() raise self.notfound()
raise
self.raw_json = result self.raw_json = result

View file

@ -0,0 +1,81 @@
# -*- coding: utf-8; -*-
################################################################################
#
# WuttaFarm --Web app to integrate with and extend farmOS
# Copyright © 2026 Lance Edgar
#
# This file is part of WuttaFarm.
#
# WuttaFarm is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# WuttaFarm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# WuttaFarm. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Master view for Water Assets in farmOS
"""
from wuttafarm.web.views.farmos.assets import AssetMasterView
class WaterAssetView(AssetMasterView):
"""
Master view for farmOS Water Assets
"""
model_name = "farmos_water_assets"
model_title = "farmOS Water Asset"
model_title_plural = "farmOS Water Assets"
route_prefix = "farmos_water_assets"
url_prefix = "/farmOS/assets/water"
farmos_asset_type = "water"
farmos_refurl_path = "/assets/water"
grid_columns = [
"thumbnail",
"drupal_id",
"name",
"archived",
]
def get_xref_buttons(self, water):
model = self.app.model
session = self.Session()
buttons = super().get_xref_buttons(water)
if wf_water := (
session.query(model.Asset)
.filter(model.Asset.farmos_uuid == water["uuid"])
.first()
):
buttons.append(
self.make_button(
f"View {self.app.get_title()} record",
primary=True,
url=self.request.route_url("water_assets.view", uuid=wf_water.uuid),
icon_left="eye",
)
)
return buttons
def defaults(config, **kwargs):
base = globals()
WaterAssetView = kwargs.get("WaterAssetView", base["WaterAssetView"])
WaterAssetView.defaults(config)
def includeme(config):
defaults(config)

View file

@ -0,0 +1,59 @@
# -*- coding: utf-8; -*-
################################################################################
#
# WuttaFarm --Web app to integrate with and extend farmOS
# Copyright © 2026 Lance Edgar
#
# This file is part of WuttaFarm.
#
# WuttaFarm is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# WuttaFarm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# WuttaFarm. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Master view for Water Assets
"""
from wuttafarm.db.model import WaterAsset
from wuttafarm.web.views.assets import AssetMasterView
class WaterAssetView(AssetMasterView):
"""
Master view for Plant Assets
"""
model_class = WaterAsset
route_prefix = "water_assets"
url_prefix = "/assets/water"
farmos_bundle = "water"
farmos_refurl_path = "/assets/water"
grid_columns = [
"thumbnail",
"drupal_id",
"asset_name",
"parents",
"archived",
]
def defaults(config, **kwargs):
base = globals()
WaterAssetView = kwargs.get("WaterAssetView", base["WaterAssetView"])
WaterAssetView.defaults(config)
def includeme(config):
defaults(config)