Nabil Taleb
Nabil Taleb

Reputation: 137

where's the problem with return from a function in python?

I didn't understand why the function does not return in python, My function counts the number of days (Friday and Saturday) between two dates but it does not return.

Here's my code :

# -*- coding: utf-8 -*-

import datetime
import calendar
calendar.setfirstweekday(calendar.SUNDAY)

from odoo import models, fields, api

class HrMission(models.Model):
    _name = "hr.employee.mission"
    _description = "hr mission"
    _inherit = "hr.employee.mission"

    days_compensation =fields.Float(compute='get_compensation', compstring='Jours de récupération', help="Jours de récupération si la mission contient les jours de repos",
                             required=True, readonly=True,)

    @api.multi
    @api.depends('mission_start_date', 'mission_end_date')
    def get_compensation(self):
        for rec in self:
            if rec.mission_start_date and rec.mission_end_date:
                time1 = datetime.datetime.strptime(rec.mission_start_date, "%Y-%m-%d")
                time2 = datetime.datetime.strptime(rec.mission_end_date, "%Y-%m-%d")
                week = {}
                leave_value = {}
        # Compute Number Of Friday And Saturday
                for i in range((time2 - time1).days):
                    day = calendar.day_name[(time1 + datetime.timedelta(days=i+1)).weekday()]
                    week[day] = week[day] + 1 if day in week else 1                    
                fri = week.get('Friday') if 'Friday' in week else 0  # Result Number 1 Of friday If "Start Date", "End date" --> "02/04/2019", "07/04/2019"
                sat = week.get('Saturday') if 'Saturday' in week else 0 # Same thing that Friday, Numbre 1 for Saturday
                friandsat = fri + sat # Result 2
                rec.days_compensation = friandsat

Upvotes: 1

Views: 134

Answers (2)

CZoellner
CZoellner

Reputation: 14801

Firstly your function has no return statement, so it will return None.

Secondly in Odoo context it's a function for computed fields. There is no need to return anything, because they have to compute values for some fields. Just set those fields as you already do for days_compensation.

Upvotes: 2

Dominique
Dominique

Reputation: 17565

You need to put this explicitly in your get_compensation() function:

return friandsat

Otherwise your function does not know that it needs to return something.

Upvotes: 2

Related Questions