Using Events System — Geraldo Reports Documentation v0.4.1-stable documentation

### Navigation

- index
- next |
- previous |
- Geraldo Reports Documentation v0.4.1-stable documentation »
- Examples »

# Using Events System

This example uses events to make customizations and also print messages while is printing:

    import os
    cur_dir = os.path.dirname(os.path.abspath(__file__))

    from geraldo.utils import A4, cm, TA_CENTER, TA_RIGHT

    from geraldo import Report, ReportBand, Label, ObjectValue, SystemField,\
        FIELD_ACTION_COUNT, BAND_WIDTH

    def element_before_print(self, generator):
        """Element with before print event - hides if the text is 'New York'"""
        self.visible = self.text != 'New York'

    def element_after_print(self, generator):
        """Element with after print event, that works"""
        print 'After print city %s'%self.text

    class SimpleListReport(Report):
        title = 'Demonstration without Django'

        class band_page_header(ReportBand):
            height = 1.3*cm
            elements = [
                SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14, 'alignment': TA_CENTER}),
                Label(text="ID", top=0.8*cm, left=0),
                Label(text="Name", top=0.8*cm, left=3*cm),
            ]
            borders = {'bottom': True}

        class band_page_footer(ReportBand):
            height = 0.5*cm
            elements = [
                Label(text='Created with Geraldo Reports', top=0.1*cm, left=0),
                SystemField(expression='Page # %(page_number)d of %(page_count)d', top=0.1*cm,
                    width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
            ]
            borders = {'top': True}

        class band_detail(ReportBand):
            height = 0.5*cm
            elements = [
                ObjectValue(attribute_name='id', top=0, left=0,),
                ObjectValue(attribute_name='name', top=0, left=3*cm,
                    # Element with before print event - hides if the text is 'New York'
                    before_print=element_before_print,

                    #Element with after print event, that works
                    after_print=element_after_print,
                    )
            ]

        # Report events overring methods

        def do_before_print(self, generator):
            """Report before print event"""
            self.title = '%s - %s objects'%(self.title, len(objects_list))

        def do_before_generate(self, generator):
            """Report before generate event"""
            print 'Starting generating!'

        def do_after_print(self, generator):
            """Report after print event"""
            print 'Printing finished!'

        def do_on_new_page(self, page, page_number, generator):
            """Report on new page event"""
            print 'Printing page # %s!'%page_number

Important: if you are setting events as methods, you must name them as “do_” plus event name (i.e. do_before_print). Otherwise, if you are setting them as attributes (setting when creating objects or setting them dinamically) you must name them without “do_”, just their names (i.e. before_print).

Executing for a list of objects (list of cities)...

>     >>> class MyObject(object):
>     ...     def __init__(self, **kwargs):
>     ...         for k,v in kwargs.items():
>     ...             setattr(self, k, v)

>     >>> objects_list = [
>     ...     MyObject(id=1, name='Rio de Janeiro'),
>     ...     MyObject(id=2, name='New York'),
>     ...     MyObject(id=3, name='Paris'),
>     ...     MyObject(id=4, name='London'),
>     ...     MyObject(id=5, name='Tokyo'),
>     ...     MyObject(id=6, name='Moscow'),
>     ...     MyObject(id=7, name='Beijing'),
>     ...     MyObject(id=8, name='Hamburg'),
>     ...     MyObject(id=9, name='New Delhi'),
>     ...     MyObject(id=10, name='Jakarta'),
>     ... ]

>     >>> report = SimpleListReport(queryset=objects_list)

PDF generation

>     >>> from geraldo.generators import PDFGenerator

>     >>> report.generate_by(PDFGenerator, filename=os.path.join(cur_dir, 'output/generated-with-events.pdf'))

### Navigation

- index
- next |
- previous |
- Geraldo Reports Documentation v0.4.1-stable documentation »
- Examples »