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

This commit is contained in:
Lance Edgar 2026-02-10 19:04:44 -06:00
parent fd2f09fcf3
commit 10666de488
8 changed files with 352 additions and 0 deletions

View file

@ -0,0 +1,120 @@
"""add Asset Types
Revision ID: cf3f8f46d8bc
Revises: 6c56bcd1c028
Create Date: 2026-02-10 18:42:24.560312
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
import wuttjamaican.db.util
# revision identifiers, used by Alembic.
revision: str = "cf3f8f46d8bc"
down_revision: Union[str, None] = "6c56bcd1c028"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# asset_type
op.create_table(
"asset_type",
sa.Column("uuid", wuttjamaican.db.util.UUID(), nullable=False),
sa.Column("name", sa.String(length=100), nullable=False),
sa.Column("description", sa.String(length=255), nullable=True),
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_asset_type")),
sa.UniqueConstraint(
"drupal_internal_id", name=op.f("uq_asset_type_drupal_internal_id")
),
sa.UniqueConstraint("farmos_uuid", name=op.f("uq_asset_type_farmos_uuid")),
sa.UniqueConstraint("name", name=op.f("uq_asset_type_name")),
)
op.create_table(
"asset_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(
"description", sa.String(length=255), 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_asset_type_version")
),
)
op.create_index(
op.f("ix_asset_type_version_end_transaction_id"),
"asset_type_version",
["end_transaction_id"],
unique=False,
)
op.create_index(
op.f("ix_asset_type_version_operation_type"),
"asset_type_version",
["operation_type"],
unique=False,
)
op.create_index(
"ix_asset_type_version_pk_transaction_id",
"asset_type_version",
["uuid", sa.literal_column("transaction_id DESC")],
unique=False,
)
op.create_index(
"ix_asset_type_version_pk_validity",
"asset_type_version",
["uuid", "transaction_id", "end_transaction_id"],
unique=False,
)
op.create_index(
op.f("ix_asset_type_version_transaction_id"),
"asset_type_version",
["transaction_id"],
unique=False,
)
def downgrade() -> None:
# asset_type
op.drop_index(
op.f("ix_asset_type_version_transaction_id"), table_name="asset_type_version"
)
op.drop_index("ix_asset_type_version_pk_validity", table_name="asset_type_version")
op.drop_index(
"ix_asset_type_version_pk_transaction_id", table_name="asset_type_version"
)
op.drop_index(
op.f("ix_asset_type_version_operation_type"), table_name="asset_type_version"
)
op.drop_index(
op.f("ix_asset_type_version_end_transaction_id"),
table_name="asset_type_version",
)
op.drop_table("asset_type_version")
op.drop_table("asset_type")

View file

@ -30,4 +30,5 @@ from wuttjamaican.db.model import *
from .users import WuttaFarmUser
# wuttafarm proper models
from .assets import AssetType
from .animals import AnimalType

View file

@ -0,0 +1,82 @@
# -*- 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 Asset Types
"""
import sqlalchemy as sa
from sqlalchemy import orm
from wuttjamaican.db import model
class AssetType(model.Base):
"""
Represents an "asset type" from farmOS
"""
__tablename__ = "asset_type"
__versioned__ = {}
__wutta_hint__ = {
"model_title": "Asset Type",
"model_title_plural": "Asset Types",
}
uuid = model.uuid_column()
name = sa.Column(
sa.String(length=100),
nullable=False,
unique=True,
doc="""
Name of the asset type.
""",
)
description = sa.Column(
sa.String(length=255),
nullable=True,
doc="""
Description for the asset type.
""",
)
farmos_uuid = sa.Column(
model.UUID(),
nullable=True,
unique=True,
doc="""
UUID for the asset type within farmOS.
""",
)
drupal_internal_id = sa.Column(
sa.String(length=50),
nullable=True,
unique=True,
doc="""
Drupal internal ID for the asset type.
""",
)
def __str__(self):
return self.name or ""

View file

@ -90,6 +90,7 @@ class FromFarmOSToWuttaFarm(FromFarmOSHandler, ToWuttaFarmHandler):
""" """
importers = super().define_importers()
importers["User"] = UserImporter
importers["AssetType"] = AssetTypeImporter
importers["AnimalType"] = AnimalTypeImporter
return importers
@ -155,6 +156,35 @@ class AnimalTypeImporter(FromFarmOS, ToWutta):
}
class AssetTypeImporter(FromFarmOS, ToWutta):
"""
farmOS API WuttaFarm importer for Asset Types
"""
model_class = model.AssetType
supported_fields = [
"farmos_uuid",
"drupal_internal_id",
"name",
"description",
]
def get_source_objects(self):
""" """
asset_types = self.farmos_client.resource.get("asset_type")
return asset_types["data"]
def normalize_source_object(self, asset_type):
""" """
return {
"farmos_uuid": UUID(asset_type["id"]),
"drupal_internal_id": asset_type["attributes"]["drupal_internal__id"],
"name": asset_type["attributes"]["label"],
"description": asset_type["attributes"]["description"],
}
class UserImporter(FromFarmOS, ToWutta):
"""
farmOS API WuttaFarm importer for Users

View file

@ -43,6 +43,11 @@ 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",

View file

@ -41,6 +41,7 @@ def includeme(config):
)
# native table views
config.include("wuttafarm.web.views.asset_types")
config.include("wuttafarm.web.views.animal_types")
# views for farmOS

View file

@ -0,0 +1,90 @@
# -*- 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 Asset Types
"""
from wuttafarm.db.model.assets import AssetType
from wuttafarm.web.views import WuttaFarmMasterView
class AssetTypeView(WuttaFarmMasterView):
"""
Master view for Asset Types
"""
model_class = AssetType
route_prefix = "asset_types"
url_prefix = "/asset-types"
grid_columns = [
"name",
"description",
]
sort_defaults = "name"
filter_defaults = {
"name": {"active": True, "verb": "contains"},
}
form_fields = [
"name",
"description",
"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, asset_type):
buttons = super().get_xref_buttons(asset_type)
if asset_type.farmos_uuid:
buttons.append(
self.make_button(
"View farmOS record",
primary=True,
url=self.request.route_url(
"farmos_asset_types.view", uuid=asset_type.farmos_uuid
),
icon_left="eye",
)
)
return buttons
def defaults(config, **kwargs):
base = globals()
AssetTypeView = kwargs.get("AssetTypeView", base["AssetTypeView"])
AssetTypeView.defaults(config)
def includeme(config):
defaults(config)

View file

@ -90,6 +90,29 @@ class AssetTypeView(FarmOSMasterView):
# description
f.set_widget("description", "notes")
def get_xref_buttons(self, asset_type):
model = self.app.model
session = self.Session()
buttons = []
if wf_asset_type := (
session.query(model.AssetType)
.filter(model.AssetType.farmos_uuid == asset_type["uuid"])
.first()
):
buttons.append(
self.make_button(
f"View {self.app.get_title()} record",
primary=True,
url=self.request.route_url(
"asset_types.view", uuid=wf_asset_type.uuid
),
icon_left="eye",
)
)
return buttons
def defaults(config, **kwargs):
base = globals()