Code

A few big changes in this commit:
[roundup.git] / roundup / roundupdb.py
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 # $Id: roundupdb.py,v 1.102 2004-03-19 04:47:59 richard Exp $
20 """Extending hyperdb with types specific to issue-tracking.
21 """
22 __docformat__ = 'restructuredtext'
24 from __future__ import nested_scopes
26 import re, os, smtplib, socket, time, random
27 import cStringIO, base64, quopri, mimetypes
29 from rfc2822 import encode_header
31 from roundup import password, date, hyperdb
33 # MessageSendError is imported for backwards compatibility
34 from roundup.mailer import Mailer, straddr, MessageSendError
36 class Database:
37     def getuid(self):
38         """Return the id of the "user" node associated with the user
39         that owns this connection to the hyperdatabase."""
40         if self.journaltag is None:
41             return None
42         elif self.journaltag == 'admin':
43             # admin user may not exist, but always has ID 1
44             return '1'
45         else:
46             return self.user.lookup(self.journaltag)
48     def getUserTimezone(self):
49         """Return user timezone defined in 'timezone' property of user class.
50         If no such property exists return 0
51         """
52         userid = self.getuid()
53         try:
54             timezone = int(self.user.get(userid, 'timezone'))
55         except (KeyError, ValueError, TypeError):
56             # If there is no class 'user' or current user doesn't have timezone 
57             # property or that property is not numeric assume he/she lives in 
58             # Greenwich :)
59             timezone = 0
60         return timezone
62     def confirm_registration(self, otk):
63         props = self.getOTKManager().getall(otk)
64         for propname, proptype in self.user.getprops().items():
65             value = props.get(propname, None)
66             if value is None:
67                 pass
68             elif isinstance(proptype, hyperdb.Date):
69                 props[propname] = date.Date(value)
70             elif isinstance(proptype, hyperdb.Interval):
71                 props[propname] = date.Interval(value)
72             elif isinstance(proptype, hyperdb.Password):
73                 props[propname] = password.Password()
74                 props[propname].unpack(value)
76         # tag new user creation with 'admin'
77         self.journaltag = 'admin'
79         # create the new user
80         cl = self.user
81       
82         props['roles'] = self.config.NEW_WEB_USER_ROLES
83         userid = cl.create(**props)
84         # clear the props from the otk database
85         self.getOTKManager().destroy(otk)
86         self.commit()
87         
88         return userid
91 class DetectorError(RuntimeError):
92     """ Raised by detectors that want to indicate that something's amiss
93     """
94     pass
96 # deviation from spec - was called IssueClass
97 class IssueClass:
98     """This class is intended to be mixed-in with a hyperdb backend
99     implementation. The backend should provide a mechanism that
100     enforces the title, messages, files, nosy and superseder
101     properties:
103     - title = hyperdb.String(indexme='yes')
104     - messages = hyperdb.Multilink("msg")
105     - files = hyperdb.Multilink("file")
106     - nosy = hyperdb.Multilink("user")
107     - superseder = hyperdb.Multilink(classname)
108     """
110     # New methods:
111     def addmessage(self, nodeid, summary, text):
112         """Add a message to an issue's mail spool.
114         A new "msg" node is constructed using the current date, the user that
115         owns the database connection as the author, and the specified summary
116         text.
118         The "files" and "recipients" fields are left empty.
120         The given text is saved as the body of the message and the node is
121         appended to the "messages" field of the specified issue.
122         """
124     # XXX "bcc" is an optional extra here...
125     def nosymessage(self, nodeid, msgid, oldvalues, whichnosy='nosy',
126             from_address=None, cc=[]): #, bcc=[]):
127         """Send a message to the members of an issue's nosy list.
129         The message is sent only to users on the nosy list who are not
130         already on the "recipients" list for the message.
131         
132         These users are then added to the message's "recipients" list.
134         If 'msgid' is None, the message gets sent only to the nosy
135         list, and it's called a 'System Message'.
136         """
137         authid = self.db.msg.safeget(msgid, 'author')
138         recipients = self.db.msg.safeget(msgid, 'recipients', [])
139         
140         sendto = []
141         seen_message = {}
142         for recipient in recipients:
143             seen_message[recipient] = 1
145         def add_recipient(userid):
146             # make sure they have an address
147             address = self.db.user.get(userid, 'address')
148             if address:
149                 sendto.append(address)
150                 recipients.append(userid)
152         def good_recipient(userid):
153             # Make sure we don't send mail to either the anonymous
154             # user or a user who has already seen the message.
155             return (userid and
156                     (self.db.user.get(userid, 'username') != 'anonymous') and
157                     not seen_message.has_key(userid))
159         # possibly send the message to the author, as long as they aren't
160         # anonymous
161         if (good_recipient(authid) and
162             (self.db.config.MESSAGES_TO_AUTHOR == 'yes' or
163              (self.db.config.MESSAGES_TO_AUTHOR == 'new' and not oldvalues))):
164             add_recipient(authid)
165         
166         if authid:
167             seen_message[authid] = 1
168         
169         # now deal with the nosy and cc people who weren't recipients.
170         for userid in cc + self.get(nodeid, whichnosy):
171             if good_recipient(userid):
172                 add_recipient(userid)        
174         if oldvalues:
175             note = self.generateChangeNote(nodeid, oldvalues)
176         else:
177             note = self.generateCreateNote(nodeid)
179         # If we have new recipients, update the message's recipients
180         # and send the mail.
181         if sendto:
182             if msgid:
183                 self.db.msg.set(msgid, recipients=recipients)
184             self.send_message(nodeid, msgid, note, sendto, from_address)
186     # backwards compatibility - don't remove
187     sendmessage = nosymessage
189     def send_message(self, nodeid, msgid, note, sendto, from_address=None):
190         '''Actually send the nominated message from this node to the sendto
191            recipients, with the note appended.
192         '''
193         users = self.db.user
194         messages = self.db.msg
195         files = self.db.file
196        
197         inreplyto = messages.safeget(msgid, 'inreplyto')
198         messageid = messages.safeget(msgid, 'messageid')
200         # make up a messageid if there isn't one (web edit)
201         if not messageid:
202             # this is an old message that didn't get a messageid, so
203             # create one
204             messageid = "<%s.%s.%s%s@%s>"%(time.time(), random.random(),
205                                            self.classname, nodeid,
206                                            self.db.config.MAIL_DOMAIN)
207             messages.set(msgid, messageid=messageid)
209         # compose title
210         cn = self.classname
211         title = self.get(nodeid, 'title') or '%s message copy'%cn
213         # figure author information
214         authid = messages.safeget(msgid, 'author')
215         authname = users.safeget(authid, 'realname')
216         if not authname:
217             authname = users.safeget(authid, 'username', '')
218         authaddr = users.safeget(authid, 'address', '')
219         if authaddr:
220             authaddr = " <%s>" % straddr( ('',authaddr) )
222         # make the message body
223         m = ['']
225         # put in roundup's signature
226         if self.db.config.EMAIL_SIGNATURE_POSITION == 'top':
227             m.append(self.email_signature(nodeid, msgid))
229         # add author information
230         if authid:
231             if len(self.get(nodeid,'messages')) == 1:
232                 m.append("New submission from %s%s:"%(authname, authaddr))
233             else:
234                 m.append("%s%s added the comment:"%(authname, authaddr))
235         else:
236             m.append("System message:")
237         m.append('')
239         # add the content
240         m.append(messages.safeget(msgid, 'content', ''))
242         # add the change note
243         if note:
244             m.append(note)
246         # put in roundup's signature
247         if self.db.config.EMAIL_SIGNATURE_POSITION == 'bottom':
248             m.append(self.email_signature(nodeid, msgid))
250         # encode the content as quoted-printable
251         charset = getattr(self.db.config, 'EMAIL_CHARSET', 'utf-8')
252         m = '\n'.join(m)
253         if charset != 'utf-8':
254             m = unicode(m, 'utf-8').encode(charset)
255         content = cStringIO.StringIO(m)
256         content_encoded = cStringIO.StringIO()
257         quopri.encode(content, content_encoded, 0)
258         content_encoded = content_encoded.getvalue()
260         # get the files for this message
261         message_files = msgid and messages.get(msgid, 'files') or None
263         # make sure the To line is always the same (for testing mostly)
264         sendto.sort()
266         # make sure we have a from address
267         if from_address is None:
268             from_address = self.db.config.TRACKER_EMAIL
270         # additional bit for after the From: "name"
271         from_tag = getattr(self.db.config, 'EMAIL_FROM_TAG', '')
272         if from_tag:
273             from_tag = ' ' + from_tag
275         subject = '[%s%s] %s'%(cn, nodeid, title)
276         author = (authname + from_tag, from_address)
278         # create the message
279         mailer = Mailer(self.db.config)
280         message, writer = mailer.get_standard_message(sendto, subject, author)
282         # set reply-to to the tracker
283         tracker_name = self.db.config.TRACKER_NAME
284         if charset != 'utf-8':
285             tracker = unicode(tracker_name, 'utf-8').encode(charset)
286         tracker_name = encode_header(tracker_name, charset)
287         writer.addheader('Reply-To', straddr((tracker_name, from_address)))
289         # message ids
290         if messageid:
291             writer.addheader('Message-Id', messageid)
292         if inreplyto:
293             writer.addheader('In-Reply-To', inreplyto)
295         # attach files
296         if message_files:
297             part = writer.startmultipartbody('mixed')
298             part = writer.nextpart()
299             part.addheader('Content-Transfer-Encoding', 'quoted-printable')
300             body = part.startbody('text/plain; charset=%s'%charset)
301             body.write(content_encoded)
302             for fileid in message_files:
303                 name = files.get(fileid, 'name')
304                 mime_type = files.get(fileid, 'type')
305                 content = files.get(fileid, 'content')
306                 part = writer.nextpart()
307                 if mime_type == 'text/plain':
308                     part.addheader('Content-Disposition',
309                         'attachment;\n filename="%s"'%name)
310                     part.addheader('Content-Transfer-Encoding', '7bit')
311                     body = part.startbody('text/plain')
312                     body.write(content)
313                 else:
314                     # some other type, so encode it
315                     if not mime_type:
316                         # this should have been done when the file was saved
317                         mime_type = mimetypes.guess_type(name)[0]
318                     if mime_type is None:
319                         mime_type = 'application/octet-stream'
320                     part.addheader('Content-Disposition',
321                         'attachment;\n filename="%s"'%name)
322                     part.addheader('Content-Transfer-Encoding', 'base64')
323                     body = part.startbody(mime_type)
324                     body.write(base64.encodestring(content))
325             writer.lastpart()
326         else:
327             writer.addheader('Content-Transfer-Encoding', 'quoted-printable')
328             body = writer.startbody('text/plain; charset=%s'%charset)
329             body.write(content_encoded)
331         mailer.smtp_send(sendto, message)
333     def email_signature(self, nodeid, msgid):
334         ''' Add a signature to the e-mail with some useful information
335         '''
336         # simplistic check to see if the url is valid,
337         # then append a trailing slash if it is missing
338         base = self.db.config.TRACKER_WEB 
339         if (not isinstance(base , type('')) or
340             not (base.startswith('http://') or base.startswith('https://'))):
341             base = "Configuration Error: TRACKER_WEB isn't a " \
342                 "fully-qualified URL"
343         elif base[-1] != '/' :
344             base += '/'
345         web = base + self.classname + nodeid
347         # ensure the email address is properly quoted
348         email = straddr((self.db.config.TRACKER_NAME,
349             self.db.config.TRACKER_EMAIL))
351         line = '_' * max(len(web)+2, len(email))
352         return '%s\n%s\n<%s>\n%s'%(line, email, web, line)
355     def generateCreateNote(self, nodeid):
356         """Generate a create note that lists initial property values
357         """
358         cn = self.classname
359         cl = self.db.classes[cn]
360         props = cl.getprops(protected=0)
362         # list the values
363         m = []
364         l = props.items()
365         l.sort()
366         for propname, prop in l:
367             value = cl.get(nodeid, propname, None)
368             # skip boring entries
369             if not value:
370                 continue
371             if isinstance(prop, hyperdb.Link):
372                 link = self.db.classes[prop.classname]
373                 if value:
374                     key = link.labelprop(default_to_id=1)
375                     if key:
376                         value = link.get(value, key)
377                 else:
378                     value = ''
379             elif isinstance(prop, hyperdb.Multilink):
380                 if value is None: value = []
381                 l = []
382                 link = self.db.classes[prop.classname]
383                 key = link.labelprop(default_to_id=1)
384                 if key:
385                     value = [link.get(entry, key) for entry in value]
386                 value.sort()
387                 value = ', '.join(value)
388             m.append('%s: %s'%(propname, value))
389         m.insert(0, '----------')
390         m.insert(0, '')
391         return '\n'.join(m)
393     def generateChangeNote(self, nodeid, oldvalues):
394         """Generate a change note that lists property changes
395         """
396         if __debug__ :
397             if not isinstance(oldvalues, type({})) :
398                 raise TypeError("'oldvalues' must be dict-like, not %s."%
399                     type(oldvalues))
401         cn = self.classname
402         cl = self.db.classes[cn]
403         changed = {}
404         props = cl.getprops(protected=0)
406         # determine what changed
407         for key in oldvalues.keys():
408             if key in ['files','messages']:
409                 continue
410             if key in ('actor', 'activity', 'creator', 'creation'):
411                 continue
412             # not all keys from oldvalues might be available in database
413             # this happens when property was deleted
414             try:                
415                 new_value = cl.get(nodeid, key)
416             except KeyError:
417                 continue
418             # the old value might be non existent
419             # this happens when property was added
420             try:
421                 old_value = oldvalues[key]
422                 if type(new_value) is type([]):
423                     new_value.sort()
424                     old_value.sort()
425                 if new_value != old_value:
426                     changed[key] = old_value
427             except:
428                 changed[key] = new_value
430         # list the changes
431         m = []
432         l = changed.items()
433         l.sort()
434         for propname, oldvalue in l:
435             prop = props[propname]
436             value = cl.get(nodeid, propname, None)
437             if isinstance(prop, hyperdb.Link):
438                 link = self.db.classes[prop.classname]
439                 key = link.labelprop(default_to_id=1)
440                 if key:
441                     if value:
442                         value = link.get(value, key)
443                     else:
444                         value = ''
445                     if oldvalue:
446                         oldvalue = link.get(oldvalue, key)
447                     else:
448                         oldvalue = ''
449                 change = '%s -> %s'%(oldvalue, value)
450             elif isinstance(prop, hyperdb.Multilink):
451                 change = ''
452                 if value is None: value = []
453                 if oldvalue is None: oldvalue = []
454                 l = []
455                 link = self.db.classes[prop.classname]
456                 key = link.labelprop(default_to_id=1)
457                 # check for additions
458                 for entry in value:
459                     if entry in oldvalue: continue
460                     if key:
461                         l.append(link.get(entry, key))
462                     else:
463                         l.append(entry)
464                 if l:
465                     l.sort()
466                     change = '+%s'%(', '.join(l))
467                     l = []
468                 # check for removals
469                 for entry in oldvalue:
470                     if entry in value: continue
471                     if key:
472                         l.append(link.get(entry, key))
473                     else:
474                         l.append(entry)
475                 if l:
476                     l.sort()
477                     change += ' -%s'%(', '.join(l))
478             else:
479                 change = '%s -> %s'%(oldvalue, value)
480             m.append('%s: %s'%(propname, change))
481         if m:
482             m.insert(0, '----------')
483             m.insert(0, '')
484         return '\n'.join(m)
486 # vim: set filetype=python ts=4 sw=4 et si