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 cot formatter filter #6254

Draft
wants to merge 3 commits into
base: issue-6239
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions specifyweb/stored_queries/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

from django.conf import settings
from django.db import transaction
from sqlalchemy import sql, orm, func, select
from specifyweb.specify.models import datamodel
from sqlalchemy import sql, orm, func
from sqlalchemy.orm import aliased
from sqlalchemy.sql.expression import asc, desc, insert, literal

from specifyweb.stored_queries.group_concat import group_by_displayed_fields
Expand Down Expand Up @@ -626,7 +628,10 @@ def build_query(session, collection, user, tableid, field_specs,
order_by_exprs = []
selected_fields = []
predicates_by_field = defaultdict(list)
#augment_field_specs(field_specs, formatauditobjs)
# NOTE: Remove if unused?
# augment_field_specs(field_specs, formatauditobjs)

cot_formatters = []
for fs in field_specs:
sort_type = SORT_TYPES[fs.sort_type]

Expand All @@ -636,12 +641,18 @@ def build_query(session, collection, user, tableid, field_specs,
query = query.add_columns(formatted_field)
selected_fields.append(formatted_field)

if fs.fieldspec.get_field() is datamodel.get_table('CollectionObject').get_field('catalogNumber') and fs.format_name is not None:
cot_formatters.append(fs.format_name)

if sort_type is not None:
order_by_exprs.append(sort_type(field))

if predicate is not None:
predicates_by_field[fs.fieldspec].append(predicate)

if len(cot_formatters) > 0:
query = filter_by_cot_formatter(model, query, collection, cot_formatters)

if implicit_or:
implicit_ors = [
reduce(sql.or_, ps)
Expand All @@ -664,3 +675,32 @@ def build_query(session, collection, user, tableid, field_specs,

logger.warning("query: %s", query.query)
return query.query, order_by_exprs


def filter_by_cot_formatter(model, query, collection, formatter_names):
cotype = aliased(models.CollectionObjectType)
query = query.join(cotype, getattr(model, 'collectionObjectType'))

# Get cotype.catalogNumberFormatName
catalog_number_format_name = getattr(cotype, 'catalogNumberFormatName')

filters = [catalog_number_format_name == formatter_name for formatter_name in formatter_names]

# Check if formatter matches collection's default
# This is done to make sure we include COs that have COTs with no catalogNumberFormatName
collection_formatter = getattr(collection, 'catalognumformatname')
if any(formatter_name == collection_formatter for formatter_name in formatter_names):
collection_alias = aliased(models.Collection)
query = query.join(collection_alias, getattr(model, 'collection'))
catalog_number_format_name_collection = getattr(collection_alias, 'catalogNumFormatName')

filters.append(
sql.and_(
catalog_number_format_name == None,
catalog_number_format_name_collection == collection_formatter
)
)

query = query.filter(sql.or_(*filters))

return query
Loading