-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1452 from archesproject/multi-card-primary-descri…
…ptor Add multi-card primary descriptor #1368
- Loading branch information
Showing
26 changed files
with
797 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
arches_for_science/media/js/views/components/functions/multicard-resource-descriptor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
define([ | ||
'jquery', | ||
'underscore', | ||
'arches', | ||
'knockout', | ||
'knockout-mapping', | ||
'views/list', | ||
'views/components/functions/primary-descriptors', | ||
'templates/views/components/functions/multicard-resource-descriptor.htm', | ||
'bindings/select2-query', | ||
], | ||
function($, _, arches, ko, koMapping, ListView, PrimaryDescriptorsView, multicardResourceDescriptor) { | ||
// Get the parent component we want to inherit from from the ko registry. | ||
let parentComponent; | ||
const setParentComponent = (found) => { | ||
parentComponent = found; | ||
} | ||
ko.components.defaultLoader.getConfig('views/components/functions/primary-descriptors', setParentComponent); | ||
|
||
return ko.components.register('views/components/functions/multicard-resource-descriptor', { | ||
viewModel: function(params) { | ||
var self = this; | ||
parentComponent.viewModel.apply(this, arguments); | ||
|
||
this.parseNodeIdsFromStringTemplate = (initialValue) => { | ||
const regex = /<(.*?)>/g; | ||
const aliases = [...initialValue.matchAll(regex)].map(matchObj => matchObj[1]); | ||
return self.graph.nodes.filter(n => aliases.includes(n.alias)).map(n => n.nodeid); | ||
} | ||
|
||
this.selectedNodes = { | ||
name: ko.observableArray( | ||
self.parseNodeIdsFromStringTemplate(self.name.string_template()) | ||
), | ||
description: ko.observableArray( | ||
self.parseNodeIdsFromStringTemplate(self.description.string_template()) | ||
), | ||
map_popup: ko.observableArray( | ||
self.parseNodeIdsFromStringTemplate(self.map_popup.string_template()) | ||
), | ||
}; | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort | ||
const sortedCards = this.graph.cards.toSorted((a, b) => { | ||
const nameA = a.name.toUpperCase(); | ||
const nameB = b.name.toUpperCase(); | ||
if (nameA < nameB) { | ||
return -1; | ||
} | ||
if (nameA > nameB) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
}); | ||
|
||
this.groupedNodesForSelect2 = []; | ||
sortedCards.forEach(card => { | ||
const stringNodes = this.graph.nodes.filter( | ||
node => node.datatype === 'string' && node.nodegroup_id === card.nodegroup_id | ||
); | ||
|
||
if (stringNodes.length) { | ||
this.groupedNodesForSelect2.push({ | ||
text: card.name, | ||
children: stringNodes.map(node => { | ||
return { | ||
id: node.nodeid, | ||
text: node.alias, | ||
} | ||
}), | ||
}); | ||
} | ||
}); | ||
|
||
Object.entries(this.selectedNodes).forEach( | ||
([observableName, observable]) => { | ||
observable.subscribe(actions => { | ||
actions.forEach(action => { | ||
self.updateTemplate(action.value, action.status, observableName) | ||
}) | ||
}, this, 'arrayChange') | ||
} | ||
); | ||
|
||
this.baseSelect2Config = { | ||
multiple: true, | ||
placeholder: arches.translations.selectPrimaryDescriptionIdentifierCard, | ||
data: self.groupedNodesForSelect2, | ||
}; | ||
|
||
this.updateTemplate = (nodeid, actionType, descriptorName) => { | ||
const templateObservable = params.config.descriptor_types[descriptorName].string_template; | ||
const priorValue = templateObservable(); | ||
const nodeAlias = self.graph.nodes.find(n => n.nodeid === nodeid).alias; | ||
|
||
if (actionType === 'deleted') { | ||
if (priorValue.startsWith(`<${nodeAlias}>`)) { | ||
templateObservable(priorValue.replace(`<${nodeAlias}>`, '')); | ||
} else { | ||
templateObservable(priorValue.replace(` <${nodeAlias}>`, '')); | ||
} | ||
} else { | ||
if (priorValue === '') { | ||
templateObservable(`<${nodeAlias}>`); | ||
} else { | ||
templateObservable(`${priorValue} <${nodeAlias}>`); | ||
} | ||
} | ||
}; | ||
}, | ||
template: multicardResourceDescriptor, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
arches_for_science/migrations/0004_improve_primary_descriptor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Generated by Django 4.2.4 on 2023-11-20 13:43 | ||
|
||
from django.db import migrations | ||
|
||
|
||
STRING_TEMPLATE_REPLACEMENTS = { | ||
"Statement_content": "statement_content", | ||
"Name_content": "name_content", | ||
"Name (top)_content": "name_top__content", | ||
"<content >": "<statement_content>", | ||
} | ||
STRING_TEMPLATE_REPLACEMENTS_REVERSED = {v: k for k, v in STRING_TEMPLATE_REPLACEMENTS.items()} | ||
|
||
ORIGINAL_DESCRIPTOR_FUNCTION_PK = "60000000-0000-0000-0000-000000000001" | ||
MULTICARD_DESCRIPTOR_FUNCTION_PK = "00b2d15a-fda0-4578-b79a-784e4138664b" | ||
|
||
|
||
def update_graphs(apps, from_function, to_function, string_replacement_map): | ||
FunctionXGraph = apps.get_model("models", "FunctionXGraph") | ||
|
||
for fn_x_graph in FunctionXGraph.objects.filter(function=from_function): | ||
fn_x_graph.function = to_function | ||
for descriptor_object in fn_x_graph.config["descriptor_types"].values(): | ||
for before, after in string_replacement_map.items(): | ||
descriptor_object["string_template"] = descriptor_object["string_template"].replace(before, after) | ||
fn_x_graph.save() | ||
|
||
|
||
def retire_core_primary_descriptor(apps, schema_editor): | ||
Function = apps.get_model("models", "Function") | ||
original_fn = Function.objects.get(pk=ORIGINAL_DESCRIPTOR_FUNCTION_PK) | ||
original_fn.functiontype = "primarydescriptors_retired" | ||
original_fn.name = "(retired) Define Resource Descriptors" | ||
original_fn.save() | ||
|
||
# create multi-card function | ||
multi_card_fn, created = Function.objects.update_or_create( | ||
pk=MULTICARD_DESCRIPTOR_FUNCTION_PK, | ||
modulename="multicard_resource_descriptor.py", | ||
classname="MulticardResourceDescriptor", | ||
functiontype="primarydescriptors", | ||
name="Multi-card Resource Descriptor", | ||
description="Configure the name, description, and map popup of a resource", | ||
defaultconfig=original_fn.defaultconfig, | ||
component="views/components/functions/multicard-resource-descriptor", | ||
) | ||
|
||
update_graphs(apps, from_function=original_fn, to_function=multi_card_fn, | ||
string_replacement_map=STRING_TEMPLATE_REPLACEMENTS) | ||
|
||
|
||
def restore_core_primary_descriptor(apps, schema_editor): | ||
Function = apps.get_model("models", "Function") | ||
original_fn = Function.objects.get(pk=ORIGINAL_DESCRIPTOR_FUNCTION_PK) | ||
original_fn.functiontype = "primarydescriptors" | ||
original_fn.name = "Define Resource Descriptors" | ||
original_fn.save() | ||
|
||
# Update FunctionXGraph records before cascade deleted below | ||
update_graphs(apps, from_function=multi_card_fn, to_function=original_fn, | ||
string_replacement_map=STRING_TEMPLATE_REPLACEMENTS_REVERSED) | ||
|
||
multi_card_fn = Function.objects.get(pk=MULTICARD_DESCRIPTOR_FUNCTION_PK) | ||
multi_card_fn.delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("arches_for_science", "0003_manifest_x_canvas_x_digitalresource"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
retire_core_primary_descriptor, | ||
restore_core_primary_descriptor, | ||
) | ||
] |
64 changes: 64 additions & 0 deletions
64
arches_for_science/migrations/0005_deploy_triggers_for_primary_descriptor.py
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
arches_for_science/pkg/extensions/functions/multicard_resource_descriptor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from arches.app.functions.primary_descriptors import AbstractPrimaryDescriptorsFunction | ||
from arches.app.models.system_settings import settings | ||
|
||
from django.utils.translation import get_language, gettext as _ | ||
|
||
# This duplicates the configuration declared in migration 0004, | ||
# but on first package load, the function will be re-registered, because | ||
# the .py file has not yet been placed in the destination folder. | ||
# Re-registration will overwrite whatever the migration inserted. | ||
details = { | ||
"functionid": "00b2d15a-fda0-4578-b79a-784e4138664b", | ||
"name": "Multi-card Resource Descriptor", | ||
"type": "primarydescriptors", | ||
"description": "Configure the name, description, and map popup of a resource", | ||
"defaultconfig": { | ||
"descriptor_types": { | ||
"name": { | ||
"nodegroup_id": "", | ||
"string_template": "", | ||
}, | ||
"map_popup": { | ||
"nodegroup_id": "", | ||
"string_template": "", | ||
}, | ||
"description": { | ||
"nodegroup_id": "", | ||
"string_template": "", | ||
}, | ||
} | ||
}, | ||
"classname": "MulticardResourceDescriptor", | ||
"component": "views/components/functions/multicard-resource-descriptor", | ||
} | ||
|
||
|
||
class MulticardResourceDescriptor(AbstractPrimaryDescriptorsFunction): | ||
"""Implemented in the database via triggers on tiles table. | ||
This implementation just fetches the calculated result from the db.""" | ||
|
||
def get_primary_descriptor_from_nodes(self, resource, config, context=None, descriptor=None): | ||
result = "" | ||
requested_language = context.get("language", None) if context else None | ||
|
||
lookup_language = requested_language or get_language() or settings.LANGUAGE_CODE | ||
try: | ||
result = resource.descriptors[lookup_language][descriptor] | ||
except KeyError: | ||
pass | ||
|
||
if result.strip() == "": | ||
result = _("Undefined") | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.