微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 我想以浮点值显示休假持续时间

我在hr_leave_rules.py中创建了half_day_allowed字段

from odoo import models,fields,api,_

class HRLeaveRules(models.Model):
    _name = 'hr_leave_rules.leave_rules'

    half_day_allowed = fields.Selection([
        ('yes',"Yes"),('no',"No")],string="Half Day Allowed",required=True)

而且,我继承了get_number_of_days字段,它们计算的休假时间是多少天,而holiday_status_id则表示休假类型.我想要做的是,如果对于特定的holiday_status_id,如果half_day_allowed为’yes’,则在get_number_of_days中它应该采用浮点值,否则它将采用整数值.为此,我尝试了下面的代码,但它没有工作.plz帮帮我.

leave_type.py

from odoo import fields,models,_
from math import ceil
from datetime import timedelta
from openerp.exceptions import UserError

HOURS_PER_DAY = 8

class leave(models.Model):
    _inherit = "hr.holidays"


    @api.onchange('number_of_days_temp')
    def _holiday_status_id(self):

        current = self.env['hr_leave_rules.leave_rules'].search([(
                'holiday_status_id','=',self.holiday_status_id.id)])

    @api.onchange('date_from')
    def _onchange_date_from(self):

        date_from = self.date_from
        date_to = self.date_to

        if date_from and not date_to:
            date_to_with_delta = fields.Datetime.from_string(date_from) + timedelta(hours=HOURS_PER_DAY)
            self.date_to = str(date_to_with_delta)
            current = self.env['hr_leave_rules.leave_rules'].search([(
                    'holiday_status_id',self.holiday_status_id.id)])

            if current.half_day_allowed == 'yes':
                if (date_to and date_from) and (date_from <= date_to):
                    self.number_of_days_temp = self._get_number_of_days(
                            date_from,date_to,self.employee_id.id)
                else:
                    self.number_of_days_temp = 0
            else:
                if (date_to and date_from) and (date_from <= date_to):
                        self.number_of_days_temp = ceil(self._get_number_of_days(
                            date_from,self.employee_id.id))
                else:
                    self.number_of_days_temp = 0

    @api.onchange('date_to')
    def _onchange_date_to(self):

        date_from = self.date_from
        date_to = self.date_to

        current = self.env['hr_leave_rules.leave_rules'].search([(
                'holiday_status_id',self.holiday_status_id.id)])

        if current.half_day_allowed == 'yes':
            if (date_to and date_from) and (date_from <= date_to):
                self.number_of_days_temp = self._get_number_of_days(
                    date_from,self.employee_id.id)
            else:
                self.number_of_days_temp = 0
        else:
            if (date_to and date_from) and (date_from <= date_to):
                self.number_of_days_temp = ceil(self._get_number_of_days(
                    date_from,self.employee_id.id))
            else:
                self.number_of_days_temp = 0

解决方法

从哪里开始……

首先,如果您有一个Yes / No的字段,则应将其定义为布尔字段,而不是Char字段. Fields Documentation.它将允许你做更简单的if / else逻辑,更不用说它只是一个更好的做法.

if half_day_allowed:
    # Do this way
else:
    # Do that way

其次,如果你有重复的代码,你应该将它分解为自己的方法以便于阅读. Don’t repeat yourself.

你重复了整整一节两次:

if half_day_allowed == 'yes':
    if (date_to and date_from) and (date_from <= date_to):
        self.number_of_days_temp = self._get_number_of_days(
                date_from,self.employee_id.id)
    else:
        self.number_of_days_temp = 0
else:
    if (date_to and date_from) and (date_from <= date_to):
            self.number_of_days_temp = ceil(self._get_number_of_days(
                date_from,self.employee_id.id))
    else:
        self.number_of_days_temp = 0

相反,只需创建一个方法并在需要时调用它.

@api.multi
def get_number_of_days_temp(self,half_day_allowed=False):
    self.ensure_one()
    if half_day_allowed == 'yes':
        # Do this
    else:
        # Do that

@api.onchange('date_from')
def _onchange_date_from(self):
    ...
    self.number_of_days_temp = self.get_number_of_days_temp(current.half_day_allowed)

@api.onchange('date_to')
def _onchange_date_to(self):
    ...
    self.number_of_days_temp = self.get_number_of_days_temp(current.half_day_allowed)

最后,为了确定未获得预期结果的问题,我们需要您提供更多信息.你到目前为止所说的只是:

I tried the below code but it is not working.

Its not showing correct get_number_of_days in both the cases i.e.,in ‘yes’ and ‘no’

这对确定问题没有帮助.有错误信息吗?它是否返回无效数据?如果是这样,那么输入和输出是什么以及期望它应该是什么?

在不知道任何进一步的情况下,我唯一可以猜测的是你的number_of_days_temp字段(你没有在你的问题中包含字段定义)被定义为一个整数字段,在这种情况下它总是会忽略小数.必须将它定义为Float才能使其工作.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