Skip to content

Commit

Permalink
fix some flake8 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon committed Oct 6, 2023
1 parent 8f6cf5d commit 9a1fde4
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 41 deletions.
59 changes: 39 additions & 20 deletions tom_dataproducts/forced_photometry/atlas.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
from django import forms
from django.conf import settings
from django.utils import timezone
from django.core.files.base import ContentFile
from crispy_forms.layout import Div, HTML
from astropy.time import Time
from tom_dataproducts.forced_photometry.forced_photometry_service import BaseForcedPhotometryQueryForm, BaseForcedPhotometryService, ForcedPhotometryServiceException
from tom_dataproducts.models import ReducedDatum, DataProduct
from tom_dataproducts.data_processor import run_data_processor
from tom_dataproducts.exceptions import InvalidFileFormatException
import tom_dataproducts.forced_photometry.forced_photometry_service as fps
from tom_dataproducts.tasks import atlas_query
from tom_targets.models import Target

class AtlasForcedPhotometryQueryForm(BaseForcedPhotometryQueryForm):
min_date = forms.CharField(label='Min date:', required=False, widget=forms.TextInput(attrs={'class': 'ml-2', 'type': 'datetime-local'}))
max_date = forms.CharField(label='Max date:', required=False, widget=forms.TextInput(attrs={'class': 'ml-2', 'type': 'datetime-local'}))
min_date_mjd = forms.FloatField(label='Min date (mjd):', required=False, widget=forms.NumberInput(attrs={'class': 'ml-2'}))
max_date_mjd = forms.FloatField(label='Max date (mjd):', required=False, widget=forms.NumberInput(attrs={'class': 'ml-2'}))
class AtlasForcedPhotometryQueryForm(fps.BaseForcedPhotometryQueryForm):
min_date = forms.CharField(
label='Min date:', required=False,
widget=forms.TextInput(attrs={'class': 'ml-2', 'type': 'datetime-local'})
)
max_date = forms.CharField(
label='Max date:', required=False,
widget=forms.TextInput(attrs={'class': 'ml-2', 'type': 'datetime-local'})
)
min_date_mjd = forms.FloatField(
label='Min date (mjd):', required=False,
widget=forms.NumberInput(attrs={'class': 'ml-2'})
)
max_date_mjd = forms.FloatField(
label='Max date (mjd):', required=False,
widget=forms.NumberInput(attrs={'class': 'ml-2'})
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -65,12 +72,14 @@ def clean(self):
return cleaned_data


class AtlasForcedPhotometryService(BaseForcedPhotometryService):
class AtlasForcedPhotometryService(fps.BaseForcedPhotometryService):
name = 'Atlas'

def __init__(self):
super().__init__
self.success_message = 'Asynchronous Atlas query is processing. Refresh the page once complete it will show up as a dataproduct in the "Manage Data" tab.'
self.success_message = ('Asynchronous Atlas query is processing. '
'Refresh the page once complete it will show '
'up as a dataproduct in the "Manage Data" tab.')

def get_form(self):
"""
Expand All @@ -91,21 +100,31 @@ def query_service(self, query_parameters):
if not max_date_mjd and query_parameters.get('max_date'):
max_date_mjd = Time(query_parameters.get('max_date')).mjd
if not Target.objects.filter(pk=query_parameters.get('target_id')).exists():
raise ForcedPhotometryServiceException(f"Target {query_parameters.get('target_id')} does not exist")
raise fps.ForcedPhotometryServiceException(f"Target {query_parameters.get('target_id')} does not exist")

if 'atlas' not in settings.FORCED_PHOTOMETRY_SERVICES:
raise ForcedPhotometryServiceException("Must specify 'atlas' settings in FORCED_PHOTOMETRY_SERVICES")
raise fps.ForcedPhotometryServiceException("Must specify 'atlas' settings in FORCED_PHOTOMETRY_SERVICES")
if not settings.FORCED_PHOTOMETRY_SERVICES.get('atlas', {}).get('url'):
raise ForcedPhotometryServiceException("Must specify a 'url' under atlas settings in FORCED_PHOTOMETRY_SERVICES")
raise fps.ForcedPhotometryServiceException(
"Must specify a 'url' under atlas settings in FORCED_PHOTOMETRY_SERVICES"
)
if not settings.FORCED_PHOTOMETRY_SERVICES.get('atlas', {}).get('api_key'):
raise ForcedPhotometryServiceException("Must specify an 'api_key' under atlas settings in FORCED_PHOTOMETRY_SERVICES")
raise fps.ForcedPhotometryServiceException(
"Must specify an 'api_key' under atlas settings in FORCED_PHOTOMETRY_SERVICES"
)

if 'django_dramatiq' in settings.INSTALLED_APPS:
atlas_query.send(min_date_mjd, max_date_mjd, query_parameters.get('target_id'), self.get_data_product_type())
atlas_query.send(min_date_mjd, max_date_mjd,
query_parameters.get('target_id'),
self.get_data_product_type())
else:
query_succeeded = atlas_query(min_date_mjd, max_date_mjd, query_parameters.get('target_id'), self.get_data_product_type())
query_succeeded = atlas_query(min_date_mjd, max_date_mjd,
query_parameters.get('target_id'),
self.get_data_product_type())
if not query_succeeded:
raise ForcedPhotometryServiceException("Atlas query failed, check the server logs for more information")
raise fps.ForcedPhotometryServiceException(
"Atlas query failed, check the server logs for more information"
)
self.success_message = "Atlas query completed. View its data product in the 'Manage Data' tab"

return True
Expand Down
12 changes: 4 additions & 8 deletions tom_dataproducts/forced_photometry/forced_photometry_service.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
from abc import ABC, abstractmethod
import copy
import logging
import requests

from crispy_forms.helper import FormHelper
from crispy_forms.layout import ButtonHolder, Layout, Submit, Div, HTML
from crispy_forms.layout import ButtonHolder, Layout, Submit
from django import forms
from django.conf import settings
from django.contrib.auth.models import Group
from django.core.files.base import ContentFile
from django.utils.module_loading import import_string

from tom_targets.models import Target

logger = logging.getLogger(__name__)


Expand All @@ -37,7 +31,9 @@ def get_service_class(name):
try:
return available_classes[name]
except KeyError:
raise ImportError(f'Could not a find a forced photometry service with the name {name}. Did you add it to TOM_FORCED_PHOTOMETRY_CLASSES?')
raise ImportError((
f'Could not a find a forced photometry service with the name {name}. '
'Did you add it to TOM_FORCED_PHOTOMETRY_CLASSES?'))


class ForcedPhotometryServiceException(Exception):
Expand Down
2 changes: 1 addition & 1 deletion tom_dataproducts/processors/atlas_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _process_photometry_from_plaintext(self, data_product):
text file, as produced by the ATLAS forced photometry service at https://fallingstar-data.com/forcedphot
The header looks like this:
###MJD m dm uJy duJy F err chi/N RA Dec x y maj min phi apfit mag5sig Sky Obs
###MJD m dm uJy duJy F err chi/N RA Dec x y maj min phi apfit mag5sig Sky Obs
:param data_product: ATLAS Photometric DataProduct which will be processed into a list of dicts
:type data_product: DataProduct
Expand Down
11 changes: 6 additions & 5 deletions tom_dataproducts/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
def atlas_query(min_date_mjd, max_date_mjd, target_id, data_product_type):
print("Calling atlas query!")
target = Target.objects.get(pk=target_id)
headers = {"Authorization": f"Token {settings.FORCED_PHOTOMETRY_SERVICES.get('atlas', {}).get('api_key')}", "Accept": "application/json"}
headers = {"Authorization": f"Token {settings.FORCED_PHOTOMETRY_SERVICES.get('atlas', {}).get('api_key')}",
"Accept": "application/json"}
base_url = settings.FORCED_PHOTOMETRY_SERVICES.get('atlas', {}).get('url')
task_url = None
while not task_url:
with requests.Session() as s:
task_data = {"ra":target.ra, "dec": target.dec, "mjd_min": min_date_mjd, "send_email": False}
task_data = {"ra": target.ra, "dec": target.dec, "mjd_min": min_date_mjd, "send_email": False}
if max_date_mjd:
task_data['mjd_max'] = max_date_mjd
resp = s.post(
Expand Down Expand Up @@ -81,11 +82,11 @@ def atlas_query(min_date_mjd, max_date_mjd, target_id, data_product_type):
dp_name = f"atlas_{Time(min_date_mjd, format='mjd').strftime('%Y_%m_%d')}"
if max_date_mjd:
dp_name += f"-{Time(max_date_mjd, format='mjd').strftime('%Y_%m_%d')}"
dp_name += f"_{urlparse(result_url)[2].rpartition('/')[2]}"
dp_name += f"_{urlparse(result_url)[2].rpartition('/')[2]}"
file = ContentFile(results.content, name=dp_name)

dp = DataProduct.objects.create(
product_id = dp_name,
product_id=dp_name,
target=target,
data=file,
data_product_type=data_product_type,
Expand All @@ -99,4 +100,4 @@ def atlas_query(min_date_mjd, max_date_mjd, target_id, data_product_type):
logger.error(f"Error processing returned Atlas data into ReducedDatums: {repr(e)}")
return False

return True
return True
8 changes: 5 additions & 3 deletions tom_dataproducts/templatetags/dataproduct_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ def dataproduct_list_all(context):
'products': products,
}


@register.inclusion_tag('tom_dataproducts/partials/query_forced_photometry.html')
def query_forced_photometry(target):
services = get_service_classes().keys()
return {'forced_photometry_services': services,
'target': target
}
return {
'forced_photometry_services': services,
'target': target
}


@register.inclusion_tag('tom_dataproducts/partials/upload_dataproduct.html', takes_context=True)
Expand Down
3 changes: 2 additions & 1 deletion tom_dataproducts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
path('data/group/<int:pk>/delete/', DataProductGroupDeleteView.as_view(), name='group-delete'),
path('data/upload/', DataProductUploadView.as_view(), name='upload'),
path('data/reduced/update/', UpdateReducedDataView.as_view(), name='update-reduced-data'),
path('data/forced_photometry/<str:service>/query/', ForcedPhotometryQueryView.as_view(), name='forced-photometry-query'),
path('data/forced_photometry/<str:service>/query/', ForcedPhotometryQueryView.as_view(),
name='forced-photometry-query'),
path('data/<int:pk>/delete/', DataProductDeleteView.as_view(), name='delete'),
path('data/<int:pk>/feature/', DataProductFeatureView.as_view(), name='feature'),
path('data/<int:dp_pk>/share/', DataShareView.as_view(), name='share'),
Expand Down
6 changes: 3 additions & 3 deletions tom_dataproducts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from tom_observations.models import ObservationRecord
from tom_observations.facility import get_service_class
from tom_dataproducts.serializers import DataProductSerializer
from tom_dataproducts.forced_photometry.forced_photometry_service import ForcedPhotometryServiceException, get_service_class
import tom_dataproducts.forced_photometry.forced_photometry_service as fps

import requests

Expand Down Expand Up @@ -122,7 +122,7 @@ def get_service_class(self):
"""
Gets the forced photometry service class
"""
return get_service_class(self.get_service())
return fps.get_service_class(self.get_service())

def get_form_class(self):
"""
Expand Down Expand Up @@ -157,7 +157,7 @@ def post(self, request, *args, **kwargs):
service = self.get_service_class()()
try:
service.query_service(form.cleaned_data)
except ForcedPhotometryServiceException as e:
except fps.ForcedPhotometryServiceException as e:
form.add_error(f"Problem querying forced photometry service: {repr(e)}")
return self.form_invalid(form)
messages.info(self.request, service.get_success_message())
Expand Down

0 comments on commit 9a1fde4

Please sign in to comment.