Skip to content
Snippets Groups Projects
Commit ceca719c authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

TMSS-369: inject the schema URL upon uploading, not upon receiving a POST.

parent cc68babc
No related branches found
No related tags found
1 merge request!220Resolve TMSS-369
...@@ -3,6 +3,7 @@ logger = logging.getLogger(__name__) ...@@ -3,6 +3,7 @@ logger = logging.getLogger(__name__)
import json import json
from lofar.sas.tmss.client.tmss_http_rest_client import TMSSsession from lofar.sas.tmss.client.tmss_http_rest_client import TMSSsession
from lofar.common import json_utils
import os import os
def populate_schemas_main(): def populate_schemas_main():
...@@ -32,36 +33,45 @@ def populate_schemas(schema_dir: str=None, templates_filename: str=None): ...@@ -32,36 +33,45 @@ def populate_schemas(schema_dir: str=None, templates_filename: str=None):
templates = json.loads(templates_file.read()) templates = json.loads(templates_file.read())
with TMSSsession.create_from_dbcreds_for_ldap() as client: with TMSSsession.create_from_dbcreds_for_ldap() as client:
base_url = client.base_url.rstrip('/').rstrip('api').rstrip('/')
for template in templates: for template in templates:
try: try:
with open(os.path.join(schema_dir, template.pop('file_name'))) as schema_file: with open(os.path.join(schema_dir, template.pop('file_name'))) as schema_file:
try: try:
json_schema = json.loads(schema_file.read()) json_schema = json.loads(schema_file.read())
template_path = template.pop('template') template_name = template.pop('template')
name = template.pop('name', json_schema.get('title', '<no name>')) name = template.pop('name', json_schema.get('title', '<no name>'))
description = template.pop('description', json_schema.get('description', '<no description>')) description = template.pop('description', json_schema.get('description', '<no description>'))
version = template.pop('version', '1') version = template.pop('version', '1')
if template_path == 'subtask_template' and 'type' in template: if template_name == 'subtask_template' and 'type' in template:
# override plain-text type by its url # override plain-text type by its url
template['type'] = client.get_path_as_json_object('subtask_type/'+template.pop('type'))['url'] template['type'] = client.get_path_as_json_object('subtask_type/'+template.pop('type'))['url']
if template_path == 'task_template' and 'type' in template: if template_name == 'task_template' and 'type' in template:
# override plain-text type by its url # override plain-text type by its url
template['type'] = client.get_path_as_json_object('task_type/'+template.pop('type'))['url'] template['type'] = client.get_path_as_json_object('task_type/'+template.pop('type'))['url']
if template_path == 'scheduling_unit_observing_strategy_template': if template_name == 'scheduling_unit_observing_strategy_template':
template['template'] = json_schema
scheduling_unit_templates = client.get_path_as_json_object('scheduling_unit_template?name=' + template.pop('scheduling_unit_template_name') + '&version=' + template.pop('scheduling_unit_template_version')) scheduling_unit_templates = client.get_path_as_json_object('scheduling_unit_template?name=' + template.pop('scheduling_unit_template_name') + '&version=' + template.pop('scheduling_unit_template_version'))
scheduling_unit_template = scheduling_unit_templates[0] scheduling_unit_template = scheduling_unit_templates[0]
template['scheduling_unit_template'] = scheduling_unit_template['url'] template['scheduling_unit_template'] = scheduling_unit_template['url']
# inject a unique id in the form of a unique URL to this schema
json_schema['$id'] = '%s/api/schemas/%s/%s/%s' % (base_url, template_name.replace('_',''), name, version)
# make sure that all urls point to the tmss base_url
json_schema = json_utils.replace_host_in_urls(json_schema, new_base_url=base_url)
if template_name == 'scheduling_unit_observing_strategy_template':
template['template'] = json_schema
else: else:
template['schema'] = json_schema template['schema'] = json_schema
logger.info("Uploading template template='%s' name='%s' version='%s'", template_path, name, version) logger.info("Uploading template template='%s' name='%s' version='%s'", template, name, version)
client.post_template(template_path=template_path, client.post_template(template_path=template_name,
name=name, name=name,
description=description, description=description,
version=version, version=version,
......
...@@ -41,17 +41,6 @@ class JSONEditorField(serializers.JSONField): ...@@ -41,17 +41,6 @@ class JSONEditorField(serializers.JSONField):
pass pass
return None return None
def to_internal_value(self, data):
json_data = super().to_internal_value(data)
try:
# make sure all referred URL's point to the current host
new_base_url = "%s://%s" % (self.context['request'].scheme, self.context['request'].get_host())
json_data = json_utils.replace_host_in_urls(json_data, new_base_url=new_base_url)
except Exception as e:
pass
return json_data
def to_representation(self, value): def to_representation(self, value):
'''create representation of the json-schema-value, '''create representation of the json-schema-value,
with all common json schema $ref's pointing to the correct host, with all common json schema $ref's pointing to the correct host,
......
...@@ -76,39 +76,6 @@ class LOFARCopyViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet): ...@@ -76,39 +76,6 @@ class LOFARCopyViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
class AbstractTemplateViewSet(LOFARViewSet): class AbstractTemplateViewSet(LOFARViewSet):
def _inject_id_in_schema(self, request):
'''inject a resolvable valid URL to the uploaded schema'''
schema = request.data['schema']
if isinstance(schema, str):
schema = json.loads(schema)
try:
# construct full url for $id of this schema
path = revese_url('get_template_json_schema', kwargs={'template': self.queryset.model._meta.model_name,
'name': request.data['name'],
'version': request.data.get('version', 1)}).rstrip('/')
schema['$id'] = '%s://%s%s' % (request.scheme, request.get_host(), path)
try:
# we explicitely want to override the uploaded schema with the $id-annotated one.
request.data._mutable = True
except:
pass
if isinstance(request.data['schema'], str):
schema = json.dumps(schema)
request.data['schema'] = schema
except Exception as e:
logger.error("Could not override schema $id with auto-generated url: %s", e)
def create(self, request, **kwargs):
self._inject_id_in_schema(request)
return super().create(request, **kwargs)
def update(self, request, pk=None, **kwargs):
self._inject_id_in_schema(request)
return super().update(request, pk, **kwargs)
def retrieve(self, request, pk=None, **kwargs): def retrieve(self, request, pk=None, **kwargs):
response = super().retrieve(request, pk, **kwargs) response = super().retrieve(request, pk, **kwargs)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment