Code

f286dbd2181ef9331098fe6c77d8cc6bd9f3329d
[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.101 2004-03-15 05:50:19 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.otks.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         del props['__time']
84         userid = cl.create(**props)
85         # clear the props from the otk database
86         self.otks.destroy(otk)
87         self.commit()
88         
89         return userid
92 class DetectorError(RuntimeError):
93     """ Raised by detectors that want to indicate that something's amiss
94     """
95     pass
97 # deviation from spec - was called IssueClass
98 class IssueClass:
99     """This class is intended to be mixed-in with a hyperdb backend
100     implementation. The backend should provide a mechanism that
101     enforces the title, messages, files, nosy and superseder
102     properties:
104     - title = hyperdb.String(indexme='yes')
105     - messages = hyperdb.Multilink("msg")
106     - files = hyperdb.Multilink("file")
107     - nosy = hyperdb.Multilink("user")
108     - superseder = hyperdb.Multilink(classname)
109     """
111     # New methods:
112     def addmessage(self, nodeid, summary, text):
113         """Add a message to an issue's mail spool.
115         A new "msg" node is constructed using the current date, the user that
116         owns the database connection as the author, and the specified summary
117         text.
119         The "files" and "recipients" fields are left empty.
121         The given text is saved as the body of the message and the node is
122         appended to the "messages" field of the specified issue.
123         """
125     # XXX "bcc" is an optional extra here...
126     def nosymessage(self, nodeid, msgid, oldvalues, whichnosy='nosy',
127             from_address=None, cc=[]): #, bcc=[]):
128         """Send a message to the members of an issue's nosy list.
130         The message is sent only to users on the nosy list who are not
131         already on the "recipients" list for the message.
132         
133         These users are then added to the message's "recipients" list.
135         If 'msgid' is None, the message gets sent only to the nosy
136         list, and it's called a 'System Message'.
137         """
138         authid = self.db.msg.safeget(msgid, 'author')
139         recipients = self.db.msg.safeget(msgid, 'recipients', [])
140         
141         sendto = []
142         seen_message = {}
143         for recipient in recipients:
144             seen_message[recipient] = 1
146         def add_recipient(userid):
147             # make sure they have an address
148             address = self.db.user.get(userid, 'address')
149             if address:
150                 sendto.append(address)
151                 recipients.append(userid)
153         def good_recipient(userid):
154             # Make sure we don't send mail to either the anonymous
155             # user or a user who has already seen the message.
156             return (userid and
157                     (self.db.user.get(userid, 'username') != 'anonymous') and
158                     not seen_message.has_key(userid))
160         # possibly send the message to the author, as long as they aren't
161         # anonymous
162         if (good_recipient(authid) and
163             (self.db.config.MESSAGES_TO_AUTHOR == 'yes' or
164              (self.db.config.MESSAGES_TO_AUTHOR == 'new' and not oldvalues))):
165             add_recipient(authid)
166         
167         if authid:
168             seen_message[authid] = 1
169         
170         # now deal with the nosy and cc people who weren't recipients.
171         for userid in cc + self.get(nodeid, whichnosy):
172             if good_recipient(userid):
173                 add_recipient(userid)        
175         if oldvalues:
176             note = self.generateChangeNote(nodeid, oldvalues)
177         else:
178             note = self.generateCreateNote(nodeid)
180         # If we have new recipients, update the message's recipients
181         # and send the mail.
182         if sendto:
183             if msgid:
184                 self.db.msg.set(msgid, recipients=recipients)
185             self.send_message(nodeid, msgid, note, sendto, from_address)
187     # backwards compatibility - don't remove
188     sendmessage = nosymessage
190     def send_message(self, nodeid, msgid, note, sendto, from_address=None):
191         '''Actually send the nominated message from this node to the sendto
192            recipients, with the note appended.
193         '''
194         users = self.db.user
195         messages = self.db.msg
196         files = self.db.file
197        
198         inreplyto = messages.safeget(msgid, 'inreplyto')
199         messageid = messages.safeget(msgid, 'messageid')
201         # make up a messageid if there isn't one (web edit)
202         if not messageid:
203             # this is an old message that didn't get a messageid, so
204             # create one
205             messageid = "<%s.%s.%s%s@%s>"%(time.time(), random.random(),
206                                            self.classname, nodeid,
207                                            self.db.config.MAIL_DOMAIN)
208             messages.set(msgid, messageid=messageid)
210         # compose title
211         cn = self.classname
212         title = self.get(nodeid, 'title') or '%s message copy'%cn
214         # figure author information
215         authid = messages.safeget(msgid, 'author')
216         authname = users.safeget(authid, 'realname')
217         if not authname:
218             authname = users.safeget(authid, 'username', '')
219         authaddr = users.safeget(authid, 'address', '')
220         if authaddr:
221             authaddr = " <%s>" % straddr( ('',authaddr) )
223         # make the message body
224         m = ['']
226         # put in roundup's signature
227         if self.db.config.EMAIL_SIGNATURE_POSITION == 'top':
228             m.append(self.email_signature(nodeid, msgid))
230         # add author information
231         if authid:
232             if len(self.get(nodeid,'messages')) == 1:
233                 m.append("New submission from %s%s:"%(authname, authaddr))
234             else:
235                 m.append("%s%s added the comment:"%(authname, authaddr))
236         else:
237             m.append("System message:")
238         m.append('')
240         # add the content
241         m.append(messages.safeget(msgid, 'content', ''))
243         # add the change note
244         if note:
245             m.append(note)
247         # put in roundup's signature
248         if self.db.config.EMAIL_SIGNATURE_POSITION == 'bottom':
249             m.append(self.email_signature(nodeid, msgid))
251         # encode the content as quoted-printable
252         charset = getattr(self.db.config, 'EMAIL_CHARSET', 'utf-8')
253         m = '\n'.join(m)
254         if charset != 'utf-8':
255             m = unicode(m, 'utf-8').encode(charset)
256         content = cStringIO.StringIO(m)
257         content_encoded = cStringIO.StringIO()
258         quopri.encode(content, content_encoded, 0)
259         content_encoded = content_encoded.getvalue()
261         # get the files for this message
262         message_files = msgid and messages.get(msgid, 'files') or None
264         # make sure the To line is always the same (for testing mostly)
265         sendto.sort()
267         # make sure we have a from address
268         if from_address is None:
269             from_address = self.db.config.TRACKER_EMAIL
271         # additional bit for after the From: "name"
272         from_tag = getattr(self.db.config, 'EMAIL_FROM_TAG', '')
273         if from_tag:
274             from_tag = ' ' + from_tag
276         subject = '[%s%s] %s'%(cn, nodeid, title)
277         author = (authname + from_tag, from_address)
279         # create the message
280         mailer = Mailer(self.db.config)
281         message, writer = mailer.get_standard_message(sendto, subject, author)
283         # set reply-to to the tracker
284         tracker_name = self.db.config.TRACKER_NAME
285         if charset != 'utf-8':
286             tracker = unicode(tracker_name, 'utf-8').encode(charset)
287         tracker_name = encode_header(tracker_name, charset)
288         writer.addheader('Reply-To', straddr((tracker_name, from_address)))
290         # message ids
291         if messageid:
292             writer.addheader('Message-Id', messageid)
293         if inreplyto:
294             writer.addheader('In-Reply-To', inreplyto)
296         # attach files
297         if message_files:
298             part = writer.startmultipartbody('mixed')
299             part = writer.nextpart()
300             part.addheader('Content-Transfer-Encoding', 'quoted-printable')
301             body = part.startbody('text/plain; charset=%s'%charset)
302             body.write(content_encoded)
303             for fileid in message_files:
304                 name = files.get(fileid, 'name')
305                 mime_type = files.get(fileid, 'type')
306                 content = files.get(fileid, 'content')
307                 part = writer.nextpart()
308                 if mime_type == 'text/plain':
309                     part.addheader('Content-Disposition',
310                         'attachment;\n filename="%s"'%name)
311                     part.addheader('Content-Transfer-Encoding', '7bit')
312                     body = part.startbody('text/plain')
313                     body.write(content)
314                 else:
315                     # some other type, so encode it
316                     if not mime_type:
317                         # this should have been done when the file was saved
318                         mime_type = mimetypes.guess_type(name)[0]
319                     if mime_type is None:
320                         mime_type = 'application/octet-stream'
321                     part.addheader('Content-Disposition',
322                         'attachment;\n filename="%s"'%name)
323                     part.addheader('Content-Transfer-Encoding', 'base64')
324                     body = part.startbody(mime_type)
325                     body.write(base64.encodestring(content))
326             writer.lastpart()
327         else:
328             writer.addheader('Content-Transfer-Encoding', 'quoted-printable')
329             body = writer.startbody('text/plain; charset=%s'%charset)
330             body.write(content_encoded)
332         mailer.smtp_send(sendto, message)
334     def email_signature(self, nodeid, msgid):
335         ''' Add a signature to the e-mail with some useful information
336         '''
337         # simplistic check to see if the url is valid,
338         # then append a trailing slash if it is missing
339         base = self.db.config.TRACKER_WEB 
340         if (not isinstance(base , type('')) or
341             not (base.startswith('http://') or base.startswith('https://'))):
342             base = "Configuration Error: TRACKER_WEB isn't a " \
343                 "fully-qualified URL"
344         elif base[-1] != '/' :
345             base += '/'
346         web = base + self.classname + nodeid
348         # ensure the email address is properly quoted
349         email = straddr((self.db.config.TRACKER_NAME,
350             self.db.config.TRACKER_EMAIL))
352         line = '_' * max(len(web)+2, len(email))
353         return '%s\n%s\n<%s>\n%s'%(line, email, web, line)
356     def generateCreateNote(self, nodeid):
357         """Generate a create note that lists initial property values
358         """
359         cn = self.classname
360         cl = self.db.classes[cn]
361         props = cl.getprops(protected=0)
363         # list the values
364         m = []
365         l = props.items()
366         l.sort()
367         for propname, prop in l:
368             value = cl.get(nodeid, propname, None)
369             # skip boring entries
370             if not value:
371                 continue
372             if isinstance(prop, hyperdb.Link):
373                 link = self.db.classes[prop.classname]
374                 if value:
375                     key = link.labelprop(default_to_id=1)
376                     if key:
377                         value = link.get(value, key)
378                 else:
379                     value = ''
380             elif isinstance(prop, hyperdb.Multilink):
381                 if value is None: value = []
382                 l = []
383                 link = self.db.classes[prop.classname]
384                 key = link.labelprop(default_to_id=1)
385                 if key:
386                     value = [link.get(entry, key) for entry in value]
387                 value.sort()
388                 value = ', '.join(value)
389             m.append('%s: %s'%(propname, value))
390         m.insert(0, '----------')
391         m.insert(0, '')
392         return '\n'.join(m)
394     def generateChangeNote(self, nodeid, oldvalues):
395         """Generate a change note that lists property changes
396         """
397         if __debug__ :
398             if not isinstance(oldvalues, type({})) :
399                 raise TypeError("'oldvalues' must be dict-like, not %s."%
400                     type(oldvalues))
402         cn = self.classname
403         cl = self.db.classes[cn]
404         changed = {}
405         props = cl.getprops(protected=0)
407         # determine what changed
408         for key in oldvalues.keys():
409             if key in ['files','messages']:
410                 continue
411             if key in ('actor', 'activity', 'creator', 'creation'):
412                 continue
413             # not all keys from oldvalues might be available in database
414             # this happens when property was deleted
415             try:                
416                 new_value = cl.get(nodeid, key)
417             except KeyError:
418                 continue
419             # the old value might be non existent
420             # this happens when property was added
421             try:
422                 old_value = oldvalues[key]
423                 if type(new_value) is type([]):
424                     new_value.sort()
425                     old_value.sort()
426                 if new_value != old_value:
427                     changed[key] = old_value
428             except:
429                 changed[key] = new_value
431         # list the changes
432         m = []
433         l = changed.items()
434         l.sort()
435         for propname, oldvalue in l:
436             prop = props[propname]
437             value = cl.get(nodeid, propname, None)
438             if isinstance(prop, hyperdb.Link):
439                 link = self.db.classes[prop.classname]
440                 key = link.labelprop(default_to_id=1)
441                 if key:
442                     if value:
443                         value = link.get(value, key)
444                     else:
445                         value = ''
446                     if oldvalue:
447                         oldvalue = link.get(oldvalue, key)
448                     else:
449                         oldvalue = ''
450                 change = '%s -> %s'%(oldvalue, value)
451             elif isinstance(prop, hyperdb.Multilink):
452                 change = ''
453                 if value is None: value = []
454                 if oldvalue is None: oldvalue = []
455                 l = []
456                 link = self.db.classes[prop.classname]
457                 key = link.labelprop(default_to_id=1)
458                 # check for additions
459                 for entry in value:
460                     if entry in oldvalue: continue
461                     if key:
462                         l.append(link.get(entry, key))
463                     else:
464                         l.append(entry)
465                 if l:
466                     l.sort()
467                     change = '+%s'%(', '.join(l))
468                     l = []
469                 # check for removals
470                 for entry in oldvalue:
471                     if entry in value: continue
472                     if key:
473                         l.append(link.get(entry, key))
474                     else:
475                         l.append(entry)
476                 if l:
477                     l.sort()
478                     change += ' -%s'%(', '.join(l))
479             else:
480                 change = '%s -> %s'%(oldvalue, value)
481             m.append('%s: %s'%(propname, change))
482         if m:
483             m.insert(0, '----------')
484             m.insert(0, '')
485         return '\n'.join(m)
487 # vim: set filetype=python ts=4 sw=4 et si