~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/bugs/gadgets.py

  • Committer: Bazaar Package Importer
  • Author(s): Moshe Zadka
  • Date: 2002-03-08 07:14:16 UTC
  • Revision ID: james.westby@ubuntu.com-20020308071416-oxvuw76tpcpi5v1q
Tags: upstream-0.15.5
ImportĀ upstreamĀ versionĀ 0.15.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Twisted, the Framework of Your Internet
 
2
# Copyright (C) 2001 Matthew W. Lefkowitz
 
3
#
 
4
# This library is free software; you can redistribute it and/or
 
5
# modify it under the terms of version 2.1 of the GNU Lesser General Public
 
6
# License as published by the Free Software Foundation.
 
7
#
 
8
# This library is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
# Lesser General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Lesser General Public
 
14
# License along with this library; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Web interface to the bugs database."""
 
18
 
 
19
# system imports
 
20
import string, cgi
 
21
 
 
22
# twisted imports
 
23
from twisted.web import widgets
 
24
 
 
25
 
 
26
class BugsPage(widgets.WidgetPage):
 
27
    """Default look for bug pages."""
 
28
 
 
29
    stylesheet = '''
 
30
    P, BODY, TD, OL, UL, MENU, BLOCKQUOTE, DIV
 
31
    {
 
32
        font-family: Lucida, Verdana, Helvetica, Arial;
 
33
    }
 
34
    '''
 
35
 
 
36
    template = '''
 
37
    <html>
 
38
    <head>
 
39
    <style>
 
40
    %%%%self.stylesheet%%%%
 
41
    </style>
 
42
    <title>%%%%self.title%%%%</title>
 
43
    <base href="%%%%request.prePathURL()%%%%">
 
44
    </head>
 
45
    <body>
 
46
    <p>
 
47
    <a href=".">View all bugs</a> || <a href="create">Submit a new bug</a>
 
48
    </p>
 
49
    %%%%self.widget%%%%
 
50
    </body>
 
51
    </html>
 
52
    '''
 
53
 
 
54
 
 
55
class BugsGadget(widgets.StreamWidget, widgets.Gadget):
 
56
    """The main bugs gadget."""
 
57
 
 
58
    title = "List of Bugs"
 
59
    page = BugsPage
 
60
 
 
61
    def __init__(self, database):
 
62
        widgets.Gadget.__init__(self)
 
63
        self.database = database
 
64
        self.putWidget('create', CreateNewForm(self.database))
 
65
        self.putWidget('view_bug', ViewBug(self.database))
 
66
 
 
67
    def stream(self, write, request):
 
68
        """Display the intro list of bugs. This is only called if there is no URI.
 
69
        """
 
70
        write(self.database.getAllBugs().addCallback(self._cbBugs))
 
71
 
 
72
    def _cbBugs(self, data):
 
73
        l = []
 
74
        l.append( '<table cellpadding=4 cellspacing=1 border=0 width="95%">'
 
75
                  '<tr bgcolor="#ff9900">'
 
76
                  '<td COLOR="#000000"><b> Summary </b> </td>'
 
77
                  '<td COLOR="#000000"><b> Type </b> </td>'
 
78
                  '<td COLOR="#000000"><b> Status </b> </td>'
 
79
                  '<td COLOR="#000000"><b> Assigned </b> </td>'
 
80
                  '<td COLOR="#000000"><b> Modification Date </b> </td>'
 
81
                  '</tr>\n' )
 
82
 
 
83
        for (id, summary, type, status, assigned, date_mod) in data:
 
84
            l.append( "<tr> <td> <a href='view_bug?bug_id=%d'>%s</a></td><td> %s </td> <td> %s</d><td> %s </td><td> %s </td></tr>\n" % (id, summary, type, status, assigned, date_mod) )
 
85
        l.append('</table>' )
 
86
        return l
 
87
 
 
88
 
 
89
class CreateNewForm(widgets.Form):
 
90
    """Form for creating new bugs."""
 
91
 
 
92
    title = "Create a new bug report:"
 
93
 
 
94
    def __init__(self, database):
 
95
        self.database = database
 
96
 
 
97
    def display(self, request):
 
98
        self.request = request
 
99
        typeNames = []
 
100
        for t in self.database.types:
 
