Changeset 4084
- Timestamp:
- 07/30/08 05:54:12 (5 months ago)
- Files:
-
- ticketboxmacro/0.11/TicketBox.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
ticketboxmacro/0.11/TicketBox.py
r4083 r4084 12 12 [[TicketBox([report:1])]] ... alternate format of report 13 13 [[TicketBox([report:9?name=val])]] ... report with dynamic variable 14 [[TicketBox({1},#50,{2},100)]] ... convination of above 14 [[TicketBox([query:status=new])]]] ... query string 15 [[TicketBox({1},[query:status=new])]] ... conbination 15 16 [[TicketBox(500pt,{1})]] ... with box width as 50 point 16 17 [[TicketBox(200px,{1})]] ... with box width as 200 pixel … … 44 45 from trac.ticket.report import ReportModule 45 46 from trac.ticket.model import Ticket 47 from trac.ticket.query import Query 46 48 47 49 ## default style values … … 54 56 r"{(?P<rptnum>\d+)}", 55 57 r"\[report:(?P<rptnum2>\d+)(?P<dv>\?.*)?\]", 58 r"\[query:(?P<query>[^\]]*)\]", 56 59 r"(?P<width>\d+(pt|px|%))", 57 60 r"(?P<title>'[^']*'|\"[^\"]*\")", … … 139 142 env = formatter.env 140 143 args = parse(content or '') 144 try: 145 db = env.get_db_cnx() 146 return doit(req, env, args, db) 147 finally: 148 if db and not hasattr(env, 'get_cnx_pool'): 149 # without db connection pool, we should close db. 150 db.close() 151 152 def doit(req, env, args, db): 141 153 items = [] 142 154 summary = None … … 172 184 # pick up ticket numbers and report numbers 173 185 for arg in args: 186 sql = None 187 params = [] 174 188 match = args_re.match(arg) 189 id_name = ticket 190 sidx = iidx = -1 175 191 if not match: 176 192 continue 177 193 elif match.group('tktnum'): 178 194 items.append(int(match.group('tktnum'))) 195 elif match.group('query'): 196 q = Query.from_string(env, match.group('query')) 197 sql, params = q.get_sql(req) 198 id_name = 'id' 179 199 elif match.group('rptnum') or match.group('rptnum2'): 180 200 num = match.group('rptnum') or match.group('rptnum2') 181 dv = {}182 # username, do not override if specified183 if not dv.has_key('USER'):184 dv['USER'] = req.authname185 if match.group('dv'):186 for expr in string.split(match.group('dv')[1:], '&'):187 k, v = string.split(expr, '=')188 dv[k] = v189 201 #env.log.debug('dynamic variables = %s' % dv) 190 db = env.get_db_cnx()191 202 curs = db.cursor() 192 203 try: 193 204 curs.execute('SELECT query FROM report WHERE id=%s' % num) 194 (query,) = curs.fetchone() 195 # replace dynamic variables with sql_sub_vars() 205 (sql,) = curs.fetchone() 206 finally: 207 curs.close() 208 if sql: 209 sql = sql.strip() 210 if sql.lower().startswith("query:"): 211 if sql.lower().startswith('query:?'): 212 raise Exception('URL style of query string is not supported.') 213 q = Query.from_string(env, sql[6:]) 214 sql, params = q.get_sql(req) 215 id_name = 'id' 216 if sql: 217 if not params: 218 # handle dynamic variables 196 219 # NOTE: sql_sub_vars() takes different arguments in 197 220 # several trac versions. … … 199 222 # For 0.10.x, arguments are (req, query, args, db) 200 223 # For 0.11 or later, arguments are (query, args, db) 201 query, dv = ReportModule(env).sql_sub_vars(query, dv, db) 202 #env.log.debug('query = %s' % query) 203 curs.execute(query, dv) 224 dv = ReportModule(env).get_var_args(req) 225 sql, params = ReportModule(env).sql_sub_vars(sql, dv, db) 226 try: 227 #env.log.debug('sql = %s' % sql) 228 curs = db.cursor() 229 curs.execute(sql, params) 204 230 rows = curs.fetchall() 205 231 if rows: 206 232 descriptions = [desc[0] for desc in curs.description] 207 233 try: 208 i dx = descriptions.index(ticket)234 iidx = descriptions.index(id_name) 209 235 except: 210 raise Exception('No such column for ticket : %r'211 % ticket)236 raise Exception('No such column for ticket number: %r' 237 % id_name ) 212 238 if summary: 213 239 try: … … 217 243 % summary) 218 244 for row in rows: 219 items.append(row[i dx])220 if summary :221 summaries[row[i dx]] = row[sidx]245 items.append(row[iidx]) 246 if summary and 0 <= sidx: 247 summaries[row[iidx]] = row[sidx] 222 248 finally: 223 if not hasattr(env, 'get_cnx_pool'): 224 # without db connection pool, we should close db. 225 curs.close() 226 db.close() 249 curs.close() 250 227 251 if summary: 228 252 # get summary text … … 242 266 html = string.join([wiki_to_oneliner("%s (#%d)" % (summaries[n],n), 243 267 env, 244 env.get_db_cnx(),245 req= formatter.req) for n in items], "<br>")268 db, 269 req=req) for n in items], "<br>") 246 270 else: 247 271 html = wiki_to_oneliner(string.join(["#%d" % c for c in items], ", "), 248 env, env.get_db_cnx(), req=formatter.req)272 env, db, req=req) 249 273 if html != '': 250 274 try:
