def file_exists(location, file_name, path=''): """this is untested and probably not going to be used """ try: LocationFiles.select(AND(LocationFiles.q.locationID==location, LocationFiles.q.attr_name==attr_name))[0] os.stat(path + attr_name) return True except: return False
def get_filepath(obj, prop, upload_dir, default=''): """obj should usually be a MetaWrapper """ prop_val = getattr(obj, prop) try: val = int(prop_val) except: return default return upload_dir + LocationFiles.get(val).attr_name
def locally_unique_file_name(file_name, location): """give a file_name test.png it should iterate through test-1.png, test-2.png etc until we reach a unique one. """ if LocationFiles.select(AND(LocationFiles.q.locationID==location, LocationFiles.q.attr_name==file_name)).count(): file_name = file_name.rsplit('.', 1) extra = file_name[-2].rsplit('-', 1) try: extra[-1] = str(int(extra[-1])+1) extra = '-'.join(extra) file_name[-2] = extra return locally_unique_file_name('.'.join(file_name), location) except ValueError: extra[-1] += '-1' file_name[-2] = '-'.join(extra) return locally_unique_file_name('.'.join(file_name), location) file_name = '.'.join(file_name) return file_name