This question has been flagged
19 Replies
46779 Views

I have a requirement, to display the message box and update the current date and time but if I use raise.osv it's stopping the flow of control and the datetime is not updated.

Avatar
Discard

These are Module files. Maybe make an extra module with this and then just use it as written @ the buttom

Author Best Answer

crete py file in your module

from osv import osv
from osv import fields
from openerp.tools.translate import _

WARNING_TYPES = [('warning','Warning'),('info','Information'),('error','Error')]

class warning(osv.osv_memory):
_name = 'warning'
_description = 'warning'
_columns = {
    'type': fields.selection(WARNING_TYPES, string='Type', readonly=True),
    'title': fields.char(string="Title", size=100, readonly=True),
    'message': fields.text(string="Message", readonly=True),
}
_req_name = 'title'

def _get_view_id(self, cr, uid):
    """Get the view id
    @return: view id, or False if no view found
    """
    res = self.pool.get('ir.model.data').get_object_reference(cr, uid, 
        'osc_integ', 'warning_form')
    return res and res[1] or False

def message(self, cr, uid, id, context):
    message = self.browse(cr, uid, id)
    message_type = [t[1]for t in WARNING_TYPES if message.type == t[0]][0]
    print '%s: %s' % (_(message_type), _(message.title))
    res = {
        'name': '%s: %s' % (_(message_type), _(message.title)),
        'view_type': 'form',
        'view_mode': 'form',
        'view_id': self._get_view_id(cr, uid),
        'res_model': 'warning',
        'domain': [],
        'context': context,
        'type': 'ir.actions.act_window',
        'target': 'new',
        'res_id': message.id
    }
    return res

def warning(self, cr, uid, title, message, context=None):
    id = self.create(cr, uid, {'title': title, 'message': message, 'type': 'warning'})
    res = self.message(cr, uid, id, context)
    return res

def info(self, cr, uid, title, message, context=None):
    id = self.create(cr, uid, {'title': title, 'message': message, 'type': 'info'})
    res = self.message(cr, uid, id, context)
    return res

def error(self, cr, uid, title, message, context=None):
    id = self.create(cr, uid, {'title': title, 'message': message, 'type': 'error'})
    res = self.message(cr, uid, id, context)
    return res

and create xml file

<openerp>
   <data>
    <record id="warning_form" model="ir.ui.view">
        <field name="name">warning.form</field>
        <field name="model">warning</field>
        <field eval="20" name="priority"/>
        <field name="arch" type="xml">
            <form string="Warning" version="7.0">
                <field name="message"  nolabel="1" />
                <footer>
                    <button string="OK" class="oe_highlight" special="cancel" />
                </footer>
            </form>
        </field>
    </record>

    <record model="ir.actions.act_window" id="action_warning">
        <field name="name">Warning</field>
        <field name="res_model">warning</field>
        <field name="view_type">form</field>
        <field name="view_mode">form</field>
        <field name="view_id" ref="warning_form" />
        <field name="target">new</field>
    </record>
</data>

</openerp>

and finally call the method

return self.pool.get('warning').info(cr, uid, title='Export imformation', message="%s products Created, %s products Updated "%(str(prod_new),str(prod_update)))
Avatar
Discard

thank you very much.

Is this module available in launchpad/github?

Best Answer

We made an addon to show warning messages and so on:

addon.py:

w_types = [('warning','Warning'),('info','Information'),('error','Error')]

class warning():
    ...
    _columns = {
        'title': fields.char(...),
        'message': fields.text(...),
    }
    _req_name = 'title'

    def _get_view_id(self, cr, uid):
        res = self.pool.get('ir.model.data').get_object_reference(cr, uid, 
            'warning', 'warning_form')
        if res:
            return res[1]
        else:
            return False

    def message(self, cr, uid, id, context):
        message = self.browse(cr, uid, id)
        message_type = [t[1]for t in w_types if message.type == t[0]][0]
        res = {
            'name': '%s: %s' % (_(message_type), _(message.title)),
            'view_type': 'form',
            'view_mode': 'form',
            'view_id': self._get_view_id(cr, uid),
            'res_model': 'warning',
            'domain': [],
            'context': context,
            'type': 'ir.actions.act_window',
            'target': 'new',
            'res_id': message.id
        }
        return res

    def warning(self, cr, uid, title, message, context=None):
        id = self.create(... {'title': title, 'message': message, 'type': 'warning'})
        res = self.message(cr, uid, id, context)
        return res

    def info(self, cr, uid, title, message, context=None):
        ...
        return res

    def error(self, cr, uid, title, message, context=None):
        ...
        return res

and the view.xml:

        <record id="warning_form" model="ir.ui.view">
            ...
            <field name="arch" type="xml">
                <form string="Warning" version="7.0">
                    <field name="message"  nolabel="1" />
                    <footer>
                        <button string="OK" class="oe_highlight" special="cancel" />
                    </footer>
                </form>
            </field>
        </record>

        <record model="ir.actions.act_window" id="action_warning">
            ...
            <field name="view_id" ref="warning_form" />
            <field name="target">new</field>
        </record>

You may have to add functionallity to make it work as dialog.

        Just return this to a function that is called from client. 
        Usage:  return self.pool.get('warning').warning(cr, uid, title='Title', message='Text')
                                               .info(...)
                                               .error(...)

( this is an extract from a module made by our Team )

Avatar
Discard
Author

how to include in my module

Did it work for you?

Author

ya its working and how to display a variable value with text

You need to combine your text and values in one string: e.g. string = "value %s is wrong..."%str(value) You can also add new functionality to the module to do this. ;)

Author

Thank you its working

Andreas: Why you don't release it as public module?

We need to make clear, if our company releases the whole module. We come back on that.

Does it works with create and write methods?

I'm getting errors. Could you please tell me what i need to include at the "..."?

didn't work in write or create function of object. Please help

Best Answer

hello i have two date time field and i calculated difference both of them but sometimes i missed to enter date so it raise the error, but i want some msg instead of error can you please help me

Avatar
Discard

provide a default value for both the fields OR check whether the value is available at the field before calculating the difference

Nice work, but I don't like to save the warning etc. to the database, only show it to my user, then drop it.