Override Print button

Using the built-in report builder, some how doesn’t really fit to the requirement from my boss. I plan to use external lib to access data from tables and produce an excel file.

Currently, when I click on Print button (see pic), I can see that my custom report (created using built-in builder) is listed there.

The data that shows just below the button is from UserTimesheet model, while the list (Records) are from UserTimesheetRecord model.

Is there anyway I can override this button to execute custom method?
If yes.. where should I write this method? should I override that in UserTimesheet model?

Is there a sample code for overriding a button? It would help a lot..

Thanks in advance
Bromo

You need to create a custom python class with a __name__ that matches the report name of your report. (This is explained in the report documentation) Then the tryton server will get that class from the pool and call the execute method function in order to get the report.

You can override any of the functions of the report api to match your needs.

Hope this helps!

1 Like

Hi @pokoli

I following your advice.. currently I am exploring using libre office way.
And I learn from the opportunity tutorial.

So I create the fodt file and follow all the codes, and I can see my report registered in the tryton dashboard.

But after I print it, and downloaded, the content of downloaded odt file is looks like the template, and the place-holder are not populated, and no errors printed in the console.

I already install the LibreOffice in my server too. Any advice?

Bromo

The tutorial is a bad example, that’s not how it works. Take a look at the Invoice template or the Sale order template

In both templates you see that you have to insert the different placeholders. I’m using Libreoffice 25.2.3.1 and there I have to go to InsertFieldMore fields. Then in the dialog select the tab named Functions. Select in the left column called Type the Placeholder and in the middle column Text. In the right column you can enter the different statements. But I suggest to take a look at the templates named above.

1 Like

Hi @edbo

Actually I am required to produce Spreadsheet in my current project,
The samples u gave (Invoice & Sell Order Template) are producing document type. Can u show me a link to create template in Libre for Spreadsheet template (excel kind)?

Thank you in advance
Bromo

Tryton is using Relatorio as templating library. See Tryton / Relatorio · GitLab and Relatorio — A templating library able to output odt and pdf files. Take a look at the indepth Example where also a spreadsheet example is shown. The ods files can be downloaded from the first link.

Hi using your steps INSERT → FIELDS → and so on, I am able to complete the opportunity tutorial (see below).. thank you very much @edbo really appreciated :grinning_face:

And successfully generate the ODT file as below

Now I try to implement this knowledge on FODS file, but I cannot find INSERT → FIELDS ???.. in the dropdown see below:

And the tutorial in the link you gave. It is using INSERT → HYPERLINK etc etc not using INSERT → FIELDS.

to be honest.. I did follow this at the beginning to fix the opportunity report (using INSERT → HYPERLINK) and somehow (maybe the way I insert is wrong).. the doc still not loading the data (see below).

Can you further advice me @edbo on FODS ?

Thank you in advance
regards
Bromo

To be honest, I don’t use spreadsheets as a template, so I’m also a bit new to this. But open examples/demo_sheets.ods · d01e530dd830c70fbe9e2380ff2b03efe09dc54b · Tryton / Relatorio · GitLab and take a look. It seems that you have to just enter the data in the cells.

Hi!

As the tutorial explains, you have to use hyperlinks for spreadsheets. So, in a .fods file, you have to create an hyperlink and write, in the URL field, something like:

relatorio://opportunity.party.rec_name

So, basically the same as in a .fodt file, but using hyperlinks instead of placeholders. In .fodt files you do have to use placeholders, not hyperlinks.

1 Like

Ok done that @csangrador , and this is my setup:

But now I got error:

Traceback (most recent call last):
...... many lines .....
  File "/home/tryton/afx_project/.env/lib/python3.10/site-packages/relatorio/templates/opendocument.py", line 429, in _relatorio_statements
    assert not opened_tags
AssertionError

I got this error when working with Placeholder at fodt, and I did closing tag.
If u see my setup at a screenshot of how I sets Hyperlink.. is there any mistake on my setup?

Regards
Bromo

I can’t really tell without seeing the hyperlinks, but this usually happens when there’s a missing /for. I can see you do have it. However, I also get this issue sometimes as it doesn’t properly recognise it because of alignment, etc.

Could you try aligning the for each= and /for to the left and let me know if that solves it? Also, maybe it’s because you merged the cells for those two (I might be wrong about this though! I just have never merged them).

Isn’t the problem the extra < and /> in the Text: field of the hyperlink?

Ok these what I did @csangrador

  1. I set the URL to: relatorio://for each=“item in o.records” → opening loop tag
  2. I set the text: for each=“item in o.records”
  3. And put simple string “Hello world”, expecting this string will be repeated x times
  4. Then for closing tag I set the URL to: relatorio://for
  5. I set the text: for

But this attempt again produce assert not opened_tags error, I aligned all to left too.

So I tried: maybe if I put the tag in URL can solved. So I did:

  1. I set the URL to: relatorio://<for each=“item in o.records”>
  2. I set the text: <for each=“item in o.records”>
  3. And put simple string “Hello world”, expecting this string will be repeated x times
  4. Then for closing tag I set the URL to: relatorio://</for>
  5. I set the text: </for>

But then it seems that relatorio engine spits a new error:

raise TemplateSyntaxError(err, template.filepath, lineno,
genshi.template.base.TemplateSyntaxError: invalid syntax in expression "__relatorio_guess_type(__relatorio_store_cache(139770805891392, <for each="item in o.records">))" of "attrs" directive (, line 1) (<string>, line 139)

Please I really need to understand whats goin on?

FYI this is my model which I want to render

class UserTimesheet(ModelSQL, ModelView):
    "User Timesheet"
    __name__ = 'afx.user.timesheet'

    # Hardcoded
    MAIN_COMPANY = 1

    year = fields.Selection([], "Year", help='Format: YYYY', required=True, states={
        'readonly': Eval('id', -1) > 0
    }, depends=['id'])
    month = fields.Selection([], "Month", sort=False, required=True, states={
        'readonly': Eval('id', -1) > 0
    }, depends=['id'])
    user = fields.Many2One('company.employee', "User", required=True, domain=[
        ('company', '=', MAIN_COMPANY)])
    unique_id = fields.Char("Uuid", required=True)
    total_works = fields.Float(
        'Hours In Month',
        digits=(16, 2), states={
        'readonly': True
    }, depends=['id'])

    records = fields.One2Many('afx.user.timesheet.record', 'timesheet', "Records", required=False)

Regards
Bromo

You should use records instead of o.records (except of course if you have extended the report context with a o keyword).

1 Like

Hi @ced

I did take out the o from o.records, and now only records

so I test on 2 format:

  1. URL without open/close tags: relatorio://for each=“item in o.records”
  2. URL with open/close tags: relatorio://<for each=“item in o.records”>

The first one, again come out assert not opened_tags error
The second one, again come out with raise TemplateSyntaxError

so if I follow the Indepth Example format I got not open tags

Regards
Bromo

The Text is ignored by relatorio.

Try

  • relatorio://for each="item in o.records"
  • relatorio:///for — these are three slashes

The Text: does not matter.

1 Like

Thank you… really appreciated @htgoebel