Report filename

167 views
Skip to first unread message

Spyros

unread,
Feb 13, 2021, 3:31:04 AM2/13/21
to Jam.py Users Mailing List
Hi all, 

I am trying to have each report generated having a unique name (which is the id of the record I am printing) but that doesn't seem to be working so far. 

If I understand correclty the documenation the name is taken from the report itemname field, so I tried the code below in my report's client in order to set the name but that doesn't work.

function on_before_print_report(report) {
    // Set the name of the report to that of the job order id
    report.item_name = report.id.value;
    // set the id
    report.id.value = report.job_order_id.lookup_value;

}

Any ideas what am I missing? 

Thanks
Spyros

Screenshot 2021-02-13 112741.png

Andrew Yushev

unread,
Feb 13, 2021, 2:07:35 PM2/13/21
to Spyros, Jam.py Users Mailing List
Hi, Spyros

Try to do it in the on_before_generate event handler in the server module.

Regards,
Andrew Yushev

сб, 13 февр. 2021 г. в 11:31, Spyros <skik...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Jam.py Users Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jam-py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jam-py/d77f41f3-2d2d-4482-931c-a6c2672e1cf9n%40googlegroups.com.

Spyros

unread,
Feb 14, 2021, 3:20:31 AM2/14/21
to Jam.py Users Mailing List
Hi Andrew, 

I tried adding the below code in both Group -> Reports- Server module (reports.py) and in print_job_order.py server module but the event is not getting triggered (where the report gets printed as before). 

def on_before_generate(report):
    debug = True
    if debug: 
        print ("Report Filename before change:", report.report_filename)
    report.report_filename = report.id.value
    if debug: 
    print ("Report Filename after change:", report.report_filename)

Regards
Spyros

Andrew Yushev

unread,
Feb 14, 2021, 2:28:21 PM2/14/21
to Spyros, Jam.py Users Mailing List
Hi, Spyros

It is strange. I tested the on_before_generate event and it is working fine.
I added these lines to Customers report in the Demo app

def on_before_generate(report):
print('on_before_generate')

def on_after_generate(report):
print('on_after_generate')
print(report.report_filename)
print(report.report_url)

and the result is  

rep.png

Regards,
Andrew Yushev

вс, 14 февр. 2021 г. в 11:20, Spyros <skik...@gmail.com>:

Spyros Kikidis

unread,
Feb 14, 2021, 3:11:58 PM2/14/21
to Andrew Yushev, Jam.py Users Mailing List
Hi Andrew,

I will check it again and let you know how it goes.

Regards
Spyros

Message has been deleted

Andrew Yushev

unread,
Feb 23, 2021, 3:30:57 PM2/23/21
to Spyros, Jam.py Users Mailing List
Hi, Spyros

The report_filename is not accessible in the on_before_generate event handler.
Please use on_after_generate event handler to use it.

Regards,
Andrew Yushev

вт, 23 февр. 2021 г. в 21:01, Spyros <skik...@gmail.com>:
Hi Andrew, 

I retested this and the event now is getting fired but it produces the below error which strange as report_filename is listed as an attribute of the Report class. 

127.0.0.1 - - [23/Feb/2021 20:58:42] "POST /api HTTP/1.1" 200 -
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\jam\wsgi.py", line 628, in on_api
    data = self.get_response(item, method, params)
  File "C:\Python39\lib\site-packages\jam\wsgi.py", line 657, in get_response
    return item.print_report(*params, safe=True), ''
  File "C:\Python39\lib\site-packages\jam\server_classes.py", line 418, in print_report
    result = copy.generate(param_values, url, ext)
  File "C:\Python39\lib\site-packages\jam\server_classes.py", line 436, in generate
    self.on_before_generate(self)
  File "bfms.reports.print_job_order", line 5, in on_before_generate
    print ("Report Filename before change:", report.report_filename)
AttributeError: 'Report' object has no attribute 'report_filename'
127.0.0.1 - - [23/Feb/2021 20:58:44] "POST /api HTTP/1.1" 200 -

Spyros

unread,
Feb 25, 2021, 5:32:33 AM2/25/21
to Jam.py Users Mailing List
Hi Andrew, 

If I understand the workflow correctly by the time the report_filename becomes available the physical file has already been generated therefore changing the report filename then will not be of benefit for my case. 
I do see in the  python code that the filename is constructed from the item_name and the current date and time hence I am thinking in two ways to workaround that, either see if I can pass a report parameter filename and use that in the generate_file_name method in the server side or add it as a parameter to the Report class object and then set it in a similare way to the ext parameter.  

