5

I am having a weird problem with tests regarding testing with temporary images and compressing images in models.py. There seems to be a problem with permissions:

ERROR: test_has_light_images (realestate.tests.test_view_listing.RealestateListingViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Storm\Envs\btre\lib\site-packages\django\test\utils.py", line 373, in inner
    return func(*args, **kwargs)
  File "C:\Users\Storm\Dev\btre_project\realestate\tests\test_view_listing.py", line 72, in test_has_light_images
    create_listing(title='listing_sample', address='sample', realtor_num=1, city='sample', state='sample', zipcode='1234', price='555555', bedrooms='1', bathrooms='1', garage='1', sqft='123', lot_size='123', image_sample=image_sample.name)
  File "C:\Users\Storm\Dev\btre_project\realestate\tests\test_view_listing.py", line 37, in create_listing
    return Listing.objects.create(title=title, address=address, realtor=realtor, city=city, state=state, zipcode=zipcode, price=price, bedrooms=bedrooms, bathrooms=bathrooms, garage=garage, sqft=sqft, lot_size=lot_size, photo_main=image_sample, photo_1=image_sample, photo_2=image_sample, photo_3=image_sample, photo_4=image_sample, photo_5=image_sample, photo_6=image_sample)
  File "C:\Users\Storm\Envs\btre\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Storm\Envs\btre\lib\site-packages\django\db\models\query.py", line 422, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Users\Storm\Dev\btre_project\realestate\models.py", line 69, in save
    new_image = compress(self.photo_main)
  File "C:\Users\Storm\Dev\btre_project\realestate\models.py", line 11, in compress
    im = Image.open(image)
  File "C:\Users\Storm\Envs\btre\lib\site-packages\PIL\Image.py", line 2774, in open
    fp.seek(0)
  File "C:\Users\Storm\Envs\btre\lib\site-packages\django\core\files\utils.py", line 20, in <lambda>
    seek = property(lambda self: self.file.seek)
  File "C:\Users\Storm\Envs\btre\lib\site-packages\django\db\models\fields\files.py", line 43, in _get_file
    self._file = self.storage.open(self.name, 'rb')
  File "C:\Users\Storm\Envs\btre\lib\site-packages\django\core\files\storage.py", line 36, in open
    return self._open(name, mode)
  File "C:\Users\Storm\Envs\btre\lib\site-packages\django\core\files\storage.py", line 224, in _open
    return File(open(self.path(name), mode))
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Storm\\AppData\\Local\\Temp\\tmp24xoaa7g'

test.py

from django.test import TestCase
from django.urls import reverse, resolve
from django.utils import timezone
import datetime

from ..models import Listing

from django.test import override_settings
from PIL import Image
from io import BytesIO
import tempfile

def get_temporary_image(temp_file):
    size = (200, 200)
    color = (255, 0, 0, 0)
    image = Image.new("RGB", size, color)
    image.save(temp_file, 'jpeg')
    return temp_file

    @override_settings(MEDIA_ROOT=tempfile.gettempdir())
    def test_has_light_images(self):
        temp_file = tempfile.NamedTemporaryFile()
        image_sample = get_temporary_image(temp_file)
        Listing.objects.create(title='listing_sample', photo_main=image_sample.name)

models.py

def compress(image):
    im = Image.open(image)
    im_io = BytesIO() 
    im.save(im_io, 'JPEG', quality=20, optimize=True)
    new_image = File(im_io, name=image.name)
    return new_image

class Listing(models.Model):
    title = models.CharField(max_length=200)
    photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/')

    def save(self, *args, **kwargs):
        new_image = compress(self.photo_main)
        self.photo_main = new_image
        super().save(*args, **kwargs)

I've tried another way of compressing images that makes the tests work but it's unsuccessful in compressing the image.

Does anyone know what's happening here?

halfer
  • 19,824
  • 17
  • 99
  • 186
Storm Llamas
  • 119
  • 1
  • 2
  • 10
  • found this which sort of explains a lot on how NamedTemporaryFile(s) are sort of broken for windows https://stackoverflow.com/questions/49868470/using-namedtemporaryfile – Storm Llamas Nov 01 '19 at 05:40

1 Answers1

1

This happens on a Windows machine using Pycharm.

The fix: Right-click your PyCharm application and run it as administrator.

Update: "From Windows start menu right click the Pycharm shortcut and select 'Run as administrator.'"

or

Change the permissions of the directory you want to save so that all users have read and write permissions.

Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
  • I don't think I have Pycharm. I ran cmd as administrator.. I am still trying to find out how to go about doing this. Also the directory is a temporary file. If I were to change that to something where all users can read and write, how might I do that? – Storm Llamas Nov 01 '19 at 03:09
  • I have no PyCharm. Maybe I need to get Pycharm? – Storm Llamas Nov 01 '19 at 03:14
  • which IDE are you using? – Usman Maqbool Nov 01 '19 at 03:20
  • I am using VSC (I literally had to search "is VScode an IDE"). So I tried running VSC as administrator but still have the same problem – Storm Llamas Nov 01 '19 at 03:33
  • 1
    doesn't change anything – jjones150 Jan 12 '21 at 22:31
  • PyCharm (run as privilege management admin) returns a child process marked with a _winapi.CloseHandel(ht) where the error becomes "OSError: [WinError 6] The handle is invalid" (This shows under "./envs/pyspark3/subprocess.py" Would deleting the activated environment "pyspark3", in this case, fix this issue? – Jamie Nov 03 '22 at 13:56