Edgewall Software

Ticket #2409: olpc_default_query2.diff

File olpc_default_query2.diff, 2.7 KB (added by nkantrowitz, 21 months ago)

Swap string.Template with .replace()

  • trac/ticket/query.py

     
    3838                            INavigationContributor, Chrome 
    3939from trac.wiki.api import IWikiSyntaxProvider, parse_args 
    4040from trac.wiki.macros import WikiMacroBase # TODO: should be moved in .api 
     41from trac.config import Option 
    4142 
    4243 
    4344class QuerySyntaxError(Exception): 
     
    518519 
    519520    implements(IRequestHandler, INavigationContributor, IWikiSyntaxProvider, 
    520521               IContentConverter) 
     522                
     523    default_query = Option('query', 'default_query',  
     524                           default='status=new|assigned|reopened&owner=$USER', 
     525                           doc='The default query for authenticated users.') 
     526     
     527    default_anonymous_query = Option('query', 'default_anonymous_query',  
     528                           default='status=new|assigned|reopened&cc~=$USER', 
     529                           doc='The default query for anonymous users.') 
    521530 
    522531    # IContentConverter methods 
    523532    def get_supported_conversions(self): 
     
    559568 
    560569        constraints = self._get_constraints(req) 
    561570        if not constraints and not 'order' in req.args: 
    562             # avoid displaying all tickets when the query module is invoked 
    563             # with no parameters. Instead show only open tickets, possibly 
    564             # associated with the user 
    565             constraints = {'status': ('new', 'assigned', 'reopened')} 
     571            # If no constraints are given in the URL, use the default ones. 
    566572            if req.authname and req.authname != 'anonymous': 
    567                 constraints['owner'] = (req.authname,) 
     573                qstring = self.default_query 
     574                user = req.authname 
    568575            else: 
    569576                email = req.session.get('email') 
    570577                name = req.session.get('name') 
    571                 if email or name: 
    572                     constraints['cc'] = ('~%s' % (email or name),) 
     578                qstring = self.default_anonymous_query 
     579                user = email or name or None 
     580                 
     581            if user: 
     582                qstring = qstring.replace('$USER', user) 
     583            self.log.debug('QueryModule: Using default query: %s', qstring) 
     584            constraints = Query.from_string(self.env, qstring).constraints 
     585            # Ensure no field constraints that depend on $USER are used 
     586            # if we have no username. 
     587            for field, vals in constraints.items(): 
     588                for val in vals: 
     589                    if val.endswith('$USER'): 
     590                        del constraints[field] 
    573591 
    574592        cols = req.args.get('col') 
    575593        if isinstance(cols,basestring):