Changes between Version 10 and Version 11 of S3XRC/ResourceReport


Ignore:
Timestamp:
08/28/10 19:25:44 (14 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • S3XRC/ResourceReport

    v10 v11  
    160160}}}
    161161
    162 Now we add the HTML representation:
     162Then we add the HTML representation:
    163163
    164164{{{
     
    172172}}}
    173173
     174The next step would be to provide a form to select the first and the last date of the interval. This would look like that:
     175
     176{{{
     177    if r.representation == "html":
     178
     179        # Filter form
     180        form = FORM(
     181                 TABLE(
     182                   TR(
     183                     T("Date from:"),
     184                     INPUT(_type="text", _name="date_from", _class="date", _value=datetime.now().date(), requires=IS_DATE()),
     185                     T("until:"),
     186                     INPUT(_type="text", _name="date_until", _class="date", _value=datetime.now().date(), requires=IS_DATE())
     187                   )
     188                 )
     189               )
     190
     191        output = dict(form=form)
     192
     193        if form.accepts(request.vars, session, keepvalues=True):
     194            # Processing of the form data goes here
     195
     196        return output
     197
     198}}}
     199
     200From this, we need to process the form data into two dates. We use {{{datetime}}} for that:
     201
     202{{{
     203        if form.accepts(request.vars, session, keepvalues=True):
     204
     205            from datetime import date, datetime, time
     206
     207            # Date from
     208            if form.vars.date_from:
     209                from = datetime.combine(form.vars.date_from, time(0,0,0))
     210
     211            # Date until
     212            if form.vars.date_until:
     213                until = datetime.combine(form.vars.date_until+timedelta(days=1), time(0,0,0))
     214
     215}}}
     216
     217To select the corresponding records, we need to extend our query:
     218
     219{{{
     220        if form.accepts(request.vars, session, keepvalues=True):
     221
     222            # Get the initial query from the resource
     223            # => this implements authorisation as well as URL queries
     224            resource = r.resource
     225            table = resource.table
     226            filter = resource.get_query()
     227
     228            from datetime import date, datetime, time
     229
     230            # Date from
     231            if form.vars.date_from:
     232                from = datetime.combine(form.vars.date_from, time(0,0,0))
     233                date_query = (table.timestmp >= from)
     234
     235            # Date until
     236            if form.vars.date_until:
     237                until = datetime.combine(form.vars.date_until+timedelta(days=1), time(0,0,0))
     238                q = (table.timetmp <= until)
     239                if date_query:
     240                    date_query = date_query & q
     241                else:
     242                    date_query = q
     243
     244            # Add our filter and rebuild the resource query
     245            filter = filter & date_query
     246            resource.build_query(filter=filter)
     247
     248}}}
     249
     250From there, it is easy to select the sum of the "cost" field:
     251
     252{{{
     253        if form.accepts(request.vars, session, keepvalues=True):
     254
     255            # Get the initial query from the resource
     256            # => this implements authorisation as well as URL queries
     257            resource = r.resource
     258            table = resource.table
     259            filter = resource.get_query()
     260
     261            from datetime import date, datetime, time
     262
     263            # Date from
     264            if form.vars.date_from:
     265                from = datetime.combine(form.vars.date_from, time(0,0,0))
     266                date_query = (table.timestmp >= from)
     267
     268            # Date until
     269            if form.vars.date_until:
     270                until = datetime.combine(form.vars.date_until+timedelta(days=1), time(0,0,0))
     271                q = (table.timetmp <= until)
     272                if date_query:
     273                    date_query = date_query & q
     274                else:
     275                    date_query = q
     276
     277            # Add our filter and rebuild the resource query
     278            filter = filter & date_query
     279            resource.build_query(filter=filter)
     280
     281            # Get the total costs
     282            costs = table.costs.sum()
     283            total = db(query).select(costs)
     284
     285            # Build the result string
     286            result = "%s: %s" % (T("The total costs of the selected projects are"), total)
     287
     288            output.update(result=result)
     289}}}
     290
     291Done with the method handler! Here at a glance:
     292
     293{{{
     294
     295def s3_xxx_yyy_report(r, **attr):
     296
     297    if r.representation == "html":
     298
     299        # Filter form
     300        form = FORM(
     301                 TABLE(
     302                   TR(
     303                     T("Date from:"),
     304                     INPUT(_type="text", _name="date_from", _class="date", _value=datetime.now().date(), requires=IS_DATE()),
     305                     T("until:"),
     306                     INPUT(_type="text", _name="date_until", _class="date", _value=datetime.now().date(), requires=IS_DATE())
     307                   )
     308                 )
     309               )
     310
     311        output = dict(form=form)
     312
     313        if form.accepts(request.vars, session, keepvalues=True):
     314
     315            # Get the initial query from the resource
     316            # => this implements authorisation as well as URL queries
     317            resource = r.resource
     318            table = resource.table
     319            filter = resource.get_query()
     320
     321            from datetime import date, datetime, time
     322
     323            # Date from
     324            if form.vars.date_from:
     325                from = datetime.combine(form.vars.date_from, time(0,0,0))
     326                date_query = (table.timestmp >= from)
     327
     328            # Date until
     329            if form.vars.date_until:
     330                until = datetime.combine(form.vars.date_until+timedelta(days=1), time(0,0,0))
     331                q = (table.timetmp <= until)
     332                if date_query:
     333                    date_query = date_query & q
     334                else:
     335                    date_query = q
     336
     337            # Add our filter and rebuild the resource query
     338            filter = filter & date_query
     339            resource.build_query(filter=filter)
     340
     341            # Get the total costs
     342            costs = table.costs.sum()
     343            total = db(query).select(costs)
     344
     345            # Build the result string
     346            result = "%s: %s" % (T("The total costs of the selected projects are"), total)
     347
     348            output.update(result=result)
     349
     350            # Select the corresponding view:
     351            response.view = "xxx/report.html"
     352
     353    else:
     354        raise HTTP(501, body=s3xrc.ERROR.BAD_FORMAT)
     355
     356    return output
     357
     358s3xrc.model.set_method("xxx", "yyy", method="report", action=s3_xxx_yyy_report)
     359}}}
     360
     361We have added a line to raise an HTTP error in case an unsupported format gets requested, and added a line to select a proper view template "xxx/report.html", which would look like that:
     362
     363{{{
     364{{extend 'layout.html'}}
     365<div class='form-container'>
     366{{try:}}{{=form}}{{except:}}{{pass}}
     367</div>
     368<div id='table-container'>
     369{{try:}}{{=result}}{{except:}}{{pass}}
     370</div>
     371}}}
     372
     373...and that's all.
     374
    174375...to be continued...
     376...to be continued...