I am trying to loop through my hard drive using python and storing the files in the database respectfully. My dynamic upload_to is working when uploading a file but when I try to save the file from the algorithm I wrote to loop through the hard drive, it does not work(upload_to). Any help please?
I will add the code below:
models.py
def upload_path(instance, filename):
return os.path.join('file', instance.folder, filename)
class File(models.Model):
#Defintion to set upload_to path
file_title = models.CharField(max_length=255)
file_type = models.CharField(max_length=255)
folder = models.CharField(max_length=255)
document = models.FileField(upload_to=upload_path)
uploaded_at = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.file_title
def delete(self):
delete_path = os.path.join('file', self.document.path)
os.remove(delete_path)
return super(File,self).delete()
views.py (snippet) - algorithm which loops through hard drive and saves to database
for sfile in os.listdir(secondary_directory):
if os.path.isfile(secondary_directory + sfile):
#Extract File Information
filename, filetype = os.path.splitext(sfile)
folder = cfile
if not exists(filename, 'file'):
#Work with file info
x = File()
x.file_title = filename
x.file_type = filetype
x.folder = folder
x.document = sfile
x.save()
Loop Algorithm:
def SyncDriveToDatabase(request):
HttpResponse(request, 'brick/loading.html')
#############################
#File Folder
#############################
primary_directory = os.path.join(root, 'file/')
if not os.path.exists(primary_directory):
os.makedirs(primary_directory)
for cfile in os.listdir(primary_directory):
if os.path.isfile(primary_directory + cfile):
#Extract File Information
filename, filetype = os.path.splitext(cfile)
folder = 'Main'
if not exists(filename, 'file'):
#Work with file info
x = File()
x.file_title = filename
x.file_type = filetype
x.folder = folder
x.document = cfile
x.save()
elif os.path.isdir(primary_directory + cfile):
secondary_directory = os.path.join(primary_directory, cfile) + '/'
for sfile in os.listdir(secondary_directory):
if os.path.isfile(secondary_directory + sfile):
#Extract File Information
filename, filetype = os.path.splitext(sfile)
folder = cfile
if not exists(filename, 'file'):
#Work with file info
x = File()
x.file_title = filename
x.file_type = filetype
x.folder = folder
x.document = sfile
x.save()
##################################
#Audio Folder
##################################
primary_directory = os.path.join(root, 'audio/')
if not os.path.exists(primary_directory):
os.makedirs(primary_directory)
#Loop through primary_directory
for cfile in os.listdir(primary_directory):
if os.path.isfile(primary_directory + cfile):
#Extract File information
filename, filetype = os.path.splitext(cfile)
album = 'Unkown'
artist = 'Unkown'
if not exists(filename, 'audio'):
a = Audio()
a.audio_title = filename
a.audio_type = filetype
a.album = album
a.artist = artist
a.document = cfile
a.save()
elif os.path.isdir(primary_directory + cfile):
artist_directory = os.path.join(primary_directory, cfile) + '/'
for sfile in os.listdir(artist_directory):
if os.path.isfile(sfile):
#Extract File information
filename, filetype = os.path.splitext(sfile)
album = 'Unkown'
artist = cfile
if not exists(filename, 'audio'):
#Work with file info
a = Audio()
a.audio_title = filename
a.audio_type = filetype
a.album = album
a.artist = artist
a.document = sfile
a.save()
elif os.path.isdir(artist_directory + sfile):
album_directory = os.path.join(artist_directory, sfile) + '/'
for tfile in os.listdir(album_directory):
if os.path.isfile(album_directory + tfile):
#Extract File information
filename, filetype = os.path.splitext(tfile)
album = sfile
artist = cfile
if not exists(filename, 'audio'):
#Work with file info
a = Audio()
a.audio_title = filename
a.audio_type = filetype
a.album = album
a.artist = artist
a.document = tfile
a.save()
######################
#Image Folder
#######################
primary_directory = os.path.join(root, 'image/')
if not os.path.exists(primary_directory):
os.makedirs(primary_directory)
for cfile in os.listdir(primary_directory):
if os.path.isfile(primary_directory + cfile):
#Extract File Information
filename, filetype = os.path.splitext(cfile)
folder = 'Main'
if not exists(filename, 'image'):
#Work with file info
i = Image()
i.image_title = filename
i.folder = folder
i.document = cfile
i.save()
elif os.path.isdir(primary_directory + cfile):
secondary_directory = os.path.join(primary_directory, cfile) + '/'
for sfile in os.listdir(secondary_directory):
if os.path.isfile(secondary_directory + sfile):
#Extract File Information
filename, filetype = os.path.splitext(sfile)
folder = cfile
if not exists(filename, 'image'):
#Work with file info
i = Image()
i.image_title = filename
i.folder = folder
i.document = sfile
i.save()
######################
#Video Folder
#######################
primary_directory = os.path.join(root, 'video/')
if not os.path.exists(primary_directory):
os.makedirs(primary_directory)
for cfile in os.listdir(primary_directory):
if os.path.isfile(primary_directory + cfile):
#Extract File Information
filename, filetype = os.path.splitext(cfile)
folder = 'Main'
if not exists(filename, 'video'):
#Work with file info
v = Video()
v.folder = folder
v.video_title = filename
v.document = cfile
v.save()
elif os.path.isdir(primary_directory + cfile):
secondary_directory = os.path.join(primary_directory, cfile) + '/'
for sfile in os.listdir(secondary_directory):
if os.path.isfile(secondary_directory + sfile):
#Extract File Information
filename, filetype = os.path.splitext(sfile)
folder = cfile
if not exists(filename, 'video'):
#Work with file info
v = Video()
v.folder = folder
v.video_title = filename
v.document = sfile
v.save()
return redirect('index')