Report name again

Regarding this question:


Is it possibly a general solution to take the rec_name if there is only one record? I patched one server to act like this and people love it.

Yes, it will be good if only one record and also when the report is single.

Probably we can add a checkbox on the report to allow the user decide if the record rec_name is included on the file.

I think it would be a great feature if we could find a generic way to modify the report name on report generation.

A method i.e. get_report_name(cls, records) should be simple to implement. But also a simple way for having better report names without programmers: a boolean on the report - ‘use rec_name’ for example.

Then you can use a Pyson expresion or a genshi expresion to be evaualated with the record context.

So any user can write whatever it wants.

But this will require some “programming knowledge”

I was thinking a lot about this issue and looking what customers want is in general building the report name with different fields out of the data on the report. I think if it’s possible it’s nice to have a predefined name with variables which can be translated and on generation the variables are filled with the data from the fields. Like the new ir.messages works.

For example the name for an invoice with number 20203929 and party X will become “invoice X - 20203929.pdf”. The report name is defined as “invoice %(invoice.party.rec_name)s - %(invoice.number)s” in a separate field on the report-model. This string then can be translated in whatever language. On report generation the variables will be replaced by the different values.

One drawback is that this only works for “single” reports like invoices.

Be careful to use “number” for the name… number sequence allows defining complex sequence with prefix, suffix and then they could be defined with unallowed characters for the filename.
In our case, we defined each sequence with a prefix defining document type then year and an incremental number. For instance an invoice is defined “IN/202012345”, purchase order is “PO/202012345”, … but if we want to create filename with those “identifiers”, character “/” is unallowed for filename.

The desktop client slugify the filename but I do not know how the web browser manage them (but I guess they handle it).

1 Like

So there is consensus that custom report names would be a nice feature :slight_smile:

I think that the class for which the report is registered should have a method?

def get_report_name(cls, records, report_name):
    if len(records) == 1:
         return records[0].rec_name
    else:
       return '{}_{}'.format(report_name, len(records))    

is what I did in my installation. What I don’t like is getting this method by records[0].__class__ aquisition.

I do not think we need an new method, the execute method could contain this kind of code. Indeed such design is already used to name file inside zip.

Mmhh. If I customize a class it seems more easy to me to rewrite the report name in a method than customizing all the excecute of the report by subclassing, registering etc. Or if it is bad design to do it in the class of the records there should be a dedicated method of the report class.

In any way, you must extend the report class. But as the report name is part of the returned value, for me we do not need to blow up the Report class.

I agree that we do not need a new method. Currently its easy to extend a report name. Here is an example:

    @classmethod
    def execute(cls, ids, data): 
        pool = Pool()
        Product = pool.get('product.product')
        result = super().execute(ids, data) 
        if len(ids) == 1:
            product, = Product.browse(ids)
            if product.name:
                result = result[:3] + (product.name,)
        return result

I remember having issues with zip files when the sequence contained the / character. And probably this is related to both clients.

The filename in zip file must be slugified: Issue 9203: File name in zipfile should be slugify - Tryton issue tracker

This is a lot of knowledge one need to have about the internals of the report. And you need to now how to register this patched report for your document class. I can do it, but I think for newcomers it is cumbersome. If this is considered the way to go, we should write a bit of documentation.

I’ve tested and yes the browser handles the presence of / in filenames…

Agree that adding some documentation on how to do it (with an example) will be also a good improvement.

So if you want to work on it, fill two issues: one for documentation an another to include the rec_name of the records in the name by default.

But I think customization comes second if we have a good default behavior. So having the record name seems the most expected behavior for single record. For multiple records, it is not clear for me what is the best solution. Because when there are few records, we could list their names (separeted by -) which is more meaningful. Maybe we could list the 5 first and add + (I prefer to avoid ...) like in some existing warnings.

1 Like