Skip to content

Commit

Permalink
Fix implementation of related actions
Browse files Browse the repository at this point in the history
  • Loading branch information
guewen committed Dec 29, 2016
1 parent 17941e0 commit 43bb875
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 108 deletions.
9 changes: 2 additions & 7 deletions queue_job/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,12 +696,7 @@ def export_product(self):
"""
def decorate(func):
if not _is_model_method(func):
raise ValueError('@related_action can only be used on methods of '
'Models')

inner_func = func.__func__
inner_func.related_action = action
inner_func.kwargs = kwargs
func.related_action = action
func.kwargs = kwargs
return func
return decorate
10 changes: 2 additions & 8 deletions queue_job/models/queue_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from odoo import models, fields, api, exceptions, _

from ..job import STATES, DONE, PENDING, Job, job
from ..job import STATES, DONE, PENDING, Job, job, related_action
from ..exception import RetryableJobError
from ..fields import JobSerialized

Expand Down Expand Up @@ -205,6 +205,7 @@ def autovacuum(self):
return True

@job
@related_action(action='testing_related_method')
@api.multi
def testing_method(self, *args, **kwargs):
""" Method used for tests
Expand All @@ -219,13 +220,6 @@ def testing_method(self, *args, **kwargs):

@api.multi
def testing_related_method(self, **kwargs):
if 'url' in kwargs:
subject = self.args[0]
return {
'type': 'ir.actions.act_url',
'target': 'new',
'url': kwargs['url'].format(subject=subject),
}
return self, kwargs


Expand Down
23 changes: 0 additions & 23 deletions queue_job/tests/common.py
Original file line number Diff line number Diff line change
@@ -1,24 +1 @@
# -*- coding: utf-8 -*-

from contextlib import contextmanager
from odoo.addons.queue_job.job import related_action


def start_related_actionify(method, **kwargs):
related_action(**kwargs)(method)


def stop_related_actionify(method):
attrs = ('related_action',)
for attr in attrs:
if hasattr(method.__func__, attr):
delattr(method.__func__, attr)


@contextmanager
def related_actionify(method, **kwargs):
try:
start_related_actionify(method, **kwargs)
yield
finally:
stop_related_actionify(method)
47 changes: 4 additions & 43 deletions queue_job/tests/test_related_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import odoo.tests.common as common
from odoo.addons.queue_job.job import Job
from .common import related_actionify


class TestRelatedAction(common.TransactionCase):
Expand All @@ -14,47 +13,9 @@ def setUp(self):
super(TestRelatedAction, self).setUp()
self.method = self.env['queue.job'].testing_method

def test_no_related_action(self):
""" Job without related action """
job = Job(self.method)
self.assertIsNone(job.related_action())

def test_return_none(self):
""" Job with related action returning None """
# default action returns None
with related_actionify(self.method):
job = Job(self.method)
self.assertIsNone(job.related_action())

def test_return(self):
""" Job with related action check if action returns correctly """
with related_actionify(self.method, action='testing_related_method'):
job = Job(self.method)
act_job, act_kwargs = job.related_action()
self.assertEqual(act_job, job.db_record())
self.assertEqual(act_kwargs, {})

def test_kwargs(self):
""" Job with related action check if action propagates kwargs """
action = 'testing_related_method'
with related_actionify(self.method, action=action, b=4):
job = Job(self.method)
self.assertEqual(job.related_action(), (job.db_record(), {'b': 4}))

def test_store_related_action(self):
""" Call the related action on the model """
action = 'testing_related_method'
with related_actionify(self.method,
action=action,
url='https://en.wikipedia.org/wiki/{subject}'):
job = Job(self.method, args=('Discworld',))
job.store()
stored_job = self.env['queue.job'].search(
[('uuid', '=', job.uuid)]
)
self.assertEqual(len(stored_job), 1)
expected = {'type': 'ir.actions.act_url',
'target': 'new',
'url': 'https://en.wikipedia.org/wiki/Discworld',
}
self.assertEquals(stored_job.open_related_action(), expected)
job = Job(self.method)
act_job, act_kwargs = job.related_action()
self.assertEqual(act_job, job.db_record())
self.assertEqual(act_kwargs, {})
73 changes: 46 additions & 27 deletions test_queue_job/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@
# Copyright 2016 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo.addons.queue_job.exception import RetryableJobError
from odoo import api, fields, models
from odoo.addons.queue_job.job import job
from odoo.addons.queue_job.job import job, related_action


