Commits

seccanj  committed 9f5fcb2

Restored compatibility with Trac 0.11. Now both 0.11 and 0.12 are supported.

  • Participants
  • Parent commits ca69ce9

Comments (0)

Files changed (6)

File testman4trac/trunk/testmanager/api.py

     
         try:
             # Get next ID
-            db = self.env.get_db_cnx()
+            db, handle_ta = get_db_for_write(self.env)
             cursor = db.cursor()
             sql = "SELECT value FROM testconfig WHERE propname='"+propname+"'"
             
             cursor = db.cursor()
             cursor.execute("UPDATE testconfig SET value='" + str(id+1) + "' WHERE propname='"+propname+"'")
             
-            db.commit()
+            if handle_ta:
+                db.commit()
         except:
             self.env.log.debug(formatExceptionInfo())
             db.rollback()
         
         try:
             # Set next ID to the input value
-            db = self.env.get_db_cnx()
+            db, handle_ta = get_db_for_write(self.env)
             cursor = db.cursor()
             cursor.execute("UPDATE testconfig SET value='" + str(value) + "' WHERE propname='"+propname+"'")
            
-            db.commit()
+            if handle_ta:
+                db.commit()
         except:
             self.env.log.debug(formatExceptionInfo())
             db.rollback()
         result += '<tr><th>'+LABELS['timestamp']+'</th><th>'+LABELS['author']+'</th><th>'+LABELS['status']+'</th></tr>'
         result += '</thead><tbody>'
         
-        db = self.env.get_db_cnx()
+        db = get_db(self.env)
         cursor = db.cursor()
 
         sql = "SELECT time, author, status FROM testcasehistory WHERE id='"+str(id)+"' AND planid='"+str(planid)+"' ORDER BY time DESC"
     def list_all_testplans(self):
         """Returns a list of all test plans."""
 
-        db = self.env.get_db_cnx()
+        db = get_db(self.env)
         cursor = db.cursor()
 
         sql = "SELECT id, catid, page_name, name, author, time FROM testplan ORDER BY catid, id"
                 except:
                     print "Error adding test catalog!"
                     print formatExceptionInfo()
-                    req.redirect(req.path_info)
+                    req.redirect(req.href.wiki(path))
 
                 # Redirect to see the new wiki page.
                 req.redirect(req.href.wiki(pagename))
                     except:
                         self.env.log.debug("Error pasting test case!")
                         self.env.log.debug(formatExceptionInfo())
-                        req.redirect(req.path_info)
+                        req.redirect(req.href.wiki(pagename))
                 
                     # Redirect to test catalog, forcing a page refresh by means of a random request parameter
                     req.redirect(req.href.wiki(pagename.rpartition('_TC')[0], random=str(datetime.now(utc).microsecond)))
                     except:
                         self.env.log.debug("Error duplicating test case!")
                         self.env.log.debug(formatExceptionInfo())
-                        req.redirect(req.path_info)
+                        req.redirect(req.href.wiki(tcId))
 
                     # Redirect tp allow for editing the copy test case
                     req.redirect(req.href.wiki(pagename, action='edit'))

File testman4trac/trunk/testmanager/macros.py

                 text += _get_custom_fields_columns(tcip, custom_ctx['testcaseinplan'][1])
 
         if fulldetails:
+            print(tc.description)
             wikidom = WikiParser(env).parse(tc.description)
             out = StringIO()
             f = Formatter(env, context)

File testman4trac/trunk/testmanager/model.py

 from trac.perm import PermissionError
 from trac.resource import Resource, ResourceNotFound
 from trac.util.datefmt import utc, utcmax
