Hello all,
I send many date fields to a report with this line in my wizard class :
data['form'] = self.read(cr, uid, ids, ['journal_id', 'date_to', 'date_from','date_deposit'], context=context)[0]
In the log, I see that for this data variable. The dates seem to be in a date format/type :
{'form': {'date_deposit': '2016-02-17', 'date_from': '2016-02-01 00:00:00', 'date_to': '2016-02-15 00:00:00', 'id': 39, 'journal_id': (18, u"Journal Caisse Populaire Desjardins d'Alma - CF (CAD)")}}
When these datas arrive in the report paser, in my render_html method, they are like this. I would say that all data is now in a unicode format :
data : {u'form': {u'journal_id': [18, u"Journal Caisse Populaire Desjardins d'Alma - CF (CAD)"], u'date_from': u'2016-02-01 00:00:00', u'date_to': u'2016-02-15 00:00:00', u'id': 39, u'date_deposit': u'2016-02-17'}}
So, in my qweb report, I can not format my date with my usual line :
<p t-esc="data.get('date_from')" t-field-options='{"format": "d MMMM y"}'/>
I rather have to format my dates with :
<p t-esc="time.strftime('%d %B %Y',time.strptime(data.get('date_from'),'%Y-%m-%d %H:%M:%S'))"/>
My question :
How could I avoid this unicode transformation of my data variable? My date are not in a date type anymore...
UPDATE #1
I have verified data inthe get_action method. Datas are still ok. No trace of unicode.
@api.v7
def get_action(self, cr, uid, ids, report_name, data=None, context=None):
_logger.error("datawwwww :: %s", str(data))
UPDATE #2
According to the tip of Axel Mendoza, I have tried to convert unicode dates to date format. But, it is still not enough to get the right format in my qweb report with
<p t-esc="data.get('date_from')" t-field-options='{"format": "d MMMM y"}'/>
I still get this in my qweb pdf :
2016-02-01
I have tried this code in me render_html method :
def render_html(self, data=None):
date_from_buf = datetime.strptime(data['form'].get('date_from'), "%Y-%m-%d %H:%M:%S")
date_from = date.strftime(date_from_buf, "%Y-%m-%d" )
Here is the log of my docargs. the date_from seems to be right there:
docargs : {'docs': wizard.create.deposit(53,), 'doc_model': u'wizard.create.deposit', 'date_from': '2016-02-01', 'lines': <bound method report.sale_lapagept.report_qweb_create_deposit_cf.lines of report.sale_lapagept.report_qweb_create_deposit_cf()>, 'date_to': datetime.datetime(2016, 2, 15, 20, 32, 58), 'doc_ids': [], 'data': {u'journal_id': [18, u"Journal Caisse Populaire Desjardins d'Alma - CF (CAD)"], u'date_from': u'2016-02-01 20:32:55', u'date_to': u'2016-02-15 20:32:58', u'id': 53, u'date_deposit': u'2016-02-19'}}
UPDATE #3
With this code :
@api.multi
def render_html(self, data=None):
date_from = datetime.strptime(data['form'].get('date_from'), "%Y-%m-%d %H:%M:%S")
I get this log :
docargs : {'docs': wizard.create.deposit(54,), 'doc_model': u'wizard.create.deposit', 'date_from': datetime.datetime(2016, 2, 1, 20, 32, 55), 'lines': <bound method report.sale_lapagept.report_qweb_create_deposit_cf.lines of report.sale_lapagept.report_qweb_create_deposit_cf()>, 'date_to': datetime.datetime(2016, 2, 15, 20, 32, 58), 'doc_ids': [], 'data': {u'journal_id': [18, u"Journal Caisse Populaire Desjardins d'Alma - CF (CAD)"], u'date_from': u'2016-02-01 20:32:55', u'date_to': u'2016-02-15 20:32:58', u'id': 54, u'date_deposit': u'2016-02-19'}}
In the qweb template, I have this code :
<p t-esc="data.get('date_from')" />
<p t-esc="o.date_from" t-field-options='{"format": "d MMMM y"}'/>
<p t-esc="date_from" t-field-options='{"format": "d MMMM y"}'/>
<p t-esc="data.get('date_from')" t-field-options='{"format": "d MMMM y"}'/>
<p t-esc="time.strftime('%d %B %Y',time.strptime(data.get('date_from'),'%Y-%m-%d %H:%M:%S'))"/>
And I still get the wrong result with {"format": "d MMMM y"}
2016-02-01 20:32:55
2016-02-01 20:32:55
2016-02-01 20:32:55
2016-02-01 20:32:55
01 février 2016
I will become crazy!
So then my other answer works nice!!