ODOO
api
In this step we will
-
explain api decorators
-
add age fields for patient object
-
calculate age from birthday as example for writing function in odoo
What is api
decorators ?
odoo.api.
one
(
method)
odoo.api.multi(
method)
Where
self is defined as record-set (some records that have related with
each other)
EG:
model.method(cr, uid, ids, args, context=context)
odoo.api.
model
(
method)
Where
self is defined as record-set
but
its contents is not relevant
(some
records that have
no
related with each other)
EG:
model.method(cr, uid, args, context=context)
odoo.api.depends(
*args)
odoo.api.constrains(
*args)
odoo.api.onchange(
*args)
odoo.api.returns(
model,
downgrade=None,
upgrade=None)
-
add age field
s
for patient object
we
will calculate the age of patient as years, months and days so we
must add three fields for age (age_year, age_month, age_day)
which
will be computed fields that get their values from one function
“calculate_age()” so to do that we have to go to ‘clinic.patient’
object and add the code bellow
age_year
= fields.Integer(string="Years", compute='calculate_age')
age_month
= fields.Integer(string="Months", compute='calculate_age')
age_day
= fields.Integer(string="Days", compute='calculate_age')
@api.one
def
calculate_age(self):
"""
function
to calcolate the age of he user
"""
if
self.birth_day :
birth_day
= str(self.birth_day)
current_date
= str(fields.Date.today())
birth_day_year_as_int
= int(birth_day[0]+birth_day[1]+birth_day[2]+birth_day[3])
birth_day_month_as_int
= int(birth_day[5]+birth_day[6])
birth_day_day_as_int
= int(birth_day[8]+birth_day[9])
current_date_year_as_int
=
int(current_date[0]+current_date[1]+current_date[2]+current_date[3])
current_date_month_as_int
= int(current_date[5]+current_date[6])
current_date_day_as_int
= int(current_date[8]+current_date[9])
period_years
= current_date_year_as_int-birth_day_year_as_int
period_months
= current_date_month_as_int-birth_day_month_as_int
period_days
= current_date_day_as_int-birth_day_day_as_int
months_list_1
= ['04','06','09','11']
months_list_2
= ['01','03','05','07','08','10','12']
if
period_days < 0:
if
str(current_date_month_as_int) == '02':
if
current_date_year_as_int%4 == 0:
period_days
= 29+period_days
if
current_date_year_as_int%4 != 0:
period_days
= 28+period_days
for
index in range(0,4):
if
current_date_month_as_int == int(months_list_1[index]):
period_days
= 30+period_days
for
index in range(0,7):
if
current_date_month_as_int == int(months_list_2 [index]):
period_days
= 31+period_days
period_months
= period_months-1
if
period_months < 0:
period_months
= 12+period_months
period_years
= period_years-1
self.age_year
= period_years
self.age_month
= period_months
self.age_day
= period_days
Then
we will deign view for these field by adding the code bellow to
‘clinic_patient_view.xml’ file
<table style="width:50%;">
<tr>
<td style="width:40%;font-size:13px;padding:2px;">
</td>
<td style="width:20%;padding:2px;">
<center>
years
</center>
</td>
<td style="width:20%;padding:2px;">
<center>
months
</center>
</td>
<td style="width:20%;padding:2px;">
<center>
days
</center>
</td>
</tr>
<tr>
<td style="width:40%;font-size:13px;padding:2px;">
<b>
Age
</b>
</td>
<td style="width:20%;padding:2px;">
<center>
<field name="age_year" />
</center>
</td>
<td style="width:20%;padding:2px;">
<center>
<field name="age_month" />
</center>
</td>
<td style="width:20%;padding:2px;">
<center>
<field name="age_day" />
</center>
</td>
</tr>
</table>
Watch On YouTube
PREVIOUS STEP NEXT STEP