Hi Francois,
if I understand correctly you want to avoid duplicated file names.
In this case unique=True is useless because web2py renames the file with a
random UUID, and the renamed filename is stored in the field.
But in the following example I created a validator that checks the
uploading filename against previous ones.
Note that it uses internal undocumented functions to achieve its purpose so
it *might* not be backward compatible.
class UPLOAD_FILENAME_IS_NOT_IN_SET(IS_IN_SET):
def __init__(self, *a, **b):
IS_IN_SET.__init__(self, *a, **b)
def __call__(self, value):
value, error = IS_IN_SET.__call__(self, value.filename.replace('
','_') )
if error == None:
return (value, self.error_message)
else:
return (value, None)
db.define_table('xlup',
Field('f_xlsfile', type='upload', notnull=True,
requires=[
IS_UPLOAD_FILENAME(extension='^xlsx?$',
error_message='Please post .xls or .xlsx Excel files
only.'),
IS_LENGTH(1048576*2, 1024,
error_message='File size must be between 1kB & 2048kB'),
],
# unique=True,
label=T('Excel file'))
)
previous_files=[
db.xlup.f_xlsfile.retrieve_file_properties(r.f_xlsfile)['filename'] for r
in db().select(db.xlup.f_xlsfile) ]
db.xlup.f_xlsfile.requires.append(UPLOAD_FILENAME_IS_NOT_IN_SET(previous_files,
error_message='file already exists'))
Denes
--