Browse Source

[IMP] bi_sql_editor : conserve cron settings, when setting materialized view to draft ; Set correct default values for cron and enable it

14.0-report-py3o-pr-506
Sylvain LE GAL 3 years ago
committed by David James
parent
commit
259d95f01c
  1. 30
      bi_sql_editor/models/bi_sql_view.py
  2. 4
      bi_sql_editor/tests/test_bi_sql_view.py

30
bi_sql_editor/models/bi_sql_view.py

@ -261,6 +261,7 @@ class BiSQLView(models.Model):
raise UserError(
_("You can only unlink draft views."
"If you want to delete them, first set them to draft."))
self.cron_id.unlink()
return super(BiSQLView, self).unlink()
@api.multi
@ -276,10 +277,7 @@ class BiSQLView(models.Model):
# Action Section
@api.multi
def button_create_sql_view_and_model(self):
for sql_view in self:
if sql_view.state != 'sql_valid':
raise UserError(_(
"You can only process this action on SQL Valid items"))
for sql_view in self.filtered(lambda x: x.state == "sql_valid"):
# Create ORM and access
sql_view._create_model_and_fields()
sql_view._create_model_access()
@ -289,31 +287,37 @@ class BiSQLView(models.Model):
sql_view._create_index()
if sql_view.is_materialized:
sql_view.cron_id = self.env['ir.cron'].create(
sql_view._prepare_cron()).id
if not sql_view.cron_id:
sql_view.cron_id = self.env['ir.cron'].create(
sql_view._prepare_cron()).id
else:
sql_view.cron_id.active = True
sql_view.state = 'model_valid'
@api.multi
def button_set_draft(self):
for sql_view in self:
for sql_view in self.filtered(lambda x: x.state != "draft"):
sql_view.menu_id.unlink()
sql_view.action_id.unlink()
sql_view.tree_view_id.unlink()
sql_view.graph_view_id.unlink()
sql_view.pivot_view_id.unlink()
sql_view.search_view_id.unlink()
if sql_view.cron_id:
sql_view.cron_id.unlink()
if sql_view.state in ('model_valid', 'ui_valid'):
# Drop SQL View (and indexes by cascade)
if sql_view.is_materialized:
sql_view._drop_view()
if sql_view.cron_id:
sql_view.cron_id.active = False
# Drop ORM
sql_view._drop_model_and_fields()
sql_view.write({'state': 'draft', 'has_group_changed': False})
sql_view.has_group_changed = False
super(BiSQLView, sql_view).button_set_draft()
return True
@api.multi
def button_create_ui(self):
@ -384,7 +388,7 @@ class BiSQLView(models.Model):
@api.multi
def _prepare_cron(self):
self.ensure_one()
now = datetime.now()
return {
'name': _('Refresh Materialized View %s') % self.view_name,
'user_id': SUPERUSER_ID,
@ -393,6 +397,10 @@ class BiSQLView(models.Model):
'state': 'code',
'code': 'model._refresh_materialized_view_cron(%s)' % self.ids,
'numbercall': -1,
'interval_number': 1,
'interval_type': 'days',
'nextcall': datetime(now.year, now.month, now.day+1),
'active': True,
}
@api.multi

4
bi_sql_editor/tests/test_bi_sql_view.py

@ -82,6 +82,10 @@ class TestBiSqlViewEditor(SingleTransactionCase):
with self.assertRaises(UserError):
self.view.unlink()
self.view.button_set_draft()
self.assertNotEqual(
self.view.cron_id, False, 'Set to draft materialized view should'
' not unlink cron'
)
self.view.unlink()
res = self.bi_sql_view.search([('name', '=', 'Partners View 2')])
self.assertEqual(len(res), 0, 'View not deleted')
Loading…
Cancel
Save