Skip to content

Commit 44de4fd

Browse files
author
Richard Jones
committed
add and use Reject exception [SF#700265]
1 parent 5eb9f8b commit 44de4fd

File tree

6 files changed

+97
-22
lines changed

6 files changed

+97
-22
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ Feature:
77
- added new auditor, emailauditor.py, that works around a bug in IE. See
88
emailauditor.py for more info.
99
- added dispatcher functionality - see upgrading.txt for more info
10+
- added Reject exception which may be raised by auditors. This is trapped
11+
by mailgw and may be used to veto creation of file attachments or
12+
messages. (sf bug 700265)
1013

1114
Fixed:
1215
- Boolean HTML templating was broken
1316
- Link HTML templating field() was broken
1417
- Fix reporting of test inclusion in postgresql test
1518

19+
1620
2004-03-24 0.7.0b1
1721
Major new features:
1822
- added postgresql backend (originally from sf patch 761740, many changes

doc/customizing.txt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Customising Roundup
33
===================
44

5-
:Version: $Revision: 1.122 $
5+
:Version: $Revision: 1.123 $
66

77
.. This document borrows from the ZopeBook section on ZPT. The original is at:
88
http://www.zope.org/Documentation/Books/ZopeBook/current/ZPT.stx
@@ -586,6 +586,37 @@ to use one, copy it to the ``'detectors'`` of your tracker instance:
586586
db.issue.react('create', newissuecopy)
587587

588588

589+
Auditor or Reactor?
590+
-------------------
591+
592+
Generally speaking, the following rules should be observed:
593+
594+
**Auditors**
595+
Are used for `vetoing creation of or changes to items`_. They might
596+
also make automatic changes to item properties.
597+
**Reactors**
598+
Detect changes in the database and react accordingly. They should avoid
599+
making changes to the database where possible, as this could create
600+
detector loops.
601+
602+
Vetoing creation of or changes to items
603+
---------------------------------------
604+
605+
Auditors may raise the ``Reject`` exception to prevent the creation of
606+
or changes to items in the database. The mail gateway, for example, will
607+
not attach files or messages to issues when the creation of those files or
608+
messages are prevented through the ``Reject`` exception. It'll also not create
609+
users if that creation is ``Reject``'ed too.
610+
611+
To use, simply add at the top of your auditor::
612+
613+
from roundup.exceptions import Reject
614+
615+
And then when your rejection criteria have been detected, simply::
616+
617+
raise Reject
618+
619+
589620
Database Content
590621
================
591622

roundup/cgi/actions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#$Id: actions.py,v 1.15 2004-03-26 00:44:11 richard Exp $
2+
13
import re, cgi, StringIO, urllib, Cookie, time, random
24

35
from roundup import hyperdb, token, date, password, rcsv
@@ -837,3 +839,4 @@ def handle(self):
837839

838840
return '\n'
839841

842+
# vim: set filetype=python ts=4 sw=4 et si

roundup/cgi/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#$Id: exceptions.py,v 1.4 2004-03-26 00:44:11 richard Exp $
2+
'''Exceptions for use in Roundup's web interface.
3+
'''
4+
5+
__docformat__ = 'restructuredtext'
6+
17
import cgi
28

39
class HTTPException(Exception):
@@ -51,3 +57,4 @@ def __str__(self):
5157
</body></html>
5258
'''%cgi.escape(self.args[0])
5359

60+
# vim: set filetype=python ts=4 sw=4 et si

roundup/exceptions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#$Id: exceptions.py,v 1.1 2004-03-26 00:44:11 richard Exp $
2+
'''Exceptions for use across all Roundup components.
3+
'''
4+
5+
__docformat__ = 'restructuredtext'
6+
7+
class Reject(Exception):
8+
'''An auditor may raise this exception when the current create or set
9+
operation should be stopped.
10+
11+
It is up to the specific interface invoking the create or set to
12+
handle this exception sanely. For example:
13+
14+
- mailgw will trap and ignore Reject for file attachments and messages
15+
- cgi will trap and present the exception in a nice format
16+
'''
17+
pass
18+
19+
# vim: set filetype=python ts=4 sw=4 et si

roundup/mailgw.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ class node. Any parts of other types are each stored in separate files
7272
an exception, the original message is bounced back to the sender with the
7373
explanatory message given in the exception.
7474
75-
$Id: mailgw.py,v 1.145 2004-03-25 22:52:12 richard Exp $
75+
$Id: mailgw.py,v 1.146 2004-03-26 00:44:11 richard Exp $
7676
"""
7777
__docformat__ = 'restructuredtext'
7878

7979
import string, re, os, mimetools, cStringIO, smtplib, socket, binascii, quopri
8080
import time, random, sys
8181
import traceback, MimeWriter, rfc822
8282

83-
from roundup import hyperdb, date, password, rfc2822
83+
from roundup import hyperdb, date, password, rfc2822, exceptions
8484
from roundup.mailer import Mailer
8585

8686
SENDMAILDEBUG = os.environ.get('SENDMAILDEBUG', '')
@@ -805,8 +805,13 @@ def handle_message(self, message):
805805
for (name, mime_type, data) in attachments:
806806
if not name:
807807
name = "unnamed"
808-
files.append(self.db.file.create(type=mime_type, name=name,
809-
content=data, **file_props))
808+
try:
809+
fileid = self.db.file.create(type=mime_type, name=name,
810+
content=data, **file_props)
811+
except exceptions.Reject:
812+
pass
813+
else:
814+
files.append(fileid)
810815
# attach the files to the issue
811816
if nodeid:
812817
# extend the existing files list
@@ -821,20 +826,23 @@ def handle_message(self, message):
821826
# create the message if there's a message body (content)
822827
#
823828
if (content and properties.has_key('messages')):
824-
message_id = self.db.msg.create(author=author,
825-
recipients=recipients, date=date.Date('.'), summary=summary,
826-
content=content, files=files, messageid=messageid,
827-
inreplyto=inreplyto, **msg_props)
828-
829-
# attach the message to the node
830-
if nodeid:
831-
# add the message to the node's list
832-
messages = cl.get(nodeid, 'messages')
833-
messages.append(message_id)
834-
props['messages'] = messages
829+
try:
830+
message_id = self.db.msg.create(author=author,
831+
recipients=recipients, date=date.Date('.'),
832+
summary=summary, content=content, files=files,
833+
messageid=messageid, inreplyto=inreplyto, **msg_props)
834+
except exceptions.Reject:
835+
pass
835836
else:
836-
# pre-load the messages list
837-
props['messages'] = [message_id]
837+
# attach the message to the node
838+
if nodeid:
839+
# add the message to the node's list
840+
messages = cl.get(nodeid, 'messages')
841+
messages.append(message_id)
842+
props['messages'] = messages
843+
else:
844+
# pre-load the messages list
845+
props['messages'] = [message_id]
838846

839847
#
840848
# perform the node change / create
@@ -948,10 +956,13 @@ def uidFromAddress(db, address, create=1, **user_props):
948956
trying = username + str(n)
949957

950958
# create!
951-
return db.user.create(username=trying, address=address,
952-
realname=realname, roles=db.config.NEW_EMAIL_USER_ROLES,
953-
password=password.Password(password.generatePassword()),
954-
**user_props)
959+
try:
960+
return db.user.create(username=trying, address=address,
961+
realname=realname, roles=db.config.NEW_EMAIL_USER_ROLES,
962+
password=password.Password(password.generatePassword()),
963+
**user_props)
964+
except exceptions.Reject:
965+
return 0
955966
else:
956967
return 0
957968

0 commit comments

Comments
 (0)