feat: add native table for Land Types; import from farmOS API

This commit is contained in:
Lance Edgar 2026-02-10 19:16:55 -06:00
parent 10666de488
commit b573ae459e
8 changed files with 340 additions and 5 deletions

View file

@ -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")

View file

@ -31,4 +31,5 @@ from .users import WuttaFarmUser
# wuttafarm proper models
from .assets import AssetType
from .land import LandType
from .animals import AnimalType

View file

@ -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 <http://www.gnu.org/licenses/>.
#
################################################################################
"""
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 ""

View file

@ -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

View file

@ -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",
},
],
}

View file

@ -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

View file

@ -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()

View file

@ -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 <http://www.gnu.org/licenses/>.
#
################################################################################
"""
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)