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.106 2004-04-05 06:13:42 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 = getattr(self.config, 'DEFAULT_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
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()
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.
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', [])
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)
166 if authid:
167 seen_message[authid] = 1
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
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 web = "Configuration Error: TRACKER_WEB isn't a " \
342 "fully-qualified URL"
343 else:
344 if not base.endswith('/'):
345 base = 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 '\n%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