Skip to content

Commit

Permalink
Delete alis with als (#4543)
Browse files Browse the repository at this point in the history
* Delete alis with als

* add translation, make label clickable

---------

Co-authored-by: kflemin <[email protected]>
  • Loading branch information
haneslinger and kflemin authored Feb 26, 2024
1 parent 148a432 commit af6c88c
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 19 deletions.
Binary file modified locale/en_US/LC_MESSAGES/django.mo
Binary file not shown.
3 changes: 3 additions & 0 deletions locale/en_US/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ msgstr "Need to configure your Program?"
msgid "CONFIRMING_DELETE_PROFILE"
msgstr "Are you sure you want to delete the profile"

msgid "CONFIRM_ACCESS_LEVEL_DELETE"
msgstr "Are you sure you want to delete this access level? This action will also delete {num_access_levels} access level instances."

msgid "CONFIRM_AND_START_MATCHING"
msgstr "Confirm mappings &amp; start matching"

Expand Down
Binary file modified locale/fr_CA/LC_MESSAGES/django.mo
Binary file not shown.
3 changes: 3 additions & 0 deletions locale/fr_CA/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ msgstr "Besoin de configurer votre programme?"
msgid "CONFIRMING_DELETE_PROFILE"
msgstr "Êtes-vous sûr de vouloir supprimer le profil"

msgid "CONFIRM_ACCESS_LEVEL_DELETE"
msgstr "Êtes-vous sûr de vouloir supprimer ce niveau d'accès? Cette action supprimera également {num_access_levels} instances de niveau d'accès."

msgid "CONFIRM_AND_START_MATCHING"
msgstr "Confirmer et commencer l'appariement"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ angular.module('BE.seed.controller.organization_access_level_tree', [])
backdrop: 'static',
resolve: {
org_id: () => $scope.org.id,
current_access_level_names: () => $scope.access_level_names
current_access_level_names: () => $scope.access_level_names,
access_level_tree: () => $scope.access_level_tree,
}
}).result.then(() => {
spinner_utility.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ angular.module('BE.seed.controller.organization_add_access_level_modal', [])
'organization_service',
'org_id',
'current_access_level_names',
'access_level_tree',
'Notification',
// eslint-disable-next-line func-names
function (
Expand All @@ -19,11 +20,53 @@ angular.module('BE.seed.controller.organization_add_access_level_modal', [])
organization_service,
org_id,
current_access_level_names,
access_level_tree,
Notification
) {
$scope.new_access_level_names = angular.copy(current_access_level_names);

$scope.is_modified = () => !_.isEqual(current_access_level_names, $scope.new_access_level_names);
$scope.num_alis_to_delete = 0;
$scope.enable_save = true;

/*
Build out access_level_instances_by_depth recursively
We need this to tell how many alis deleting a level would delete
*/
const access_level_instances_by_depth = {};
const calculate_access_level_instances_by_depth = function (tree, depth = 1) {
if (tree == undefined) return;
if (access_level_instances_by_depth[depth] == undefined) access_level_instances_by_depth[depth] = [];
tree.forEach((ali) => {
access_level_instances_by_depth[depth].push({ id: ali.id, name: ali.data.name });
calculate_access_level_instances_by_depth(ali.children, depth + 1);
});
};

const get_num_alis_at_depth = function(depth) {
if (Object.keys(access_level_instances_by_depth).length === 0){
calculate_access_level_instances_by_depth(access_level_tree, 1);
}
return access_level_instances_by_depth[depth]?.length ?? 0
}

// A deleted level may mean deleting part of the tree itself
check_for_deletions = () => {
// if no levels are being deleted, no warning
$scope.enable_save = true;
num_levels_to_delete = current_access_level_names.length - $scope.new_access_level_names.length;
if (num_levels_to_delete <= 0 ) {
return
}

// Get num alis that will be deleted
depths_to_delete = _.range(current_access_level_names.length-num_levels_to_delete+1, current_access_level_names.length+1)
$scope.num_alis_to_delete = depths_to_delete.reduce((acc, curr) => acc + get_num_alis_at_depth(curr), 0)

// if no alis are being deleted, no warning
if ($scope.num_alis_to_delete > 0){
$scope.enable_save = false;
}
};

$scope.save_access_level_names = () => {
organization_service.update_organization_access_level_names(org_id, $scope.new_access_level_names)
Expand All @@ -36,10 +79,12 @@ angular.module('BE.seed.controller.organization_add_access_level_modal', [])

$scope.remove_level = () => {
$scope.new_access_level_names.pop();
check_for_deletions();
};

$scope.add_level = () => {
$scope.new_access_level_names.push('');
check_for_deletions();
};

$scope.cancel = () => {
Expand Down
1 change: 1 addition & 0 deletions seed/static/seed/locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
"COMPLETE_AND_REFRESH": "Complete and Refresh Page",
"CONFIGURE_PROGRAM": "Need to configure your Program?",
"CONFIRMING_DELETE_PROFILE": "Are you sure you want to delete the profile",
"CONFIRM_ACCESS_LEVEL_DELETE": "Are you sure you want to delete this access level? This action will also delete {num_access_levels} access level instances.",
"CONFIRM_AND_START_MATCHING": "Confirm mappings &amp; start matching",
"CONTINUE": "Continue",
"CONTINUE_QUESTION": "Are you sure you want to continue?",
Expand Down
1 change: 1 addition & 0 deletions seed/static/seed/locales/fr_CA.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
"COMPLETE_AND_REFRESH": "Complétez et Actualisez la Page",
"CONFIGURE_PROGRAM": "Besoin de configurer votre programme?",
"CONFIRMING_DELETE_PROFILE": "Êtes-vous sûr de vouloir supprimer le profil",
"CONFIRM_ACCESS_LEVEL_DELETE": "Êtes-vous sûr de vouloir supprimer ce niveau d'accès? Cette action supprimera également {num_access_levels} instances de niveau d'accès.",
"CONFIRM_AND_START_MATCHING": "Confirmer et commencer l'appariement",
"CONTINUE": "Continuer",
"CONTINUE_QUESTION": "Es-tu sur de vouloir continuer?",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ <h4>{$:: 'Add Levels to the Organizational Structure Tree' | translate $}</h4>
</div>
</div>
<div class="modal-footer">
<span ng-show="num_alis_to_delete > 0" style="display: flex; align-items:flex-start">
<input type="checkbox" ng-model="enable_save" id="enable-save" name="enable-save"/>
<label for="enable-save" translate="CONFIRM_ACCESS_LEVEL_DELETE" translate-values="{num_access_levels: num_alis_to_delete}" class="confirm-label"></label>
</span>
<button type="button" class="btn btn-default" ng-click="cancel()" translate>Cancel</button>
<button type="button" class="btn btn-primary" ng-click="save_access_level_names()" ng-disabled="!is_modified()" translate>Save</button>
<button type="button" class="btn btn-primary" ng-click="save_access_level_names()" ng-disabled="!is_modified() || !enable_save" translate>Save</button>
</div>
4 changes: 4 additions & 0 deletions seed/static/seed/scss/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5448,6 +5448,10 @@ tags-input .tags .tag-item {
padding: 10px;
color: #c24e00;
}
.confirm-label {
padding-left: 5px;
text-align: left;
}
.cell-pass {
background-color: #d0e6d4;
border-top: 1px solid #d4d4d4;
Expand Down
14 changes: 4 additions & 10 deletions seed/tests/test_organization_access_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_edit_access_level_names_column_names(self):
)
assert raw_result.status_code == 400

def test_edit_access_level_names_too_few(self):
def test_edit_access_level_names_delete_root(self):
# get try to clear access_level_names
url = reverse_lazy('api:v3:organization-access_levels-access-level-names', args=[self.org.id])
raw_result = self.client.post(
Expand All @@ -104,24 +104,18 @@ def test_edit_access_level_names_too_few(self):
)
assert raw_result.status_code == 400

def test_edit_access_level_names_delete_level(self):
# get try to add too few levels
url = reverse_lazy('api:v3:organization-access_levels-access-level-names', args=[self.org.id])
raw_result = self.client.post(
url,
data=json.dumps({"access_level_names": ["just one"]}),
content_type='application/json',
)
assert raw_result.status_code == 400

# adding multiple works
url = reverse_lazy('api:v3:organization-access_levels-access-level-names', args=[self.org.id])
raw_result = self.client.post(
url,
data=json.dumps({"access_level_names": ["one", "two"]}),
content_type='application/json',
)
assert raw_result.status_code == 200
assert Organization.objects.get(pk=self.org.id).access_level_names == ["one", "two"]
assert AccessLevelInstance.objects.count() == 1
assert AccessLevelInstance.objects.first().name == "root"

def test_edit_access_level_names_duplicates(self):
url = reverse_lazy('api:v3:organization-access_levels-access-level-names', args=[self.org.id])
Expand Down
17 changes: 11 additions & 6 deletions seed/views/v3/access_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import xlrd
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Max
from django.http import JsonResponse
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status, viewsets
Expand Down Expand Up @@ -190,14 +189,20 @@ def access_level_names(self, request, organization_pk=None):
'status': 'error',
'message': 'Query param `access_level_names` must be a list of strings'
}, status=status.HTTP_400_BAD_REQUEST)

# assert access_level_names is deep enough
depth = AccessLevelInstance.objects.filter(organization=org).aggregate(max_depth=Max('depth')).get("max_depth", 0)
if len(new_access_level_names) < depth:
if len(new_access_level_names) < 1:
return JsonResponse({
'status': 'error',
'message': 'Query param `access_level_names` is shorter than depth of existing tree'
'message': 'There must be at least one access level.'
}, status=status.HTTP_400_BAD_REQUEST)
if any([n == "" for n in new_access_level_names]):
return JsonResponse({
'status': 'error',
'message': 'Access Level Instance may not be ""'
}, status=status.HTTP_400_BAD_REQUEST)

# delete alis at deleted depths
depth = len(new_access_level_names)
AccessLevelInstance.objects.filter(organization=org, depth__gt=depth).delete()

# save names
org.access_level_names = new_access_level_names
Expand Down

0 comments on commit af6c88c

Please sign in to comment.