Open taljacob2 opened 2 years ago
Hi, it seems that we have 2 problems in need of fixing here:
I've tried many ways already: trying to delete them manually at the end of a test, using "get_or_create" instead of "create" in the data migrations, and also many different changes with no success. Will continue to look further into it.
(The code I used here is: 262803a. It is the updated version in the PR of #65 right now)
Clean The Database
Steps I have made to setup a fresh start for the database:
- I have run
vagrant up
.- I have killed Django server, with:
vagrant ssh cd /vagrant killall -9 python exit
- I have deleted the database (
db.sqlite3
file).
This has created the image files in the backend (that their name is appended with a generated ID, as expected), and inserted their path string to the database.
Let's view it in the database through the shell:
For example, let's focus John Doe.
As you can see, this works fine.
The profile_pic
and resume
return the path to the saved images we saw in the shell.
Now let's check this on admin view:
It seems that the images in John Doe's profile are correctly linked to the images created in the backend:
Let's try to delete John Doe's profile, and see if the images get deleted from the backend too.
The images are still in the backend, though we have deleted John Doe's personal_profile
:
So this is why although pytest's tests delete the profile_profile
s they create, the images that were created still don't get deleted, and remain in the backend.
Create our own implementation for PersonalProfile.delete
that:
personal_profile.profile_pic
personal_profile.resume
We can try this StackOverflow Answer for deleting Pillow images.
personal_profile
's record from the database (as the ORM does).personal_profile.delete
Override personal_profile.delete
(thanks to this StackOverflow Answer),
by navigating to personal_profile/models.py
,
and in the PersonalProfile
class, add the following method:
import os
from app.settings import MEDIA_ROOT
class PersonalProfile(models.Model):
def delete(self):
# Get the media root path, and remove the first `/vagrant` folder.
media_root_path = os.path.join(*(MEDIA_ROOT.split(os.path.sep)[2:]))
file_paths_to_delete = [media_root_path + "/" + self.profile_pic.name,
media_root_path + "/" + self.resume.name]
# Filter list.
file_paths_to_delete = [i for i in file_paths_to_delete if
i is not None]
# Delete files from the backend.
if file_paths_to_delete:
for file_path in file_paths_to_delete:
print(f'deleting {file_path}')
os.remove(file_path, dir_fd=None)
# Call Django's ORM to delete this record.
super(PersonalProfile, self).delete()
Suggested Implementation for
personal_profile.delete
Override
personal_profile.delete
(thanks to this StackOverflow Answer), by navigating topersonal_profile/models.py
, and in thePersonalProfile
class, add the following method:import os from app.settings import MEDIA_ROOT class PersonalProfile(models.Model): def delete(self): # Get the media root path, and remove the first `/vagrant` folder. media_root_path = os.path.join(*(MEDIA_ROOT.split(os.path.sep)[2:])) file_paths_to_delete = [media_root_path + "/" + self.profile_pic.name, media_root_path + "/" + self.resume.name] # Filter list. file_paths_to_delete = [i for i in file_paths_to_delete if i is not None] # Delete files from the backend. if file_paths_to_delete: for file_path in file_paths_to_delete: print(f'deleting {file_path}') os.remove(file_path, dir_fd=None) # Call Django's ORM to delete this record. super(PersonalProfile, self).delete()
@taljacob2 Thank you so much for your kind help! Unfortunately, this still doesn't work, I'm trying to go in that direction of trying to specifically delete the files from the db.
I used https://stackoverflow.com/questions/16041232/django-delete-filefield to fix part of the bug- specifically, delete the files that were created for the tests! The rest of the "doubled" files are created as part of the data migrations, I need to find a solution for that as well- but it is not connected to the tests.
Description
Hi, In the Personal-Profile app, I found out that after running:
the test pass, but create leftover images. We need pytest to delete them automatically.
See the screenshot (commit 5011071733e6e754ae1727eaaeffcfedaaf8af7c):
Also in PR #65 (commit dcef35d276e1cc84c5581030a102dc4762382816):