Skip to content

Commit

Permalink
Merge pull request #532 from oda-hub/endpoint-astro-entity-info
Browse files Browse the repository at this point in the history
Endpoint astro entity info
  • Loading branch information
burnout87 committed May 4, 2023
2 parents 6c59322 + b693836 commit fe7aaf5
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 31 deletions.
37 changes: 19 additions & 18 deletions cdci_data_analysis/analysis/drupal_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from ..flask_app.sentry import sentry

import sentry_sdk
from dateutil import parser, tz
from datetime import datetime
from enum import Enum, auto
Expand Down Expand Up @@ -846,7 +845,7 @@ def get_data_product_list_by_source_name(product_gallery_url, gallery_jwt_token,
return product_list
headers = get_drupal_request_headers(gallery_jwt_token)

source_entity_list = get_source_astrophysical_entity_id_by_source_and_alternative_name(product_gallery_url,
source_entity_list = get_source_astrophysical_entity_info_by_source_and_alternative_name(product_gallery_url,
gallery_jwt_token,
source_name=src_name,
sentry_dsn=sentry_dsn)
Expand All @@ -855,19 +854,20 @@ def get_data_product_list_by_source_name(product_gallery_url, gallery_jwt_token,
if len(source_entity_list) >= 1:
source_entity_id = source_entity_list[0]['nid']

log_res = execute_drupal_request(f"{product_gallery_url}/data_products/source_products/{source_entity_id}",
headers=headers,
sentry_dsn=sentry_dsn)
output_get = analyze_drupal_output(log_res, operation_performed="retrieving the astrophysical entity information")
if isinstance(output_get, list):
for obj in output_get:
refactored_obj = {}
for k, v in obj.items():
refactored_key = k
if k.startswith('field_'):
refactored_key = k.replace('field_', '')
refactored_obj[refactored_key] = v
product_list.append(refactored_obj)
if source_entity_id is not None:
log_res = execute_drupal_request(f"{product_gallery_url}/data_products/source_products/{source_entity_id}",
headers=headers,
sentry_dsn=sentry_dsn)
output_get = analyze_drupal_output(log_res, operation_performed="retrieving the astrophysical entity information")
if isinstance(output_get, list):
for obj in output_get:
refactored_obj = {}
for k, v in obj.items():
refactored_key = k
if k.startswith('field_'):
refactored_key = k.replace('field_', '')
refactored_obj[refactored_key] = v
product_list.append(refactored_obj)

return product_list

Expand Down Expand Up @@ -902,8 +902,9 @@ def get_source_astrophysical_entity_id_by_source_name(product_gallery_url, galle
return entities_id


def get_source_astrophysical_entity_id_by_source_and_alternative_name(product_gallery_url, gallery_jwt_token, source_name=None, sentry_dsn=None) \
-> Optional[str]:
# TODO to verify if it will always return one single object for a single source name (or alternative name)
def get_source_astrophysical_entity_info_by_source_and_alternative_name(product_gallery_url, gallery_jwt_token, source_name=None, sentry_dsn=None) \
-> Optional[list]:
# get from the drupal the relative id
headers = get_drupal_request_headers(gallery_jwt_token)

Expand Down Expand Up @@ -1173,7 +1174,7 @@ def post_data_product_to_gallery(product_gallery_url, gallery_jwt_token, convert
arg_source_coord = {}
if source_coord_obj_list is not None and source_coord_obj_list[src_name_idx] != {}:
arg_source_coord = source_coord_obj_list[src_name_idx]
source_entity_list = get_source_astrophysical_entity_id_by_source_and_alternative_name(product_gallery_url, gallery_jwt_token,
source_entity_list = get_source_astrophysical_entity_info_by_source_and_alternative_name(product_gallery_url, gallery_jwt_token,
source_name=src_name,
sentry_dsn=sentry_dsn)
source_entity_id = None
Expand Down
4 changes: 3 additions & 1 deletion cdci_data_analysis/analysis/tokenHelper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import jwt
import oda_api.token

from marshmallow import ValidationError
from typing import Tuple, Optional, Union

from cdci_data_analysis.analysis.exceptions import BadRequest
from cdci_data_analysis.flask_app.schemas import EmailOptionsTokenSchema
Expand Down Expand Up @@ -111,7 +113,7 @@ def mutate_token_email_payload(token_payload):
return updated_token


def validate_token_from_request(token, secret_key, required_roles=None, action=""):
def validate_token_from_request(token, secret_key, required_roles=None, action="") -> Tuple[Union[str, dict, None], Optional[int]]:
if token is None:
return 'A token must be provided.', 403
try:
Expand Down
61 changes: 61 additions & 0 deletions cdci_data_analysis/flask_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,67 @@ def get_all_astro_entities():
return output_list


@app.route('/get_astro_entity_info_by_source_name', methods=['GET'])
def get_astro_entity_info_by_source_name():
logger.info("request.args: %s ", request.args)
logger.info("request.files: %s ", request.files)

app_config = app.config.get('conf')
# token = request.args.get('token', None)
# secret_key = app_config.secret_key
#
# output, output_code = tokenHelper.validate_token_from_request(token=token, secret_key=secret_key,
# action="getting the info of an astro entity from the product gallery")
#
# if output_code is not None:
# if output is None or not isinstance(output, str):
# output = 'Error while validating the token'
# return make_response(output, output_code)
#
# decoded_token = output
#
# par_dic = request.values.to_dict()
# par_dic.pop('token')

sentry_dsn = sentry.sentry_url

gallery_secret_key = app_config.product_gallery_secret_key
product_gallery_url = app_config.product_gallery_url
# user_email = tokenHelper.get_token_user_email_address(decoded_token)
# user_id_product_creator = drupal_helper.get_user_id(product_gallery_url=product_gallery_url,
# user_email=user_email,
# sentry_dsn=sentry_dsn)
# update the token
# gallery_jwt_token = drupal_helper.generate_gallery_jwt_token(gallery_secret_key, user_id=user_id_product_creator)

src_name = request.args.get('src_name', None)

source_entity_info = drupal_helper.get_source_astrophysical_entity_info_by_source_and_alternative_name(product_gallery_url,
gallery_jwt_token=None,
source_name=src_name,
sentry_dsn=sentry_dsn)

refactored_astro_entity_info = {}
astro_entity_info = {}
if len(source_entity_info) >= 1:
astro_entity_info = source_entity_info[0]

for k, v in astro_entity_info.items():
refactored_key = k
if k.startswith('field_'):
refactored_key = k.replace('field_', '')
refactored_astro_entity_info[refactored_key] = v
if 'nid' in astro_entity_info:
nid = astro_entity_info['nid']

refactored_astro_entity_info['url_preview'] = os.path.join(product_gallery_url, f'node/{nid}/preview')

output_obj = json.dumps(refactored_astro_entity_info)

return output_obj



@app.route('/get_data_product_list_by_source_name', methods=['GET'])
def get_data_product_list_by_source_name():
logger.info("request.args: %s ", request.args)
Expand Down
2 changes: 1 addition & 1 deletion tests/observation_yaml_dummy_files/obs_rev_2542.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1770,4 +1770,4 @@
- 254200330010.0
title: Orion_3
tstart: '2022-08-27T22:00:00.999'
tstop: '2022-08-28T03:42:57.000'
tstop: '2022-08-28T03:42:57.000'
78 changes: 67 additions & 11 deletions tests/test_server_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2287,7 +2287,7 @@ def test_product_gallery_get_data_products_list_for_given_source(dispatcher_live
'instrument': 'empty',
'ra': 150,
'dec': 350,
'src_name': source_params['src_name'],
'src_name': source_name,
'content_type': 'data_product',
'token': encoded_token,
'insert_new_source': True
Expand All @@ -2297,6 +2297,7 @@ def test_product_gallery_get_data_products_list_for_given_source(dispatcher_live
)

assert c.status_code == 200

params = {
'token': encoded_token,
'src_name': source_name
Expand Down Expand Up @@ -2355,6 +2356,64 @@ def test_product_gallery_get_data_products_list_for_given_source(dispatcher_live
assert diff2 == set()
assert diff1 == set()


@pytest.mark.test_drupal
@pytest.mark.parametrize("source_name", ["known", "unknown"])
@pytest.mark.parametrize("anonymous", ["known", "unknown"])
def test_product_gallery_astro_entity_info(dispatcher_live_fixture_with_gallery, dispatcher_test_conf_with_gallery, source_name, anonymous):
server = dispatcher_live_fixture_with_gallery

logger.info("constructed server: %s", server)

# let's generate a valid token
token_payload = {
**default_token_payload,
"roles": "general, gallery contributor",
}
encoded_token = jwt.encode(token_payload, secret_key, algorithm='HS256')

if source_name == 'unknown':
source_name = 'test astro entity' + '_' + str(uuid.uuid4())

params = {
'src_name': source_name
}
if not anonymous:
params['token'] = encoded_token

c = requests.get(os.path.join(server, "get_astro_entity_info_by_source_name"),
params=params
)

assert c.status_code == 200
drupal_res_obj = c.json()

assert drupal_res_obj == {}

else:
source_name = "V404 Cyg"
params = {
'src_name': source_name
}

if not anonymous:
params['token'] = encoded_token

c = requests.get(os.path.join(server, "get_astro_entity_info_by_source_name"),
params=params
)

assert c.status_code == 200
drupal_res_obj_source_name = c.json()
assert 'source_ra' in drupal_res_obj_source_name
assert 'source_dec' in drupal_res_obj_source_name
assert 'alternative_names_long_str' in drupal_res_obj_source_name
assert 'title' in drupal_res_obj_source_name
assert 'url' in drupal_res_obj_source_name
assert 'url_preview' in drupal_res_obj_source_name
assert 'nid' in drupal_res_obj_source_name


@pytest.mark.test_drupal
def test_product_gallery_get_period_of_observation_attachments(dispatcher_live_fixture_with_gallery, dispatcher_test_conf_with_gallery):
server = dispatcher_live_fixture_with_gallery
Expand All @@ -2377,13 +2436,14 @@ def test_product_gallery_get_period_of_observation_attachments(dispatcher_live_f
'T2': now.strftime('%Y-%m-%dT%H:%M:%S')
}

file_obj = {'yaml_file_0': open('observation_yaml_dummy_files/obs_rev_2542.yaml', 'rb'),
'yaml_file_1': open('observation_yaml_dummy_files/obs_rev_1.yaml', 'rb')}
with open('observation_yaml_dummy_files/obs_rev_2542.yaml', 'rb') as yaml_test_fn:
file_obj = {'yaml_file_0': yaml_test_fn}

c = requests.post(os.path.join(server, "post_observation_to_gallery"),
params={**params},
files=file_obj
)

c = requests.post(os.path.join(server, "post_observation_to_gallery"),
params={**params},
files=file_obj
)

assert c.status_code == 200

Expand All @@ -2401,10 +2461,6 @@ def test_product_gallery_get_period_of_observation_attachments(dispatcher_live_f
with open('observation_yaml_dummy_files/obs_rev_2542.yaml', 'r') as f_yaml_file_yaml_file_content_obs_rev_2542:
yaml_file_content_obs_rev_2542 = f_yaml_file_yaml_file_content_obs_rev_2542.read()

with open('observation_yaml_dummy_files/obs_rev_1.yaml', 'r') as f_yaml_file_yaml_file_content_obs_rev_1:
yaml_file_content_obs_rev_1 = f_yaml_file_yaml_file_content_obs_rev_1.read()

assert yaml_file_content_obs_rev_1 in drupal_res_obj['file_content']
assert yaml_file_content_obs_rev_2542 in drupal_res_obj['file_content']


Expand Down

0 comments on commit fe7aaf5

Please sign in to comment.