Improve report name

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.

1 Like

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: File name in zipfile should be slugify (#9203) · Issues · Tryton / Tryton · GitLab

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

How about adding two fields to ir_action_report:

  • report_name
  • zip_report_name

and have those set to a sensible default but having the user be able to write their own PYSON in there (and of course slugify that - preferrably when saving the report settings/data - and throw an error if it contains invalid chars (’/’, ‘’, ‘:’ , …) – need to slugify again later when generating anyway since you could use various DB fields to get the values which could in turn contain invalid chars but that should be easy.

If this is something that (from a workflow/… point of view) would be considered for inclusion in the standard tryton source I can write a patch for this and submit it, but let’s discuss first if you think this is feasible (I do think it is definitely better than having to subclass Report just to change file name(s) … be that by having a method to override or by customizing execute)

For me we can not manage to template the report name because reports are based on list of records so there is no direct variable that can be used. For the zip name, it is even worst because it must be unique for any record and this can not be checked with a template.
Finally Issue 9509: Report could take the name from the record(s) rec_name - Tryton issue tracker improved the naming in a generic way that avoid the need of customization for the majority.

True i did not think of the “list of records” aspect … that doesn’t make it easier.
Anway while I do not need the report names to be changed, currently the users change the names manually when saving, so it would save some time to have an easy way to customize …

anyway while a method to get the name might make customization easier I realise this is something probably not needed by many so might be overkill… guess I will use something like the execute() code that pokoli posted in April last year – since I need to create my own report class now anyway for other reasons