This question has been flagged
2 Replies
4918 Views

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!


Avatar
Discard

So then my other answer works nice!!

Best Answer

In the get_action method the data are not serialized yet, so it's not converted to unicode. The values that you pass using the data are values unrelated to any kind of python data type, normally those values are serialize those values in strings, int and float numbers to be sent it to the browser to be processed by the view action manager to determine what need to be done with the action dict and call the server controller action for the respective type of action, that's when the strings are converted to unicode. So if you wanna the value of the date_from as a datetime value type, you should convert it in the render_html method of your parser before pass it to the render of the report template and the same with all the values that you need to convert it before the report is rendered.

#Update 1

The thing is that the format pattern in t-field-options in qweb is not evaluated using datetime.strftime, it's using babel.dates.format_date function so you need to check on that library method for allowed patterns

or

you could format it by yourself using datetime.strftime before put it on the docargs data

Avatar
Discard
Author

I have tried to convert unicode to date format... see UPDATE #2.

You are just doing an extra last step trying to convert the datetime value to string again, just put as a datetime value for the docargs data, t-field-options format operates over datetime values not over strings.

Author

May be docargs are still transformed in string by the render() method called at the end of my render_html() method. I will check this tomorrow

see my ·Update 1

Author

thanks for your patience.