From b573ae459ecac9f7f41126bf2d9d2afba2bc8187 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 10 Feb 2026 19:16:55 -0600 Subject: [PATCH] feat: add native table for Land Types; import from farmOS API --- .../versions/9f2243df9566_add_land_types.py | 115 ++++++++++++++++++ src/wuttafarm/db/model/__init__.py | 1 + src/wuttafarm/db/model/land.py | 74 +++++++++++ src/wuttafarm/importing/farmos.py | 28 +++++ src/wuttafarm/web/menus.py | 15 ++- src/wuttafarm/web/views/__init__.py | 1 + src/wuttafarm/web/views/farmos/land_types.py | 23 ++++ src/wuttafarm/web/views/land_types.py | 88 ++++++++++++++ 8 files changed, 340 insertions(+), 5 deletions(-) create mode 100644 src/wuttafarm/db/alembic/versions/9f2243df9566_add_land_types.py create mode 100644 src/wuttafarm/db/model/land.py create mode 100644 src/wuttafarm/web/views/land_types.py diff --git a/src/wuttafarm/db/alembic/versions/9f2243df9566_add_land_types.py b/src/wuttafarm/db/alembic/versions/9f2243df9566_add_land_types.py new file mode 100644 index 0000000..4e45439 --- /dev/null +++ b/src/wuttafarm/db/alembic/versions/9f2243df9566_add_land_types.py @@ -0,0 +1,115 @@ +"""add Land Types + +Revision ID: 9f2243df9566 +Revises: cf3f8f46d8bc +Create Date: 2026-02-10 19:10:02.851756 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +import wuttjamaican.db.util + + +# revision identifiers, used by Alembic. +revision: str = "9f2243df9566" +down_revision: Union[str, None] = "cf3f8f46d8bc" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + + # land_type + op.create_table( + "land_type", + sa.Column("uuid", wuttjamaican.db.util.UUID(), nullable=False), + sa.Column("name", sa.String(length=100), nullable=False), + sa.Column("farmos_uuid", wuttjamaican.db.util.UUID(), nullable=True), + sa.Column("drupal_internal_id", sa.String(length=50), nullable=True), + sa.PrimaryKeyConstraint("uuid", name=op.f("pk_land_type")), + sa.UniqueConstraint( + "drupal_internal_id", name=op.f("uq_land_type_drupal_internal_id") + ), + sa.UniqueConstraint("farmos_uuid", name=op.f("uq_land_type_farmos_uuid")), + sa.UniqueConstraint("name", name=op.f("uq_land_type_name")), + ) + op.create_table( + "land_type_version", + sa.Column( + "uuid", wuttjamaican.db.util.UUID(), autoincrement=False, nullable=False + ), + sa.Column("name", sa.String(length=100), autoincrement=False, nullable=True), + sa.Column( + "farmos_uuid", + wuttjamaican.db.util.UUID(), + autoincrement=False, + nullable=True, + ), + sa.Column( + "drupal_internal_id", + sa.String(length=50), + autoincrement=False, + nullable=True, + ), + 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_land_type_version") + ), + ) + op.create_index( + op.f("ix_land_type_version_end_transaction_id"), + "land_type_version", + ["end_transaction_id"], + unique=False, + ) + op.create_index( + op.f("ix_land_type_version_operation_type"), + "land_type_version", + ["operation_type"], + unique=False, + ) + op.create_index( + "ix_land_type_version_pk_transaction_id", + "land_type_version", + ["uuid", sa.literal_column("transaction_id DESC")], + unique=False, + ) + op.create_index( + "ix_land_type_version_pk_validity", + "land_type_version", + ["uuid", "transaction_id", "end_transaction_id"], + unique=False, + ) + op.create_index( + op.f("ix_land_type_version_transaction_id"), + "land_type_version", + ["transaction_id"], + unique=False, + ) + + +def downgrade() -> None: + + # land_type + op.drop_index( + op.f("ix_land_type_version_transaction_id"), table_name="land_type_version" + ) + op.drop_index("ix_land_type_version_pk_validity", table_name="land_type_version") + op.drop_index( + "ix_land_type_version_pk_transaction_id", table_name="land_type_version" + ) + op.drop_index( + op.f("ix_land_type_version_operation_type"), table_name="land_type_version" + ) + op.drop_index( + op.f("ix_land_type_version_end_transaction_id"), table_name="land_type_version" + ) + op.drop_table("land_type_version") + op.drop_table("land_type") diff --git a/src/wuttafarm/db/model/__init__.py b/src/wuttafarm/db/model/__init__.py index f07057f..0951c72 100644 --- a/src/wuttafarm/db/model/__init__.py +++ b/src/wuttafarm/db/model/__init__.py @@ -31,4 +31,5 @@ from .users import WuttaFarmUser # wuttafarm proper models from .assets import AssetType +from .land import LandType from .animals import AnimalType diff --git a/src/wuttafarm/db/model/land.py b/src/wuttafarm/db/model/land.py new file mode 100644 index 0000000..dc4f0f3 --- /dev/null +++ b/src/wuttafarm/db/model/land.py @@ -0,0 +1,74 @@ +# -*- 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 . +# +################################################################################ +""" +Model definition for Land Types +""" + +import sqlalchemy as sa +from sqlalchemy import orm + +from wuttjamaican.db import model + + +class LandType(model.Base): + """ + Represents a "land type" from farmOS + """ + + __tablename__ = "land_type" + __versioned__ = {} + __wutta_hint__ = { + "model_title": "Land Type", + "model_title_plural": "Land Types", + } + + uuid = model.uuid_column() + + name = sa.Column( + sa.String(length=100), + nullable=False, + unique=True, + doc=""" + Name of the land type. + """, + ) + + farmos_uuid = sa.Column( + model.UUID(), + nullable=True, + unique=True, + doc=""" + UUID for the land type within farmOS. + """, + ) + + drupal_internal_id = sa.Column( + sa.String(length=50), + nullable=True, + unique=True, + doc=""" + Drupal internal ID for the land type. + """, + ) + + def __str__(self): + return self.name or "" diff --git a/src/wuttafarm/importing/farmos.py b/src/wuttafarm/importing/farmos.py index 8470496..842ba76 100644 --- a/src/wuttafarm/importing/farmos.py +++ b/src/wuttafarm/importing/farmos.py @@ -91,6 +91,7 @@ class FromFarmOSToWuttaFarm(FromFarmOSHandler, ToWuttaFarmHandler): importers = super().define_importers() importers["User"] = UserImporter importers["AssetType"] = AssetTypeImporter + importers["LandType"] = LandTypeImporter importers["AnimalType"] = AnimalTypeImporter return importers @@ -185,6 +186,33 @@ class AssetTypeImporter(FromFarmOS, ToWutta): } +class LandTypeImporter(FromFarmOS, ToWutta): + """ + farmOS API → WuttaFarm importer for Land Types + """ + + model_class = model.LandType + + supported_fields = [ + "farmos_uuid", + "drupal_internal_id", + "name", + ] + + def get_source_objects(self): + """ """ + land_types = self.farmos_client.resource.get("land_type") + return land_types["data"] + + def normalize_source_object(self, land_type): + """ """ + return { + "farmos_uuid": UUID(land_type["id"]), + "drupal_internal_id": land_type["attributes"]["drupal_internal__id"], + "name": land_type["attributes"]["label"], + } + + class UserImporter(FromFarmOS, ToWutta): """ farmOS API → WuttaFarm importer for Users diff --git a/src/wuttafarm/web/menus.py b/src/wuttafarm/web/menus.py index 402eb28..017e0ab 100644 --- a/src/wuttafarm/web/menus.py +++ b/src/wuttafarm/web/menus.py @@ -43,16 +43,21 @@ class WuttaFarmMenuHandler(base.MenuHandler): "title": "Assets", "type": "menu", "items": [ - { - "title": "Asset Types", - "route": "asset_types", - "perm": "asset_types.list", - }, { "title": "Animal Types", "route": "animal_types", "perm": "animal_types.list", }, + { + "title": "Land Types", + "route": "land_types", + "perm": "land_types.list", + }, + { + "title": "Asset Types", + "route": "asset_types", + "perm": "asset_types.list", + }, ], } diff --git a/src/wuttafarm/web/views/__init__.py b/src/wuttafarm/web/views/__init__.py index 51a7f7e..ecb4a69 100644 --- a/src/wuttafarm/web/views/__init__.py +++ b/src/wuttafarm/web/views/__init__.py @@ -42,6 +42,7 @@ def includeme(config): # native table views config.include("wuttafarm.web.views.asset_types") + config.include("wuttafarm.web.views.land_types") config.include("wuttafarm.web.views.animal_types") # views for farmOS diff --git a/src/wuttafarm/web/views/farmos/land_types.py b/src/wuttafarm/web/views/farmos/land_types.py index 02c0560..9a7bb8b 100644 --- a/src/wuttafarm/web/views/farmos/land_types.py +++ b/src/wuttafarm/web/views/farmos/land_types.py @@ -77,6 +77,29 @@ class LandTypeView(FarmOSMasterView): "label": land_type["attributes"]["label"], } + def get_xref_buttons(self, land_type): + model = self.app.model + session = self.Session() + buttons = [] + + if wf_land_type := ( + session.query(model.LandType) + .filter(model.LandType.farmos_uuid == land_type["uuid"]) + .first() + ): + buttons.append( + self.make_button( + f"View {self.app.get_title()} record", + primary=True, + url=self.request.route_url( + "land_types.view", uuid=wf_land_type.uuid + ), + icon_left="eye", + ) + ) + + return buttons + def defaults(config, **kwargs): base = globals() diff --git a/src/wuttafarm/web/views/land_types.py b/src/wuttafarm/web/views/land_types.py new file mode 100644 index 0000000..c9711a4 --- /dev/null +++ b/src/wuttafarm/web/views/land_types.py @@ -0,0 +1,88 @@ +# -*- 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 . +# +################################################################################ +""" +Master view for Land Types +""" + +from wuttafarm.db.model.land import LandType +from wuttafarm.web.views import WuttaFarmMasterView + + +class LandTypeView(WuttaFarmMasterView): + """ + Master view for Land Types + """ + + model_class = LandType + route_prefix = "land_types" + url_prefix = "/land-types" + + grid_columns = [ + "name", + ] + + sort_defaults = "name" + + filter_defaults = { + "name": {"active": True, "verb": "contains"}, + } + + form_fields = [ + "name", + "farmos_uuid", + "drupal_internal_id", + ] + + def configure_grid(self, grid): + g = grid + super().configure_grid(g) + + # name + g.set_link("name") + + def get_xref_buttons(self, land_type): + buttons = super().get_xref_buttons(land_type) + + if land_type.farmos_uuid: + buttons.append( + self.make_button( + "View farmOS record", + primary=True, + url=self.request.route_url( + "farmos_land_types.view", uuid=land_type.farmos_uuid + ), + icon_left="eye", + ) + ) + + return buttons + + +def defaults(config, **kwargs): + base = globals() + + LandTypeView = kwargs.get("LandTypeView", base["LandTypeView"]) + LandTypeView.defaults(config) + + +def includeme(config): + defaults(config)