comparison roundup/mailgw.py @ 1905:dc43e339e607

Centralised conversion of user-input data to hyperdb values (bug [SF#802405], bug [SF#817217], rfe [SF#816994])
author Richard Jones <richard@users.sourceforge.net>
date Tue, 11 Nov 2003 00:35:14 +0000
parents 9d8d5fa0d9ad
children 20cfd25cffda
comparison
equal deleted inserted replaced
1904:9445caec3ff5 1905:dc43e339e607
71 set() method to add the message to the item's spool; in the second case we 71 set() method to add the message to the item's spool; in the second case we
72 are calling the create() method to create a new node). If an auditor raises 72 are calling the create() method to create a new node). If an auditor raises
73 an exception, the original message is bounced back to the sender with the 73 an exception, the original message is bounced back to the sender with the
74 explanatory message given in the exception. 74 explanatory message given in the exception.
75 75
76 $Id: mailgw.py,v 1.136 2003-11-03 18:34:03 jlgijsbers Exp $ 76 $Id: mailgw.py,v 1.137 2003-11-11 00:35:13 richard Exp $
77 """ 77 """
78 78
79 import string, re, os, mimetools, cStringIO, smtplib, socket, binascii, quopri 79 import string, re, os, mimetools, cStringIO, smtplib, socket, binascii, quopri
80 import time, random, sys 80 import time, random, sys
81 import traceback, MimeWriter, rfc822 81 import traceback, MimeWriter, rfc822
849 self.db.commit() 849 self.db.commit()
850 850
851 return nodeid 851 return nodeid
852 852
853 853
854 def setPropArrayFromString(self, cl, propString, nodeid = None): 854 def setPropArrayFromString(self, cl, propString, nodeid=None):
855 ''' takes string of form prop=value,value;prop2=value 855 ''' takes string of form prop=value,value;prop2=value
856 and returns (error, prop[..]) 856 and returns (error, prop[..])
857 ''' 857 '''
858 properties = cl.getprops()
859 props = {} 858 props = {}
860 errors = [] 859 errors = []
861 for prop in string.split(propString, ';'): 860 for prop in string.split(propString, ';'):
862 # extract the property name and value 861 # extract the property name and value
863 try: 862 try:
864 propname, value = prop.split('=') 863 propname, value = prop.split('=')
865 except ValueError, message: 864 except ValueError, message:
866 errors.append('not of form [arg=value,value,...;' 865 errors.append('not of form [arg=value,value,...;'
867 'arg=value,value,...]') 866 'arg=value,value,...]')
868 return (errors, props) 867 return (errors, props)
869 868 # convert the value to a hyperdb-usable value
870 # ensure it's a valid property name
871 propname = propname.strip() 869 propname = propname.strip()
872 try: 870 try:
873 proptype = properties[propname] 871 props[propname] = hyperdb.rawToHyperdb(self.db, cl, nodeid,
874 except KeyError: 872 propname, value)
875 errors.append('refers to an invalid property: "%s"'%propname) 873 except hyperdb.HyperdbValueError, message:
876 continue 874 errors.append(message)
877
878 # convert the string value to a real property value
879 if isinstance(proptype, hyperdb.String):
880 props[propname] = value.strip()
881 if isinstance(proptype, hyperdb.Password):
882 props[propname] = password.Password(value.strip())
883 elif isinstance(proptype, hyperdb.Date):
884 try:
885 props[propname] = date.Date(value.strip()).local(self.db.getUserTimezone())
886 except ValueError, message:
887 errors.append('contains an invalid date for %s.'%propname)
888 elif isinstance(proptype, hyperdb.Interval):
889 try:
890 props[propname] = date.Interval(value)
891 except ValueError, message:
892 errors.append('contains an invalid date interval for %s.'%
893 propname)
894 elif isinstance(proptype, hyperdb.Link):
895 linkcl = self.db.classes[proptype.classname]
896 propkey = linkcl.labelprop(default_to_id=1)
897 try:
898 props[propname] = linkcl.lookup(value)
899 except KeyError, message:
900 errors.append('"%s" is not a value for %s.'%(value, propname))
901 elif isinstance(proptype, hyperdb.Multilink):
902 # get the linked class
903 linkcl = self.db.classes[proptype.classname]
904 propkey = linkcl.labelprop(default_to_id=1)
905 if nodeid:
906 curvalue = cl.get(nodeid, propname)
907 else:
908 curvalue = []
909
910 # handle each add/remove in turn
911 # keep an extra list for all items that are
912 # definitely in the new list (in case of e.g.
913 # <propname>=A,+B, which should replace the old
914 # list with A,B)
915 set = 0
916 newvalue = []
917 for item in value.split(','):
918 item = item.strip()
919
920 # handle +/-
921 remove = 0
922 if item.startswith('-'):
923 remove = 1
924 item = item[1:]
925 elif item.startswith('+'):
926 item = item[1:]
927 else:
928 set = 1
929
930 # look up the value
931 try:
932 item = linkcl.lookup(item)
933 except KeyError, message:
934 errors.append('"%s" is not a value for %s.'%(item,
935 propname))
936 continue
937
938 # perform the add/remove
939 if remove:
940 try:
941 curvalue.remove(item)
942 except ValueError:
943 errors.append('"%s" is not currently in for %s.'%(item,
944 propname))
945 continue
946 else:
947 newvalue.append(item)
948 if item not in curvalue:
949 curvalue.append(item)
950
951 # that's it, set the new Multilink property value,
952 # or overwrite it completely
953 if set:
954 props[propname] = newvalue
955 else:
956 props[propname] = curvalue
957 elif isinstance(proptype, hyperdb.Boolean):
958 value = value.strip()
959 props[propname] = value.lower() in ('yes', 'true', 'on', '1')
960 elif isinstance(proptype, hyperdb.Number):
961 value = value.strip()
962 props[propname] = float(value)
963 return errors, props 875 return errors, props
964 876
965 877
966 def extractUserFromList(userClass, users): 878 def extractUserFromList(userClass, users):
967 '''Given a list of users, try to extract the first non-anonymous user 879 '''Given a list of users, try to extract the first non-anonymous user

Roundup Issue Tracker: http://roundup-tracker.org/