django_any/models.py
def any_filepath_field(field, **kwargs):
    """
    Lookup for nearest existing file

    """
    def get_some_file(path):
        subdirs, files = [], []
        for entry in os.listdir(path):
            entry_path = os.path.join(path, entry)
            if os.path.isdir(entry_path):
                subdirs.append(entry_path)
            else:
                if not field.match or re.match(field.match,entry):
                    files.append(entry_path)

        if files:
            return random.choice(files)
        
        if field.recursive:
            for subdir in subdirs:
                result = get_some_file(subdir)
                if result:
                    return result

    result = get_some_file(field.path)
    if result is None and not field.null:
        raise TypeError("Can't found file in %s for non nullable FilePathField" % field.path)
    return result
def any_filepath_field(field, **kwargs):
    """
    Lookup for nearest existing file

    """
    def get_some_file(path):
        subdirs, files = [], []
        for entry in os.listdir(path):
            entry_path = os.path.join(path, entry)
            if os.path.isdir(entry_path):
                subdirs.append(entry_path)
            else:
                if not field.match or re.match(field.match,entry):
                    files.append(entry_path)

        if files:
            return random.choice(files)
        
        if field.recursive:
            for subdir in subdirs:
                result = get_some_file(subdir)
                if result:
                    return result

    result = get_some_file(field.path)
    if result is None and not field.null:
        raise TypeError("Can't found file in %s for non nullable FilePathField" % field.path)
    return result
def any_filepath_field ( field , * * kwargs ) : def get_some_file ( path ) : subdirs , files = [ ] , [ ] for entry in os . listdir ( path ) : entry_path = os . path . join ( path , entry ) if os . path . isdir ( entry_path ) : subdirs . append ( entry_path ) else : if not field . match or re . match ( field . match , entry ) : files . append ( entry_path ) if files : return random . choice ( files ) if field . recursive : for subdir in subdirs : result = get_some_file ( subdir ) if result : return result result = get_some_file ( field . path ) if result is None and not field . null : raise TypeError ( "Can't found file in %s for non nullable FilePathField" % field . path ) return result
Lookup for nearest existing file
Lookup for nearest existing file