-from trac.util.text import empty, CRLF
+from trac.util.text import CRLF
 from trac.util.translation import _, N_, gettext
 from trac.wiki.api import WikiSystem
 from trac.wiki.model import WikiPage
         """
         self['status'] = status
 
-        @self.env.with_transaction(db)
-        def do_set_status(db):
-            cursor = db.cursor()
-            sql = 'INSERT INTO testcasehistory (id, planid, time, author, status) VALUES (%s, %s, %s, %s, %s)'
-            cursor.execute(sql, (self.values['id'], self.values['planid'], to_any_timestamp(datetime.now(utc)), author, status))
+        db, handle_ta = get_db_for_write(self.env, db)
+
+        cursor = db.cursor()
+        sql = 'INSERT INTO testcasehistory (id, planid, time, author, status) VALUES (%s, %s, %s, %s, %s)'
+        cursor.execute(sql, (self.values['id'], self.values['planid'], to_any_timestamp(datetime.now(utc)), author, status))
+
+        if handle_ta:
+            db.commit()
 
     def list_history(self, db=None):
         """
         and author, starting from the most recent.
         """
         if db is None:
-            db = self._get_db(db)
+            db = get_db(self.env, db)
         
         cursor = db.cursor()
 
 
     # IEnvironmentSetupParticipant methods
     def environment_created(self):
-        self.upgrade_environment(self.env.get_db_cnx())
+        self.upgrade_environment(get_db(self.env))
 
     def environment_needs_upgrade(self, db):
         return self._need_initialization(db)

File testman4trac/trunk/testmanager/web_ui.py

 from trac.util import to_unicode
 from trac.util.compat import sorted, set, any
 from trac.util.text import CRLF
-from trac.util.translation import _, tag_
+from trac.util.translation import _
 from trac.web.chrome import ITemplateProvider, INavigationContributor, \
                             add_stylesheet, add_script, add_ctxtnav
 from trac.wiki.formatter import Formatter

File tracgenericclass/trunk/tracgenericclass/model.py

 from trac.db import Table, Column, Index
 from trac.resource import Resource, ResourceNotFound
 from trac.util.datefmt import utc, utcmax
-from trac.util.text import empty, CRLF
+from trac.util.text import CRLF
 from trac.util.translation import _, N_, gettext
 from trac.wiki.api import WikiSystem
 from trac.wiki.model import WikiPage
         
         self._old = {}
 
-    def _get_db(self, db):
-        return db or self.env.get_read_db()
-
     def get_key_prop_names(self):
         """
         Returns an array with the fields representing the identity
         self.env.log.debug('>>> _fetch_object')
     
         if db is None:
-            db = self._get_db(db)
+            db = get_db(self.env, db)
         
         if not self.pre_fetch_object(db):
             return
             if field in self.time_fields:
                 self.values[field] = from_any_timestamp(value)
             elif value is None:
-                self.values[field] = empty
+                self.values[field] = '0'
             else:
                 self.values[field] = value
 
         for name, value in cursor:
             if name in custom_fields:
                 if value is None:
-                    self.values[name] = empty
+                    self.values[name] = '0'
                 else:
                     self.values[name] = value
 
         """
         try:
             value = self.values[name]
-            if value is not empty:
+            if value is not '0':
                 return value
             field = [field for field in self.fields if field['name'] == name]
             if field:
         """
         self.env.log.debug('>>> insert')
 
-        assert not self.exists, 'Cannot insert an existing ticket'
+        assert not self.exists, 'Cannot insert an existing object'
+
+        db, handle_ta = get_db_for_write(self.env, db)
 
         # Add a timestamp
         if when is None:
         self.values['time'] = self.values['changetime'] = when
 
         # Perform type conversions
+        self.env.log.debug('  Performing type conversions')
         values = dict(self.values)
         for field in self.time_fields:
             if field in values:
                 values[field] = to_any_timestamp(values[field])
         
         # Insert record
+        self.env.log.debug('  Getting fields')
         std_fields = []
         custom_fields = []
         for f in self.fields:
                 else:
                     std_fields.append(fname)
 
-        @self.env.with_transaction(db)
-        def do_insert(db):
-            if not self.pre_insert(db):
-                return
-            
-            cursor = db.cursor()
-            cursor.execute("INSERT INTO %s (%s) VALUES (%s)"
-                           % (self.realm,
-                              ','.join(std_fields),
-                              ','.join(['%s'] * len(std_fields))),
-                           [values[name] for name in std_fields])
+        if not self.pre_insert(db):
+            return
 
-            # Insert custom fields
-            key_names = self.get_key_prop_names()
-            key_values = self.get_key_prop_values()
-            if custom_fields:
-                self.env.log.debug('  Inserting custom fields')
-                cursor.executemany("""
-                INSERT INTO %s_custom (%s,name,value) VALUES (%s,%%s,%%s)
-                """ 
-                % (self.realm, 
-                   ','.join(key_names),
-                   ','.join(['%s'] * len(key_names))),
-                [to_list((key_values, name, self[name])) for name in custom_fields])
+        self.env.log.debug('  Inserting record')
+        cursor = db.cursor()
+        cursor.execute("INSERT INTO %s (%s) VALUES (%s)"
+                       % (self.realm,
+                          ','.join(std_fields),
+                          ','.join(['%s'] * len(std_fields))),
+                       [values[name] for name in std_fields])
 
-            self.post_insert(db)
+        # Insert custom fields
+        key_names = self.get_key_prop_names()
+        key_values = self.get_key_prop_values()
+        if custom_fields:
+            self.env.log.debug('  Inserting custom fields')
+            cursor.executemany("""
+            INSERT INTO %s_custom (%s,name,value) VALUES (%s,%%s,%%s)
+            """ 
+            % (self.realm, 
+               ','.join(key_names),
+               ','.join(['%s'] * len(key_names))),
+            [to_list((key_values, name, self[name])) for name in custom_fields])
+
+        self.post_insert(db)
                 
