@@ -909,10 +909,11 @@ def compress(self, data_list):
909909 raise NotImplementedError ('Subclasses must implement this method.' )
910910
911911class FilePathField (ChoiceField ):
912- def __init__ (self , path , match = None , recursive = False , required = True ,
913- widget = None , label = None , initial = None , help_text = None ,
914- * args , ** kwargs ):
912+ def __init__ (self , path , match = None , recursive = False , allow_files = True ,
913+ allow_folders = False , required = True , widget = None , label = None ,
914+ initial = None , help_text = None , * args , ** kwargs ):
915915 self .path , self .match , self .recursive = path , match , recursive
916+ self .allow_files , self .allow_folders = allow_files , allow_folders
916917 super (FilePathField , self ).__init__ (choices = (), required = required ,
917918 widget = widget , label = label , initial = initial , help_text = help_text ,
918919 * args , ** kwargs )
@@ -927,15 +928,23 @@ def __init__(self, path, match=None, recursive=False, required=True,
927928
928929 if recursive :
929930 for root , dirs , files in sorted (os .walk (self .path )):
930- for f in files :
931- if self .match is None or self .match_re .search (f ):
932- f = os .path .join (root , f )
933- self .choices .append ((f , f .replace (path , "" , 1 )))
931+ if self .allow_files :
932+ for f in files :
933+ if self .match is None or self .match_re .search (f ):
934+ f = os .path .join (root , f )
935+ self .choices .append ((f , f .replace (path , "" , 1 )))
936+ if self .allow_folders :
937+ for f in dirs :
938+ if self .match is None or self .match_re .search (f ):
939+ f = os .path .join (root , f )
940+ self .choices .append ((f , f .replace (path , "" , 1 )))
934941 else :
935942 try :
936943 for f in sorted (os .listdir (self .path )):
937944 full_file = os .path .join (self .path , f )
938- if os .path .isfile (full_file ) and (self .match is None or self .match_re .search (f )):
945+ if (((self .allow_files and os .path .isfile (full_file )) or
946+ (self .allow_folders and os .path .isdir (full_file ))) and
947+ (self .match is None or self .match_re .search (f ))):
939948 self .choices .append ((full_file , f ))
940949 except OSError :
941950 pass
0 commit comments