11

I have written a constraint for a particular field and I want to refresh the view of calendar when the constraint fails.

Below is the code I had tried

def _check_date_drag(self, cr, uid, ids, context=None):
        mom_obj = self.pool.get('mom.meeting')
    res = {}
    for item in self.browse(cr, uid, ids):
        mom_ids = mom_obj.search(cr, uid, 
[('meet_ref','=',item.number), ('mdt','<',item.start_datetime)], 
context=context)
        if mom_ids:
            res = {
                  'view_type': 'form',
                'view_mode': 'form',
                'res_model': 'calendar.event',
                'type': 'ir.actions.act_window',
                'target': 'new',
            }
            return False and res
    return True



    _constraints = [

        (_check_date_drag, 'MOM is already created for this calendar 
event! Kindly refresh the page to discard the changes!', 
['start_datetime']),
    ]

If the constraint fails(i.e at the return False), I want to refresh the calendar view.

Anyone with idea kindly please guide me with some idea.I want to drag that(Green Arrow) meeting event I want to drag that(Green Arrow) meeting event After Drag and Drop, Constraint message will display After Drag and Drop, Constraint message will display When I click on OK button of warning message, event does not move to its original place When I click on OK button of warning message, event does not move to its original place

I want calendar to reload when i click on OK button

Shravy
  • 656
  • 1
  • 23
  • 61
  • 1
    instead of `return False and res` try `return res` – Walid Mashal Apr 27 '17 at 05:36
  • if its only return res, Constraint message does not get displayed. – Shravy Apr 27 '17 at 06:07
  • Yeah, because it is refreshing the page, so you dont see the message being displayed, but if you use 'return False and res' it returns either true or false which will not refresh the page – Walid Mashal Apr 27 '17 at 06:30
  • It is not refreshing the page. – Shravy Apr 27 '17 at 07:50
  • Anyone with any idea on this one? – Shravy May 03 '17 at 10:23
  • You can edit openerp base code and achieve the functionality.. if that is legally allowed.. – Walid Mashal May 03 '17 at 11:15
  • you need to find the orm.py file which in my case is inside openerp > osv folder. and go inside class BaseModel and find the function _validate and in the end you will see the follwing code: `if error_msgs: raise except_orm('ValidateError', '\n'.join(error_msgs)) else: self._invalids.clear()' – Walid Mashal May 03 '17 at 11:27
  • now here you can add your own logic for your specific 'start_datetime' field so that it does not execute for all other fields while validating them – Walid Mashal May 03 '17 at 11:30
  • @WalidMashal, I want to achieve refreshing the calendar view from python code and i do not want to get in the base code and I do not know whether the specified file consists the solution to my problem as i have already checked in models.py file – Shravy May 03 '17 at 12:10

1 Answers1

1

You can try one of the following (untested):

1) Add some javascript to refresh the view on dialogue close.

2) Catch the constraint error, and return action to display the same view (essentially refreshing the page). Pass the error information in the context, and make the view display the errors in the context in the end. This way when the execution stops because of the error, the refreshed page will already be there.

Hope it works for you.

dgeorgiev
  • 919
  • 7
  • 22
  • i am trying to do the same thing as you mentioned in 2nd point, but its not really happening. because the calendar event is already changed ir-respective of the constraint execution.. – Shravy May 03 '17 at 12:07
  • Since the constraint prevents the changes to the db, just load the resource again from the db, not the 'cached' one on which the write was attempted. Alternatively, you can revert what was about to be written when catching the constraint exception. – dgeorgiev May 04 '17 at 06:11