Skip to content

Commit

Permalink
Merge pull request #921 from linea-it/develop
Browse files Browse the repository at this point in the history
v0.15.1
  • Loading branch information
glaubervila authored Dec 1, 2017
2 parents a7b07b6 + 0ab81bf commit 796f62f
Show file tree
Hide file tree
Showing 65 changed files with 15,419 additions and 810 deletions.
3 changes: 2 additions & 1 deletion api/dri/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@
router.register(r'userquery_validate', userquery_views.QueryValidate, base_name='validate_query')
router.register(r'userquery_preview', userquery_views.QueryPreview, base_name='preview_query')
router.register(r'userquery_create_table', userquery_views.CreateTable, base_name='create_table')
router.register(r'userquery_properties', userquery_views.TableProperties, base_name='table')
router.register(r'userquery_property', userquery_views.TableProperties, base_name='table')
router.register(r'userquery_target', userquery_views.TargetViewerRegister, base_name='target_viewer_register')

# Aladin API
router.register(r'aladin/image', aladin_views.ImageViewSet)
Expand Down
20 changes: 17 additions & 3 deletions api/lib/sqlalchemy_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ def get_engine(self):
def get_dialect(self):
return oracle

def get_raw_sql_limit(self, line_number):
return "FETCH FIRST %s ROWS ONLY" % line_number
def get_raw_sql_limit(self, offset, limit):
return "OFFSET %s ROWS FETCH NEXT %s ROWS ONLY" % (offset, limit)

def get_raw_sql_table_properties_(self, table, schema=None):
sql = "SELECT column_name, data_type FROM all_tab_columns WHERE table_name = '%s'" % table
if schema:
sql += " AND owner = '%s'" % schema

return sql


class DBSqlite:
Expand All @@ -58,6 +65,9 @@ def get_dialect(self):
def get_raw_sql_limit(self, line_number):
return "LIMIT(%s)" % line_number

def get_table_properties(self, table, schema=None):
raise("Implement this method")


# classe generica - nao ligada a este problema
class DBBase:
Expand Down Expand Up @@ -131,6 +141,10 @@ def get_table_columns(self, table, schema=None):

return [value['name'] for value in self.inspect.get_columns(table, schema)]

def get_table_properties(self, table, schema=None):
sql = self.database.get_raw_sql_table_properties_(table, schema=schema)
return self.fetchall_dict(sql)

def get_count(self, table, schema=None):
with self.engine.connect() as con:
with warnings.catch_warnings():
Expand Down Expand Up @@ -278,7 +292,7 @@ def __init__(self, table, schema=None):
@compiles(DropTable)
def _drop_table(element, compiler, **kw):
_schema = "%s." % element.schema if element.schema is not None else ''
return "DROP TABLE %s%s" % (_schema, element.name)
return "DROP TABLE %s%s" % (_schema, element.table)

