Skip to content

Commit

Permalink
Landmark widget: add "Normalize names" button to Landmarks tab
Browse files Browse the repository at this point in the history
This button will instruct the back-end to ensure all landmark names and
landmark group names don't have any leading or trailing whitespace.

See #2285
  • Loading branch information
tomka committed Mar 4, 2025
1 parent 0a45b2f commit 5c912ad
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ Landmark widget:
and the Landmark Layer. This makes indentifying problematic landmarks much
quicker.

- A "Normalize names" button is added to the Landmarks tab. This button will
instruct the back-end to ensure all landmark names and landmark group names
don't have any leading or trailing whitespace.

Stack viewer:

- WebGL layers can now define a so called blending window through the layer
Expand Down
43 changes: 43 additions & 0 deletions django/applications/catmaid/control/landmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1701,3 +1701,46 @@ def post(self, request:Request, project_id) -> Response:
'created_landmarks': n_created_landmarks,
'links': link_map
})


class LandmarkNameNormalizer(APIView):

@method_decorator(requires_user_role(UserRole.Admin))
def post(self, request:Request, project_id) -> Response:
"""Ensure all landmark names and landmark group names follow the same
rules: no whitespace before or after the name.
"""

classes = get_class_to_id_map(project_id)
relations = get_relation_to_id_map(project_id)
landmark_class = classes['landmark']
landmarkgroup_class = classes['landmarkgroup']
part_of_rel = relations['part_of']
annotated_with_rel = relations['annotated_with']

updated_landmarks = []
landmarks = ClassInstance.objects.filter(project_id=project_id,
class_column=landmark_class)
for landmark in landmarks:
trimmed_name = landmark.name.strip()
if trimmed_name != landmark.name:
landmark.name = trimmed_name
updated_landmarks.append(landmark)
landmark.save()

updated_groups = []
landmark_groups = ClassInstance.objects.filter(project_id=project_id,
class_column=landmarkgroup_class)
for group in landmark_groups:
trimmed_name = group.name.strip()
if trimmed_name != group.name:
group.name = trimmed_name
updated_groups.append(group)
group.save()

return Response({
'normalized_landmark_names': len(updated_landmarks),
'normalized_group_names': len(updated_groups),
'total_landmarks': len(landmarks),
'total_groups': len(landmark_groups),
})
22 changes: 22 additions & 0 deletions django/applications/catmaid/static/js/widgets/landmark-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,28 @@
target.update();
}
},
{
type: 'button',
label: 'Normalize names',
title: 'Ensure all landmarks and landmark groups in this project follow the same rules (no whitespace prefix/suffix). Requires project admin role for the active user.',
onclick: function() {
CATMAID.Landmarks.normalizeLandmarkAndGroupNames(project.id)
.then(response => {
if (response.normalized_landmark_names == 0 &&
response.normalized_group_names == 0) {
CATMAID.msg('All names already normalized', 'All landmark names and landmark group names are already normalized');
} else if (response.normalized_group_names == 0) {
CATMAID.msg("Success", `Normalized ${response.normalized_landmark_names} landmark names. All group names were already normalized.`);
} else if (response.normalized_landmark_names == 0) {
CATMAID.msg("Success", `Normalized ${response.normalized_group_names} group names. All landmark names were already normalized.`);
} else {
CATMAID.msg("Success", `Normalized ${response.normalized_landmark_names} landmark names and ${response.normalized_group_names} group names.`);
}
target.updateLandmarksAndGroups(project.id);
})
.catch(CATMAID.handleError);
}
},
{
type: 'child',
element: newLandmarkSection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,13 @@
return index;
},

/**
* Ensure all landmark names and group names are following the same rules.
* Most importantly: no whitespace before or after the name.
*/
normalizeLandmarkAndGroupNames(projectId) {
return CATMAID.fetch(`${projectId}/landmarks/normalize-names`,'POST');
},
};

/**
Expand Down
1 change: 1 addition & 0 deletions django/applications/catmaid/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@
# Landmarks
urlpatterns += [
re_path(rf'^(?P<project_id>{integer})/landmarks/$', landmarks.LandmarkList.as_view()),
re_path(rf'^(?P<project_id>{integer})/landmarks/normalize-names$', landmarks.LandmarkNameNormalizer.as_view()),
re_path(rf'^(?P<project_id>{integer})/landmarks/(?P<landmark_id>[0-9]+)/$', landmarks.LandmarkDetail.as_view()),
re_path(rf'^(?P<project_id>{integer})/landmarks/(?P<landmark_id>[0-9]+)/locations/$',
landmarks.LandmarkLocationList.as_view()),
Expand Down

0 comments on commit 5c912ad

Please sign in to comment.