Changeset 4652


Ignore:
Timestamp:
Oct 29, 2008, 6:24:30 PM (15 years ago)
Author:
Andrej Tokarčík
Message:

ProgressMeterMacro: 'status' arg handling (and more...)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • progressmetermacro/0.11/progressmeter/macro.py

    r4208 r4652  
    1010from trac.web.chrome import add_stylesheet, ITemplateProvider
    1111from trac.wiki.macros import WikiMacroBase
    12 from trac.ticket.query import Query
     12from trac.ticket.query import Query, QueryModule
    1313
    1414
     
    2323    implements(IWikiMacroProvider, ITemplateProvider)
    2424
    25     # IWikiMacroProvider methods
     25    ## IWikiMacroProvider methods
    2626    def expand_macro(self, formatter, name, content):
    27         # Stripping content -- allows using spaces within arguments --
    28         # and checking whether there is not argument 'status'
    29         content = ','.join([x.strip() for x in content.split(',') if not x.strip().startswith('status')])
     27        # Parsing arguments (copied from ticket/query.py from standard trac distribution)
     28        # suggested by dhellmann
     29        args, kwargs = parse_args(content, strict=False)
    3030
    31         # Parsing arguments (copied from ticket/query.py from standard trac distribution)
    32         # suggested by dhellman
    33         req = formatter.req
    34         query_string = ''
    35         argv, kwargs = parse_args(content, strict=False)
    36         if len(argv) > 0 and not 'ticket_value' in kwargs: # 0.10 compatibility hack
    37             kwargs['ticket_value'] = argv[0]
     31        # get statuses which will be used in the query strings
     32        statuses = kwargs.pop('status', 'closed').split('|')
    3833
    39         ticket_value = kwargs.pop('ticket_value', 'list').strip().lower()
    40         query_string = '&'.join(['%s=%s' % item for item in kwargs.iteritems()])
    41         cnt = {}
    42         qs_add = {'total': '', 'closed': '&status=closed', 'active': '&status=!closed'}
     34        # Create query strings for some ticket statuses
     35        _qstr = '&'.join(['%s=%s' % item
     36                                 for item in kwargs.iteritems()])
     37
     38        query_string = dict()
     39        query_string['total'] = _qstr
     40
     41        types = {'active': '!%s', 'closed': '%s'}
     42        for t in types:
     43            query_string[t] = '&'.join([query_string['total'], 'status=' +
     44              '|'.join([types[t] % status for status in statuses])])
     45
     46        # Execute queries
     47        cnt = dict()
    4348        for key in ('closed', 'total'):
    44             query = Query.from_string(self.env, query_string + qs_add[key])
    45             tickets = query.execute(req)
     49            query = Query.from_string(self.env, query_string[key])
     50            tickets = query.execute(formatter.req)
    4651            cnt[key] = (tickets and len(tickets) or 0)
    4752
     
    5055
    5156        # Getting percent of active/closed tickets
    52         percents = {'closed': float(cnt['closed']) / float(cnt['total'])}
     57        try:
     58            percents = {'closed': float(cnt['closed']) / float(cnt['total'])}
     59        except ZeroDivisionError:
     60            raise Exception('No tickets found for provided constraints')
    5361        percents['active'] = 1 - percents['closed']
    5462
     63        # Start displaying
    5564        add_stylesheet(formatter.req, 'progressmeter/css/progressmeter.css')
    56 
    5765        main_div = tag.div(class_='milestone')
    5866
    59         # Add title above progress bar
    60         argv and main_div.children.append(tag.h2(argv))
     67        # add title above progress bar
     68        args and main_div.children.append(tag.h2(args))
    6169
    62         # Add progress bar
     70        # add progress bar
    6371        table = tag.table(class_='progress')(tag.tr())
     72
     73        # create links
     74        links = dict()
     75        for key in query_string:
     76            # Trac QueryModule doesn't know how to handle status=x|y (what
     77            # is used in the query strings) so transforming to
     78            # status=x&status=y (what will be used in hyperlinks)
     79            links[key] = query_string[key].replace('|', '&status=')
    6480
    6581        for key in reversed(percents.keys()):
     
    6884            table.children[0](tag.td(style='width: '+percents[key], class_=key)
    6985              (tag.a(title="%i of %i tickets %s" % (cnt[key], cnt['total'], key.title()),
    70               href="%s?%s" % (formatter.href.query(),query_string + qs_add[key]))))
     86              href="%s?%s" % (formatter.href.query(), links[key]))))
    7187        main_div.children.append(table)
    7288
    73         # Add percentage displaied to the right of the progress bar
     89        # add percentage displaied to the right of the progress bar
    7490        percent_para = tag.p(class_='percent')(percents['closed'])
    7591        main_div.children.append(percent_para)
    7692
    77         # Add ticket count below progress bar
     93        # add ticket count below progress bar
    7894        ticket_count = tag.dl()
    7995
    80         for key in qs_add.keys():
     96        for key in links.keys():
    8197            ticket_count.children.append(tag.dt()("%s tickets:" % key.title()))
    8298            ticket_count.children.append(tag.dd()(tag.a(str(cnt[key]),
    83               href="%s?%s" % (formatter.href.query(), query_string + qs_add[key]))))
     99              href="%s?%s" % (formatter.href.query(), links[key]))))
    84100        main_div.children.append(ticket_count)
    85101
     
    87103
    88104
    89     # ITemplateProvider methods
     105    ## ITemplateProvider methods
    90106    def get_htdocs_dirs(self):
    91         """ Makes the 'htdocs' folder available for Trac. """
     107        """Makes the 'htdocs' folder available for Trac."""
    92108        from pkg_resources import resource_filename
    93109        return [('progressmeter', resource_filename('progressmeter', 'htdocs'))]
Note: See TracChangeset for help on using the changeset viewer.