def drop_table(self, table, schema=None):
"""
Expand Down
28 changes: 20 additions & 8 deletions api/userquery/create_table_as.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
from common.notify import Notify
from userquery.models import Job
from userquery.models import Table
from product.models import Product

from .target_viewer import register_table_in_the_target_viewer


class CreateTableAs:
def __init__(self, job_id, user_id, table_name, schema=None, timeout=None):
def __init__(self, job_id, user_id, table_name, associate_target_viewer, schema=None, timeout=None):
self.table_name = table_name
self.schema = schema
self.timeout = timeout
self.associate_target_viewer = associate_target_viewer

self.user = User.objects.get(pk=user_id)
self.job = Job.objects.get(pk=job_id)
Expand All @@ -30,11 +34,13 @@ def __init__(self, job_id, user_id, table_name, schema=None, timeout=None):
# state variables
self.is_table_successfully_created = False
self.error_message = None
self.table = None

def do_all(self):
self._notify_by_email_start()
self._update_job_status_before_table_creation()
self._create_table_by_job_id()
self._associate_target_viewer()
self._update_job_status_after_table_creation_attempt()
self._send_notifications_by_email_after_table_creation_attempt()

Expand All @@ -58,21 +64,27 @@ def _send_notifications_by_email_after_table_creation_attempt(self):
self._notify_user_by_email_failure(self.error_message)

def _create_table_by_job_id(self):
self.logger.debug("_create_table_by_job_id - job_id: %s" % self.job.pk)
self.logger.info("_create_table_by_job_id - job_id: %s" % self.job.pk)

db = DBBase('catalog')
try:
db.create_table_raw_sql(self.table_name, self.job.sql_sentence, schema=self.schema, timeout=self.timeout)
self.is_table_successfully_created = True

table = Table(table_name=self.table_name,
display_name=self.job.display_name,
owner=self.job.owner,
schema=self.schema)
table.save()
self.table = Table(table_name=self.table_name,
display_name=self.job.display_name,
owner=self.job.owner,
schema=self.schema)
self.table.save()
except Exception as e:
self.error_message = str(e)
self.logger.debug("CreateTableAs Error: %s" % self.error_message)
self.logger.info("CreateTableAs Error: %s" % self.error_message)

db.drop_table(self.table_name, schema=self.schema)

def _associate_target_viewer(self):
if self.associate_target_viewer:
register_table_in_the_target_viewer(self.user, self.table.pk)

def _notify_by_email_start(self):
if self.user.email:
Expand Down
20 changes: 20 additions & 0 deletions api/userquery/migrations/0021_auto_20171129_1218.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2017-11-29 12:18
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('userquery', '0020_auto_20171116_1858'),
]

operations = [
migrations.AlterField(
model_name='query',
name='description',
field=models.CharField(max_length=256, null=True, verbose_name='Description'),
),
]
20 changes: 20 additions & 0 deletions api/userquery/migrations/0022_auto_20171129_1223.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2017-11-29 12:23
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('userquery', '0021_auto_20171129_1218'),
]

operations = [
migrations.AlterField(
model_name='query',
name='description',
field=models.CharField(blank=True, max_length=256, null=True, verbose_name='Description'),
),
]
22 changes: 22 additions & 0 deletions api/userquery/migrations/0023_table_product_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2017-11-30 17:21
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('product', '0081_cutoutjob_cjb_image_formats'),
('userquery', '0022_auto_20171129_1223'),
]

operations = [
migrations.AddField(
model_name='table',
name='product_id',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='product_id', to='product.Product', verbose_name='Product'),
),
]
21 changes: 21 additions & 0 deletions api/userquery/migrations/0024_auto_20171130_1746.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2017-11-30 17:46
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('userquery', '0023_table_product_id'),
]

operations = [
migrations.AlterField(
model_name='table',
name='product_id',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='product', to='product.Product', verbose_name='Product'),
),
]
20 changes: 20 additions & 0 deletions api/userquery/migrations/0025_auto_20171130_1757.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2017-11-30 17:57
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('userquery', '0024_auto_20171130_1746'),
]

operations = [
migrations.RenameField(
model_name='table',
old_name='product_id',
new_name='product',
),
]
5 changes: 4 additions & 1 deletion api/userquery/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@


class Query(models.Model):
# the same user can't have repeated names.
name = models.CharField(
max_length=128, null=False, verbose_name='Name')
description = models.CharField(
max_length=256, null=False, verbose_name='Description')
max_length=256, null=True, blank=True, verbose_name='Description')
owner = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
Expand Down Expand Up @@ -76,3 +77,5 @@ class Table(models.Model):
verbose_name='Owner', default=None)
schema = models.CharField(
max_length=128, null=True, verbose_name='Schema')
product = models.ForeignKey(
'product.Product', verbose_name='Product', related_name='product', null=True, default=None)
6 changes: 5 additions & 1 deletion api/userquery/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework import serializers
from .models import *
from product.models import Product


class QuerySerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -42,6 +43,8 @@ class Meta:
class TableSerializer(serializers.HyperlinkedModelSerializer):
owner = serializers.ReadOnlyField(source='owner.username')
id = serializers.ReadOnlyField()
product_id = serializers.PrimaryKeyRelatedField(
queryset=Product.objects.all(), many=False)

class Meta:
model = Table
Expand All @@ -50,5 +53,6 @@ class Meta:
'table_name',
'display_name',
'owner',
'schema'
'schema',
'product_id'
)
55 changes: 55 additions & 0 deletions api/userquery/target_viewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import logging

from product_register.ImportProcess import Import
from product.models import Product
from userquery.models import Table


def register_table_in_the_target_viewer(user, table_pk, description=None):
logger = logging.getLogger('register_table_in_the_target_viewer')
logger.info("Register the new table as a product")

table = Table.objects.get(pk=table_pk)

# Dados para o registro
data = list([{
"process_id": None,
"name": table.table_name,
"display_name": table.display_name,
"database": 'catalog',
"schema": table.schema,
"table": table.table_name,
"filter": [],
# review
"releases": [],
"fields": [],
"association": [],
"type": "catalog",
"class": "objects",
"description": description
}])

logger.debug("Register Data")
logger.debug(data)

# Registar o novo produto
import_product = Import()

import_product.user = user
import_product.owner = user
import_product.site = None
import_product.process = None

import_product.import_products(data)

product = Product.objects.get(
prd_display_name=table.display_name,
table__tbl_name=table.table_name,
table__tbl_schema=table.schema,
table__tbl_database='catalog'
)

table.product = product
table.save()

logger.info("New Product -id %s- was Registered" % product.pk)
4 changes: 2 additions & 2 deletions api/userquery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


@shared_task(name="create_table")
def create_table(job_id, user_id, table_name, schema=None, timeout=None):
create_table_as = CreateTableAs(job_id, user_id, table_name, schema=schema, timeout=timeout)
def create_table(job_id, user_id, table_name, associate_target_viewer, schema=None, timeout=None):
create_table_as = CreateTableAs(job_id, user_id, table_name, associate_target_viewer, schema=schema, timeout=timeout)
logger = create_table_as.logger

logger.info("Task create_table_as has started")
Expand Down
6 changes: 0 additions & 6 deletions api/userquery/templates/job_notification_finish.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ <h1 style="font-family: Bitter, Georgia, serif; font-size: 16px; font-weight: bo
<tr>
<td>The table {{ table_name }} is available</td>
</tr>
<tr>
<td>and can be accessed at this link</td>
</tr>
<tr>
<td><a target="_blank" href="{{ url }}">{{ url }}</a></td>
</tr>
</tbody>
</table>
</td>
Expand Down
Loading

0 comments on commit 796f62f

Please sign in to comment.