Skip to content

Commit

Permalink
Added label integrity checking for sub-organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
axelstudios committed May 24, 2019
1 parent 56a0fcb commit acd24b3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion seed/management/commands/add_member_to_org.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
:copyright (c) 2014 - 2018, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Department of Energy) and contributors. All rights reserved. # NOQA
:copyright (c) 2014 - 2019, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Department of Energy) and contributors. All rights reserved. # NOQA
:author
"""
from django.core.management.base import BaseCommand
Expand Down
18 changes: 10 additions & 8 deletions seed/models/data_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,27 +927,29 @@ def update_status_label(self, label_class, rule, linked_id):
label_org_id = rule.status_label.super_organization_id

if rule.table_name == 'PropertyState':
property_org_id = Property.objects.get(pk=linked_id).organization_id
if property_org_id == label_org_id:
property_parent_org_id = Property.objects.get(pk=linked_id).organization.get_parent().id
if property_parent_org_id == label_org_id:
label_class.objects.get_or_create(property_id=linked_id,
statuslabel_id=rule.status_label_id)
else:
raise IntegrityError(
'Label with org_id={} cannot be applied to a record with org_id={}.'.format(
'Label with super_organization_id={} cannot be applied to a property with parent '
'organization_id={}.'.format(
label_org_id,
property_org_id
property_parent_org_id
)
)
else:
taxlot_org_id = TaxLot.objects.get(pk=linked_id).organization_id
if taxlot_org_id == label_org_id:
taxlot_parent_org_id = TaxLot.objects.get(pk=linked_id).organization.get_parent().id
if taxlot_parent_org_id == label_org_id:
label_class.objects.get_or_create(taxlot_id=linked_id,
statuslabel_id=rule.status_label_id)
else:
raise IntegrityError(
'Label with org_id={} cannot be applied to a record with org_id={}.'.format(
'Label with super_organization_id={} cannot be applied to a taxlot with parent '
'organization_id={}.'.format(
label_org_id,
taxlot_org_id
taxlot_parent_org_id
)
)
return True
Expand Down
7 changes: 4 additions & 3 deletions seed/utils/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ def json_serializer(obj):
def compare_orgs_between_label_and_target(sender, pk_set, instance, model, action, **kwargs):
for id in pk_set:
label = model.objects.get(pk=id)
if instance.organization_id != label.super_organization_id:
if instance.organization.get_parent().id != label.super_organization_id:
raise IntegrityError(
'Label with org_id={} cannot be applied to a record with org_id={}.'.format(
'Label with super_organization_id={} cannot be applied to a record with parent '
'organization_id={}.'.format(
label.super_organization_id,
instance.organization_id
instance.organization.get_parent().id
)
)
14 changes: 8 additions & 6 deletions seed/views/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ def label_factory(self, inventory_type, label_id, inventory_id):
Model = self.models[inventory_type]

# Ensure the the label org and inventory org are the same
inventory_org_id = getattr(Model, inventory_type).get_queryset().get(pk=inventory_id).organization_id
label_org_id = Model.statuslabel.get_queryset().get(pk=label_id).super_organization_id
if inventory_org_id == label_org_id:
inventory_parent_org_id = getattr(Model, inventory_type).get_queryset().get(pk=inventory_id).organization\
.get_parent().id
label_super_org_id = Model.statuslabel.get_queryset().get(pk=label_id).super_organization_id
if inventory_parent_org_id == label_super_org_id:
create_dict = {
'statuslabel_id': label_id,
"{}_id".format(inventory_type): inventory_id
Expand All @@ -190,9 +191,10 @@ def label_factory(self, inventory_type, label_id, inventory_id):
return Model(**create_dict)
else:
raise IntegrityError(
'Label with org_id={} cannot be applied to a record with org_id={}.'.format(
label_org_id,
inventory_org_id
'Label with super_organization_id={} cannot be applied to a record with parent '
'organization_id={}.'.format(
label_super_org_id,
inventory_parent_org_id
)
)

Expand Down

0 comments on commit acd24b3

Please sign in to comment.