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

Fix TNS Catalog search and add help text #782

Merged
merged 5 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 20 additions & 1 deletion tom_catalogs/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
1 change: 1 addition & 0 deletions tom_catalogs/harvesters/jplhorizons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
36 changes: 27 additions & 9 deletions tom_catalogs/harvesters/tns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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']


Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tom_catalogs/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class TestHarvester(AbstractHarvester):
name = 'TEST'
help_text = "This is a test harvester."

def query(self, term):
if term == 'notfound':
Expand Down Expand Up @@ -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'}
Expand Down
Loading