0
votes

I'm trying to become invisible a button when the current date is not the same as the date of a field, I mean I need to show the button only when the current date will be the same as the date of a field.

I'm trying to use to do this in the xml the context_today () but I could not do it, my syntax for hidden button is:

attrs = "{'invisible': [('f_inicio', '!=', ((context_today ()). strftime ('%% Y-%% m-%% d')))]}"

The error message I get is "NameError: name 'context_today' is not defined".

I tried also with the following code:

attrs = "{'invisible': [('f_inicio', '!=', __import__ ('time'). strftime ('%% Y-%% m-%% d'))]}"

With the latter does not give me errors but does not do what is required, hide the button when the f_inicio not equal to the current date.

If I can lend a hand is the greatly appreciate,

Note: The "f_inicio" is of type "datetime" and the button I want to hide this placing in the section "header" of the view.

I have seen the response of @Arya(OpenERP How to make a button invisible when datetime field != date today) but I have not worked either.

2

2 Answers

1
votes

As f_inicio is a datetime and you only need to compare the date the simplest would be a boolean functional field. I am typing this from memory on the train so apologies in advance for any errors.

from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT (can't remember this eactly - check)

_columns = {'f_inicio_today': fields.function(_is_f_inicio_today, method = True, type = 'boolean', readonly = True, string = 'Is f_inicio today')

def _is_f_inicio_today(self, cr, uid, ids, field, args, context = None):
   res = {}
   for item in self.browse(cr, uid, ids, context = contect):   
      if datetime.strptime(item.f_inicio, DEFAULT_SERVER_DATETIME_FORMAT).strftime(DEFAULT_SERVER_DATE_FORMAT) = fields.date.today():
         res[item.id] = True
      else:
         res[item.id] = False

   return res

Then in your form as in the previous answer

<field name="f_inicio_today" invisible="1" />

attrs = "{'invisible': [('f_inicio_today', '=', True)]}"

One other point to note; if the f_inicio field can change on the form, you will need to add an on change and in the result return the new calculated value of the functional field. It is perfectly OK to return these from an on_change and the new value will be used to re-set the attrs.

0
votes

In py file,

you can create a field named "current_date" as datetime and provide _defaults value for current_date

_defaults={
      'current_date': time.strftime("%Y-%m-%d %H:%M:%S")
}

attrs = "{'invisible': [('f_inicio', '!=', current_date)]}"

Note:

Also you can only compare two datetime field or date field. If datetime is compared with date, the condition is always false.

datetime: "2013-09-26 05:07:18"
date:"2013-09-26 00:00:00"