From 573cabd6b19fc4a44b6e17687e04337086181f4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 25 Oct 2018 21:01:10 +0200 Subject: [PATCH] Fill default timesheet work from project relate We set in the context a specific key to search for project work in default_work. And the relate fills this context key. issue7777 review48641002 --- CHANGELOG | 2 ++ __init__.py | 9 ++++---- tests/test_project.py | 53 +++++++++++++++++++++++++++++++++++++++++++ timesheet.py | 27 ++++++++++++++++++---- timesheet.xml | 4 ++++ 5 files changed, 87 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 5b13458d3b..5f40e750e5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Fill default timesheet work from project relate + Version 5.0.0 - 2018-10-01 * Bug fixes (see mercurial logs for details) * Remove support for Python 2.7 diff --git a/__init__.py b/__init__.py index 9d853241f7..365c4a033d 100644 --- a/__init__.py +++ b/__init__.py @@ -2,15 +2,16 @@ # this repository contains the full copyright notices and license terms. from trytond.pool import Pool -from .work import * -from .timesheet import * +from . import work +from . import timesheet from . import party def register(): Pool.register( - Work, - TimesheetWork, + work.Work, + timesheet.Line, + timesheet.Work, module='project', type_='model') Pool.register( party.PartyReplace, diff --git a/tests/test_project.py b/tests/test_project.py index 1a9b025402..888bc67500 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -129,6 +129,59 @@ class ProjectTestCase(ModuleTestCase): ProjectWork.delete([p_work]) self.assertEqual(TimesheetWork.search([]), []) + @with_transaction() + def test_timesheet_default_work_empty(self): + "Test timesheet default work with no context" + pool = Pool() + ProjectWork = pool.get('project.work') + TimesheetLine = pool.get('timesheet.line') + + company = create_company() + with set_company(company): + p_work = ProjectWork() + p_work.name = "Work" + p_work.timesheet_available = True + p_work.save() + + self.assertFalse(TimesheetLine.default_work()) + + @with_transaction() + def test_timesheet_default_work_single(self): + "Test timesheet default work with 1 work in context" + pool = Pool() + ProjectWork = pool.get('project.work') + TimesheetLine = pool.get('timesheet.line') + + company = create_company() + with set_company(company): + p_work = ProjectWork() + p_work.name = "Work" + p_work.timesheet_available = True + p_work.save() + + with Transaction().set_context({'project.work': [p_work.id]}): + self.assertEqual( + TimesheetLine.default_work(), + p_work.timesheet_works[0].id) + + @with_transaction() + def test_timesheet_default_work_multiple(self): + "Test timesheet default work with multiple work in context" + pool = Pool() + ProjectWork = pool.get('project.work') + TimesheetLine = pool.get('timesheet.line') + + company = create_company() + with set_company(company): + works = ProjectWork.create([ + {'name': "Work 1", 'timesheet_available': True}, + {'name': "Work 2", 'timesheet_available': True}, + ]) + + with Transaction().set_context( + {'project.work': map(int, works)}): + self.assertFalse(TimesheetLine.default_work()) + def suite(): suite = trytond.tests.test_tryton.suite() diff --git a/timesheet.py b/timesheet.py index ff8abc193f..039b51da10 100644 --- a/timesheet.py +++ b/timesheet.py @@ -1,22 +1,41 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. from trytond.pool import PoolMeta, Pool +from trytond.transaction import Transaction -__all__ = ['TimesheetWork'] +class Line(metaclass=PoolMeta): + __name__ = 'timesheet.line' + + @classmethod + def default_work(cls): + pool = Pool() + Work = pool.get('timesheet.work') + try: + default = super().default_work() + except AttributeError: + default = None + project_works = Transaction().context.get('project.work') + if project_works is not None: + works = Work.search([ + ('origin.id', 'in', project_works, 'project.work'), + ]) + if len(works) == 1: + default = works[0].id + return default -class TimesheetWork(metaclass=PoolMeta): +class Work(metaclass=PoolMeta): __name__ = 'timesheet.work' @classmethod def _get_origin(cls): - return super(TimesheetWork, cls)._get_origin() + ['project.work'] + return super()._get_origin() + ['project.work'] def _validate_company(self): pool = Pool() ProjectWork = pool.get('project.work') - result = super(TimesheetWork, self)._validate_company() + result = super()._validate_company() if isinstance(self.origin, ProjectWork): result &= self.company == self.origin.company return result diff --git a/timesheet.xml b/timesheet.xml index f25b357c91..602020015e 100644 --- a/timesheet.xml +++ b/timesheet.xml @@ -9,6 +9,10 @@ this repository contains the full copyright notices and license terms. --> +