1- #$Id: actions.py,v 1.30 2004-05-28 00:56:50 richard Exp $
1+ #$Id: actions.py,v 1.31 2004-06-06 12:40:30 a1s Exp $
22
33import re , cgi , StringIO , urllib , Cookie , time , random
44
@@ -27,6 +27,7 @@ def __init__(self, client):
2727 self .userid = client .userid
2828 self .base = client .base
2929 self .user = client .user
30+ self .context = templating .context (client )
3031
3132 def execute (self ):
3233 """Execute the action specified by this object."""
@@ -49,14 +50,21 @@ def permission(self):
4950 if (self .permissionType and
5051 not self .hasPermission (self .permissionType )):
5152 info = {'action' : self .name , 'classname' : self .classname }
52- raise Unauthorised , _ ('You do not have permission to '
53+ raise Unauthorised , self . _ ('You do not have permission to '
5354 '%(action)s the %(classname)s class.' )% info
5455
5556 def hasPermission (self , permission ):
5657 """Check whether the user has 'permission' on the current class."""
5758 return self .db .security .hasPermission (permission , self .client .userid ,
5859 self .client .classname )
5960
61+ def gettext (self , msgid ):
62+ """Return the localized translation of msgid"""
63+ return templating .translationService .translate (domain = "roundup" ,
64+ msgid = msgid , context = self .context )
65+
66+ _ = gettext
67+
6068class ShowAction (Action ):
6169 def handle (self , typere = re .compile ('[@:]type' ),
6270 numre = re .compile ('[@:]number' )):
@@ -68,14 +76,14 @@ def handle(self, typere=re.compile('[@:]type'),
6876 elif numre .match (key ):
6977 n = self .form [key ].value .strip ()
7078 if not t :
71- raise ValueError , 'No type specified'
79+ raise ValueError , self . _ ( 'No type specified' )
7280 if not n :
73- raise SeriousError , _ ('No ID entered' )
81+ raise SeriousError , self . _ ('No ID entered' )
7482 try :
7583 int (n )
7684 except ValueError :
7785 d = {'input' : n , 'classname' : t }
78- raise SeriousError , _ (
86+ raise SeriousError , self . _ (
7987 '"%(input)s" is not an ID (%(classname)s ID required)' )% d
8088 url = '%s%s%s' % (self .base , t , n )
8189 raise Redirect , url
@@ -95,14 +103,15 @@ def handle(self):
95103 # make sure we don't try to retire admin or anonymous
96104 if self .classname == 'user' and \
97105 self .db .user .get (nodeid , 'username' ) in ('admin' , 'anonymous' ):
98- raise ValueError , _ ('You may not retire the admin or anonymous user' )
106+ raise ValueError , self ._ (
107+ 'You may not retire the admin or anonymous user' )
99108
100109 # do the retire
101110 self .db .getclass (self .classname ).retire (nodeid )
102111 self .db .commit ()
103112
104113 self .client .ok_message .append (
105- _ ('%(classname)s %(itemid)s has been retired' )% {
114+ self . _ ('%(classname)s %(itemid)s has been retired' )% {
106115 'classname' : self .classname .capitalize (), 'itemid' : nodeid })
107116
108117class SearchAction (Action ):
@@ -224,7 +233,7 @@ def handle(self):
224233 """
225234 # get the CSV module
226235 if rcsv .error :
227- self .client .error_message .append (_ (rcsv .error ))
236+ self .client .error_message .append (self . _ (rcsv .error ))
228237 return
229238
230239 cl = self .db .classes [self .classname ]
@@ -257,7 +266,7 @@ def handle(self):
257266 # confirm correct weight
258267 if len (idlessprops ) != len (values ):
259268 self .client .error_message .append (
260- _ ('Not enough values on line %(line)s' )% {'line' :line })
269+ self . _ ('Not enough values on line %(line)s' )% {'line' :line })
261270 return
262271
263272 # extract the new values
@@ -304,7 +313,7 @@ def handle(self):
304313 # all OK
305314 self .db .commit ()
306315
307- self .client .ok_message .append (_ ('Items edited OK' ))
316+ self .client .ok_message .append (self . _ ('Items edited OK' ))
308317
309318class _EditAction (Action ):
310319 def isEditingSelf (self ):
@@ -322,7 +331,8 @@ def editItemPermission(self, props):
322331 """
323332 if self .classname == 'user' :
324333 if props .has_key ('roles' ) and not self .hasPermission ('Web Roles' ):
325- raise Unauthorised , _ ("You do not have permission to edit user roles" )
334+ raise Unauthorised , self ._ (
335+ "You do not have permission to edit user roles" )
326336 if self .isEditingSelf ():
327337 return 1
328338 if self .hasPermission ('Edit' ):
@@ -501,7 +511,8 @@ def handle(self):
501511 message = self ._editnodes (props , links )
502512 except (ValueError , KeyError , IndexError , exceptions .Reject ), message :
503513 import traceback ;traceback .print_exc ()
504- self .client .error_message .append (_ ('Edit Error: ' ) + str (message ))
514+ self .client .error_message .append (
515+ self ._ ('Edit Error: %s' ) % str (message ))
505516 return
506517
507518 # commit now that all the tricky stuff is done
@@ -532,7 +543,8 @@ def handle(self):
532543 try :
533544 props , links = self .client .parsePropsFromForm (create = 1 )
534545 except (ValueError , KeyError ), message :
535- self .client .error_message .append (_ ('Error: ' ) + str (message ))
546+ self .client .error_message .append (self ._ ('Error: %s' )
547+ % str (message ))
536548 return
537549
538550 # handle the props - edit or create
@@ -541,7 +553,7 @@ def handle(self):
541553 messages = self ._editnodes (props , links )
542554 except (ValueError , KeyError , IndexError , exceptions .Reject ), message :
543555 # these errors might just be indicative of user dumbness
544- self .client .error_message .append (_ ('Error: ' ) + str (message ))
556+ self .client .error_message .append (_ ('Error: %s ' ) % str (message ))
545557 return
546558
547559 # commit now that all the tricky stuff is done
@@ -680,7 +692,7 @@ def handle(self):
680692 self .client .set_cookie (self .user )
681693
682694 # nice message
683- message = _ ('You are now registered, welcome!' )
695+ message = self . _ ('You are now registered, welcome!' )
684696 url = '%suser%s?@ok_message=%s' % (self .base , self .userid ,
685697 urllib .quote (message ))
686698
@@ -706,13 +718,13 @@ def handle(self):
706718
707719 # registration isn't allowed to supply roles
708720 if props .has_key ('roles' ):
709- raise Unauthorised , _ ( "It is not permitted to supply roles "
710- "at registration." )
721+ raise Unauthorised , self . _ (
722+ "It is not permitted to supply roles at registration." )
711723
712724 username = props ['username' ]
713725 try :
714726 self .db .user .lookup (username )
715- self .client .error_message .append (_ ('Error: A user with the '
727+ self .client .error_message .append (self . _ ('Error: A user with the '
716728 'username "%(username)s" already exists' )% props )
717729 return
718730 except KeyError :
@@ -775,7 +787,7 @@ def handle(self):
775787 now , self .client .cookie_path )
776788
777789 # Let the user know what's going on
778- self .client .ok_message .append (_ ('You are logged out' ))
790+ self .client .ok_message .append (self . _ ('You are logged out' ))
779791
780792class LoginAction (Action ):
781793 def handle (self ):
@@ -786,7 +798,7 @@ def handle(self):
786798 """
787799 # we need the username at a minimum
788800 if not self .form .has_key ('__login_name' ):
789- self .client .error_message .append (_ ('Username required' ))
801+ self .client .error_message .append (self . _ ('Username required' ))
790802 return
791803
792804 # get the login info
@@ -801,21 +813,23 @@ def handle(self):
801813 self .client .userid = self .db .user .lookup (self .client .user )
802814 except KeyError :
803815 name = self .client .user
804- self .client .error_message .append (_ ('No such user "%(name)s"' )% locals ())
816+ self .client .error_message .append (self ._ ('No such user "%(name)s"' )
817+ % locals ())
805818 self .client .make_user_anonymous ()
806819 return
807820
808821 # verify the password
809822 if not self .verifyPassword (self .client .userid , password ):
810823 self .client .make_user_anonymous ()
811- self .client .error_message .append (_ ('Incorrect password' ))
824+ self .client .error_message .append (self . _ ('Incorrect password' ))
812825 return
813826
814827 # Determine whether the user has permission to log in.
815828 # Base behaviour is to check the user has "Web Access".
816829 if not self .hasPermission ("Web Access" ):
817830 self .client .make_user_anonymous ()
818- self .client .error_message .append (_ ("You do not have permission to login" ))
831+ self .client .error_message .append (
832+ self ._ ("You do not have permission to login" ))
819833 return
820834
821835 # now we're OK, re-open the database for real, using the user
0 commit comments