Skip to content

Commit 990a34b

Browse files
author
Johannes Gijsbers
committed
Actually use FormError, so we can move the handling up to inner_main().
Implement newItemAction using editItemAction, removing duplication.
1 parent e8566e0 commit 990a34b

File tree

1 file changed

+25
-60
lines changed

1 file changed

+25
-60
lines changed

roundup/cgi/client.py

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: client.py,v 1.139 2003-09-10 13:04:05 jlgijsbers Exp $
1+
# $Id: client.py,v 1.140 2003-09-24 14:53:58 jlgijsbers Exp $
22

33
__doc__ = """
44
WWW request handler (also used in the stand-alone server).
@@ -31,7 +31,6 @@ class NotModified(HTTPException):
3131
# used by a couple of routines
3232
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
3333

34-
# XXX actually _use_ FormError
3534
class FormError(ValueError):
3635
''' An "expected" exception occurred during form parsing.
3736
- ie. something we know can go wrong, and don't want to alarm the
@@ -280,6 +279,9 @@ def inner_main(self):
280279
except NotFound:
281280
# pass through
282281
raise
282+
except FormError, e:
283+
self.error_message.append(_('Form Error: ') + str(e))
284+
self.write(self.renderContext())
283285
except:
284286
# everything else
285287
self.write(cgitb.html())
@@ -726,12 +728,7 @@ def registerAction(self):
726728
727729
return 1 on successful login
728730
'''
729-
# parse the props from the form
730-
try:
731-
props = self.parsePropsFromForm()[0][('user', None)]
732-
except (ValueError, KeyError), message:
733-
self.error_message.append(_('Error: ') + str(message))
734-
return
731+
props = self.parsePropsFromForm()[0][('user', None)]
735732

736733
# make sure we're allowed to register
737734
if not self.registerPermission(props):
@@ -934,12 +931,7 @@ def editItemAction(self):
934931
935932
See parsePropsFromForm and _editnodes for special variables
936933
'''
937-
# parse the props from the form
938-
try:
939-
props, links = self.parsePropsFromForm()
940-
except (ValueError, KeyError), message:
941-
self.error_message.append(_('Parse Error: ') + str(message))
942-
return
934+
props, links = self.parsePropsFromForm()
943935

944936
# handle the props
945937
try:
@@ -956,6 +948,8 @@ def editItemAction(self):
956948
self.classname, self.nodeid, urllib.quote(message),
957949
urllib.quote(self.template))
958950

951+
newItemAction = editItemAction
952+
959953
def editItemPermission(self, props):
960954
''' Determine whether the user has permission to edit this item.
961955
@@ -980,37 +974,6 @@ def editItemPermission(self, props):
980974
return 1
981975
return 0
982976

983-
def newItemAction(self):
984-
''' Add a new item to the database.
985-
986-
This follows the same form as the editItemAction, with the same
987-
special form values.
988-
'''
989-
# parse the props from the form
990-
try:
991-
props, links = self.parsePropsFromForm()
992-
except (ValueError, KeyError), message:
993-
self.error_message.append(_('Error: ') + str(message))
994-
return
995-
996-
# handle the props - edit or create
997-
try:
998-
# when it hits the None element, it'll set self.nodeid
999-
messages = self._editnodes(props, links)
1000-
1001-
except (ValueError, KeyError, IndexError), message:
1002-
# these errors might just be indicative of user dumbness
1003-
self.error_message.append(_('Error: ') + str(message))
1004-
return
1005-
1006-
# commit now that all the tricky stuff is done
1007-
self.db.commit()
1008-
1009-
# redirect to the new item's page
1010-
raise Redirect, '%s%s%s?@ok_message=%s&@template=%s'%(self.base,
1011-
self.classname, self.nodeid, urllib.quote(messages),
1012-
urllib.quote(self.template))
1013-
1014977
def newItemPermission(self, props):
1015978
''' Determine whether the user has permission to create (edit) this
1016979
item.
@@ -1632,14 +1595,14 @@ def parsePropsFromForm(self, num_re=re.compile('^\d+$')):
16321595
for entry in extractFormList(form[key]):
16331596
m = self.FV_DESIGNATOR.match(entry)
16341597
if not m:
1635-
raise ValueError, \
1598+
raise FormError, \
16361599
'link "%s" value "%s" not a designator'%(key, entry)
16371600
value.append((m.group(1), m.group(2)))
16381601

16391602
# make sure the link property is valid
16401603
if (not isinstance(propdef[propname], hyperdb.Multilink) and
16411604
not isinstance(propdef[propname], hyperdb.Link)):
1642-
raise ValueError, '%s %s is not a link or '\
1605+
raise FormError, '%s %s is not a link or '\
16431606
'multilink property'%(cn, propname)
16441607

16451608
all_links.append((cn, nodeid, propname, value))
@@ -1660,7 +1623,7 @@ def parsePropsFromForm(self, num_re=re.compile('^\d+$')):
16601623
# does the property exist?
16611624
if not propdef.has_key(propname):
16621625
if mlaction != 'set':
1663-
raise ValueError, 'You have submitted a %s action for'\
1626+
raise FormError, 'You have submitted a %s action for'\
16641627
' the property "%s" which doesn\'t exist'%(mlaction,
16651628
propname)
16661629
# the form element is probably just something we don't care
@@ -1678,7 +1641,7 @@ def parsePropsFromForm(self, num_re=re.compile('^\d+$')):
16781641
else:
16791642
# multiple values are not OK
16801643
if isinstance(value, type([])):
1681-
raise ValueError, 'You have submitted more than one value'\
1644+
raise FormError, 'You have submitted more than one value'\
16821645
' for the %s property'%propname
16831646
# value might be a file upload...
16841647
if not hasattr(value, 'filename') or value.filename is None:
@@ -1701,13 +1664,13 @@ def parsePropsFromForm(self, num_re=re.compile('^\d+$')):
17011664
confirm = form[key]
17021665
break
17031666
else:
1704-
raise ValueError, 'Password and confirmation text do '\
1667+
raise FormError, 'Password and confirmation text do '\
17051668
'not match'
17061669
if isinstance(confirm, type([])):
1707-
raise ValueError, 'You have submitted more than one value'\
1670+
raise FormError, 'You have submitted more than one value'\
17081671
' for the %s property'%propname
17091672
if value != confirm.value:
1710-
raise ValueError, 'Password and confirmation text do '\
1673+
raise FormError, 'Password and confirmation text do '\
17111674
'not match'
17121675
value = password.Password(value)
17131676

@@ -1725,12 +1688,12 @@ def parsePropsFromForm(self, num_re=re.compile('^\d+$')):
17251688
try:
17261689
value = db.classes[link].lookup(value)
17271690
except KeyError:
1728-
raise ValueError, _('property "%(propname)s": '
1691+
raise FormError, _('property "%(propname)s": '
17291692
'%(value)s not a %(classname)s')%{
17301693
'propname': propname, 'value': value,
17311694
'classname': link}
17321695
except TypeError, message:
1733-
raise ValueError, _('you may only enter ID values '
1696+
raise FormError, _('you may only enter ID values '
17341697
'for property "%(propname)s": %(message)s')%{
17351698
'propname': propname, 'message': message}
17361699
elif isinstance(proptype, hyperdb.Multilink):
@@ -1744,12 +1707,12 @@ def parsePropsFromForm(self, num_re=re.compile('^\d+$')):
17441707
try:
17451708
entry = link_cl.lookup(entry)
17461709
except KeyError:
1747-
raise ValueError, _('property "%(propname)s": '
1710+
raise FormError, _('property "%(propname)s": '
17481711
'"%(value)s" not an entry of %(classname)s')%{
17491712
'propname': propname, 'value': entry,
17501713
'classname': link}
17511714
except TypeError, message:
1752-
raise ValueError, _('you may only enter ID values '
1715+
raise FormError, _('you may only enter ID values '
17531716
'for property "%(propname)s": %(message)s')%{
17541717
'propname': propname, 'message': message}
17551718
l.append(entry)
@@ -1775,7 +1738,7 @@ def parsePropsFromForm(self, num_re=re.compile('^\d+$')):
17751738
try:
17761739
existing.remove(entry)
17771740
except ValueError:
1778-
raise ValueError, _('property "%(propname)s": '
1741+
raise FormError, _('property "%(propname)s": '
17791742
'"%(value)s" not currently in list')%{
17801743
'propname': propname, 'value': entry}
17811744
else:
@@ -1826,7 +1789,7 @@ def parsePropsFromForm(self, num_re=re.compile('^\d+$')):
18261789
elif isinstance(proptype, hyperdb.Number):
18271790
value = float(value)
18281791
except ValueError, msg:
1829-
raise ValueError, _('Error with %s property: %s')%(
1792+
raise FormError, _('Error with %s property: %s')%(
18301793
propname, msg)
18311794

18321795
# register that we got this property
@@ -1842,6 +1805,8 @@ def parsePropsFromForm(self, num_re=re.compile('^\d+$')):
18421805
# no existing value
18431806
if not propdef.has_key(propname):
18441807
raise
1808+
except IndexError, message:
1809+
raise FormError(str(message))
18451810

18461811
# make sure the existing multilink is sorted
18471812
if isinstance(proptype, hyperdb.Multilink):
@@ -1898,7 +1863,7 @@ def parsePropsFromForm(self, num_re=re.compile('^\d+$')):
18981863
s.append('Required %s %s %s not supplied'%(thing[0], p,
18991864
', '.join(required)))
19001865
if s:
1901-
raise ValueError, '\n'.join(s)
1866+
raise FormError, '\n'.join(s)
19021867

19031868
# When creating a FileClass node, it should have a non-empty content
19041869
# property to be created. When editing a FileClass node, it should
@@ -1910,7 +1875,7 @@ def parsePropsFromForm(self, num_re=re.compile('^\d+$')):
19101875
if not props.get('content', ''):
19111876
del all_props[(cn, id)]
19121877
elif props.has_key('content') and not props['content']:
1913-
raise ValueError, _('File is empty')
1878+
raise FormError, _('File is empty')
19141879
return all_props, all_links
19151880

19161881
def fixNewlines(text):

0 commit comments

Comments
 (0)