def get_groups(self, by='component'): cursor = self.db.cursor() groups = [] if by in ['status', 'resolution', 'severity', 'priority']: cursor.execute( "SELECT name FROM enum WHERE type = %s " "AND IFNULL(name,'') != '' ORDER BY value", by) elif by in ['component', 'milestone', 'version']: cursor.execute("SELECT name FROM %s " "WHERE IFNULL(name,'') != '' ORDER BY name" % by) elif by == 'owner': cursor.execute("SELECT DISTINCT owner AS name FROM ticket " "ORDER BY owner") elif by not in Ticket.std_fields: fields = get_custom_fields(self.env) field = [f for f in fields if f['name'] == by] if not field: return [] return [o for o in field[0]['options'] if o] while 1: row = cursor.fetchone() if not row: break groups.append(row['name'] or '') return groups
def render_view(self, id): if self.perm.has_permission(perm.MILESTONE_DELETE): self.req.hdf.setValue('milestone.href.delete', self.env.href.milestone(id, 'delete')) if self.perm.has_permission(perm.MILESTONE_MODIFY): self.req.hdf.setValue('milestone.href.edit', self.env.href.milestone(id, 'edit')) milestone = self.get_milestone(id) self.req.hdf.setValue('title', 'Milestone %s' % milestone['name']) self.req.hdf.setValue('milestone.mode', 'view') add_to_hdf(milestone, self.req.hdf, 'milestone') available_groups = [ 'component', 'version', 'severity', 'priority', 'owner' ] available_groups += [ f['name'] for f in get_custom_fields(self.env) if f['type'] == 'select' or f['type'] == 'radio' ] add_to_hdf(available_groups, self.req.hdf, 'milestone.stats.available_groups') by = self.args.get('by', 'component') self.req.hdf.setValue('milestone.stats.grouped_by', by) tickets = get_tickets_for_milestone(self.env, self.db, id, by) stats = calc_ticket_stats(tickets) add_to_hdf(stats, self.req.hdf, 'milestone.stats') queries = get_query_links(self.env, milestone['name']) add_to_hdf(queries, self.req.hdf, 'milestone.queries') showempty = self.args.has_key('showempty') if showempty: self.req.hdf.setValue('milestone.stats.show_empty', '1') groups = self.get_groups(by) group_no = 0 for group in groups: group_tickets = [t for t in tickets if t[by] == group] if not showempty and not group_tickets: continue prefix = 'milestone.stats.groups.%s' % group_no self.req.hdf.setValue('%s.name' % prefix, group) percent_total = 0 if len(tickets) > 0: percent_total = float(len(group_tickets)) / float(len(tickets)) self.req.hdf.setValue('%s.percent_total' % prefix, str(percent_total * 100)) stats = calc_ticket_stats(group_tickets) add_to_hdf(stats, self.req.hdf, prefix) queries = get_query_links(self.env, milestone['name'], by, group) add_to_hdf(queries, self.req.hdf, '%s.queries' % prefix) group_no += 1
def _render_editor(self, constraints, order, desc): self.req.hdf.setValue('title', 'Custom Query') util.add_to_hdf(constraints, self.req.hdf, 'query.constraints') self.req.hdf.setValue('query.order', order or 'priority') if desc: self.req.hdf.setValue('query.desc', '1') def add_options(field, constraints, prefix, cursor, sql): options = [] check = constraints.has_key(field) cursor.execute(sql) while 1: row = cursor.fetchone() if not row: break option = {'name': row[0]} if check and (row[0] in constraints[field]): option['selected'] = 1 options.append(option) util.add_to_hdf(options, self.req.hdf, prefix + field) if check: del constraints[field] cursor = self.db.cursor() add_options( 'status', constraints, 'query.options.', cursor, "SELECT name FROM enum WHERE type='status' ORDER BY value") add_options( 'resolution', constraints, 'query.options.', cursor, "SELECT name FROM enum WHERE type='resolution' ORDER BY value") add_options('component', constraints, 'query.options.', cursor, "SELECT name FROM component ORDER BY name") add_options('milestone', constraints, 'query.options.', cursor, "SELECT name FROM milestone ORDER BY name") add_options('version', constraints, 'query.options.', cursor, "SELECT name FROM version ORDER BY name") add_options( 'priority', constraints, 'query.options.', cursor, "SELECT name FROM enum WHERE type='priority' ORDER BY value") add_options( 'severity', constraints, 'query.options.', cursor, "SELECT name FROM enum WHERE type='severity' ORDER BY value") custom_fields = get_custom_fields(self.env) for custom in custom_fields: if custom['type'] == 'select' or custom['type'] == 'radio': check = constraints.has_key(custom['name']) options = filter(None, custom['options']) for i in range(len(options)): options[i] = {'name': options[i]} if check and (options[i]['name'] in constraints[custom['name']]): options[i]['selected'] = 1 custom['options'] = options util.add_to_hdf(custom_fields, self.req.hdf, 'query.custom')
def get_constraints(self): constraints = {} custom_fields = [f['name'] for f in get_custom_fields(self.env)] constrained_fields = [k for k in self.args.keys() if k in Ticket.std_fields or k in custom_fields] for field in constrained_fields: vals = self.args[field] if type(vals) is ListType: vals = map(lambda x: x.value, filter(None, vals)) elif vals.value: vals = [vals.value] else: continue constraints[field] = vals return constraints
def _render_results(self, constraints, order, desc): self.req.hdf.setValue('title', 'Custom Query') self.req.hdf.setValue( 'query.edit_href', self.env.href.query(constraints, order, desc, action='edit')) # FIXME: the user should be able to configure which columns should # be displayed headers = ['id', 'summary', 'status', 'component', 'owner'] cols = headers if not 'priority' in cols: cols.append('priority') if order != 'id' and not order in Ticket.std_fields: # order by priority by default order = 'priority' for i in range(len(headers)): self.req.hdf.setValue('query.headers.%d.name' % i, headers[i]) if headers[i] == order: self.req.hdf.setValue( 'query.headers.%d.href' % i, self.env.href.query(constraints, order, not desc)) self.req.hdf.setValue('query.headers.%d.order' % i, desc and 'desc' or 'asc') else: self.req.hdf.setValue( 'query.headers.%d.href' % i, self.env.href.query(constraints, headers[i])) sql = [] sql.append("SELECT " + ", ".join(headers)) custom_fields = [f['name'] for f in get_custom_fields(self.env)] for k in [k for k in constraints.keys() if k in custom_fields]: sql.append(", %s.value AS %s" % (k, k)) sql.append(" FROM ticket") for k in [k for k in constraints.keys() if k in custom_fields]: sql.append(" LEFT OUTER JOIN ticket_custom AS %s ON " \ "(id=%s.ticket AND %s.name='%s')" % (k, k, k, k)) for col in [ c for c in ['status', 'resolution', 'priority', 'severity'] if c in cols ]: sql.append(" INNER JOIN (SELECT name AS %s_name, value AS %s_value " \ "FROM enum WHERE type='%s')" \ " ON %s_name=%s" % (col, col, col, col, col)) clauses = [] for k, v in constraints.items(): if len(v) > 1: inlist = ["'" + util.sql_escape(item) + "'" for item in v] clauses.append("%s IN (%s)" % (k, ",".join(inlist))) elif k in ['keywords', 'cc']: clauses.append("%s LIKE '%%%s%%'" % (k, util.sql_escape(v[0]))) else: clauses.append("%s='%s'" % (k, util.sql_escape(v[0]))) if clauses: sql.append(" WHERE " + " AND ".join(clauses)) if order in ['status', 'resolution', 'priority', 'severity']: sql.append(" ORDER BY %s_value" % order) else: sql.append(" ORDER BY " + order) if desc: sql.append(" DESC") sql = "".join(sql) self.log.debug("SQL Query: %s" % sql) results = self.get_results(sql) util.add_to_hdf(results, self.req.hdf, 'query.results')