Edgewall Software

Ticket #7293: dynamic_variables.diff

File dynamic_variables.diff, 4.7 KB (added by andrei2102@…, 4 years ago)
  • trac/ticket/report.py

     
    3434from trac.util.text import to_unicode, unicode_urlencode 
    3535from trac.util.translation import _ 
    3636from trac.web.api import IRequestHandler, RequestDone 
    37 from trac.web.chrome import add_ctxtnav, add_link, add_stylesheet, \ 
     37from trac.web.chrome import add_ctxtnav, add_link, add_script, add_stylesheet, \ 
    3838                            INavigationContributor, Chrome 
    3939from trac.wiki import IWikiSyntaxProvider, WikiParser 
    4040 
     
    219219 
    220220    def _render_view(self, req, db, id): 
    221221        """Retrieve the report results and pre-process them for rendering.""" 
    222         try: 
    223             args = self.get_var_args(req) 
    224         except ValueError,e: 
    225             raise TracError(_('Report failed: %(error)s', error=e)) 
    226222 
    227223        if id == -1: 
    228224            # If no particular report was requested, display 
     
    241237                raise ResourceNotFound( 
    242238                    _('Report %(num)s does not exist.', num=id), 
    243239                    _('Invalid Report Number')) 
    244  
     240         
     241        try: 
     242            args = self.get_var_args(req, sql) 
     243        except ValueError,e: 
     244            raise TracError(_('Report failed: %(error)s', error=e)) 
    245245        # If this is a saved custom query. redirect to the query module 
    246246        # 
    247247        # A saved query is either an URL query (?... or query:?...), 
     
    498498                            del req.session[var] 
    499499                except (ValueError, KeyError): 
    500500                    pass 
     501            if len(data['args']) > 0 : 
     502                add_script(req, 'common/js/folding.js') 
    501503            return 'report_view.html', data, None 
    502504 
    503505    def add_alternate_links(self, req, args): 
     
    578580 
    579581        return cols, info 
    580582 
    581     def get_var_args(self, req): 
     583    def get_var_args(self, req, sql): 
    582584        report_args = {} 
     585         
     586        def repl_literal(expr): 
     587            var_re = re.compile("[$]([A-Z]+)") 
     588            parts = var_re.split(expr[1:-1]) 
     589            if len(parts) == 1: 
     590                return [] 
     591            return [str(param) for param in parts[1::2]] 
     592         
    583593        for arg in req.args.keys(): 
    584594            if not arg.isupper(): 
    585595                continue 
     
    588598        # Set some default dynamic variables 
    589599        if 'USER' not in report_args: 
    590600            report_args['USER'] = req.authname 
    591  
     601         
     602        for arg in repl_literal(sql) : 
     603            if not report_args.has_key(arg) : 
     604                report_args[arg] = '' 
     605         
    592606        return report_args 
    593607 
    594608    def sql_sub_vars(self, sql, args, db=None): 
     
    599613            try: 
    600614                arg = args[aname] 
    601615            except KeyError: 
    602                 raise TracError(_("Dynamic variable '%(name)s' not defined.", 
    603                                   name='$%s' % aname)) 
     616                arg = '' 
    604617            values.append(arg) 
    605618 
    606619        var_re = re.compile("[$]([A-Z]+)") 
  • trac/ticket/templates/report_view.html

     
    1111 
    1212  <body> 
    1313    <div id="content" class="report"> 
    14  
     14      <script type="text/javascript"> 
     15          jQuery(document).ready(function($) { 
     16            $("fieldset legend").enableFolding(false); 
     17          }); 
     18      </script> 
    1519      <h1>$title 
    1620        <span py:if="numrows and report.id != -1" class="numrows">($numrows matches)</span> 
    1721      </h1> 
     
    1923      <div py:if="description" id="description" xml:space="preserve"> 
    2024        ${wiki_to_html(context, description)} 
    2125      </div> 
    22  
     26      <form py:if="len(args) > 1" method="GET" action=""> 
     27        <fieldset id="filters" > 
     28          <legend style="cursor: pointer;"> 
     29            <a href="#no1">Filters</a> 
     30          </legend> 
     31          <table> 
     32            <py:for each="variable in args"> 
     33               <py:if test="variable not in ['id', 'USER']"> 
     34                 <tr> 
     35                   <td>${variable}: </td> 
     36                   <td><input type="text" name="${variable}" value="${req.args[variable] or None}" /></td> 
     37                 </tr> 
     38               </py:if> 
     39            </py:for> 
     40          </table> 
     41        </fieldset> 
     42        <br /> 
     43        <div class="buttons" align="right"> 
     44            <input type="submit" value="Update" name="filter"/> 
     45        </div> 
     46        <br /> 
     47      </form> 
    2348      <div py:if="report.id != -1" class="buttons"> 
    2449        <form py:if="'REPORT_MODIFY' in perm(report.resource)" action="" method="get"> 
    2550          <div>