Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
jchate6 committed Jul 17, 2024
2 parents 61d5c71 + d7a9cad commit 0635dfc
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 175 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Once configured, a `TNS` button should appear below the Target Name on the defau
If you have customized the Target Details page of your TOM, or if you would like to add entrypoints to the tom_tns form from other TOM pages, including those referencing a specific data product or reduced datum's values, then you can do that by including the code below somewhere in your templates:

```html
<a href="{% url tns:report-tns pk=target.id datum_pk=datum.pk %}" title=TNS class="btn btn-info">Submit to TNS</a>
<a href="{% url 'tns:report-tns' pk=target.id datum_pk=datum.pk %}" title=TNS class="btn btn-info">Submit to TNS</a>
```

The datum_pk is optional. If it is not specified, the latest photometry reduced datum will be used to pre-fill the discovery report form, and the latest spectroscopy reduced datum will be used to pre-fill the classification report form. If you specifiy a datum pk, then that datum and associated data product will be used to pre-fill the proper forms.
Expand Down
308 changes: 155 additions & 153 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ packages = [{include = "tom_tns"}]

[tool.poetry.dependencies]
python = "^3.8.1,<3.12"
tomtoolkit = "^2.15"
tomtoolkit = "^2.18"

[tool.poetry.group.lint.dependencies]
flake8 = ">=6.0,<7.1"
flake8 = ">=6.0,<7.2"

[tool.poetry-dynamic-versioning]
enable = true
Expand Down
6 changes: 3 additions & 3 deletions tom_tns/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.conf import settings
from django.core.exceptions import ValidationError

from tom_tns.tns_api import (get_tns_values, get_tns_credentials, get_reverse_tns_values,
from tom_tns.tns_api import (get_tns_values, group_names, get_reverse_tns_values,
pre_upload_files_to_tns, submit_through_hermes)
from tom_dataproducts.models import DataProduct

Expand Down Expand Up @@ -91,7 +91,7 @@ def __init__(self, *args, **kwargs):
self.fields['nondetection_flux_units'].initial = (1, "ABMag")

# set choices of reporting groups to list set in settings.py
bot_tns_group_names = get_tns_credentials().get('group_names', [])
bot_tns_group_names = group_names()
if not bot_tns_group_names:
bot_tns_group_names = [settings.TOM_NAME]
tns_group_list = []
Expand Down Expand Up @@ -340,7 +340,7 @@ def __init__(self, *args, **kwargs):
self.fields['fits_file'].choices = kwargs['initial']['fits_file_choices']

# set choices of reporting groups to list set in settings.py
bot_tns_group_names = get_tns_credentials().get('group_names', [])
bot_tns_group_names = group_names()
if not bot_tns_group_names:
bot_tns_group_names = [settings.TOM_NAME]
tns_group_list = []
Expand Down
2 changes: 1 addition & 1 deletion tom_tns/templatetags/tns_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def classify_with_tns(context):
reduced_datum = spectra.latest()
if reduced_datum:
initial['observation_date'] = reduced_datum.timestamp
if reduced_datum.data_product.get_file_extension().lower() in ['.ascii', '.txt']:
if reduced_datum.data_product and reduced_datum.data_product.get_file_extension().lower() in ['.ascii', '.txt']:
initial['ascii_file'] = (reduced_datum.data_product.pk, reduced_datum.data_product.get_file_name())
instrument_name = reduced_datum.value.get('instrument')
if instrument_name in TNS_INSTRUMENT_IDS:
Expand Down
51 changes: 37 additions & 14 deletions tom_tns/tns_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def default_authors():
return settings.BROKERS.get('TNS', {}).get('default_authors', '')


def group_names():
""" Returns the Group names list from settings, either from Hermes config or TNS config
"""
if submit_through_hermes():
return settings.DATA_SHARING.get('hermes', {}).get('GROUP_NAMES', [])
else:
return settings.BROKERS.get('TNS', {}).get('group_names', [])


def get_tns_credentials():
"""
Get the TNS credentials from settings.py.
Expand Down Expand Up @@ -72,7 +81,7 @@ def get_tns_values(option_list):
all_tns_values = cache.get("all_tns_values", {})
if not all_tns_values:
all_tns_values, _ = populate_tns_values()
selected_values = all_tns_values[option_list]
selected_values = all_tns_values.get(option_list, [])
tuple_list = []
if isinstance(selected_values, list):
tuple_list = [(i, v) for i, v in enumerate(selected_values)]
Expand All @@ -97,24 +106,38 @@ def get_reverse_tns_values(option_list, value):

def populate_tns_values():
"""pull all the values from the TNS API and Cache for an hour"""

# Need to spoof a web based user agent or TNS will block the request :(
SPOOF_USER_AGENT = 'Mozilla/5.0 (X11; Linux i686; rv:110.0) Gecko/20100101 Firefox/110.0.'

# Use sandbox URL if no url found in settings.py
tns_base_url = get_tns_credentials().get('tns_base_url', 'https://sandbox.wis-tns.org/api')
all_tns_values = {}
reversed_tns_values = {}
try:
resp = requests.get(urljoin(tns_base_url, 'api/values/'),
headers={'user-agent': SPOOF_USER_AGENT})
resp.raise_for_status()
all_tns_values = resp.json().get('data', {})
if submit_through_hermes():
# Get the tns values from the HERMES api
hermes_tns_options_url = urljoin(settings.DATA_SHARING.get('hermes', {}).get(
'BASE_URL', ''), 'api/v0/tns_options/')
headers = {'Authorization': f"Token {settings.DATA_SHARING.get('hermes', {}).get('HERMES_API_KEY', '')}"}
try:
resp = requests.get(hermes_tns_options_url, headers=headers)
resp.raise_for_status()
all_tns_values = resp.json()
except Exception as e:
logging.warning(f"Failed to retrieve tns values from Hermes: {repr(e)}")
else:
# Need to spoof a web based user agent or TNS will block the request :(
SPOOF_USER_AGENT = 'Mozilla/5.0 (X11; Linux i686; rv:110.0) Gecko/20100101 Firefox/110.0.'

# Use sandbox URL if no url found in settings.py
tns_base_url = get_tns_credentials().get('tns_base_url', 'https://sandbox.wis-tns.org/api')
try:
resp = requests.get(urljoin(tns_base_url, 'api/values/'),
headers={'user-agent': SPOOF_USER_AGENT})
resp.raise_for_status()
all_tns_values = resp.json().get('data', {})

except Exception as e:
logging.warning(f"Failed to retrieve tns values: {repr(e)}")

if all_tns_values:
reversed_tns_values = reverse_tns_values(all_tns_values)
cache.set("all_tns_values", all_tns_values, 3600)
cache.set("reverse_tns_values", reversed_tns_values, 3600)
except Exception as e:
logging.warning(f"Failed to retrieve tns values: {repr(e)}")
return all_tns_values, reversed_tns_values


Expand Down
2 changes: 1 addition & 1 deletion tom_tns/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_context_data(self, **kwargs):
context['default_form'] = 'classify'
except ReducedDatum.DoesNotExist:
pass
context['tns_configured'] = bool(get_tns_credentials())
context['tns_configured'] = submit_through_hermes() or bool(get_tns_credentials())
context['target'] = target
context['version'] = __version__ # from tom_tns.__init__.py
# We want to establish a default tab to display.
Expand Down

0 comments on commit 0635dfc

Please sign in to comment.