Skip to content

Commit

Permalink
Merge branch 'development' into enhancement/announcements-reorder
Browse files Browse the repository at this point in the history
  • Loading branch information
vlagrassa committed May 31, 2024
2 parents 4e73ebe + 26e7032 commit a2cb69a
Show file tree
Hide file tree
Showing 15 changed files with 1,271 additions and 455 deletions.
400 changes: 386 additions & 14 deletions src/modules/site-v2/base/views/api/trait.py

Large diffs are not rendered by default.

114 changes: 10 additions & 104 deletions src/modules/site-v2/base/views/tools/phenotype_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from extensions import cache, compress
from sqlalchemy import or_, func

from caendr.api.phenotype import query_phenotype_metadata, get_trait, filter_trait_query_by_text, filter_trait_query_by_tags
from caendr.api.phenotype import query_phenotype_metadata, get_trait_categories
from caendr.services.cloud.postgresql import rollback_on_error_handler

from caendr.services.logger import logger
Expand Down Expand Up @@ -56,42 +56,11 @@ def phenotype_database():
Phenotype Database table (non-bulk)
"""
form = EmptyForm()
# Get the list of traits for non-bulk files

# Get the list of unique tags
try:
query = query_phenotype_metadata()

# Get the list of unique tags
tags = [ tr.tags.split(', ') for tr in query if tr.tags ]
tags_list = [tg for tr_tag in tags for tg in tr_tag]
unique_tags = list(set(tags_list))

if request.method == 'POST':
selected_tags = request.json.get('selected_tags', [])
search_val = request.json.get('search_val', '')
page = int(request.json.get('page', 1))
current_page = int(request.json.get('current_page', 1))
per_page = 10

# Filter by search value and tags, if provided
query = filter_trait_query_by_text(query, search_val)
query = filter_trait_query_by_tags(query, selected_tags)

# Paginate the query, rolling back on error
with rollback_on_error_handler():
pagination = query.paginate(page=page, per_page=per_page)

json_data = [ tr.to_json() for tr in pagination.items ]
pagination_data = {
'has_next': pagination.has_next,
'has_prev': pagination.has_prev,
'prev_num': pagination.prev_num,
'next_num': pagination.next_num,
'total_pages': pagination.pages,
'current_page': current_page
}
return jsonify({'data': json_data, 'pagination': pagination_data })

categories = get_trait_categories()

except Exception as ex:
logger.error(f'Failed to retrieve the list of traits: {ex}')
abort(500, description='Failed to retrieve the list of traits')
Expand All @@ -102,73 +71,11 @@ def phenotype_database():
"tool_alt_parent_breadcrumb": { "title": "Tools", "url": url_for('tools.tools') },

# Data
'categories': unique_tags,
'categories': categories,
'form': form
})


@phenotype_database_bp.route('/traits-zhang')
@cache.memoize(60*60)
@compress.compressed()
def get_zhang_traits_json():
"""
Phenotype Database table (bulk)
Fetch table content by request for each page and render the data for Datatables()
"""
try:
# get parameters for query
draw = request.args.get('draw', type=int)
start = request.args.get('start', type=int)
length = request.args.get('length', type=int)
search_value = bleach.clean(request.args.get('search[value]', '')).lower()

query = query_phenotype_metadata(is_bulk_file=True)
total_records = query.count()

# Filter by search value, if provided
query = filter_trait_query_by_text(query, search_value)

# Query PhenotypeMetadata (include phenotype values for each trait)
with rollback_on_error_handler():
data = query.offset(start).limit(length).from_self().\
join(PhenotypeMetadata.phenotype_values).all()

json_data = [ trait.to_json_with_values() for trait in data ]

filtered_records = query.count()

response_data = {
"draw": draw,
"recordsTotal": total_records,
"recordsFiltered": filtered_records,
"data": json_data
}

except Exception as ex:
logger.error(f'Failed to retrieve the list of traits: {ex}')
response_data = []
return jsonify(response_data)

@phenotype_database_bp.route('/traits-list', methods=['POST'])
@cache.memoize(60*60)
@compress.compressed()
def get_traits_json():
"""
Get traits data for non-bulk files in JSON format (include phenotype values)
"""
trait_id = request.json.get('trait_id')
err_msg = f'Failed to retrieve metadata for trait {trait_id}'

if trait_id:
try:
trait = get_trait(trait_id).to_json_with_values()
return jsonify(trait)

except Exception as ex:
logger.error(f'{err_msg}: {ex}')

return jsonify({ 'message': err_msg }), 404


#
# Submission Flow
Expand All @@ -191,14 +98,13 @@ def submit_traits():

# Check for URL vars specifying an initial trait
# These will be inherited from submit_start
initial_trait_name = request.args.get('trait')
initial_trait_set = request.args.get('dataset')
initial_trait_id = request.args.get('trait')

# Try looking up the specified trait
if initial_trait_name:
if initial_trait_id:
try:
initial_trait = Trait(dataset=initial_trait_set, trait_name=initial_trait_name)
except NotFoundError:
initial_trait = Trait.from_id(initial_trait_id)
except (NotFoundError, ValueError):
flash('That trait could not be found.', 'danger')
initial_trait = None
else:
Expand Down
Loading

0 comments on commit a2cb69a

Please sign in to comment.