+        if handle_ta:
+            db.commit()
+
+        self.env.log.debug('  Setting up internal fields')
         self.exists = True
         self.resource = self.resource(id=self.get_resource_id())
         self._old = {}
 
+        self.env.log.debug('  Calling listeners')
         from tracgenericclass.api import GenericClassSystem
         for listener in GenericClassSystem(self.env).change_listeners:
             listener.object_created(self.realm, self)
         if not self._old and not comment:
             return False # Not modified
 
+        db, handle_ta = get_db_for_write(self.env, db)
+
         if when is None:
             when = datetime.now(utc)
         when_ts = to_any_timestamp(when)
 
-        @self.env.with_transaction(db)
-        def do_save(db):
-            if not self.pre_save_changes(db):
-                return
+        if not self.pre_save_changes(db):
+            return
+        
+        cursor = db.cursor()
+
+        # store fields
+        custom_fields = [f['name'] for f in self.fields if f.get('custom')]
+        
+        key_names = self.get_key_prop_names()
+        key_values = self.get_key_prop_values()
+        sql_where = '1=1'
+        for k in key_names:
+            sql_where += " AND " + k + "=%%s" 
+
+        for name in self._old.keys():
+            if name in custom_fields:
+                cursor.execute(("""
+                    SELECT * FROM %s_custom 
+                    WHERE name=%%s AND 
+                    """ + sql_where) % self.realm, to_list((name, key_values)))
+                    
+                if cursor.fetchone():
+                    cursor.execute(("""
+                        UPDATE %s_custom SET value=%%s
+                        WHERE name=%%s AND 
+                        """ + sql_where) % self.realm, to_list((self[name], name, key_values)))
+                else:
+                    cursor.execute("""
+                        INSERT INTO %s_custom (%s,name,value) 
+                        VALUES (%s,%%s,%%s)
+                        """ 
+                        % (self.realm, 
+                        ','.join(key_names),
+                        ','.join(['%s'] * len(key_names))),
+                        to_list((key_values, name, self[name])))
+            else:
+                cursor.execute(("""
+                    UPDATE %s SET %s=%%s WHERE 
+                    """ + sql_where) 
+                    % (self.realm, name),
+                    to_list((self[name], key_values)))
             
-            cursor = db.cursor()
+            cursor.execute(("""
+                INSERT INTO %s_change
+                    (%s, time,author,field,oldvalue,newvalue)
+                VALUES (%s, %%s, %%s, %%s, %%s, %%s)
+                """
+                % (self.realm, 
+                ','.join(key_names),
+                ','.join(['%s'] * len(key_names)))),
+                to_list((key_values, when_ts, author, name, 
+                self._old[name], self[name])))
+        
+        self.post_save_changes(db)
 
-            # store fields
-            custom_fields = [f['name'] for f in self.fields if f.get('custom')]
-            
-            key_names = self.get_key_prop_names()
-            key_values = self.get_key_prop_values()
-            sql_where = '1=1'
-            for k in key_names:
-                sql_where += " AND " + k + "=%%s" 
-
-            for name in self._old.keys():
-                if name in custom_fields:
-                    cursor.execute(("""
-                        SELECT * FROM %s_custom 
-                        WHERE name=%%s AND 
-                        """ + sql_where) % self.realm, to_list((name, key_values)))
-                        
-                    if cursor.fetchone():
-                        cursor.execute(("""
-                            UPDATE %s_custom SET value=%%s
-                            WHERE name=%%s AND 
-                            """ + sql_where) % self.realm, to_list((self[name], name, key_values)))
-                    else:
-                        cursor.execute("""
-                            INSERT INTO %s_custom (%s,name,value) 
-                            VALUES (%s,%%s,%%s)
-                            """ 
-                            % (self.realm, 
-                            ','.join(key_names),
-                            ','.join(['%s'] * len(key_names))),
-                            to_list((key_values, name, self[name])))
-                else:
-                    cursor.execute(("""
-                        UPDATE %s SET %s=%%s WHERE 
-                        """ + sql_where) 
-                        % (self.realm, name),
-                        to_list((self[name], key_values)))
-                
-                cursor.execute(("""
-                    INSERT INTO %s_change
-                        (%s, time,author,field,oldvalue,newvalue)
-                    VALUES (%s, %%s, %%s, %%s, %%s, %%s)
-                    """
-                    % (self.realm, 
-                    ','.join(key_names),
-                    ','.join(['%s'] * len(key_names)))),
-                    to_list((key_values, when_ts, author, name, 
-                    self._old[name], self[name])))
-            
-            self.post_save_changes(db)
+        if handle_ta:
+            db.commit()
 
         old_values = self._old
         self._old = {}
 
         self.env.log.debug('>>> delete')
 
