Skip to content

Commit

Permalink
fix add_consolidated_sibling_co_ids
Browse files Browse the repository at this point in the history
  • Loading branch information
acwhite211 committed Dec 11, 2024
1 parent 1fc3b6d commit 0cab0dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
14 changes: 10 additions & 4 deletions specifyweb/interactions/cog_preps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional, Set
from typing import Any, List, Optional, Set
from django.db.models import Subquery
from django.db.models.query import QuerySet
from specifyweb.specify.models import Collectionobject, Collectionobjectgroup, Collectionobjectgroupjoin, Loan, Loanpreparation, Preparation, Recordset, Recordsetitem
Expand Down Expand Up @@ -144,17 +144,23 @@ def get_cog_consolidated_preps_co_ids(cog: Collectionobjectgroup) -> Set[Collect
# Return set of distinct CollectionObjectIDs associated with the preparations
return set(prep.collectionobject.id for prep in preps)

def get_consolidated_sibling_co_ids(co_ids: List[int]) -> Set[Collectionobject]:
def add_consolidated_sibling_co_ids(request_co_ids: List[Any], id_fld: Optional[str]=None) -> List[Any]:
"""
Get the consolidated sibling CO IDs of the COs in the list
"""
cog_sibling_co_ids = set()
if id_fld is None:
# id_fld = 'id'
id_fld = 'catalognumber'
id_fld = id_fld.lower()
co_ids = Collectionobject.objects.filter(**{f"{id_fld}__in": request_co_ids}).values_list('id', flat=True)
cogs = get_cogs_from_co_ids(co_ids)
for cog in cogs:
cog_sibling_co_ids.update(get_cog_consolidated_preps_co_ids(cog))
# cog_sibling_co_ids -= set(co_ids)

return cog_sibling_co_ids
cog_sibling_co_idfld_ids = Collectionobject.objects.filter(id__in=cog_sibling_co_ids).values_list(id_fld, flat=True)
return list(set(request_co_ids).union(set(cog_sibling_co_idfld_ids)))

def get_consolidated_co_siblings_from_rs(rs: Recordset) -> Set[Collectionobject]:
"""
Expand All @@ -164,7 +170,7 @@ def get_consolidated_co_siblings_from_rs(rs: Recordset) -> Set[Collectionobject]
if is_co_recordset(rs):
cogs = get_cogs_from_co_recordset(rs)
for cog in cogs:
cog_sibling_co_ids.update(get_cog_consolidated_preps_co_ids(cog))
cog_sibling_co_ids.union(get_cog_consolidated_preps_co_ids(cog))
# cog_sibling_co_ids -= set(rs.recordsetitems.values_list('recordid', flat=True))

return cog_sibling_co_ids
9 changes: 4 additions & 5 deletions specifyweb/interactions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

from specifyweb.interactions.cog_preps import (
get_cog_consolidated_preps,
get_cog_consolidated_preps_co_ids,
get_consolidated_co_siblings_from_rs,
get_consolidated_sibling_co_ids,
add_consolidated_sibling_co_ids,
remove_all_cog_sibling_preps_from_loan,
)
from specifyweb.middleware.general import require_GET
Expand Down Expand Up @@ -96,7 +95,8 @@ def preps_available_rs(request, recordset_id):

# Get consolidated CO ids if the recordset is a COG
rs = Recordset.objects.filter(id=recordset_id).first()
cog_co_ids = get_consolidated_co_siblings_from_rs(rs)
# cog_co_ids = get_consolidated_co_siblings_from_rs(rs)
cog_co_ids = set()
cog_co_ids_str = ','.join(map(str, cog_co_ids)) if cog_co_ids else 'NULL'

cursor = connection.cursor()
Expand Down Expand Up @@ -169,8 +169,7 @@ def preps_available_ids(request):

isLoan = request.POST.get('isLoan', 'false').lower() == 'true'

cog_co_ids = get_consolidated_sibling_co_ids(co_ids)
co_ids += cog_co_ids
co_ids = add_consolidated_sibling_co_ids(co_ids, id_fld)

sql = """
select co.CatalogNumber, co.collectionObjectId, t.FullName, t.taxonId, p.preparationid, pt.name, p.countAmt, sum(lp.quantity-lp.quantityreturned) Loaned,
Expand Down

0 comments on commit 0cab0dc

Please sign in to comment.