Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a realtionship to CO to be able to add parent COG #5433

Open
wants to merge 12 commits into
base: production
Choose a base branch
from
37 changes: 35 additions & 2 deletions specifyweb/businessrules/rules/cojo_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,49 @@ def cojo_pre_save(cojo):
):
raise BusinessRuleException('ChildCog is already in use as a child in another COG.')

if (
cojo.childco is not None
and cojo.childco.cojo is not None
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.')

if (cojo.childcog_id == cojo.parentcog_id):
raise BusinessRuleException(f"Cannot add a COG to itself. COG name: {cojo.childcog.name}")

@orm_signal_handler('post_save', 'Collectionobjectgroup')
def cog_post_save(cog):
# Delete cojos that point to this COG to ensure we avoid multiple COGs having the same child cog
Collectionobjectgroupjoin.objects.filter(childcog=cog).delete()
cojo = Collectionobjectgroupjoin.objects.filter(childcog=cog).first()
if cojo and cojo.parentcog != cog.parentcog:
Collectionobjectgroupjoin.objects.filter(childcog=cog).delete()
if cog.parentcog is not None:
Collectionobjectgroupjoin.objects.get_or_create(
childcog=cog,
childco=None,
parentcog=cog.parentcog
)
)

@orm_signal_handler('post_save', 'Collectionobject')
def co_post_save(co):
# Delete cojos that point to this CO to ensure we avoid multiple COGs having the same child co
cojo = Collectionobjectgroupjoin.objects.filter(childco=co).first()
if cojo and cojo.parentcog != co.parentcog:
Collectionobjectgroupjoin.objects.filter(childcog=co).delete()
if co.parentcog is not None:
Collectionobjectgroupjoin.objects.get_or_create(
childcog=None,
childco=co,
parentcog=co.parentcog
)

@orm_signal_handler('post_save', 'Collectionobjectgroupjoin')
def cojo_post_save(cojo):
if (cojo.childcog is not None):
cojo.childcog.parentcog = cojo.parentcog
cojo.childcog.save()

if (cojo.childco is not None):
cojo.childco.parentcog = cojo.parentcog
cojo.childco.save()
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,23 @@ export const businessRuleDefs: MappedBusinessRuleDefs = {
},

CollectionObjectGroupJoin: {
customInit: (
CollectionObjectGroupJoin: SpecifyResource<CollectionObjectGroupJoin>
): void => {
if (
CollectionObjectGroupJoin.get('childCog') ===
CollectionObjectGroupJoin.get('parentCog') &&
typeof CollectionObjectGroupJoin.get('childCog') === 'string' &&
typeof CollectionObjectGroupJoin.get('parentCog') === 'string'
) {
setSaveBlockers(
CollectionObjectGroupJoin,
CollectionObjectGroupJoin.specifyTable.field.childCog,
['Cog added to itself'],
'Cog to COG'
);
}
},
fieldChecks: {
/*
* Only a single CO in a COG can be set as primary.
Expand Down
Loading
Loading