Skip to content

Commit

Permalink
Mccalluc/user files next steps (#1772)
Browse files Browse the repository at this point in the history
* Simplify fq; check status of solr response

* Better handling if assay or study are missing

* Handle missing "fl"

* Fix import

* I changed the way this query is built

* Include the exception itself, in response to Jennifers comment

* Typo

* Use "none" to communicate error condition, rather than exception

* paren wrapping the preferred over backslashes

* Try-else

* paren on its own line

* "is None" is better?

* Remove the error check that has not gotten support
  • Loading branch information
mccalluc committed Jun 12, 2017
1 parent 22c8b21 commit 363a806
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
4 changes: 2 additions & 2 deletions refinery/data_set_manager/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ def test_generate_solr_params(self):
# empty params
query = generate_solr_params_for_assay(QueryDict({}), self.valid_uuid)
self.assertEqual(str(query),
'fq=%28assay_uuid%3A{}%29'
'fq=assay_uuid%3A%28{}%29'
'&facet.field=Cell Type&'
'facet.field=Analysis&facet.field=Organism&'
'facet.field=Cell Line&facet.field=Type&'
Expand Down Expand Up @@ -777,7 +777,7 @@ def test_generate_solr_params(self):
parameter_qdict, self.valid_uuid
)
self.assertEqual(str(query),
'fq=%28assay_uuid%3A{}%29'
'fq=assay_uuid%3A%28{}%29'
'&facet.field=cats&'
'facet.field=mouse&facet.field=dog&'
'facet.field=horse&fl=cats%2C'
Expand Down
49 changes: 30 additions & 19 deletions refinery/data_set_manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,22 +543,26 @@ def generate_solr_params_for_user(params, user_uuid):
study = Study.objects.get(
investigation=investigation
)
except Study.DoesNotExist:
except Study.DoesNotExist as e:
logger.error('Expected at least one Study for %s: %s',
investigation, e)
# Do not need to re-raise this one.
continue
# It's not an error not to have data,
# but there's nothing more to do here.
except Study.MultipleObjectsReturned:
logger.error('Expected only one study for %s', investigation)
except Study.MultipleObjectsReturned as e:
logger.error('Expected only one Study for %s: %s',
investigation, e)
raise

try:
assay = Assay.objects.get(study=study)
except Assay.DoesNotExist:
except Assay.DoesNotExist as e:
logger.error('Expected at least one Assay for %s: %s',
study, e)
# Do not need to re-raise this one.
continue
# Again, it's not an error not to have data,
# but there's nothing more to do here.
except:
logger.error('Expected only one assay for %s', study)
except Assay.MultipleObjectsReturned as e:
logger.error('Expected only one Assay for %s: %s',
study, e)
raise

assay_uuids.append(assay.uuid)
Expand All @@ -585,7 +589,11 @@ def generate_solr_params_for_assay(params, assay_uuid):
return _generate_solr_params(params, assay_uuids=[assay_uuid])


def _generate_solr_params(params, assay_uuids=[]):
def _generate_solr_params(params, assay_uuids):
"""
Either returns a solr url parameter string,
or None if assay_uuids is empty.
"""

file_types = 'fq=type:("Raw Data File" OR ' \
'"Derived Data File" OR ' \
Expand Down Expand Up @@ -615,12 +623,9 @@ def _generate_solr_params(params, assay_uuids=[]):
'facet.limit=-1'
])

solr_params = 'fq=({})'.format(
' OR '.join(map(
lambda id: 'assay_uuid:{}'.format(id),
assay_uuids
))
)
if len(assay_uuids) == 0:
return None
solr_params = 'fq=assay_uuid:({})'.format(' OR '.join(assay_uuids))

if facet_field:
facet_field = facet_field.split(',')
Expand Down Expand Up @@ -808,8 +813,14 @@ def format_solr_response(solr_response):
return "Error loading json."

# Reorganizes solr response into easier to digest objects.
order_facet_fields = solr_response_json.get('responseHeader').get(
'params').get('fl').split(',')
try:
order_facet_fields_joined = (solr_response_json
['responseHeader']['params']['fl'])
except KeyError:
order_facet_fields = []
else:
order_facet_fields = order_facet_fields_joined.split(',')

if solr_response_json.get('facet_counts'):
facet_field_counts = solr_response_json.get('facet_counts').get(
'facet_fields')
Expand Down
10 changes: 8 additions & 2 deletions refinery/user_files_manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rest_framework.response import Response
from rest_framework.views import APIView


logger = logging.getLogger(__name__)


Expand All @@ -20,8 +21,13 @@ def get(self, request):
params = request.query_params

solr_params = generate_solr_params_for_user(
params,
user_uuid=request.user.uuid)
params,
user_uuid=request.user.uuid
)
if solr_params is None:
return Response({})
# TODO: Make this look like an empty solr response

solr_response = search_solr(solr_params, 'data_set_manager')
solr_response_json = format_solr_response(solr_response)

Expand Down

0 comments on commit 363a806

Please sign in to comment.