|
|
@ -18,12 +18,12 @@ _logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
@api.model |
|
|
|
def _instanciate(self, model_data): |
|
|
|
"""Return a class for the custom model given by |
|
|
|
parameters ``model_data``.""" |
|
|
|
""" Return a class for the custom model given by |
|
|
|
parameters ``model_data``. """ |
|
|
|
# This monkey patch is meant to avoid create/search tables for those |
|
|
|
# materialized views. Doing "super" doesn't work. |
|
|
|
class CustomModel(models.Model): |
|
|
|
_name = pycompat.to_native(model_data["model"]) |
|
|
|
_name = pycompat.to_text(model_data["model"]) |
|
|
|
_description = model_data["name"] |
|
|
|
_module = False |
|
|
|
_custom = True |
|
|
@ -43,6 +43,7 @@ IrModel._instanciate = _instanciate |
|
|
|
|
|
|
|
class BiSQLView(models.Model): |
|
|
|
_name = "bi.sql.view" |
|
|
|
_description = "BI SQL View" |
|
|
|
_order = "sequence" |
|
|
|
_inherit = ["sql.request.mixin"] |
|
|
|
|
|
|
@ -204,7 +205,6 @@ class BiSQLView(models.Model): |
|
|
|
|
|
|
|
# Constrains Section |
|
|
|
@api.constrains("is_materialized") |
|
|
|
@api.multi |
|
|
|
def _check_index_materialized(self): |
|
|
|
for rec in self.filtered(lambda x: not x.is_materialized): |
|
|
|
if rec.bi_sql_view_field_ids.filtered(lambda x: x.is_index): |
|
|
@ -213,7 +213,6 @@ class BiSQLView(models.Model): |
|
|
|
) |
|
|
|
|
|
|
|
@api.constrains("view_order") |
|
|
|
@api.multi |
|
|
|
def _check_view_order(self): |
|
|
|
for rec in self: |
|
|
|
if rec.view_order: |
|
|
@ -225,7 +224,6 @@ class BiSQLView(models.Model): |
|
|
|
|
|
|
|
# Compute Section |
|
|
|
@api.depends("bi_sql_view_field_ids.graph_type") |
|
|
|
@api.multi |
|
|
|
def _compute_computed_action_context(self): |
|
|
|
for rec in self: |
|
|
|
action = { |
|
|
@ -251,7 +249,6 @@ class BiSQLView(models.Model): |
|
|
|
rec.computed_action_context = str(action) |
|
|
|
|
|
|
|
@api.depends("is_materialized") |
|
|
|
@api.multi |
|
|
|
def _compute_materialized_text(self): |
|
|
|
for sql_view in self: |
|
|
|
sql_view.materialized_text = ( |
|
|
@ -259,7 +256,6 @@ class BiSQLView(models.Model): |
|
|
|
) |
|
|
|
|
|
|
|
@api.depends("technical_name") |
|
|
|
@api.multi |
|
|
|
def _compute_view_name(self): |
|
|
|
for sql_view in self: |
|
|
|
sql_view.view_name = "{}{}".format( |
|
|
@ -268,7 +264,6 @@ class BiSQLView(models.Model): |
|
|
|
) |
|
|
|
|
|
|
|
@api.depends("technical_name") |
|
|
|
@api.multi |
|
|
|
def _compute_model_name(self): |
|
|
|
for sql_view in self: |
|
|
|
sql_view.model_name = "{}{}".format( |
|
|
@ -282,7 +277,6 @@ class BiSQLView(models.Model): |
|
|
|
self.has_group_changed = True |
|
|
|
|
|
|
|
# Overload Section |
|
|
|
@api.multi |
|
|
|
def write(self, vals): |
|
|
|
res = super(BiSQLView, self).write(vals) |
|
|
|
if vals.get("sequence", False): |
|
|
@ -290,7 +284,6 @@ class BiSQLView(models.Model): |
|
|
|
rec.menu_id.sequence = rec.sequence |
|
|
|
return res |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def unlink(self): |
|
|
|
if any(view.state not in ("draft", "sql_valid") for view in self): |
|
|
|
raise UserError( |
|
|
@ -302,7 +295,6 @@ class BiSQLView(models.Model): |
|
|
|
self.cron_id.unlink() |
|
|
|
return super(BiSQLView, self).unlink() |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def copy(self, default=None): |
|
|
|
self.ensure_one() |
|
|
|
default = dict(default or {}) |
|
|
@ -315,7 +307,6 @@ class BiSQLView(models.Model): |
|
|
|
return super(BiSQLView, self).copy(default=default) |
|
|
|
|
|
|
|
# Action Section |
|
|
|
@api.multi |
|
|
|
def button_create_sql_view_and_model(self): |
|
|
|
for sql_view in self.filtered(lambda x: x.state == "sql_valid"): |
|
|
|
# Create ORM and access |
|
|
@ -335,7 +326,6 @@ class BiSQLView(models.Model): |
|
|
|
sql_view.cron_id.active = True |
|
|
|
sql_view.state = "model_valid" |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def button_set_draft(self): |
|
|
|
for sql_view in self.filtered(lambda x: x.state != "draft"): |
|
|
|
sql_view.menu_id.unlink() |
|
|
@ -360,7 +350,6 @@ class BiSQLView(models.Model): |
|
|
|
super(BiSQLView, sql_view).button_set_draft() |
|
|
|
return True |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def button_create_ui(self): |
|
|
|
self.tree_view_id = self.env["ir.ui.view"].create(self._prepare_tree_view()).id |
|
|
|
self.graph_view_id = ( |
|
|
@ -378,17 +367,14 @@ class BiSQLView(models.Model): |
|
|
|
self.menu_id = self.env["ir.ui.menu"].create(self._prepare_menu()).id |
|
|
|
self.write({"state": "ui_valid"}) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def button_update_model_access(self): |
|
|
|
self._drop_model_access() |
|
|
|
self._create_model_access() |
|
|
|
self.write({"has_group_changed": False}) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def button_refresh_materialized_view(self): |
|
|
|
self._refresh_materialized_view() |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def button_open_view(self): |
|
|
|
return { |
|
|
|
"type": "ir.actions.act_window", |
|
|
@ -398,7 +384,6 @@ class BiSQLView(models.Model): |
|
|
|
} |
|
|
|
|
|
|
|
# Prepare Function |
|
|
|
@api.multi |
|
|
|
def _prepare_model(self): |
|
|
|
self.ensure_one() |
|
|
|
field_id = [] |
|
|
@ -413,7 +398,6 @@ class BiSQLView(models.Model): |
|
|
|
"field_id": field_id, |
|
|
|
} |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_model_access(self): |
|
|
|
self.ensure_one() |
|
|
|
res = [] |
|
|
@ -431,7 +415,6 @@ class BiSQLView(models.Model): |
|
|
|
) |
|
|
|
return res |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_cron(self): |
|
|
|
now = datetime.now() |
|
|
|
return { |
|
|
@ -449,7 +432,6 @@ class BiSQLView(models.Model): |
|
|
|
"active": True, |
|
|
|
} |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_rule(self): |
|
|
|
self.ensure_one() |
|
|
|
return { |
|
|
@ -459,7 +441,6 @@ class BiSQLView(models.Model): |
|
|
|
"global": True, |
|
|
|
} |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_tree_view(self): |
|
|
|
self.ensure_one() |
|
|
|
return { |
|
|
@ -473,7 +454,6 @@ class BiSQLView(models.Model): |
|
|
|
), |
|
|
|
} |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_graph_view(self): |
|
|
|
self.ensure_one() |
|
|
|
return { |
|
|
@ -487,7 +467,6 @@ class BiSQLView(models.Model): |
|
|
|
), |
|
|
|
} |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_pivot_view(self): |
|
|
|
self.ensure_one() |
|
|
|
return { |
|
|
@ -501,7 +480,6 @@ class BiSQLView(models.Model): |
|
|
|
), |
|
|
|
} |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_search_view(self): |
|
|
|
self.ensure_one() |
|
|
|
return { |
|
|
@ -524,7 +502,6 @@ class BiSQLView(models.Model): |
|
|
|
), |
|
|
|
} |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_action(self): |
|
|
|
self.ensure_one() |
|
|
|
view_mode = self.view_order |
|
|
@ -548,7 +525,6 @@ class BiSQLView(models.Model): |
|
|
|
"context": str(action), |
|
|
|
} |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_action_name(self): |
|
|
|
self.ensure_one() |
|
|
|
if not self.is_materialized: |
|
|
@ -558,7 +534,6 @@ class BiSQLView(models.Model): |
|
|
|
datetime.utcnow().strftime(_("%m/%d/%Y %H:%M:%S UTC")), |
|
|
|
) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_menu(self): |
|
|
|
self.ensure_one() |
|
|
|
return { |
|
|
@ -573,7 +548,6 @@ class BiSQLView(models.Model): |
|
|
|
_logger.info("Executing SQL Request %s ..." % req) |
|
|
|
self.env.cr.execute(req) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _drop_view(self): |
|
|
|
for sql_view in self: |
|
|
|
self._log_execute( |
|
|
@ -582,7 +556,6 @@ class BiSQLView(models.Model): |
|
|
|
) |
|
|
|
sql_view.size = False |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _create_view(self): |
|
|
|
for sql_view in self: |
|
|
|
sql_view._drop_view() |
|
|
@ -595,7 +568,6 @@ class BiSQLView(models.Model): |
|
|
|
% (sql_view.materialized_text, sql_view.view_name, str(e)) |
|
|
|
) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _create_index(self): |
|
|
|
for sql_view in self: |
|
|
|
for sql_field in sql_view.bi_sql_view_field_ids.filtered( |
|
|
@ -606,7 +578,6 @@ class BiSQLView(models.Model): |
|
|
|
% (sql_field.index_name, sql_view.view_name, sql_field.name) |
|
|
|
) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _create_model_and_fields(self): |
|
|
|
for sql_view in self: |
|
|
|
# Create model |
|
|
@ -617,20 +588,17 @@ class BiSQLView(models.Model): |
|
|
|
req = "DROP TABLE %s" % sql_view.view_name |
|
|
|
self._log_execute(req) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _create_model_access(self): |
|
|
|
for sql_view in self: |
|
|
|
for item in sql_view._prepare_model_access(): |
|
|
|
self.env["ir.model.access"].create(item) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _drop_model_access(self): |
|
|
|
for sql_view in self: |
|
|
|
self.env["ir.model.access"].search( |
|
|
|
[("model_id", "=", sql_view.model_name)] |
|
|
|
).unlink() |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _drop_model_and_fields(self): |
|
|
|
for sql_view in self: |
|
|
|
if sql_view.rule_id: |
|
|
@ -638,7 +606,6 @@ class BiSQLView(models.Model): |
|
|
|
if sql_view.model_id: |
|
|
|
sql_view.model_id.with_context(_force_unlink=True).unlink() |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _hook_executed_request(self): |
|
|
|
self.ensure_one() |
|
|
|
req = ( |
|
|
@ -656,12 +623,10 @@ class BiSQLView(models.Model): |
|
|
|
self._log_execute(req) |
|
|
|
return self.env.cr.fetchall() |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_request_check_execution(self): |
|
|
|
self.ensure_one() |
|
|
|
return "CREATE VIEW {} AS ({});".format(self.view_name, self.query) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _prepare_request_for_execution(self): |
|
|
|
self.ensure_one() |
|
|
|
query = ( |
|
|
@ -684,7 +649,6 @@ class BiSQLView(models.Model): |
|
|
|
query, |
|
|
|
) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _check_execution(self): |
|
|
|
"""Ensure that the query is valid, trying to execute it. |
|
|
|
a non materialized view is created for this check. |
|
|
@ -738,7 +702,6 @@ class BiSQLView(models.Model): |
|
|
|
) |
|
|
|
return sql_views._refresh_materialized_view() |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _refresh_materialized_view(self): |
|
|
|
for sql_view in self.filtered(lambda x: x.is_materialized): |
|
|
|
req = "REFRESH {} VIEW {}".format( |
|
|
@ -754,7 +717,6 @@ class BiSQLView(models.Model): |
|
|
|
lang=self.env.user.lang |
|
|
|
).name = sql_view._prepare_action_name() |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _refresh_size(self): |
|
|
|
for sql_view in self: |
|
|
|
req = "SELECT pg_size_pretty(pg_total_relation_size('%s'));" % ( |
|
|
@ -763,7 +725,6 @@ class BiSQLView(models.Model): |
|
|
|
self._log_execute(req) |
|
|
|
sql_view.size = self.env.cr.fetchone()[0] |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def button_preview_sql_expression(self): |
|
|
|
self.button_validate_sql_expression() |
|
|
|
res = self._execute_sql_request() |
|
|
|