Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

i want to create a alert message, when i select date field as 12/12/1400

By
Jothimani
on 4/4/14, 8:42 AM 1,573 views

Hi i want to create a age calculator in openerp.

in my age calculator, if i select date of birth before 100 years; here i want to show a alert message like "Please select a date of birth in between 100 years... thankyou"

here i include my programs,

in my module.py

from datetime import date,datetime
from time import strptime
from dateutil import parser
from openerp.osv import osv, fields

    class jebatraining(osv.osv):
        _name = "age_calculator"
        _description = "Age Calculator"

        def onchange_getage(self,cr,uid,ids,dob,context=None):

            current_date=datetime.now()

            current_year=current_date.year

            birth_date=parser.parse(dob)

            if (current_year>=birth_date.year):

                current_age=current_year-birth_date.year

            else:
                current_age=0

                return {'value':{'age': current_age}} 

        _columns = {

            'dob':fields.date('Date of Birth', required=True),
            'age':fields.integer('Age', size=3),

            }
    jebatraining()

in my module_view.xml

<record id="age_calculator_form" model="ir.ui.view">
    <field name="name">jebatraining.form_view</field>
    <field name="model">age_calculator</field>
    <field name="type">form</field>
    <field name="arch" type="xml">
        <form string="age calculator">

            <field name="dob" on_change = "onchange_getage(dob)" string="Date of Birth"/> 
            <field name="age" string="Age"/>

        </form>
    </field>
 </record>

anyone help me. thanks friends................

0
Weste
On 4/4/14, 10:24 AM

Your onchange function should be something like this:

def onchange_getage(self,cr,uid,ids,dob,context=None):
            current_date=datetime.now()
            current_year=current_date.year
            if current_year > dob.year + 100:
                      raise osv.except_osv(_('Invalid Action!'), _('This date of birth is too old!')) 
            birth_date=parser.parse(dob)
            if (current_year>=birth_date.year):
                current_age=current_year-birth_date.year
            else:
                current_age=0
                return {'value':{'age': current_age}}

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 4/4/14, 8:42 AM
Seen: 1573 times
Last updated: 3/16/15, 8:10 AM