Forums

Deleting a folder on uploading a file through admin

Greetings, I've been trying to make it so when I upload a new profile picture for an artist in my website, the folder where it was contained is deleted first but what I've tried doesn't seem to work. Is the server protected against that?

Here's my code:

def get_image_path(instance, filename):
    if os.path.exists(os.path.join('/home/[[PATH]]/media/images', str(instance.id))):
        shutil.rmtree(os.path.join('/home/[[PATH]]/media/images', str(instance.id)))
    return os.path.join('media/images', str(instance.id), filename)

class Artist(models.Model):
    name = models.CharField(max_length=255)
    soundcloud = models.URLField(max_length=255, blank=True, null=True)
    description = models.TextField()
    profile_picture = models.ImageField(upload_to=get_image_path, blank=True, null=True)
    current_roster = models.BooleanField(default=True)

    def __str__(self):
        return self.name.encode('utf8')

It doesn't seem work. Is there anyone who could help me please? The [[PATH]] is for safety purposes, it's the actual path (I don't know if it's actually dangerous to give that information but just in case).

Thank you very much!

Are you seeing any sorts of errors? Look in the error log on your web tab if you haven't already?

No, I checked my error log and there are no errors.

Can you tell us a bit more about what's going wrong? What do you expect to happen, what happens instead, how do you test it?

Nothing much happens, the new image is set but the former one isn't deleted, which is the point. I tested basically by doing that a few times.

try putting some debug print statements into the get_image_path function? Maybe it's not being called when you think it is...

I did that. When I print out whether that directory I asked about exists, it outputs False. What's the way to check whether a directory exists within the project folder?

Thank you very much!

os.path.exists is the right function to call. Could you be making some sort of mistake in the os.path.join? Maybe it's worth printing the joined path too?

I do and it's /home/username/projectfolder/media/images/numericidofelement

I'm quite new at programming. I'm not disclosing the exact path of the project because I feel I could compromise its security. Am I right in doing so? Does it make a difference? To be honest, I'd like to know because I don't know want to sound too paranoid without a purpose, please.

I can't see how exposing the real path would raise any risk, no. We all know your username anyway, since it's at the bottom of your posts ;-)

So, question -- now you've put a print into that get_image function -- is it happening every single time you try and upload an image? Or is it only ever happening once, when you load the app for the first time?

I tried using a print on the function but I couldn't see where the output went so I used a file writer. It's happened every time I've tried: the path output seems okay but when I print out the boolean statement checking whether the path exists I get a False.

Hey, thank you very much for your help! I managed to find the problem!

:) what was the problem in the end?

I hadn't written the path properly... I'm quite new at programming so I still make rookie mistakes like that...

We all have to start somewhere :-) Glad you got it working!