Ignore:
Timestamp:
02/16/2012 11:46:39 PM (15 months ago)
Author:
matobaa
Message:

append trac-admin command

Location:
trunk/AnalyzeCustomField
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/AnalyzeCustomField/analyzecustomfield/admin.py

    r31 r32  
    33 
    44from pkg_resources import ResourceManager 
    5 from trac.admin.api import IAdminPanelProvider 
     5from trac.admin.api import IAdminPanelProvider, IAdminCommandProvider 
    66from trac.core import Component, implements 
    77from trac.ticket.api import TicketSystem 
    88from trac.util.translation import _ 
    99from trac.web.chrome import ITemplateProvider 
     10import json 
    1011 
    1112# なにを見たいか? 
     
    1920# レポートを書き換える必要があるかもしれない 
    2021 
    21 class Analyzer(Component): 
     22def analyze(self): 
    2223    count_values = """ 
    2324        SELECT name, COUNT(value) FROM ( 
     
    2728    count_all = "SELECT count(id) from ticket" 
    2829     
    29     implements(ITemplateProvider, IAdminPanelProvider ) 
     30    ticket = TicketSystem(self.env) 
     31    cfields = ticket.get_custom_fields() 
     32    db = self.env.get_read_db() 
     33    cursor = db.cursor() 
     34    # QTY of Valid data, without Default value 
     35    valid = {} 
     36    cursor.execute(count_valid) 
     37    for name, count in cursor: 
     38        valid[name] = count 
     39    # QTY of data valiation for valiation 
     40    cursor.execute(count_values) 
     41    valiation = {} 
     42    for name, count in cursor: 
     43        valiation[name] = count 
     44    # rebuild cfields 
     45    for field in cfields: 
     46        field['valiation'] = valiation.get(field['name'], 0) 
     47        field['valid'] = valid.get(field['name'], 0) 
     48    data = {'cfields':cfields} 
     49    # Ticket QTY 
     50    cursor.execute(count_all) 
     51    data['qty'] = cursor.fetchone() 
     52    # build notdefined list 
     53    notdefined = [] 
     54    for name in [name for name in valid.keys() if name not in [field.get('name') for field in cfields]]: 
     55        notdefined.append({ 
     56                        'name':name, 
     57                        'label':'N/A', 
     58                        'type':'N/A', 
     59                        'valiation':valiation.get(name, 0), 
     60                        'valid':valid.get(name, 0)}) 
     61    data['notdefined'] = notdefined 
     62    return data 
     63 
     64class AdminPanel(Component): 
     65    implements(ITemplateProvider, IAdminPanelProvider) 
    3066 
    3167    # ITemplateProvider methods 
     
    3672        return [] 
    3773 
    38     #IAdminPanelProvider methods 
     74    # IAdminPanelProvider methods 
    3975    def get_admin_panels(self,req): 
    4076        if 'TICKET_ADMIN' in req.perm: 
     
    4480    def render_admin_panel(self, req, category, page, path_info): 
    4581        req.perm.require('TICKET_ADMIN') 
    46         data = self._analyze() 
     82        data = analyze(self) 
    4783        return ('analayzecustomfield.html', data) 
    4884 
    49     def _analyze(self): 
    50         ticket = TicketSystem(self.env) 
    51         cfields = ticket.get_custom_fields() 
    52         db = self.env.get_read_db() 
    53         cursor = db.cursor() 
    54         # QTY of Valid data, without Default value 
    55         valid = {} 
    56         cursor.execute(self.count_valid) 
    57         for name, count in cursor: 
    58             valid[name] = count 
    59         # QTY of data valiation for valiation 
    60         cursor.execute(self.count_values) 
    61         valiation = {} 
    62         for name, count in cursor: 
    63             valiation[name] = count 
    64         # rebuild cfields 
    65         for field in cfields: 
    66             field['valiation'] = valiation.get(field['name'], 0) 
    67             field['valid'] = valid.get(field['name'], 0) 
    68         data = {'cfields':cfields} 
    69         # Ticket QTY 
    70         cursor.execute(self.count_all) 
    71         data['qty'] = cursor.fetchone() 
    72         # build notdefined list 
    73         notdefined = [] 
    74         for name in [name for name in valid.keys() if name not in [field.get('name') for field in cfields]]: 
    75             notdefined.append({ 
    76                             'name':name, 
    77                             'label':'N/A', 
    78                             'type':'N/A', 
    79                             'valiation':valiation.get(name, 0), 
    80                             'valid':valid.get(name, 0)}) 
    81         data['notdefined'] = notdefined 
    82         return data 
     85class Command(Component): 
     86    implements(IAdminCommandProvider) 
     87     
     88    # IAdminCommandProvider methods 
     89    def get_admin_commands(self): 
     90        yield ('customfield analyze', '', 'Analyze custom field', None, self.command) 
     91     
     92    def command(self): 
     93        data = analyze(self) 
     94        print json.dumps(data, indent=2) 
  • trunk/AnalyzeCustomField/setup.py

    r31 r32  
    66setup( 
    77    name='AnalyzeCustomField', 
    8     version='0.1', 
     8    version='0.2', 
    99    license='Modified BSD', 
    1010    author='MATOBA Akihiro', 
Note: See TracChangeset for help on using the changeset viewer.