feat: add edit/sync support for Log.groups
This commit is contained in:
parent
797c045f67
commit
1d303a818c
4 changed files with 41 additions and 6 deletions
|
|
@ -632,6 +632,7 @@ class ToFarmOSLog(ToFarmOS):
|
||||||
"quick",
|
"quick",
|
||||||
"assets",
|
"assets",
|
||||||
"locations",
|
"locations",
|
||||||
|
"groups",
|
||||||
"quantities",
|
"quantities",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -697,6 +698,7 @@ class ToFarmOSLog(ToFarmOS):
|
||||||
"locations": [
|
"locations": [
|
||||||
(l["asset_type"], UUID(l["uuid"])) for l in normal["locations"]
|
(l["asset_type"], UUID(l["uuid"])) for l in normal["locations"]
|
||||||
],
|
],
|
||||||
|
"groups": [(g["asset_type"], UUID(g["uuid"])) for g in normal["groups"]],
|
||||||
"quantities": [UUID(uuid) for uuid in normal["quantity_uuids"]],
|
"quantities": [UUID(uuid) for uuid in normal["quantity_uuids"]],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -739,6 +741,16 @@ class ToFarmOSLog(ToFarmOS):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
rels["location"] = {"data": locations}
|
rels["location"] = {"data": locations}
|
||||||
|
if "groups" in self.fields:
|
||||||
|
groups = []
|
||||||
|
for asset_type, uuid in source_data["groups"]:
|
||||||
|
groups.append(
|
||||||
|
{
|
||||||
|
"type": f"asset--{asset_type}",
|
||||||
|
"id": str(uuid),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
rels["group"] = {"data": groups}
|
||||||
if "quantities" in self.fields:
|
if "quantities" in self.fields:
|
||||||
quantities = []
|
quantities = []
|
||||||
for uuid in source_data["quantities"]:
|
for uuid in source_data["quantities"]:
|
||||||
|
|
|
||||||
|
|
@ -459,6 +459,7 @@ class FromWuttaFarmLog(FromWuttaFarm):
|
||||||
"quick",
|
"quick",
|
||||||
"assets",
|
"assets",
|
||||||
"locations",
|
"locations",
|
||||||
|
"groups",
|
||||||
"quantities",
|
"quantities",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -474,6 +475,7 @@ class FromWuttaFarmLog(FromWuttaFarm):
|
||||||
"quick": self.config.parse_list(log.quick) if log.quick else [],
|
"quick": self.config.parse_list(log.quick) if log.quick else [],
|
||||||
"assets": [(a.asset_type, a.farmos_uuid) for a in log.assets],
|
"assets": [(a.asset_type, a.farmos_uuid) for a in log.assets],
|
||||||
"locations": [(l.asset_type, l.farmos_uuid) for l in log.locations],
|
"locations": [(l.asset_type, l.farmos_uuid) for l in log.locations],
|
||||||
|
"groups": [(g.asset_type, g.farmos_uuid) for g in log.groups],
|
||||||
"quantities": [qty.farmos_uuid for qty in log.quantities],
|
"quantities": [qty.farmos_uuid for qty in log.quantities],
|
||||||
"_src_object": log,
|
"_src_object": log,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -402,10 +402,13 @@ class AssetRefs(WuttaSet):
|
||||||
Schema type for Assets field (on a Log record)
|
Schema type for Assets field (on a Log record)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, request, for_asset=None, is_location=None, **kwargs):
|
def __init__(
|
||||||
|
self, request, for_asset=None, is_group=None, is_location=None, **kwargs
|
||||||
|
):
|
||||||
super().__init__(request, **kwargs)
|
super().__init__(request, **kwargs)
|
||||||
self.for_asset = for_asset
|
self.is_group = is_group
|
||||||
self.is_location = is_location
|
self.is_location = is_location
|
||||||
|
self.for_asset = for_asset
|
||||||
|
|
||||||
def serialize(self, node, appstruct):
|
def serialize(self, node, appstruct):
|
||||||
if not appstruct:
|
if not appstruct:
|
||||||
|
|
@ -421,6 +424,8 @@ class AssetRefs(WuttaSet):
|
||||||
|
|
||||||
if "values" not in kwargs:
|
if "values" not in kwargs:
|
||||||
query = session.query(model.Asset)
|
query = session.query(model.Asset)
|
||||||
|
if self.is_group is not None:
|
||||||
|
query = query.join(model.GroupAsset)
|
||||||
if self.is_location is not None:
|
if self.is_location is not None:
|
||||||
query = query.filter(model.Asset.is_location == self.is_location)
|
query = query.filter(model.Asset.is_location == self.is_location)
|
||||||
if self.for_asset:
|
if self.for_asset:
|
||||||
|
|
|
||||||
|
|
@ -263,10 +263,8 @@ class LogMasterView(WuttaFarmMasterView):
|
||||||
f.set_default("assets", log.assets)
|
f.set_default("assets", log.assets)
|
||||||
|
|
||||||
# groups
|
# groups
|
||||||
if self.creating or self.editing:
|
f.set_node("groups", AssetRefs(self.request, is_group=True))
|
||||||
f.remove("groups") # TODO: need to support this
|
if not self.creating:
|
||||||
else:
|
|
||||||
f.set_node("groups", AssetRefs(self.request))
|
|
||||||
# nb. must explicity declare value for non-standard field
|
# nb. must explicity declare value for non-standard field
|
||||||
f.set_default("groups", log.groups)
|
f.set_default("groups", log.groups)
|
||||||
|
|
||||||
|
|
@ -333,6 +331,7 @@ class LogMasterView(WuttaFarmMasterView):
|
||||||
|
|
||||||
self.set_assets(log, data["assets"])
|
self.set_assets(log, data["assets"])
|
||||||
self.set_locations(log, data["locations"])
|
self.set_locations(log, data["locations"])
|
||||||
|
self.set_groups(log, data["groups"])
|
||||||
|
|
||||||
return log
|
return log
|
||||||
|
|
||||||
|
|
@ -370,6 +369,23 @@ class LogMasterView(WuttaFarmMasterView):
|
||||||
assert location
|
assert location
|
||||||
log.locations.remove(location)
|
log.locations.remove(location)
|
||||||
|
|
||||||
|
def set_groups(self, log, desired):
|
||||||
|
model = self.app.model
|
||||||
|
session = self.Session()
|
||||||
|
current = [g.uuid for g in log.groups]
|
||||||
|
|
||||||
|
for uuid in desired:
|
||||||
|
if uuid not in current:
|
||||||
|
group = session.get(model.Asset, uuid)
|
||||||
|
assert group
|
||||||
|
log.groups.append(group)
|
||||||
|
|
||||||
|
for uuid in current:
|
||||||
|
if uuid not in desired:
|
||||||
|
group = session.get(model.Asset, uuid)
|
||||||
|
assert group
|
||||||
|
log.groups.remove(group)
|
||||||
|
|
||||||
def get_farmos_url(self, log):
|
def get_farmos_url(self, log):
|
||||||
return self.app.get_farmos_url(f"/log/{log.drupal_id}")
|
return self.app.get_farmos_url(f"/log/{log.drupal_id}")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue