Skip to content

Commit

Permalink
Merge pull request #3170 from SEED-platform/2831-fix/delete-analysis-…
Browse files Browse the repository at this point in the history
…with-property

2831 fix/delete analysis with property
  • Loading branch information
nllong committed Mar 18, 2022
2 parents 1f65433 + f0dc392 commit d51e784
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 6 deletions.
Binary file modified locale/en_US/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 6 additions & 0 deletions locale/en_US/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ msgstr "Add or remove labels from {num, plural, one{1 selected property} other{#
msgid "ADD_REMOVE_LABELS_FROM_TAXLOTS"
msgstr "Add or remove labels from {num, plural, one{1 selected tax lot} other{# selected tax lots}}"

msgid "ALSO_DELETE_ANALYSES"
msgstr "Also delete {num,plural, one{1 analysis} other{# analyses}}."

msgid "ALSO_DELETE_BATCH_ANALYSES"
msgstr "Also delete {num,plural, one{1 associated batch analysis} other{# associated batch analyses}}."

msgid "AND"
msgstr "AND"

Expand Down
Binary file modified locale/fr_CA/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 6 additions & 0 deletions locale/fr_CA/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ msgstr "Ajouter ou supprimer des étiquettes de {num, plural, one{1 propriété
msgid "ADD_REMOVE_LABELS_FROM_TAXLOTS"
msgstr "Ajouter ou supprimer des étiquettes de {num, plural, one{1 lot d'impôt sélectionné} other{# lots d'impôt sélectionnés}}"

msgid "ALSO_DELETE_ANALYSES"
msgstr "Supprimez également {num,plural, one{ 1 analyse } other{# analyses }} ."

msgid "ALSO_DELETE_BATCH_ANALYSES"
msgstr "Supprimez également {num,plural, one{ 1 analyse de lot associée } other{# analyses de lot associées }} ."

msgid "AND"
msgstr "ET"

Expand Down
2 changes: 1 addition & 1 deletion seed/models/analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_property_view_info(self, property_id=None):
analysis_property_views = self.analysispropertyview_set

