feat: expose "group membership" for assets
This commit is contained in:
parent
b2c3d3a301
commit
e8a8ce2528
5 changed files with 58 additions and 6 deletions
|
|
@ -32,6 +32,22 @@ class AssetHandler(GenericHandler):
|
|||
:term:`handler`.
|
||||
"""
|
||||
|
||||
def get_groups(self, asset):
|
||||
model = self.app.model
|
||||
session = self.app.get_session(asset)
|
||||
|
||||
grplog = (
|
||||
session.query(model.Log)
|
||||
.join(model.LogAsset)
|
||||
.filter(model.LogAsset.asset == asset)
|
||||
.filter(model.Log.is_group_assignment == True)
|
||||
.order_by(model.Log.timestamp.desc())
|
||||
.first()
|
||||
)
|
||||
if grplog:
|
||||
return grplog.groups
|
||||
return []
|
||||
|
||||
def get_locations(self, asset):
|
||||
model = self.app.model
|
||||
session = self.app.get_session(asset)
|
||||
|
|
|
|||
|
|
@ -228,7 +228,7 @@ class AnimalAssetView(AssetMasterView):
|
|||
"birthdate",
|
||||
"is_sterile",
|
||||
"sex",
|
||||
"group_membership",
|
||||
"groups",
|
||||
"owners",
|
||||
"locations",
|
||||
"archived",
|
||||
|
|
@ -245,6 +245,7 @@ class AnimalAssetView(AssetMasterView):
|
|||
"asset_type",
|
||||
"owners",
|
||||
"locations",
|
||||
"groups",
|
||||
"archived",
|
||||
"drupal_id",
|
||||
"farmos_uuid",
|
||||
|
|
|
|||
|
|
@ -66,6 +66,10 @@ class AssetMasterView(WuttaFarmMasterView):
|
|||
|
||||
farmos_entity_type = "asset"
|
||||
|
||||
labels = {
|
||||
"groups": "Group Membership",
|
||||
}
|
||||
|
||||
sort_defaults = "asset_name"
|
||||
|
||||
filter_defaults = {
|
||||
|
|
@ -136,6 +140,9 @@ class AssetMasterView(WuttaFarmMasterView):
|
|||
# parents
|
||||
g.set_renderer("parents", self.render_parents_for_grid)
|
||||
|
||||
# groups
|
||||
g.set_renderer("groups", self.render_groups_for_grid)
|
||||
|
||||
# owners
|
||||
g.set_label("owners", "Owner")
|
||||
g.set_renderer("owners", self.render_owners_for_grid)
|
||||
|
|
@ -174,6 +181,21 @@ class AssetMasterView(WuttaFarmMasterView):
|
|||
|
||||
return ", ".join([user.username for user in asset.owners])
|
||||
|
||||
def render_groups_for_grid(self, asset, field, value):
|
||||
asset_handler = self.app.get_asset_handler()
|
||||
groups = asset_handler.get_groups(asset)
|
||||
|
||||
if self.farmos_style_grid_links:
|
||||
links = []
|
||||
for group in groups:
|
||||
url = self.request.route_url(
|
||||
f"{group.asset_type}_assets.view", uuid=group.uuid
|
||||
)
|
||||
links.append(tags.link_to(str(group), url))
|
||||
return ", ".join(links)
|
||||
|
||||
return ", ".join([str(group) for group in groups])
|
||||
|
||||
def render_locations_for_grid(self, asset, field, value):
|
||||
asset_handler = self.app.get_asset_handler()
|
||||
locations = asset_handler.get_locations(asset)
|
||||
|
|
@ -229,6 +251,15 @@ class AssetMasterView(WuttaFarmMasterView):
|
|||
# nb. must explicity declare value for non-standard field
|
||||
f.set_default("locations", asset_handler.get_locations(asset))
|
||||
|
||||
# groups
|
||||
if self.creating or self.editing:
|
||||
# nb. this is a calculated field
|
||||
f.remove("groups")
|
||||
else:
|
||||
f.set_node("groups", AssetRefs(self.request))
|
||||
# nb. must explicity declare value for non-standard field
|
||||
f.set_default("groups", asset_handler.get_groups(asset))
|
||||
|
||||
# parents
|
||||
if self.creating or self.editing:
|
||||
f.remove("parents") # TODO: add support for this
|
||||
|
|
@ -367,7 +398,7 @@ class AllAssetView(AssetMasterView):
|
|||
"thumbnail",
|
||||
"drupal_id",
|
||||
"asset_name",
|
||||
"group_membership",
|
||||
"groups",
|
||||
"asset_type",
|
||||
"parents",
|
||||
"owners",
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ from wuttafarm.web.grids import (
|
|||
NullableBooleanFilter,
|
||||
DateTimeFilter,
|
||||
)
|
||||
from wuttafarm.web.forms.schema import FarmOSRef
|
||||
from wuttafarm.web.forms.schema import FarmOSRef, FarmOSAssetRefs
|
||||
|
||||
|
||||
class AnimalView(AssetMasterView):
|
||||
|
|
@ -87,9 +87,9 @@ class AnimalView(AssetMasterView):
|
|||
"is_sterile",
|
||||
"notes",
|
||||
"asset_type_name",
|
||||
"groups",
|
||||
"owners",
|
||||
"locations",
|
||||
"groups",
|
||||
"archived",
|
||||
"thumbnail_url",
|
||||
"image_url",
|
||||
|
|
@ -147,7 +147,7 @@ class AnimalView(AssetMasterView):
|
|||
|
||||
def render_groups_for_grid(self, animal, field, value):
|
||||
groups = []
|
||||
for group in animal["group_objects"]:
|
||||
for group in animal["groups"]:
|
||||
if self.farmos_style_grid_links:
|
||||
url = self.request.route_url(
|
||||
"farmos_group_assets.view", uuid=group["uuid"]
|
||||
|
|
@ -209,6 +209,7 @@ class AnimalView(AssetMasterView):
|
|||
group = {
|
||||
"uuid": group["id"],
|
||||
"name": group["attributes"]["name"],
|
||||
"asset_type": "group",
|
||||
}
|
||||
group_objects.append(group)
|
||||
group_names.append(group["name"])
|
||||
|
|
@ -218,7 +219,7 @@ class AnimalView(AssetMasterView):
|
|||
"animal_type": animal_type_object,
|
||||
"animal_type_uuid": animal_type_object["uuid"],
|
||||
"animal_type_name": animal_type_object["name"],
|
||||
"group_objects": group_objects,
|
||||
"groups": group_objects,
|
||||
"group_names": group_names,
|
||||
"birthdate": birthdate,
|
||||
"sex": animal["attributes"]["sex"] or colander.null,
|
||||
|
|
@ -273,6 +274,8 @@ class AnimalView(AssetMasterView):
|
|||
# groups
|
||||
if self.creating or self.editing:
|
||||
f.remove("groups") # TODO
|
||||
else:
|
||||
f.set_node("groups", FarmOSAssetRefs(self.request))
|
||||
|
||||
def get_api_payload(self, animal):
|
||||
payload = super().get_api_payload(animal)
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ class AssetMasterView(FarmOSMasterView):
|
|||
"name": "Asset Name",
|
||||
"asset_type_name": "Asset Type",
|
||||
"locations": "Location",
|
||||
"groups": "Group Membership",
|
||||
"thumbnail_url": "Thumbnail URL",
|
||||
"image_url": "Image URL",
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue