feat: add edit/sync support for Log.locations
also set `Log.owners` to current user, when creating new log
This commit is contained in:
parent
6d80937e0c
commit
797c045f67
5 changed files with 47 additions and 8 deletions
|
|
@ -631,6 +631,7 @@ class ToFarmOSLog(ToFarmOS):
|
||||||
"notes",
|
"notes",
|
||||||
"quick",
|
"quick",
|
||||||
"assets",
|
"assets",
|
||||||
|
"locations",
|
||||||
"quantities",
|
"quantities",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -693,6 +694,9 @@ class ToFarmOSLog(ToFarmOS):
|
||||||
"notes": normal["notes"],
|
"notes": normal["notes"],
|
||||||
"quick": normal["quick"],
|
"quick": normal["quick"],
|
||||||
"assets": [(a["asset_type"], UUID(a["uuid"])) for a in normal["assets"]],
|
"assets": [(a["asset_type"], UUID(a["uuid"])) for a in normal["assets"]],
|
||||||
|
"locations": [
|
||||||
|
(l["asset_type"], UUID(l["uuid"])) for l in normal["locations"]
|
||||||
|
],
|
||||||
"quantities": [UUID(uuid) for uuid in normal["quantity_uuids"]],
|
"quantities": [UUID(uuid) for uuid in normal["quantity_uuids"]],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -725,6 +729,16 @@ class ToFarmOSLog(ToFarmOS):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
rels["asset"] = {"data": assets}
|
rels["asset"] = {"data": assets}
|
||||||
|
if "locations" in self.fields:
|
||||||
|
locations = []
|
||||||
|
for asset_type, uuid in source_data["locations"]:
|
||||||
|
locations.append(
|
||||||
|
{
|
||||||
|
"type": f"asset--{asset_type}",
|
||||||
|
"id": str(uuid),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
rels["location"] = {"data": locations}
|
||||||
if "quantities" in self.fields:
|
if "quantities" in self.fields:
|
||||||
quantities = []
|
quantities = []
|
||||||
for uuid in source_data["quantities"]:
|
for uuid in source_data["quantities"]:
|
||||||
|
|
|
||||||
|
|
@ -458,6 +458,7 @@ class FromWuttaFarmLog(FromWuttaFarm):
|
||||||
"notes",
|
"notes",
|
||||||
"quick",
|
"quick",
|
||||||
"assets",
|
"assets",
|
||||||
|
"locations",
|
||||||
"quantities",
|
"quantities",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -472,6 +473,7 @@ class FromWuttaFarmLog(FromWuttaFarm):
|
||||||
"notes": log.notes,
|
"notes": log.notes,
|
||||||
"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],
|
||||||
"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,9 +402,10 @@ 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, **kwargs):
|
def __init__(self, request, for_asset=None, is_location=None, **kwargs):
|
||||||
super().__init__(request, **kwargs)
|
super().__init__(request, **kwargs)
|
||||||
self.for_asset = for_asset
|
self.for_asset = for_asset
|
||||||
|
self.is_location = is_location
|
||||||
|
|
||||||
def serialize(self, node, appstruct):
|
def serialize(self, node, appstruct):
|
||||||
if not appstruct:
|
if not appstruct:
|
||||||
|
|
@ -420,6 +421,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_location is not None:
|
||||||
|
query = query.filter(model.Asset.is_location == self.is_location)
|
||||||
if self.for_asset:
|
if self.for_asset:
|
||||||
query = query.filter(model.Asset.uuid != self.for_asset.uuid)
|
query = query.filter(model.Asset.uuid != self.for_asset.uuid)
|
||||||
query = query.order_by(model.Asset.asset_name)
|
query = query.order_by(model.Asset.asset_name)
|
||||||
|
|
|
||||||
|
|
@ -521,7 +521,7 @@ class AssetRefsWidget(Widget):
|
||||||
def deserialize(self, field, pstruct):
|
def deserialize(self, field, pstruct):
|
||||||
""" """
|
""" """
|
||||||
if not pstruct:
|
if not pstruct:
|
||||||
return colander.null
|
return set()
|
||||||
|
|
||||||
return set(pstruct.split(","))
|
return set(pstruct.split(","))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -271,10 +271,8 @@ class LogMasterView(WuttaFarmMasterView):
|
||||||
f.set_default("groups", log.groups)
|
f.set_default("groups", log.groups)
|
||||||
|
|
||||||
# locations
|
# locations
|
||||||
if self.creating or self.editing:
|
f.set_node("locations", AssetRefs(self.request, is_location=True))
|
||||||
f.remove("locations") # TODO: need to support this
|
if not self.creating:
|
||||||
else:
|
|
||||||
f.set_node("locations", AssetRefs(self.request))
|
|
||||||
# nb. must explicity declare value for non-standard field
|
# nb. must explicity declare value for non-standard field
|
||||||
f.set_default("locations", log.locations)
|
f.set_default("locations", log.locations)
|
||||||
|
|
||||||
|
|
@ -326,10 +324,15 @@ class LogMasterView(WuttaFarmMasterView):
|
||||||
data = form.validated
|
data = form.validated
|
||||||
|
|
||||||
if self.creating:
|
if self.creating:
|
||||||
model_class = self.get_model_class()
|
|
||||||
|
# log_type
|
||||||
log.log_type = self.get_farmos_log_type()
|
log.log_type = self.get_farmos_log_type()
|
||||||
|
|
||||||
self.set_assets(log, data["assets"] or [])
|
# owner
|
||||||
|
log.owners = [self.request.user]
|
||||||
|
|
||||||
|
self.set_assets(log, data["assets"])
|
||||||
|
self.set_locations(log, data["locations"])
|
||||||
|
|
||||||
return log
|
return log
|
||||||
|
|
||||||
|
|
@ -350,6 +353,23 @@ class LogMasterView(WuttaFarmMasterView):
|
||||||
assert asset
|
assert asset
|
||||||
log.assets.remove(asset)
|
log.assets.remove(asset)
|
||||||
|
|
||||||
|
def set_locations(self, log, desired):
|
||||||
|
model = self.app.model
|
||||||
|
session = self.Session()
|
||||||
|
current = [l.uuid for l in log.locations]
|
||||||
|
|
||||||
|
for uuid in desired:
|
||||||
|
if uuid not in current:
|
||||||
|
location = session.get(model.Asset, uuid)
|
||||||
|
assert location
|
||||||
|
log.locations.append(location)
|
||||||
|
|
||||||
|
for uuid in current:
|
||||||
|
if uuid not in desired:
|
||||||
|
location = session.get(model.Asset, uuid)
|
||||||
|
assert location
|
||||||
|
log.locations.remove(location)
|
||||||
|
|
||||||
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