| 1 | # svntrac
|
|---|
| 2 | #
|
|---|
| 3 | # Copyright (C) 2003 Jonas Borgström <jonas@xyche.com>
|
|---|
| 4 | #
|
|---|
| 5 | # svntrac is free software; you can redistribute it and/or
|
|---|
| 6 | # modify it under the terms of the GNU General Public License as
|
|---|
| 7 | # published by the Free Software Foundation; either version 2 of the
|
|---|
| 8 | # License, or (at your option) any later version.
|
|---|
| 9 | #
|
|---|
| 10 | # svntrac is distributed in the hope that it will be useful,
|
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | # General Public License for more details.
|
|---|
| 14 | #
|
|---|
| 15 | # You should have received a copy of the GNU General Public License
|
|---|
| 16 | # along with this program; if not, write to the Free Software
|
|---|
| 17 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 18 | #
|
|---|
| 19 | # Author: Jonas Borgström <jonas@xyche.com>
|
|---|
| 20 |
|
|---|
| 21 | from util import *
|
|---|
| 22 | from Module import Module
|
|---|
| 23 | import perm
|
|---|
| 24 | import db
|
|---|
| 25 |
|
|---|
| 26 | import time
|
|---|
| 27 | import StringIO
|
|---|
| 28 |
|
|---|
| 29 | class Report (Module):
|
|---|
| 30 | template_key = 'report_template'
|
|---|
| 31 |
|
|---|
| 32 | def __init__(self, config, args, pool):
|
|---|
| 33 | Module.__init__(self, config, args, pool)
|
|---|
| 34 |
|
|---|
| 35 | def get_info (self, id):
|
|---|
| 36 | cnx = db.get_connection()
|
|---|
| 37 | cursor = cnx.cursor ()
|
|---|
| 38 |
|
|---|
| 39 | if id == -1:
|
|---|
| 40 | # If no special report was requested, display
|
|---|
| 41 | # a list of available reports instead
|
|---|
| 42 | cursor.execute ("SELECT id AS report, title "
|
|---|
| 43 | "FROM report "
|
|---|
| 44 | "ORDER BY report")
|
|---|
| 45 | title = 'available reports'
|
|---|
| 46 | else:
|
|---|
| 47 | cursor.execute ('SELECT title, sql from report WHERE id=%s', id)
|
|---|
| 48 | row = cursor.fetchone()
|
|---|
| 49 | title = row[0]
|
|---|
| 50 | sql = row[1]
|
|---|
| 51 | cursor.execute (sql)
|
|---|
| 52 |
|
|---|
| 53 | # FIXME: fetchall should probably not be used.
|
|---|
| 54 | info = cursor.fetchall()
|
|---|
| 55 | cols = cursor.rs.col_defs
|
|---|
| 56 | # Set all NULL elements to ''
|
|---|
| 57 | info = map(lambda row: map(lambda x: x or '', row), info)
|
|---|
| 58 | return [cols, info, title]
|
|---|
| 59 |
|
|---|
| 60 | def render_headers (self, out, row):
|
|---|
| 61 | """
|
|---|
| 62 | render a html table header with the column names from the sql query.
|
|---|
| 63 | """
|
|---|
| 64 | out.write ('<tr>')
|
|---|
| 65 | for x in row:
|
|---|
| 66 | out.write ('<th>%s</th>' % x[0])
|
|---|
| 67 | out.write ('</tr>')
|
|---|
| 68 |
|
|---|
| 69 | def render_row (self, out, row, cols):
|
|---|
| 70 | """
|
|---|
| 71 | render one html table row from one sql result row.
|
|---|
| 72 |
|
|---|
| 73 | Some values are handled specially: ticker and report numbers
|
|---|
| 74 | are hyper linked...
|
|---|
| 75 | """
|
|---|
| 76 | out.write ('<tr>')
|
|---|
| 77 | idx = 0
|
|---|
| 78 | for value in row:
|
|---|
| 79 | if cols[idx][0] in ['ticket', '#']:
|
|---|
| 80 | out.write('<td><a href="%s">#%s</a></td>' % (ticket_href(value),
|
|---|
| 81 | value))
|
|---|
| 82 | elif cols[idx][0] == 'report':
|
|---|
| 83 | out.write('<td><a href="%s">{%s}</a></td>'
|
|---|
| 84 | % (report_href(value), value))
|
|---|
| 85 |
|
|---|
| 86 | elif cols[idx][0] in ['time', 'date', 'created', 'modified']:
|
|---|
| 87 | out.write('<td>%s</td>'
|
|---|
| 88 | % time.strftime('%F', time.localtime(int(value))))
|
|---|
| 89 |
|
|---|
| 90 | else:
|
|---|
| 91 | out.write('<td>%s</td>' % value)
|
|---|
| 92 | idx = idx + 1
|
|---|
| 93 | out.write ('</tr>')
|
|---|
| 94 |
|
|---|
| 95 | def create_report (self, title, sql):
|
|---|
| 96 | perm.assert_permission (perm.REPORT_CREATE)
|
|---|
| 97 |
|
|---|
| 98 | cnx = db.get_connection()
|
|---|
| 99 | cursor = cnx.cursor ()
|
|---|
| 100 |
|
|---|
| 101 | cursor.execute ('INSERT INTO report (id, title, sql)'
|
|---|
| 102 | 'VALUES (NULL, %s, %s)', title, sql)
|
|---|
| 103 | id = cnx.db.sqlite_last_insert_rowid ()
|
|---|
| 104 | cnx.commit ()
|
|---|
| 105 | redirect (report_href (id))
|
|---|
| 106 |
|
|---|
| 107 | def delete_report (self, id):
|
|---|
| 108 | perm.assert_permission (perm.REPORT_DELETE)
|
|---|
| 109 |
|
|---|
| 110 | cnx = db.get_connection()
|
|---|
| 111 | cursor = cnx.cursor ()
|
|---|
| 112 |
|
|---|
| 113 | cursor.execute ('DELETE FROM report WHERE id=%s', id)
|
|---|
| 114 | cnx.commit ()
|
|---|
| 115 | redirect (report_href ())
|
|---|
| 116 |
|
|---|
| 117 | def commit_changes (self, id):
|
|---|
| 118 | """
|
|---|
| 119 | saves report changes to the database
|
|---|
| 120 | """
|
|---|
| 121 | perm.assert_permission (perm.REPORT_MODIFY)
|
|---|
| 122 |
|
|---|
| 123 | cnx = db.get_connection()
|
|---|
| 124 | cursor = cnx.cursor ()
|
|---|
| 125 |
|
|---|
| 126 | title = self.args['title']
|
|---|
| 127 | sql = self.args['sql']
|
|---|
| 128 |
|
|---|
| 129 | cursor.execute ('UPDATE report SET title=%s, sql=%s WHERE id=%s',
|
|---|
| 130 | title, sql, id)
|
|---|
| 131 | cnx.commit ()
|
|---|
| 132 | redirect (report_href (id))
|
|---|
| 133 |
|
|---|
| 134 | def render_report_editor (self, out, id, action='commit'):
|
|---|
| 135 | cnx = db.get_connection()
|
|---|
| 136 | cursor = cnx.cursor()
|
|---|
| 137 |
|
|---|
| 138 | if id == -1:
|
|---|
| 139 | title = sql = ""
|
|---|
| 140 | else:
|
|---|
| 141 | rs = cursor.execute ('SELECT title, sql FROM report WHERE id=%s', id)
|
|---|
| 142 | row = cursor.fetchone()
|
|---|
| 143 | title = row[0]
|
|---|
| 144 | sql = row[1]
|
|---|
| 145 |
|
|---|
| 146 | out.write (
|
|---|
| 147 | '<form action="" method="post">'
|
|---|
| 148 | '<input type="hidden" name="mode" value="report">'
|
|---|
| 149 | '<input type="hidden" name="id" value="%d">'
|
|---|
| 150 | '<input type="hidden" name="action" value="%s">'
|
|---|
| 151 | 'title:<br><input type="text" name="title" value="%s" size="50">'
|
|---|
| 152 | '<br>sql query:'
|
|---|
| 153 | '<br>'
|
|---|
| 154 | '<textarea name="sql" cols="70" rows="10">%s</textarea>'
|
|---|
| 155 | '<br>'
|
|---|
| 156 | '<input type="submit" value="commit"> '
|
|---|
| 157 | '<input type="reset" value="reset">'
|
|---|
| 158 | '</form>' % (id, action, title, sql)
|
|---|
| 159 | )
|
|---|
| 160 |
|
|---|
| 161 | def render_report_list (self, out, id):
|
|---|
| 162 | """
|
|---|
| 163 | uses a user specified sql query to extract some information
|
|---|
| 164 | from the database and presents it as a html table.
|
|---|
| 165 | """
|
|---|
| 166 | try:
|
|---|
| 167 | [cols, rows, title] = self.get_info (id)
|
|---|
| 168 | except Exception, e:
|
|---|
| 169 | out.write ('<h3>report failed: %s</h3>' % e)
|
|---|
| 170 | out.write ('<p><a href="%s">edit</a></p>' % report_href(id, 'edit'))
|
|---|
| 171 | return
|
|---|
| 172 | if perm.has_permission (perm.REPORT_CREATE):
|
|---|
| 173 | out.write ('<a href="%s">new report</a>' % report_href (None, 'new'))
|
|---|
| 174 | out.write ('<h3>%s</h3><p>' % title)
|
|---|
| 175 | if id != -1:
|
|---|
| 176 | if perm.has_permission (perm.REPORT_MODIFY):
|
|---|
| 177 | out.write ('<a href="%s">edit</a> | ' % report_href(id, 'edit'))
|
|---|
| 178 | if perm.has_permission (perm.REPORT_CREATE):
|
|---|
| 179 | out.write ('<a href="%s">copy</a> | ' % report_href(id, 'copy'))
|
|---|
| 180 | if perm.has_permission (perm.REPORT_DELETE):
|
|---|
| 181 | out.write ('<a href="%s">delete</a>' % report_href(id, 'delete'))
|
|---|
| 182 | out.write ('</p>')
|
|---|
| 183 |
|
|---|
| 184 | out.write ('<table class="report">')
|
|---|
| 185 | self.render_headers (out, cols)
|
|---|
| 186 | for row in rows:
|
|---|
| 187 | self.render_row (out, row, cols)
|
|---|
| 188 | out.write ('</table>')
|
|---|
| 189 |
|
|---|
| 190 | def render (self):
|
|---|
| 191 | # did the user ask for any special report?
|
|---|
| 192 | if self.args.has_key('id'):
|
|---|
| 193 | id = int(self.args['id'])
|
|---|
| 194 | else:
|
|---|
| 195 | # show a list of available reports
|
|---|
| 196 | id = -1
|
|---|
| 197 |
|
|---|
| 198 | if self.args.has_key('action'):
|
|---|
| 199 | action = self.args['action']
|
|---|
| 200 | else:
|
|---|
| 201 | action = 'list'
|
|---|
| 202 | out = StringIO.StringIO()
|
|---|
| 203 |
|
|---|
| 204 | if action == 'create':
|
|---|
| 205 | self.create_report (self.args['title'], self.args['sql'])
|
|---|
| 206 | elif action == 'delete':
|
|---|
| 207 | self.delete_report (id)
|
|---|
| 208 | elif action == 'commit':
|
|---|
| 209 | self.commit_changes (id)
|
|---|
| 210 | elif action == 'new':
|
|---|
| 211 | self.render_report_editor (out, -1, 'create')
|
|---|
| 212 | elif action == 'copy':
|
|---|
| 213 | self.render_report_editor (out, id, 'create')
|
|---|
| 214 | elif action == 'edit':
|
|---|
| 215 | self.render_report_editor (out, id, 'commit')
|
|---|
| 216 | else:
|
|---|
| 217 | self.render_report_list (out, id)
|
|---|
| 218 |
|
|---|
| 219 | self.namespace['content'] = out.getvalue()
|
|---|