diff --git a/tom_catalogs/forms.py b/tom_catalogs/forms.py index c2f955ef0..08583890e 100644 --- a/tom_catalogs/forms.py +++ b/tom_catalogs/forms.py @@ -7,7 +7,26 @@ class CatalogQueryForm(forms.Form): Form used for catalog harvesters ``CatalogQueryView``. """ term = forms.CharField() - service = forms.ChoiceField(choices=lambda: [(key, key) for key in get_service_classes().keys()]) + catalog_choices = [] + service = forms.ChoiceField(choices=[]) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields['service'].choices = self.get_catalog_choices() + + def get_catalog_choices(self): + """ + Returns a list of catalog choices for the form including Help text if available + :return: + """ + catalog_choices = [] + for catalog_name in get_service_classes().keys(): + if getattr(get_service_classes()[catalog_name], "help_text", None): + catalog_choices.append( + (catalog_name, f'{catalog_name} -- {get_service_classes()[catalog_name].help_text}')) + else: + catalog_choices.append((catalog_name, catalog_name)) + return catalog_choices def get_target(self): """ diff --git a/tom_catalogs/harvesters/jplhorizons.py b/tom_catalogs/harvesters/jplhorizons.py index 5c5a41c29..dd1276905 100644 --- a/tom_catalogs/harvesters/jplhorizons.py +++ b/tom_catalogs/harvesters/jplhorizons.py @@ -11,6 +11,7 @@ class JPLHorizonsHarvester(AbstractHarvester): """ name = 'JPL Horizons' + help_text = 'Query the JPL Horizons Minor Body catalog.' def query(self, term, location=None, start=None, end=None, step=None): if all((start, end, step)): diff --git a/tom_catalogs/harvesters/tns.py b/tom_catalogs/harvesters/tns.py index 121c7c086..bb63758b1 100644 --- a/tom_catalogs/harvesters/tns.py +++ b/tom_catalogs/harvesters/tns.py @@ -13,11 +13,16 @@ TNS_URL = 'https://www.wis-tns.org' try: + # Check if there is an API key in the HARVESTERS section of settings.py TNS_CREDENTIALS = settings.HARVESTERS['TNS'] except (AttributeError, KeyError): - TNS_CREDENTIALS = { - 'api_key': '' - } + try: + # Otherwise, check if there is an API key in the BROKERS section of settings.py + TNS_CREDENTIALS = settings.BROKERS['TNS'] + except (AttributeError, KeyError): + TNS_CREDENTIALS = { + 'api_key': '' + } def get(term): @@ -31,12 +36,24 @@ def get(term): get_data = [('api_key', (None, TNS_CREDENTIALS['api_key'])), ('data', (None, json.dumps(json_file)))] - response = requests.post(get_url, files=get_data, headers=TNSBroker.tns_headers()) - response_data = json.loads(response.text) - - if 400 <= response_data.get('id_code') <= 403: - raise ImproperCredentialsException('TNS: ' + str(response_data.get('id_message'))) - + try: + response = requests.post(get_url, files=get_data, headers=TNSBroker.tns_headers()) + response_data = json.loads(response.text) + + if 400 <= response_data.get('id_code') <= 403: + raise ImproperCredentialsException('TNS: ' + str(response_data.get('id_message'))) + except AttributeError: + raise ImproperCredentialsException(f"TNS Catalog Search. This requires TNS Broker configuration. " + f"Please see {TNSBroker.help_url} for more information") + + reply = response_data['data']['reply'] + # If TNS succeeds in finding an object, it returns a reply containing the `objname`. + # If TNS fails to find the object, it returns a reply in the form: + # {'name': {'110': {'message': 'No results found.', 'message_id': 110}}, + # 'objid': {'110': {'message': 'No results found.', 'message_id': 110}}} + # In this case, we return None + if not reply.get('objname', None): + return None return response_data['data']['reply'] @@ -47,6 +64,7 @@ class TNSHarvester(AbstractHarvester): """ name = 'TNS' + help_text = 'Requires object name without prefix.' def query(self, term): self.catalog_data = get(term) diff --git a/tom_catalogs/tests/tests.py b/tom_catalogs/tests/tests.py index be33abb6b..bfc34e365 100644 --- a/tom_catalogs/tests/tests.py +++ b/tom_catalogs/tests/tests.py @@ -7,6 +7,7 @@ class TestHarvester(AbstractHarvester): name = 'TEST' + help_text = "This is a test harvester." def query(self, term): if term == 'notfound': @@ -37,6 +38,7 @@ def setUp(self): def test_service_available(self): response = self.client.get(reverse('tom_catalogs:query')) self.assertContains(response, TestHarvester.name) + self.assertContains(response, TestHarvester.help_text) def test_do_search(self): data = {'term': 'atarget', 'service': 'TEST'}