0

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')
Herco
  • 21
  • 3
  • where is cfile in folder = cfile – Sugumar Venkatesan Jul 09 '18 at 12:04
  • I don't understand what is not working. You don't seem to be uploading anything. – Daniel Roseman Jul 09 '18 at 12:09
  • the cfile is a file which is stored in the variable cfile. I am not uploading anything yes, I am just trying to save data to the model. The file is retrieved from the database. – Herco Jul 09 '18 at 12:15
  • I have added the entire loop alogrithm so that you can understand what cfile is – Herco Jul 09 '18 at 12:17
  • Also what does not work is when I just save data to the hard drive (Not uploading from website, saving from algorithm) it does not save the upload_to in FileField correctly, it saves it as /media/filename instead of /media/folder/filename – Herco Jul 09 '18 at 12:25
  • But my point is, if you are not doing any uploading, why do you think upload_to should be called at all? – Daniel Roseman Jul 09 '18 at 12:56
  • Makes sense, Daniel. But then how can I store the correct file path when saving the file? Cause it should still save to a specific path but right now it is just saving to the MEDIA_ROOT directory? Each time i try to research about saving files to databases i just get modelforms which i dont want since i am not uploading but just saving, if u know what i mean? – Herco Jul 09 '18 at 13:16

1 Answers1

0

I fixed my problem using Daniel's input. I changed the value for my document and it worked. See my new code below:

Old Code:

...
x.document = sfile

x.save()

New Code:

x.document = os.path.join('file', folder, sfile)

x.save()
Herco
  • 21
  • 3