source: timingandestimationplugin/branches/trac0.10/timingandestimationplugin/reportmanager.py

Last change on this file was 2390, checked in by Russ Tyndall, 16 years ago

TimingAndEstimationPlugin:

closes #1737

Applied Colin Guthrie's patch to make managing custom reports easier.

Thanks for the patch!

File size: 5.2 KB
Line 
1from trac.core import *
2
3
4class CustomReportManager:
5  """A Class to manage custom reports"""
6  version = 1
7  name = "custom_report_manager_version"
8  env = None
9  log = None
10 
11  def __init__(self, env, log):
12    self.env = env
13    self.log = log
14    self.upgrade()
15 
16  def upgrade(self): 
17    # Check to see what version we have
18    db = self.env.get_db_cnx()
19    cursor = db.cursor()
20    cursor.execute("SELECT value FROM system WHERE name=%s", (self.name,))
21    try:
22      version = int(cursor.fetchone()[0])
23    except:
24      version = 0
25      cursor.execute("INSERT INTO system (name,value) VALUES(%s,%s)",
26                     (self.name, version))
27       
28    if version > self.version:
29      raise TracError("Fatal Error: You appear to be running two plugins with conflicting versions "
30                      "of the CustomReportManager class. Please ensure that '%s' is updated to "
31                      "version %s of the file reportmanager.py (currently using version %s)."
32                      % (__name__, str(version), str(self.version)))
33   
34    # Do the staged updates
35    try:
36      if version < 1:
37        cursor.execute("CREATE TABLE custom_report ("
38                       "id         INTEGER,"
39                       "uuid       VARCHAR(64),"
40                       "maingroup  VARCHAR(255),"
41                       "subgroup   VARCHAR(255),"
42                       "version    INTEGER,"
43                       "ordering   INTEGER)")
44     
45      #if version < 2:
46      #  cursor.execute("...")
47   
48      # Updates complete, set the version
49      cursor.execute("UPDATE system SET value=%s WHERE name=%s", 
50                     (self.version, self.name))
51      db.commit()
52      db.close()
53   
54    except Exception, e:
55      self.log.error("CustomReportManager Exception: %s" % (e,));
56      db.rollback()
57 
58  def add_report(self, title, author, description, query, uuid, version, maingroup, subgroup=""):
59    # First check to see if we can load an existing version of this report
60    db = self.env.get_db_cnx()
61    cursor = db.cursor()
62    try:
63      cursor.execute("SELECT id, version FROM custom_report "
64                     "WHERE uuid=%s", (uuid,))
65      (id, currentversion) = cursor.fetchone()         
66    except:
67      id = None
68      currentversion = 0
69   
70    try:
71      if not id:
72        cursor.execute("SELECT MAX(id) FROM report")
73        next_id = int(cursor.fetchone()[0]) + 1
74        self.log.debug("Inserting new report with uuid '%s'" % (uuid,));
75
76        # Get the ordering of any current reports in this group/subgroup.
77        try:
78          cursor.execute("SELECT MAX(ordering) FROM custom_report "
79                         "WHERE maingroup=%s AND subgroup=%s", (maingroup, subgroup))
80          ordering = int(cursor.fetchone()[0]) + 1
81        except:
82          ordering = 0
83       
84        cursor.execute("INSERT INTO report (id, title, author, description, query) "
85                       "VALUES (%s, %s, %s, %s, %s)",
86                       (next_id, title, author, description, query))
87        cursor.execute("INSERT INTO custom_report (id, uuid, maingroup, subgroup, version, ordering) "
88                       "VALUES (%s, %s, %s, %s, %s, %s)",
89                       (next_id, uuid, maingroup, subgroup, version, ordering))
90        db.commit()
91        db.close()
92        return True
93      if currentversion < version:
94        self.log.debug("Updating report with uuid '%s' to version %s" % (uuid,version));
95        cursor.execute("UPDATE report SET title=%s, author=%s, description=%s, query=%s "
96                       "WHERE id=%s", (title, author, description, query, id))
97        cursor.execute("UPDATE custom_report SET version=%s, maingroup=%s, subgroup=%s "
98                       "WHERE id=%s", (version, maingroup, subgroup, id))
99        db.commit()
100        db.close()
101        return True
102    except Exception, e:
103      self.log.error("CustomReportManager Exception: %s" % (e,));
104      db.rollback()
105   
106    return False
107 
108  def get_report_by_uuid(self, uuid):
109    db = self.env.get_db_cnx()
110    cursor = db.cursor()
111    rv = None
112    try:
113      cursor.execute("SELECT report.id,report.title FROM custom_report "
114                     "LEFT JOIN report ON custom_report.id=report.id "
115                     "WHERE custom_report.uuid=%s", (uuid,))
116      row = cursor.fetchone()
117      rv = (row[0], row[1])
118    except:
119      pass
120   
121    return rv
122 
123  def get_reports_by_group(self, group):
124    db = self.env.get_db_cnx()
125    cursor = db.cursor()
126    rv = {}
127    try:
128      cursor.execute("SELECT custom_report.subgroup,report.id,report.title "
129                     "FROM custom_report "
130                     "LEFT JOIN report ON custom_report.id=report.id "
131                     "WHERE custom_report.maingroup=%s "
132                     "ORDER BY custom_report.subgroup,custom_report.ordering", (group,))
133      for subgroup,id,title in cursor:
134        if not rv.has_key(subgroup):
135          rv[subgroup] = { "title": subgroup,
136                           "reports": [] }
137        rv[subgroup]["reports"].append( { "id": int(id), "title": title } )
138    except:
139      pass
140   
141    return rv
142
Note: See TracBrowser for help on using the repository browser.