101
            typeNames.append((t, string.capitalize(t)))
 
102
        self.formFields = [
 
103
            ['string', 'Name: ', 'name', ''],
 
104
            ['string', 'Email:', 'email', ''],
 
105
            ['string', 'Version:', 'version', ''],
 
106
            ['string', 'OS:', 'os', ''],
 
107
            ['checkbox', 'Security Related:', 'security', 0],
 
108
            ['menu', 'Type:', 'type', typeNames],
 
109
            ['string', 'Summary:', 'summary', ''],
 
110
            ['text', 'Description:', 'description', ''],
 
111
            ]
 
112
 
 
113
        return widgets.Form.display(self, self.request)
 
114
 
 
115
    def process(self, write, request, submit, name, email, version, os, security, type, summary, description):
 
116
        write(self.database.createBug(name, email, version, os, security, type, summary, description).addCallback(self._cbPosted))
 
117
 
 
118
    def _cbPosted(self, result):
 
119
        return ["Posted new bug."]
 
120
 
 
121
 
 
122
class AddCommentsForm(widgets.Form):
 
123
    """Form for adding comments to a bug."""
 
124
 
 
125
    def __init__(self, database):
 
126
        self.database = database
 
127
 
 
128
    def getFormFields(self, request, fieldSet=[]):
 
129
        bug_id = int(request.args.get('bug_id',[0])[0])
 
130
        formFields = [['string', 'Name: ', 'name', ''],
 
131
                      ['string', 'Email:', 'email', ''],
 
132
                      ['text', 'Comment:', 'comment', ''],
 
133
                      ['hidden', '', 'bug_id', bug_id],
 
134
                     ]
 
135
        return widgets.Form.getFormFields(self, request, formFields)
 
136
 
 
137
    def process(self, write, request, submit, bug_id, name, email, comment):
 
138
        write(self.database.createComment(bug_id, name, email, comment).addCallback(self._cbPosted))
 
139
 
 
140
    def _cbPosted(self, result):
 
141
        return ["Added new comment."]
 
142
 
 
143
 
 
144
class ViewBug(widgets.Widget):
 
145
    """Display a bug."""
 
146
 
 
147
    def __init__(self, database):
 
148
        self.database = database
 
149
        self.addComments = AddCommentsForm(database)
 
150
 
 
151
    def display(self, request):
 
152
        bug_id = int(request.args.get('bug_id',[0])[0])
 
153
        db = self.database
 
154
        return [db.getBug(bug_id).addCallback(self._cbBug), "\n<p>&nbsp; </p>\n<h3>Add a comment</h3>\n"] + \
 
155
               self.addComments.display(request) + \
 
156
               ['<p>&nbsp; </p>\n', db.getBugComments(bug_id).addCallback(self._cbComments)]
 
157
 
 
158
    def _cbBug(self, result):
 
159
        l = []
 
160
        desc = result[0][12]
 
161
        email = cgi.escape(result[0][2], 1)
 
162
        result = map(cgi.escape, map(str, result[0]))
 
163
        l.append('<h3>%s</h3>\n' % result[11])
 
164
        l.append('<p>\n<b>Submitted by <a href="mailto:%s">%s</a> on %s</b><br>\n' % (email, result[1], result[4]))
 
165
        l.append('<b>Last modified:</b> %s<br>\n' % result[5])
 
166
        l.append('<b>Version:</b> %s<br>\n'% result[6])
 
167
        l.append('<b>OS:</b> %s<br>\n' % result[7])
 
168
        l.append('<b>Type:</b> %s<br>\n' % result[9])
 
169
        l.append('<b>Status:</b> %s<br>\n' % result[10])
 
170
        l.append('<b>Assigned to:</b> %s<br>\n</p>\n' % result[3])
 
171
        l.append('<pre>\n%s\n</pre>' % desc)
 
172
        return l
 
173
 
 
174
    def _cbComments(self, result):
 
175
        l  = ["<h3>Comments</h3>\n"]
 
176
        for id, name, email, date, comment in result:
 
177
            l.append('<p><b><a href="mailto:%s">%s</a> on %s:</b><br>\n'
 
178
                     '<pre>%s</pre></p>\n' % (cgi.escape(email, 1), cgi.escape(name), date, comment))
 
179
        return l