Skip to content

Commit

Permalink
Merge branch 'dev' into 777-add-antares-broker-documentation-in-light…
Browse files Browse the repository at this point in the history
…-of-python-310-incompatibility
  • Loading branch information
jchate6 authored Dec 16, 2023
2 parents fcd7dfa + da0abb6 commit 3ef5779
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 19 deletions.
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
6 changes: 6 additions & 0 deletions tom_common/static/tom_common/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ body {
#login-buttons > li {
margin: 0 2px 0 2px;
}

/* see https://github.com/twbs/bootstrap/issues/23295 */
textarea {
overflow-y: auto;
overflow-x: hidden;
}
9 changes: 9 additions & 0 deletions tom_observations/facilities/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from astropy import units as u

from tom_observations.facility import BaseRoboticObservationFacility, BaseRoboticObservationForm
from tom_observations.observation_template import GenericTemplateForm
from tom_common.exceptions import ImproperCredentialsException
from tom_targets.models import Target

Expand Down Expand Up @@ -426,6 +427,10 @@ def isodatetime(value):
return payloads


class GEMTemplateForm(GenericTemplateForm, GEMObservationForm):
pass


class GEMFacility(BaseRoboticObservationFacility):
"""
The ``GEMFacility`` is the interface to the Gemini Telescope. For information regarding Gemini observing and the
Expand All @@ -440,6 +445,10 @@ class GEMFacility(BaseRoboticObservationFacility):
def get_form(self, observation_type):
return GEMObservationForm

# TODO: this should be called get_template_form_class
def get_template_form(self, observation_type):
return GEMTemplateForm

@classmethod
def submit_observation(clz, observation_payload):
obsids = []
Expand Down
17 changes: 17 additions & 0 deletions tom_observations/facilities/lt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ def __init__(self, *args, **kwargs):
)


class LTTemplateForm(LTQueryForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper.layout = Layout(
HTML('''
<p>
This plugin is a stub for the Liverpool Telescope plugin. In order to install the full plugin, please
see the instructions <a href="https://github.com/TOMToolkit/tom_lt" target="_blank">here</a>.
</p>
'''),
HTML('''<a class="btn btn-outline-primary" href={% url 'tom_observations:template-list' %}>Back</a>''')
)


class LTFacility(GenericObservationFacility):
name = 'LT'
observation_forms = {
Expand All @@ -28,6 +42,9 @@ class LTFacility(GenericObservationFacility):
def get_form(self, observation_type):
return LTQueryForm

def get_template_form(self, observation_type):
return LTTemplateForm

def submit_observation(self, observation_payload):
return

Expand Down
6 changes: 3 additions & 3 deletions tom_observations/facilities/ocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(self, facility_name):
def get_setting(self, key):
return settings.FACILITIES.get(self.facility_name, self.default_settings).get(key, self.default_settings[key])

def check_configuration(self):
def get_unconfigured_settings(self):
"""
Check that the settings for this facility are present, and return list of any required settings that are blank.
"""
Expand Down Expand Up @@ -993,12 +993,12 @@ def __init__(self, *args, **kwargs):
def button_layout(self):
"""
Override Button layout from BaseObservationForm.
Submit button will be disabled if there are any unconfigured settings found by check_configuration().
Submit button will be disabled if there are any unconfigured settings found by get_unconfigured_settings().
"""
target_id = self.initial.get('target_id')

return ButtonHolder(
Submit('submit', 'Submit', disabled=bool(self.facility_settings.check_configuration())),
Submit('submit', 'Submit', disabled=bool(self.facility_settings.get_unconfigured_settings())),
Submit('validate', 'Validate'),
HTML(f'''<a class="btn btn-outline-primary" href={{% url 'tom_targets:detail' {target_id} %}}>
Back</a>''')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
{% block content %}
{{ form|as_crispy_errors }}
<h3>Submit an observation to {{ form.facility.value }} for <a href="{% url 'targets:detail' pk=target.id %}">{{target.name}}</a></h3>
{% if unconfigured %}
<div class="alert alert-danger">Some {{ form.facility.value }} Facility settings ({{ unconfigured }}) are not configured.</div>
{% if missing_configurations %}
<div class="alert alert-danger">Some {{ form.facility.value }} Facility settings ({{ missing_configurations }}) are not configured.</div>
{% endif %}
{% if target.type == 'SIDEREAL' %}
<div class="row">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
{% block title %}Create an Observation Template{% endblock %}
{% block content %}
<h2>Create a new Observation Template for {{ form.facility.value }}</h2>
{% if missing_configurations %}
<div class="alert alert-danger">Some {{ form.facility.value }} Facility settings ({{ missing_configurations }}) are not configured.</div>
{% endif %}
{% crispy form %}
{% endblock %}
{% endblock %}
15 changes: 13 additions & 2 deletions tom_observations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ def get_context_data(self, **kwargs):
context.update(facility_context)

try:
context['unconfigured'] = ", ".join(facility.facility_settings.check_configuration())
context['missing_configurations'] = ", ".join(facility.facility_settings.get_unconfigured_settings())
except AttributeError:
context['unconfigured'] = ''
context['missing_configurations'] = ''

return context

Expand Down Expand Up @@ -611,6 +611,17 @@ class ObservationTemplateCreateView(FormView):
def get_facility_name(self):
return self.kwargs['facility']

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)

facility = get_service_class(self.get_facility_name())()
# Check configuration of facility and pass names of missing settings to context as 'missing_configurations'.
try:
context['missing_configurations'] = ", ".join(facility.facility_settings.get_unconfigured_settings())
except AttributeError:
context['missing_configurations'] = ''
return context

def get_form_class(self):
facility_name = self.get_facility_name()

Expand Down
2 changes: 1 addition & 1 deletion tom_setup/templates/tom_setup/settings.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ USE_L10N = False

USE_TZ = True

DATETIME_FORMAT = 'Y-m-d H:m:s'
DATETIME_FORMAT = 'Y-m-d H:i:s'
DATE_FORMAT = 'Y-m-d'


Expand Down

0 comments on commit 3ef5779

Please sign in to comment.