Skip to content

Commit

Permalink
Merge pull request #5468 from specify/issue-5458
Browse files Browse the repository at this point in the history
Fix isPrimary for Consolidated COGs
  • Loading branch information
sharadsw authored Dec 13, 2024
2 parents c75cf54 + 88def41 commit bd12da7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
20 changes: 19 additions & 1 deletion specifyweb/businessrules/rules/cojo_rules.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import os
import sys
from enum import Enum

from specifyweb.businessrules.exceptions import BusinessRuleException
from specifyweb.businessrules.orm_signal_handler import orm_signal_handler
from specifyweb.specify.models import Collectionobjectgroupjoin

class COGType(Enum):
DISCRETE = "Discrete"
CONSOLIDATED = "Consolidated"
DRILL_CORE = "Drill Core"

def is_running_tests():
return any(module in sys.modules for module in ('pytest', 'unittest'))

Expand Down Expand Up @@ -44,4 +51,15 @@ def cojo_pre_save(cojo):
and cojo.childco.cojo.id is not cojo.id
and not is_running_tests()
):
raise BusinessRuleException('ChildCo is already in use as a child in another COG.')
raise BusinessRuleException('ChildCo is already in use as a child in another COG.')

@orm_signal_handler('post_save', 'Collectionobjectgroupjoin')
def cojo_post_save(cojo):
"""
For Consolidated COGs, mark the first CO child as primary if none have been set by the user
"""
co_children = Collectionobjectgroupjoin.objects.filter(parentcog=cojo.parentcog, childco__isnull=False)
if len(co_children) > 0 and not co_children.filter(isprimary=True).exists() and cojo.parentcog.cogtype.type == COGType.CONSOLIDATED.value:
first_child = co_children.first()
first_child.isprimary = True
first_child.save()
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,26 @@ export const businessRuleDefs: MappedBusinessRuleDefs = {
};
},
},
onAdded: (CollectionObjectGroupJoin) => {
onAdded: (cojo, collection) => {
if (
CollectionObjectGroupJoin.get('childCog') ===
CollectionObjectGroupJoin.get('parentCog') &&
typeof CollectionObjectGroupJoin.get('childCog') === 'string' &&
typeof CollectionObjectGroupJoin.get('parentCog') === 'string'
cojo.get('childCog') === cojo.get('parentCog') &&
typeof cojo.get('childCog') === 'string' &&
typeof cojo.get('parentCog') === 'string'
) {
setSaveBlockers(
CollectionObjectGroupJoin,
CollectionObjectGroupJoin.specifyTable.field.childCog,
cojo,
cojo.specifyTable.field.childCog,
[resourcesText.cogAddedToItself()],
COG_TOITSELF
);
}

// Trigger Consolidated COGs field check when a child is added
if (collection?.related?.specifyTable === tables.CollectionObjectGroup) {
const cog =
collection.related as SpecifyResource<CollectionObjectGroup>;
cog.businessRuleManager?.checkField('cogType');
}
},
onRemoved(_, collection) {
// Trigger Consolidated COGs field check when a child is deleted
Expand Down

0 comments on commit bd12da7

Please sign in to comment.