return {
'number_of_analysis_property_views': analysis_property_views.count(),
'number_of_analysis_property_views': self.analysispropertyview_set.count(),
'views': list(analysis_property_views.values_list('id', flat=True).distinct()),
'cycles': list(analysis_property_views.values_list('cycle', flat=True).distinct())
}
Expand Down
26 changes: 25 additions & 1 deletion seed/static/seed/js/controllers/delete_modal_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,32 @@ angular.module('BE.seed.controller.delete_modal', [])
'$q',
'$uibModalInstance',
'inventory_service',
'analyses_service',
'property_view_ids',
'taxlot_view_ids',
function ($scope, $q, $uibModalInstance, inventory_service, property_view_ids, taxlot_view_ids) {
function ($scope, $q, $uibModalInstance, inventory_service, analyses_service, property_view_ids, taxlot_view_ids) {
$scope.property_view_ids = _.uniq(property_view_ids);
$scope.taxlot_view_ids = _.uniq(taxlot_view_ids);
$scope.delete_state = 'delete';
$scope.delete = {'analyses': true, 'batch_analyses': false};

$scope.generating_analysis_info = true;
let analysis_ids = [];
let batch_analysis_ids = [];
inventory_service.get_canonical_properties($scope.property_view_ids).then(function (inventory_data) {
analyses_service.get_analyses_for_canonical_properties(inventory_data.properties).then(function (results) {
for (i in results.analyses) {
if (results.analyses[i].number_of_analysis_property_views > 1) {
batch_analysis_ids.push(results.analyses[i].id);
} else {
analysis_ids.push(results.analyses[i].id);
}
}
$scope.analysis_ids = _.uniq(analysis_ids);
$scope.batch_analysis_ids = _.uniq(batch_analysis_ids);
$scope.generating_analysis_info = false;
});
});

$scope.delete_inventory = function () {
$scope.delete_state = 'prepare';
Expand All @@ -22,11 +42,14 @@ angular.module('BE.seed.controller.delete_modal', [])

if ($scope.property_view_ids.length) promises.push(inventory_service.delete_property_states($scope.property_view_ids));
if ($scope.taxlot_view_ids.length) promises.push(inventory_service.delete_taxlot_states($scope.taxlot_view_ids));
if ($scope.delete.analyses) for (i in $scope.analysis_ids) promises.push(analyses_service.delete_analysis($scope.analysis_ids[i]));
if ($scope.delete.batch_analyses) for (i in $scope.batch_analysis_ids) promises.push(analyses_service.delete_analysis($scope.batch_analysis_ids[i]));

return $q.all(promises).then(function (results) {
$scope.deletedProperties = 0;
$scope.deletedTaxlots = 0;
_.forEach(results, function (result, index) {
if (!result.data) return;
if (result.data.status === 'success') {
if (index === 0 && $scope.property_view_ids.length) $scope.deletedProperties = result.data.properties;
else $scope.deletedTaxlots = result.data.taxlots;
Expand All @@ -39,6 +62,7 @@ angular.module('BE.seed.controller.delete_modal', [])
}
$scope.delete_state = 'success';
}).catch(function (resp) {
console.log('resp', resp);
$scope.delete_state = 'fail';
if (resp.status === 422) {
$scope.error = resp.data.message;
Expand Down
12 changes: 12 additions & 0 deletions seed/static/seed/js/services/analyses_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ angular.module('BE.seed.service.analyses', [])
});
};

const get_analyses_for_canonical_properties = function (property_ids) {
const org = user_service.get_organization().id;
return $http.post('/api/v3/analyses/get_analyses_for_properties/', { property_ids: property_ids }, {
params: { organization_id: org }
}).then(function (response) {
return response.data;
}).catch(function (response) {
return response.data;
});
};

const get_analyses_for_canonical_property = function (property_id) {
const org = user_service.get_organization().id;
return $http.get('/api/v3/analyses/?organization_id=' + org + '&property_id=' + property_id).then(function (response) {
Expand Down Expand Up @@ -219,6 +230,7 @@ angular.module('BE.seed.service.analyses', [])
const analyses_factory = {
get_analyses_for_org: get_analyses_for_org,
get_analyses_for_canonical_property: get_analyses_for_canonical_property,
get_analyses_for_canonical_properties: get_analyses_for_canonical_properties,
get_analysis_for_org: get_analysis_for_org,
get_analysis_messages_for_org: get_analysis_messages_for_org,
get_analysis_views_for_org: get_analysis_views_for_org,
Expand Down
10 changes: 10 additions & 0 deletions seed/static/seed/js/services/inventory_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ angular.module('BE.seed.service.inventory', []).factory('inventory_service', [
});
};

inventory_service.get_canonical_properties = function (view_ids) {
return $http.post('/api/v3/properties/get_canonical_properties/', { view_ids: view_ids }, {
params: { organization_id: user_service.get_organization().id }
}).then(function (response) {
return response.data;
}).catch(function (response) {
return response.data;
});
};

inventory_service.get_property = function (view_id) {
// Error checks
if (_.isNil(view_id)) {
Expand Down
2 changes: 2 additions & 0 deletions seed/static/seed/locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"ADD_FILES_TO": "Add files to {dataset_name}.",
"ADD_REMOVE_LABELS_FROM_PROPERTIES": "Add or remove labels from {num, plural, one{1 selected property} other{# selected properties}}",
"ADD_REMOVE_LABELS_FROM_TAXLOTS": "Add or remove labels from {num, plural, one{1 selected tax lot} other{# selected tax lots}}",
"ALSO_DELETE_ANALYSES": "Also delete {num,plural, one{1 analysis} other{# analyses}}.",
"ALSO_DELETE_BATCH_ANALYSES": "Also delete {num,plural, one{1 associated batch analysis} other{# associated batch analyses}}.",
"AND": "AND",
"API Documentation": "API Documentation",
"API Key": "API Key",
Expand Down
2 changes: 2 additions & 0 deletions seed/static/seed/locales/fr_CA.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"ADD_FILES_TO": "Ajoutez des fichiers à {dataset_name}.",
"ADD_REMOVE_LABELS_FROM_PROPERTIES": "Ajouter ou supprimer des étiquettes de {num, plural, one{1 propriété sélectionnée} other{# propriétés sélectionnées}}",
"ADD_REMOVE_LABELS_FROM_TAXLOTS": "Ajouter ou supprimer des étiquettes de {num, plural, one{1 lot d'impôt sélectionné} other{# lots d'impôt sélectionnés}}",
"ALSO_DELETE_ANALYSES": "Supprimez également {num,plural, one{ 1 analyse } other{# analyses }} .",
"ALSO_DELETE_BATCH_ANALYSES": "Supprimez également {num,plural, one{ 1 analyse de lot associée } other{# analyses de lot associées }} .",
"AND": "ET",
"API Documentation": "Documentation de l'API",
"API Key": "Clé API",
Expand Down
16 changes: 13 additions & 3 deletions seed/static/seed/partials/delete_modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@ <h4 class="modal-title" id="exportModalLabel" ng-show="delete_state === 'incompl
<h4 class="modal-title" id="exportModalLabel" ng-show="delete_state === 'fail'" translate>Failed to delete inventory</h4>
</div>
<div class="modal-body row" style="padding-bottom: 0;">
<div class="col-sm-12" ng-show="delete_state === 'delete'"
translate="OK_TO_DELETE_NUM_PROPERTIES_AND_TAXLOTS"
translate-values="{ num_properties: property_view_ids.length, num_taxlots: taxlot_view_ids.length }">
<div ng-show="delete_state === 'delete'">
<p class="col-sm-12" translate="OK_TO_DELETE_NUM_PROPERTIES_AND_TAXLOTS" translate-values="{num_properties: property_view_ids.length, num_taxlots: taxlot_view_ids.length}"></p>
<div class="col-sm-12" ng-show="generating_analysis_info">
Collecting analysis info...
</div>
<div class="col-sm-12" ng-if="analysis_ids.length > 0" ng-show="!generating_analysis_info">
<label for="input_delete_analyses" translate="ALSO_DELETE_ANALYSES" translate-values="{num: analysis_ids.length}"></label>
<input type="checkbox" id="input_delete_analyses" ng-checked="delete.analyses" ng-model="delete.analyses"/>
</div>
<div class="col-sm-12" ng-if="batch_analysis_ids.length > 0">
<label for="input_delete_batch_analyses" translate="ALSO_DELETE_BATCH_ANALYSES" translate-values="{num: batch_analysis_ids.length}"></label>
<input type="checkbox" id="input_delete_batch_analyses" ng-checked="delete.batch_analyses" ng-model="delete.batch_analyses"/>
</div>
</div>
<div class="col-sm-12" ng-show="delete_state === 'incomplete' || delete_state === 'success'">
<div ng-if="(deletedProperties + deletedTaxlots) > 0"
Expand Down
2 changes: 1 addition & 1 deletion seed/tests/test_analyses_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_list_with_property(self):

analysis_b = next((x for x in result['analyses'] if x['id'] == self.analysis_b.id), None)
self.assertIsNotNone(analysis_b)
self.assertEqual(analysis_b['number_of_analysis_property_views'], 1)
self.assertEqual(analysis_b['number_of_analysis_property_views'], 2)
self.assertEqual(len(analysis_b['cycles']), 1)

def test_list_organization_missing(self):
Expand Down
41 changes: 41 additions & 0 deletions seed/views/v3/analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
from seed.utils.api import api_endpoint_class, OrgMixin
from seed.utils.api_schema import AutoSchemaHelper

import logging
logger = logging.getLogger(__name__)


class CreateAnalysisSerializer(AnalysisSerializer):
property_view_ids = serializers.ListField(child=serializers.IntegerField(), allow_empty=False)
Expand Down Expand Up @@ -109,6 +112,7 @@ def list(self, request):
.distinct()
.order_by('-id')
)
logger.error(f'--- {analyses_queryset.query}')
else:
analyses_queryset = (
Analysis.objects.filter(organization=organization_id)
Expand All @@ -125,6 +129,43 @@ def list(self, request):
'analyses': analyses
})

@swagger_auto_schema(manual_parameters=[AutoSchemaHelper.query_org_id_field(True)])
@require_organization_id_class
@api_endpoint_class
@ajax_request_class
@has_perm_class('requires_member')
@action(detail=False, methods=['post'])
def get_analyses_for_properties(self, request):
"""
List all the analyses associated with provided canonical property ids
---
parameters:
- name: organization_id
description: The organization_id for this user's organization
required: true
paramType: query
- name: property_ids
description: List of canonical property ids
paramType: body
"""
property_ids = request.data.get('property_ids', [])
organization_id = int(self.get_organization(request))
analyses = []
analyses_queryset = (
Analysis.objects.filter(organization=organization_id, analysispropertyview__property__in=property_ids)
.distinct()
.order_by('-id')
)
for analysis in analyses_queryset:
serialized_analysis = AnalysisSerializer(analysis).data
serialized_analysis.update(analysis.get_property_view_info(None))
serialized_analysis.update({'highlights': analysis.get_highlights(None)})
analyses.append(serialized_analysis)
return JsonResponse({
'status': 'success',
'analyses': analyses
})

@swagger_auto_schema(manual_parameters=[AutoSchemaHelper.query_org_id_field(True)])
@require_organization_id_class
@api_endpoint_class
Expand Down
29 changes: 29 additions & 0 deletions seed/views/v3/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
update_result_with_master)
from seed.utils.inventory_filter import get_filtered_results

import logging
logger = logging.getLogger(__name__)

# Global toggle that controls whether or not to display the raw extra
# data fields in the columns returned for the view.
DISPLAY_RAW_EXTRADATA = True
Expand Down Expand Up @@ -935,6 +938,32 @@ def retrieve(self, request, pk=None):
else:
return JsonResponse(result, status=status.HTTP_404_NOT_FOUND)

@swagger_auto_schema_org_query_param
@api_endpoint_class
@ajax_request_class
@has_perm_class('can_view_data')
@action(detail=False, methods=['post'])
def get_canonical_properties(self, request):
"""
List all the canonical properties associated with provided view ids
---
parameters:
- name: organization_id
description: The organization_id for this user's organization
required: true
paramType: query
- name: view_ids
description: List of property view ids
paramType: body
"""
view_ids = request.data.get('view_ids', [])
property_queryset = PropertyView.objects.filter(id__in=view_ids).distinct()
property_ids = list(property_queryset.values_list('property_id', flat=True))
return JsonResponse({
'status': 'success',
'properties': property_ids
})

@swagger_auto_schema(
manual_parameters=[AutoSchemaHelper.query_org_id_field()],
request_body=UpdatePropertyPayloadSerializer,
Expand Down

0 comments on commit d51e784

Please sign in to comment.