Skip to content

Commit

Permalink
Register map aladin (#1143)
Browse files Browse the repository at this point in the history
* partial

* Closed #1142 was added the option to register Aladin images together with map processes.
  • Loading branch information
glaubervila authored Jun 5, 2019
1 parent 9cdc5ef commit a4298c2
Showing 1 changed file with 67 additions and 11 deletions.
78 changes: 67 additions & 11 deletions api/product_register/ImportProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
from rest_framework.response import Response

from .models import Site, Authorization, ExternalProcess, Export
from django.conf import settings
import os
import requests
from time import sleep
import zipfile
from urllib.parse import urljoin
from aladin.models import Image as AladinImage


class Import():
Expand All @@ -26,7 +33,7 @@ def __init__(self):
# 'vac': ProductModel
# })

self._products_classes = dict({})
self._products_classes = dict({})

def start_import(self, request):

Expand Down Expand Up @@ -168,7 +175,6 @@ def process_tags(self, process, tags, add_release=True):
except Tag.DoesNotExist:
raise Exception("this Tag '%s' is not valid." % tag_name)


def process_export(self, process, data):

# Associar Process a Export
Expand Down Expand Up @@ -196,8 +202,6 @@ def import_products(self, data):
else:
raise Exception("Product Type '%s' not implemented yet." % product.get('type'))



# Verificar a classe dos produtos se for galaxy cluster ou cluster members deve ser feita a ligacao entre as 2
if ('galaxy_clusters' in self._products_classes) and ('cluster_members' in self._products_classes):
self.link_galaxy_cluster_with_cluster_members()
Expand Down Expand Up @@ -252,7 +256,6 @@ def register_catalog(self, data):
else:
date = datetime.now()


# Verificar se o process id do produto e igual ao proccess id do external_proccess
# TODO esta etapa deve ser substituida com a implementacao de import inputs ou provenance
if self.process is not None and self.process.epr_original_id != str(data.get("process_id")):
Expand Down Expand Up @@ -320,7 +323,6 @@ def register_catalog(self, data):
# guarda a classe do produto e a instancia de ProductModel
self._products_classes[product.prd_class.pcl_name] = product


return True
else:
raise Exception(
Expand All @@ -335,7 +337,7 @@ def get_product_class(self, name):
acls = list()
for cls in ProductClass.objects.all():
acls.append(cls.pcl_name)
raise Exception('It is class is not available. these are available: %s' % (', '.join(acls)))
raise Exception('It is class is not available [ %s ]. these are available: %s' % (name, ', '.join(acls)))

def register_catalog_content(self, catalog, data, created):
"""
Expand Down Expand Up @@ -498,7 +500,6 @@ def link_galaxy_cluster_with_cluster_members(self):
"this cluster members product %s does not have a property with ucd meta.id.cross to be associated with "
"the galaxy cluster product." % cm.prd_display_name)


def link_galaxy_cluster_with_vac_cluster(self):
"""
Este metodo e especifico para o processo de WAZP.
Expand All @@ -521,11 +522,14 @@ def link_galaxy_cluster_with_vac_cluster(self):
}
)


# =============================< MAP >=============================

def register_map(self, data):

database = data.get('database', 'catalog')

if not self.db:
self.db = CatalogDB()
self.db = CatalogDB(db=database)

if not self.db.table_exists(data.get('table'), schema=data.get('schema', None)):
raise Exception("Table or view %s.%s does not exist" %
Expand All @@ -535,7 +539,7 @@ def register_map(self, data):
cls = self.get_product_class(data.get('class'))

# Recuperar o filtro
filter = self.get_filter(data.get('filter'))
filter = self.get_filter(data.get('map_filter', '-'))

tbl_info = data.get('ora_table_info', None)
tbl_rows = None
Expand Down Expand Up @@ -585,6 +589,11 @@ def register_map(self, data):
# Registrar O produto a seus respectivos Tags
if 'fields' in data:
self.product_tag(product, data.get('fields'), add_release)

# Registrar Imagem Aladin caso exista
if 'aladin' in data:
self.register_aladin_image(product, data.get('aladin'))

else:
raise Exception(
"A failure has occurred and it was not possible to register the product '%s'." % (data.get('name')))
Expand Down Expand Up @@ -623,7 +632,54 @@ def get_filter(self, filter_name):
a = ', '.join(a)
raise Exception("This filter '%s' is not valid. Available filters is [ %s ]" % (filter_name, a))

def register_aladin_image(self, product, data):

if 'donwload_url' in data and 'filename' in data:
relative_map_path = os.path.join(settings.DATA_DIR, "maps")
base_path = os.path.join(relative_map_path, product.prd_class.pcl_name)
if not os.path.exists(base_path):
os.mkdir(base_path)
sleep(1)

map_path = os.path.join(base_path, str(product.id))
if not os.path.exists(map_path):
os.mkdir(map_path)
sleep(1)

aladin_path_zip = self.download_aladin_image(data.get('donwload_url'), data.get('filename'), map_path)

with zipfile.ZipFile(aladin_path_zip, "r") as zip_ref:
zip_ref.extractall(map_path)

host = settings.BASE_HOST
result_dir = os.path.join(relative_map_path, aladin_path_zip.replace('.zip', ''))

result_sorce = result_dir.replace(settings.DATA_DIR, settings.DATA_SOURCE)

map_final_url = urljoin(host, result_sorce)

image, created = AladinImage.objects.update_or_create(
product=product, defaults={
'img_url': map_final_url,
})

def download_aladin_image(self, url, filename, path):

file_path = os.path.join(path, filename)

r = requests.get(url)
with open(file_path, "wb") as code:
code.write(r.content)

if os.path.exists(file_path):
return file_path

else:
# TODO lancar excessao por nao ter baixado o arquivo.
return None

# =============================< MASK >=============================

def register_mask(self, data):
if not self.db:
self.db = CatalogDB()
Expand Down

0 comments on commit a4298c2

Please sign in to comment.