Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] raise if not context #1

Open
wants to merge 1 commit into
base: fix-get-ress-calendar
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 4 additions & 19 deletions resource_calendar_working_day/res_company.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2015 AKRETION (<http://www.akretion.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# coding: utf-8
# © 2015 @ Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import fields, models


Expand Down
54 changes: 27 additions & 27 deletions resource_calendar_working_day/resource.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2015 AKRETION (<http://www.akretion.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# coding: utf-8
# © 2015 @ Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import fields, models, api, _
from openerp.exceptions import Warning
from openerp.exceptions import Warning as UserError
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
from datetime import datetime, timedelta

Expand All @@ -34,13 +19,7 @@ def _get_date(self, start_date, delay, resource_id=False):
if isinstance(start_date, str):
start_date = fields.Date.from_string(start_date)
if not self.id:
model = self._context['params']['model']
obj_id = self._context['params']['id']
obj = self.env[model].browse(obj_id)
self = obj.company_id.calendar_id
if not self:
raise Warning(_('Error !'), _(
'You need to define a calendar for the company !'))
self = self._update_self()
dt_leave = self.get_leave_intervals(
resource_id, start_datetime=None, end_datetime=None)
worked_days = (
Expand All @@ -61,3 +40,24 @@ def _get_date(self, start_date, delay, resource_id=False):
date.weekday()) in worked_days:
delay = delay - delta
return date

@api.model
def _update_self(self):
if not self._context:
raise UserError(_(
"Impossible to guess the calendar to use\n"
"'context' variable is empty.\n"
"Developer tip: Consider to use inspect lib to get back "
"the context in ResourceCalendar._get_date()"))
model = self._context['params']['model']
obj_id = self._context['params']['id']
obj = self.env[model].browse(obj_id)
if 'company_id' in obj._fields.keys():
company = obj.company_id
elif self._uid:
company = self.env['res.users'].browse(self._uid).company_id
self = company.calendar_id
if not self:
raise UserError(_(
'You need to define a calendar for the company !'))
return self