Problem: I have an ImageField in a model. The TestCase isn’t able to find the default image file to perform a image resize on it (a @receiver(pre_save, sender=UserProfile) decorator thing)
class UserProfile(Model):
user = ForeignKey(AUTH_USER_MODEL ...)
...
photo = ImageField(
verbose_name=_("Photo"),
upload_to="media",
null=True,
blank=True,
help_text=_("a photo will spark recognition from others."),
default="default.png")
Overview: I’m running tests locally from pycharm to a docker container. This project is based on a django cookiecutter project.
I did a search for the target file in the presave hook and the file was found in these 2 directories ['/app/media/default.png', '/opt/project/media/default.png']
I am getting a FileNotFound error on '/app/<my_project_name>/media/default.png'
How do I get the media file to look in these directories?
@receiver(pre_save, sender=UserProfile)
def user_profile_face_photo_reduction(sender, instance, *args, **kwargs):
"""
This creates the thumbnail photo for a concept
"""
test_image_size(instance) # this is the function that gives problems. it's below.
im = generic_resize_image(size=face_dimensions, image=instance.photo)
save_images_as_filename(
filename=Path(instance.photo.path).with_suffix(".png"),
image=im,
instance=instance,
)
def test_image_size(instance=None, size_limit=None):
"""
size_limit is a namedtuple with a height and width that is too large to save.
instance needs to have a photo attribute to work
"""
if instance and instance.photo:
print([x.absolute() for x in sorted(Path("/").rglob("default.png"))]) # this is where I got the actual location info
assert instance.photo.size <= 10_000_000, ValueError("Image size is too big") # THE PROBLEM LINE IS THIS ONE.
if (
instance.photo.width > size_limit.width
or instance.photo.height > size_limit.height
):
raise ValueError(_("the picture dimensions are too big"))
I started off creating a "media" directory in the app_root folder (/Users//<project_name>/). That was a dumb move. There is already a media folder in the (/Users//<project_name>/<project_name>/media) folder which I didn't see. I tried to force it using tricks like the docker mounting trick and changing my code, it was a waste of time.
I upvoted @heyylateef's answer for the override_settings because that is a neat decorator.