# -*- 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/>.
#
################################################################################
"""
View for farmOS Groups
"""
import datetime
import colander
from wuttafarm.web.views.farmos import FarmOSMasterView
from wuttaweb.forms.schema import WuttaDateTime
from wuttaweb.forms.widgets import WuttaDateTimeWidget
[docs]
class GroupView(FarmOSMasterView):
"""
View for farmOS Groups
"""
model_name = "farmos_group"
model_title = "farmOS Group"
model_title_plural = "farmOS Groups"
route_prefix = "farmos_group_assets"
url_prefix = "/farmOS/groups"
farmos_refurl_path = "/assets/group"
grid_columns = [
"name",
"is_fixed",
"is_location",
"archived",
"changed",
]
sort_defaults = "name"
form_fields = [
"name",
"is_fixed",
"is_location",
"archived",
"notes",
"created",
"changed",
]
[docs]
def get_grid_data(self, columns=None, session=None):
groups = self.farmos_client.resource.get("asset", "group")
return [self.normalize_group(a) for a in groups["data"]]
[docs]
def get_instance(self):
group = self.farmos_client.resource.get_id(
"asset", "group", self.request.matchdict["uuid"]
)
self.raw_json = group
return self.normalize_group(group["data"])
[docs]
def get_instance_title(self, group):
return group["name"]
def normalize_group(self, group):
if created := group["attributes"].get("created"):
created = datetime.datetime.fromisoformat(created)
created = self.app.localtime(created)
if changed := group["attributes"].get("changed"):
changed = datetime.datetime.fromisoformat(changed)
changed = self.app.localtime(changed)
if self.farmos_4x:
archived = group["attributes"]["archived"]
else:
archived = group["attributes"]["status"] == "archived"
if notes := group["attributes"]["notes"]:
notes = notes["value"]
return {
"uuid": group["id"],
"drupal_id": group["attributes"]["drupal_internal__id"],
"name": group["attributes"]["name"],
"created": created,
"changed": changed,
"is_fixed": group["attributes"]["is_fixed"],
"is_location": group["attributes"]["is_location"],
"archived": archived,
"notes": notes or colander.null,
}
def defaults(config, **kwargs):
base = globals()
GroupView = kwargs.get("GroupView", base["GroupView"])
GroupView.defaults(config)
def includeme(config):
defaults(config)