This question has been flagged
1 Reply
5940 Views

I added an option (named Confirm partners attendance to the meeting) to the menu More of a tree view (event.meeting.registration tree). If I click on this option, it redirects me to the form named mail.compose.message, which is what I want. Here is my code:

<act_window name="Confirm partners attendance to the meeting"
    res_model="mail.compose.message"
    src_model="event.meeting.registration"
    view_mode="form"
    target="new"
    id="act_confirm_partners_attendance_to_event_meeting"/>

But, I need to check a condition before opening the form of mail.compose.message. If that condition is True, the form will be opened, if the condition is False, I want to raise an ORM exception.

So, my question is: how can I add an option to the menu More, and when clicking on it, check a condition before doing anything?

Note: I tried to check the condition in the default_get function of the res_model (mail.compose.message), but if the condition is False and the ORM exception is being shown, the form of mail.compose.message is being opened anyway, so when I close the ORM exception, the form is still there. And the right behaviour is that when I close the ORM exception, the form of mail.compose.message must be closed too.

Can anyone help me, please?

EDIT

I'm going to try it through an action server. I don't have any idea of ir.actions.server and the documentation is very poor. I was looking for them in several modules but even so I'm not able to understand them very well.

<record id="my_custom_action" model="ir.actions.server">
    <field name="name">My new option</field>
    <field name="model_id" ref="??????????"/>
    <field name="state">code</field>
    <field name="code">my_condition = self.my_event_meeting_registration_function(cr, uid, ids, context)</field>
</record>

<record id="ir_open_mail_compose_message_wizard" model="ir.values">
    <field eval="'client_action_multi'" name="key2"/>
    <field eval="'event.meeting.registration'" name="model"/>
    <field name="name">My new option</field>
    <field eval="'ir.actions.server,%d'%my_custom_action" name="value"/>
</record>

Please, correct me if I have anything wrong. What do I have to write in the ref of model_id?

Avatar
Discard
Best Answer

The only way I can think of on top of my head to serve your requirement is to create a method in event.meeting.registration to do the validation, raise an error if the validation did not pass, and return the action.  Then you create a server action that call this method, put this action in the More instead of act_confirm_partners_attendance_to_event_meeting.

Avatar
Discard
Author

Thank you Ivan! How can I create a server action and put it in the More menu?

You can find sample in addons/stock/wizard/stock_partial_move_view.xml.

Author

Thank you very much for your help @Ivan! I was looking the code you recommended me and more ones, but I hadn't worked with ir.actions.server before and I'm not able to understand them very well. Can you help me a bit, please? I've edited my question.

I'll use the sample that I've provided as an example (please do this while looking at the file as it is very hard to paste the XML here). action_partial_move_server is the action.server. The model_id is model_stock_move, this indicates which model will have the method called in the code field. So, the stock.move model should have action_partial_move method. If you take a look at addons/stock/stock.py indeed there is action_partial_move method for stock.move. Take a look at that method which returns an action.window (the view that you want to display). Before returning that window, it does "something". To put this action into the more button, you need the 2nd record from the XML, which is ir_open_partial_move_wizard, of ir.values. The important part of this record is setting the key2 to 'client_action_multi' and linking it with the action in the value field. So, you need 2 records in the XML, one for the action server, one for the ir.values. Next, you need to develop the method to be called by action server. This method will validate whatever that you need to validate and return the view (action window) upon successful validation.

Author

I did it as you told me and it works! Thank you very much for your explanations and your patience, @Ivan!

Pleasure to help.