file_name = self.item_name + '_' + datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S.%f') + '.' + ext

Any thoughts?

Spyros

unread,
Feb 25, 2021, 12:22:15 PM2/25/21
to Jam.py Users Mailing List
I have made changes in the server_classes.py file and in particular in Report class to add another attribute named "filename" which I set in the report's client module (see below)

function on_before_print_report(report) {
    // set the report id value to that of the selected job order id
    report.id.value = report.job_order_id.lookup_value;
    // Set the name of the report to that of the job order id
    report.filename = report.id.value;
}

On the server side I have modified the generate_file_name to set the report filename according to the new attribute if it set in the client else to continue using the item_name. I have also dropped the datetime to 
use the date only as the time is not important for my case. 

def generate_file_name(self, ext=None):
      if not ext:
          ext = 'ods'
      if self.filename == None:
          file_name = self.item_name + '_' + datetime.date.today().strftime('%d-%m-%Y') + '.' + ext
      else:
          file_name = self.filename + '_' + datetime.date.today().strftime('%d-%m-%Y') + '.' + ext
#file_name = self.item_name + '_' + datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S.%f') + '.' + ext
file_name = escape(file_name, {':': '-', '/': '_', '\\': '_'})
return os.path.abspath(os.path.join(self.task.work_dir, 'static', 'reports', file_name))

Not sure if the above was the reason but after my changes the report conversion was failing in on_convert_report method with the below error

         127.0.0.1 - - [25/Feb/2021 20:15:35] "POST /api HTTP/1.1" 200 -
         Filename: COM-21-0002
         In on_convert_report

        report.report_filename: C:\bfmsagent\static\reports\COM-21-0002_25-02-2021.ods
        report.task.work_dir C:\bfmsagent
        expected str, bytes or os.PathLike object, not NoneType
        127.0.0.1 - - [25/Feb/2021 20:15:36] "POST /api HTTP/1.1" 200 -

I have modified the on_convert_report method to check if the report.ext is empty and if it is then to use the report.extension attribute that is set in the Reports client module 

def on_convert_report(report):
    print ("In on_convert_report\n")
    try:
#         if os.name == "nt":
#             import _winreg
#             regpath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\soffice.exe"
#             root = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regpath)
#             s_office = _winreg.QueryValue(root, "")
#         else:
        s_office = "D:\Programs\LibreOfficePortable\App\libreoffice\program\soffice.exe"

        print ("report.report_filename:",report.report_filename)
        print ("report.task.work_dir", report.task.work_dir)
        if (report.ext):
            extension = report.ext
            print ("extension:",report.ext)
        else:
            extension = report.extension
            print ("extension:",report.extension)
            
        convertion = Popen([s_office, '--headless', '--convert-to', extension,
        report.report_filename, '--outdir', os.path.join(report.task.work_dir, 'static', 'reports') ],
        stderr=STDOUT,stdout=PIPE)#, shell=True)
        out, err = convertion.communicate()
        converted = True
    except Exception as e:
         print(e)

This change did the trick and now the report gets converted properly with the changed filename as I wanted 

127.0.0.1 - - [25/Feb/2021 19:56:41] "POST /api HTTP/1.1" 200 -
Filename: COM-21-0001
In on_convert_report
report.report_filename: C:\bfmsagent\static\reports\COM-21-0001_25-02-2021.ods
report.task.work_dir C:\bfmsagent
extension: pdf
127.0.0.1 - - [25/Feb/2021 19:56:50] "POST /api HTTP/1.1" 200 -

Screenshot 2021-02-25 202218.png

Andrew Yushev

unread,
Feb 25, 2021, 3:07:11 PM2/25/21
to Spyros, Jam.py Users Mailing List
The other way is to change report item_name in the on_before_generate event handler.

чт, 25 февр. 2021 г. в 20:22, Spyros <skik...@gmail.com>:

Spyros

unread,
Feb 26, 2021, 1:06:28 AM2/26/21
to Jam.py Users Mailing List
Hi Andrew, 

Thanks for the input, I did moved the change in the on_before_generate event handler and it worked perfectly so I remove all changes from server_classes as I want to stay with vanila code :)
Reply all
Reply to author
Forward
0 new messages