Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to avoid unicode transformation of my data passed to a report parser?

By
Pascal Tremblay
on 2/17/16, 11:26 AM 670 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!


So then my other answer works nice!!

Axel Mendoza
on 2/17/16, 2:12 PM
1

Axel Mendoza

--Axel Mendoza--
10323
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 2/17/16, 2:30 PM

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

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

Pascal Tremblay
on 2/18/16, 9:03 PM

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.

Axel Mendoza
on 2/18/16, 9:50 PM

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

Pascal Tremblay
on 2/18/16, 11:19 PM

see my ·Update 1

Axel Mendoza
on 2/19/16, 12:16 AM

thanks for your patience.

Pascal Tremblay
on 2/19/16, 7:42 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 2/17/16, 11:26 AM
Seen: 670 times
Last updated: 2/19/16, 7:41 AM