Skip to content

Commit

Permalink
Add a tool to create root node in trees
Browse files Browse the repository at this point in the history
Fixes #5391
  • Loading branch information
CarolineDenis committed Nov 12, 2024
1 parent 1a84216 commit 4fb5a23
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
25 changes: 25 additions & 0 deletions specifyweb/frontend/js_src/lib/components/TreeView/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useBooleanState } from '../../hooks/useBooleanState';
import { useCachedState } from '../../hooks/useCachedState';
import { useId } from '../../hooks/useId';
import { treeText } from '../../localization/tree';
import { ping } from '../../utils/ajax/ping';
import type { GetSet, RA } from '../../utils/types';
import { toggleItem } from '../../utils/utils';
import { Button } from '../Atoms/Button';
Expand All @@ -14,6 +15,7 @@ import type {
FilterTablesByEndsWith,
SerializedResource,
} from '../DataModel/helperTypes';
import { idFromUrl } from '../DataModel/resource';
import { deserializeResource } from '../DataModel/serializers';
import { ResourceView } from '../Forms/ResourceView';
import { getPref } from '../InitialContext/remotePrefs';
Expand Down Expand Up @@ -106,6 +108,22 @@ export function Tree<
[baseUrl, statsThreshold]
);

const treeDefinition = treeDefinitionItems[0].treeDef;
const treeDefId = idFromUrl(treeDefinition);
const createRootNode = async (): Promise<void> => {
if (treeDefId === undefined) {
console.error('treeDefId is undefined');
} else {
await ping(
`/api/specify_tree/${tableName.toLowerCase()}/${treeDefId}/add_root/`,
{
method: 'POST',
headers: { Accept: 'application/json' },
}
);
}
};

return (
<div
className={`
Expand Down Expand Up @@ -196,6 +214,13 @@ export function Tree<
})}
</div>
</div>
{rows.length === 0 ? (
<Button.Icon
icon="plus"
title={treeText.addRootNode()}
onClick={createRootNode}
/>
) : undefined}
<ul role="tree rowgroup">
{rows.map((row, index) => (
<TreeRow
Expand Down
3 changes: 3 additions & 0 deletions specifyweb/frontend/js_src/lib/localization/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,9 @@ export const treeText = createDictionary({
addTree: {
'en-us': 'Add Tree',
},
addRootNode: {
'en-us': 'Add root node',
},
treePicker: {
'en-us': 'Tree Picker',
},
Expand Down
30 changes: 30 additions & 0 deletions specifyweb/specify/tree_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import wraps
import http
from typing import Literal, Tuple
from django.db import transaction
from django.http import HttpResponse
Expand Down Expand Up @@ -395,6 +396,35 @@ def repair_tree(request, tree: TREE_TABLE):
tree_extras.renumber_tree(table)
tree_extras.validate_tree_numbering(table)

@tree_mutation
def add_root(request, tree, treeid):
"Creates a root node in a specific tree."

tree_name = tree.title()
tree_target = get_object_or_404(f"{tree_name}treedef", id=treeid)
tree_def_item = getattr(models, f"{tree_name}treedefitem")
item = getattr(models, tree_name)

filter_kwargs = {
'rankid': 0,
'definition_id': treeid
# f"{tree}treedefid": treeid
}

if item.objects.filter(**filter_kwargs).count() > 0:
raise Exception("Root node already exists.")

root = item.objects.create(
name="Root",
isaccepted=1,
nodenumber=1,
rankid=0,
parent=None,
definition=tree_target,
definitionitem=tree_def_item
)

return http.HttpResponse('', status=204)

@login_maybe_required
@require_GET
Expand Down
1 change: 1 addition & 0 deletions specifyweb/specify/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
url(r'^(?P<rankid>\d+)/tree_rank_item_count/$', tree_views.tree_rank_item_count),
url(r'^(?P<parentid>\d+)/predict_fullname/$', tree_views.predict_fullname),
url(r'^(?P<treedef>\d+)/(?P<parentid>\w+)/stats/$', tree_views.tree_stats),
url(r'^(?P<treeid>\w+)/add_root/$', tree_views.add_root),
url(r'^(?P<treedef>\d+)/(?P<parentid>\w+)/(?P<sortfield>\w+)/$', tree_views.tree_view),
url(r'^repair/$', tree_views.repair_tree),
])),
Expand Down

0 comments on commit 4fb5a23

Please sign in to comment.