-        @self.env.with_transaction(db)
-        def do_delete(db):
-            if not self.pre_delete(db):
-                return
+        db, handle_ta = get_db_for_write(self.env, db)
+
+        if not self.pre_delete(db):
+            return
+            
+        #Attachment.delete_all(self.env, 'ticket', self.id, db)
+
+        cursor = db.cursor()
+
+        key_names = self.get_key_prop_names()
+        key_values = self.get_key_prop_values()
+
+        sql_where = 'WHERE 1=1'
+        for k in key_names:
+            sql_where += " AND " + k + "=%%s" 
+
+        self.env.log.debug("Deleting %s: %s" % (self.realm, sql_where))
+        for k in key_names:
+            self.env.log.debug("%s = %s" % (k, self[k]))
+                       
+        cursor.execute(("DELETE FROM %s " + sql_where)
+            % self.realm, key_values)
+        cursor.execute(("DELETE FROM %s_change " + sql_where)
+            % self.realm, key_values)
+        cursor.execute(("DELETE FROM %s_custom " + sql_where) 
+            % self.realm, key_values)
+
+        self.post_delete(db)
                 
-            #Attachment.delete_all(self.env, 'ticket', self.id, db)
+        if handle_ta:
+            db.commit()
 
-            cursor = db.cursor()
-
-            key_names = self.get_key_prop_names()
-            key_values = self.get_key_prop_values()
-
-            sql_where = 'WHERE 1=1'
-            for k in key_names:
-                sql_where += " AND " + k + "=%%s" 
-
-            self.env.log.debug("Deleting %s: %s" % (self.realm, sql_where))
-            for k in key_names:
-                self.env.log.debug("%s = %s" % (k, self[k]))
-                           
-            cursor.execute(("DELETE FROM %s " + sql_where)
-                % self.realm, key_values)
-            cursor.execute(("DELETE FROM %s_change " + sql_where)
-                % self.realm, key_values)
-            cursor.execute(("DELETE FROM %s_custom " + sql_where) 
-                % self.realm, key_values)
-
-            self.post_delete(db)
-                
         from tracgenericclass.api import GenericClassSystem
         for listener in GenericClassSystem(self.env).change_listeners:
             listener.object_deleted(self.realm, self)
         """
         self.env.log.debug('>>> save_as')
 
+        db, handle_ta = get_db_for_write(self.env, db)
+
         old_key = self.key
         if self.pre_save_as(old_key, new_key, db):
             self.key = new_key
         
             self.post_save_as(old_key, new_key, db)
 
+            if handle_ta:
+                db.commit()
+
         self.env.log.debug('<<< save_as')
         
     def get_non_empty_prop_names(self):
         self.env.log.debug('>>> list_matching_objects')
         
         if db is None:
-            db = self._get_db(db)
+            db = get_db(self.env, db)
 
         self.pre_list_matching_objects(db)
 

File tracgenericclass/trunk/tracgenericclass/util.py

 import sys
 import traceback
 
+from datetime import datetime
+
 from trac.core import *
 
     
 
 checked_utimestamp = False
 has_utimestamp = False
+checked_compatibility = False
+has_read_db = False
     
 def to_any_timestamp(date_obj):
     global checked_utimestamp
         from trac.util.datefmt import to_timestamp
         return to_timestamp(date_obj)
 
-
 def from_any_timestamp(ts):
     global checked_utimestamp
     global has_utimestamp
 
     if has_utimestamp:
         from trac.util.datefmt import from_utimestamp
-
         return from_utimestamp(ts)
     else:
         # Trac 0.11
-        import datetime
         from trac.util.datefmt import utc
 
         return datetime.fromtimestamp(ts, utc)
 
-        
+def get_db(env, db=None):
+    global checked_compatibility
+    global has_read_db
+
+    if db:
+        return db
+
+    if not checked_compatibility:
+        check_compatibility(env)
+
+    if has_read_db:
+        return env.get_read_db()
+    else:
+        # Trac 0.11
+        return env.get_db_cnx()
+
+def get_db_for_write(env, db=None):
+    global checked_compatibility
+    global has_read_db
+
+    if db:
+        return (db, False)
+
+    if not checked_compatibility:
+        check_compatibility()
+
+    if has_read_db:
+        return (env.get_read_db(), True)
+    else:
+        # Trac 0.11
+        return (env.get_db_cnx(), True)
+
 def check_utimestamp():
     global checked_utimestamp
     global has_utimestamp
 
     checked_utimestamp = True
 
+def check_compatibility(env):
+    global checked_compatibility
+    global has_read_db
+
+    try:
+        if env.get_read_db():
+            has_read_db = True
+    except:
+        # Trac 0.11
+        has_read_db = False
+
+    checked_compatibility = True
+
 
 def to_list(params=[]):
     result = []