class QueueJob(models.Model):

_inherit = 'queue.job'

@api.multi
def testing_related__none(self, **kwargs):
return None

@api.multi
def testing_related__url(self, **kwargs):
assert 'url' in kwargs, "url required"
subject = self.args[0]
return {
'type': 'ir.actions.act_url',
'target': 'new',
'url': kwargs['url'].format(subject=subject),
}


class TestQueueJob(models.Model):
Expand All @@ -14,20 +32,6 @@ class TestQueueJob(models.Model):

name = fields.Char()

@job
@api.multi
def normal_job(self, *args, **kwargs):
""" Method used for tests
Return always the arguments and keyword arguments received
"""
return
if kwargs.get('raise_retry'):
raise RetryableJobError('Must be retried later')
if kwargs.get('return_context'):
return self.env.context
return args, kwargs

@job
def no_description(self):
return
Expand All @@ -44,17 +48,6 @@ def job_with_retry_pattern__no_zero(self):
def mapped(self, func):
return super(TestQueueJob, self).mapped(func)

@api.multi
def testing_related_method(self, **kwargs):
if 'url' in kwargs:
subject = self.args[0]
return {
'type': 'ir.actions.act_url',
'target': 'new',
'url': kwargs['url'].format(subject=subject),
}
return self, kwargs


class TestQueueChannel(models.Model):

Expand All @@ -72,3 +65,29 @@ def job_b(self):
@job(default_channel='root.sub.subsub')
def job_sub_channel(self):
return


class TestRelatedAction(models.Model):

_name = 'test.related.action'
_description = "Test model for related actions"

@job
def testing_related_action__no(self):
return

@job
@related_action() # default action returns None
def testing_related_action__return_none(self):
return

@job
@related_action(action='testing_related_method', b=4)
def testing_related_action__kwargs(self):
return

@job
@related_action(action='testing_related__url',
url='https://en.wikipedia.org/wiki/{subject}')
def testing_related_action__store(self):
return
1 change: 1 addition & 0 deletions test_queue_job/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_test_queue_job,access_test_queue_job,model_test_queue_job,,1,1,1,1
access_test_queue_channel,access_test_queue_channel,model_test_queue_channel,,1,1,1,1
access_test_related_action,access_test_related_action,model_test_related_action,,1,1,1,1
1 change: 1 addition & 0 deletions test_queue_job/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from . import test_job
from . import test_job_channels
from . import test_related_actions
46 changes: 46 additions & 0 deletions test_queue_job/tests/test_related_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# Copyright 2014-2016 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

import odoo.tests.common as common
from odoo.addons.queue_job.job import Job


class TestRelatedAction(common.TransactionCase):
""" Test Related Actions """

def setUp(self):
super(TestRelatedAction, self).setUp()
self.model = self.env['test.related.action']
self.method = self.env['queue.job'].testing_method

def test_no_related_action(self):
""" Job without related action """
job = Job(self.model.testing_related_action__no)
self.assertIsNone(job.related_action())

def test_return_none(self):
""" Job with related action returning None """
# default action returns None
job = Job(self.model.testing_related_action__return_none)
self.assertIsNone(job.related_action())

def test_kwargs(self):
""" Job with related action check if action propagates kwargs """
job_ = Job(self.model.testing_related_action__kwargs)
self.assertEqual(job_.related_action(), (job_.db_record(), {'b': 4}))

def test_store_related_action(self):
""" Call the related action on the model """
job = Job(self.model.testing_related_action__store,
args=('Discworld',))
job.store()
stored_job = self.env['queue.job'].search(
[('uuid', '=', job.uuid)]
)
self.assertEqual(len(stored_job), 1)
expected = {'type': 'ir.actions.act_url',
'target': 'new',
'url': 'https://en.wikipedia.org/wiki/Discworld',
}
self.assertEquals(stored_job.open_related_action(), expected)

0 comments on commit 43bb875

Please sign in to comment.