Repository: wagtail/wagtail

Issue:
check for raw in signal handlers
The signal handlers such as [pre_save_image_feature_detection](https://github.com/wagtail/wagtail/blob/master/wagtail/wagtailimages/signal_handlers.py) should check for `raw` to detect loading from a fixture and do nothing in this case.  Otherwise you get an error loading fixtures if the source file doesn't exist and `WAGTAILIMAGES_FEATURE_DETECTION_ENABLED=True`


Discussion:
Update - this likely still exists and file content has moved to 

`https://github.com/wagtail/wagtail/blob/main/wagtail/images/signal_handlers.py`
https://github.com/wagtail/wagtail/blob/main/wagtail/images/signal_handlers.py


```python
def pre_save_image_feature_detection(instance, **kwargs):
    # .. add check here for kwargs['raw']
```

See https://docs.djangoproject.com/en/4.0/ref/signals/#pre-save for `raw`

Flagging as good first issue, best to start with unit tests on this and then work towards solving the issue. use `@override_settings` in the unit tests to trigger the exact scenario as described in the parent comment.
Hello,  Can i work on this issue. Thers arent any PRs so if its possible can i try this one?
Go for it @anujaraj - before you start on code be sure to follow the steps above and check you can reproduce the issue. Write a comment with any extra info. Including what version you were using of Wagtail/ Django / Python. 
I use the folowing technologies -
Python - 3.6.8
Django- 3.2.12
Wagtail-2.17.0
Also if i could know the initial first few steps to reproduce the error. By this Is mean. I should first create a fixture. save a image in it. remove the image and load the fixture again ?
I am not sure as to how i should use @override_settings to reproduce the issue. It would be graet if you could guide me on this

may i work on this issue? it has been inactive for so long
Go for it @4the4ryushin - be sure you can reproduce and understand the problem. I'd recommend you find where to write a unit test for this and start with writing a test that fails. Then change the code to get the test passing. 
should i write the test in  https://github.com/wagtail/wagtail/blob/main/wagtail/tests/test_signals.py

Similar - except this is an images application so it needs to go in https://github.com/wagtail/wagtail/blob/main/wagtail/images/tests/test_signal_handlers.py
opps yea that's what i meant , my bad 
do i need to write the test that uses fixture ? because that's what raw == true means
are there any predefined fixtures  in the code base for tests that i can use?
@4the4ryushin I encourage you to look through the code yourself and try to find either a. Fixtures that have images or b. Similar test code where we are testing code that has logic based in raw. 
Got it that's what I was thinking just wanted to confirm that it's the right way or not 
@lb-  sorry for the delay , i was little busy, now i am all set to fix this issue found the fixtures and done with tests, i wanted to ask a little query in the parent comment it says at raw == True, am i supposed to literally pass and do nothing, or i still should check for focal_points and so as it has been done originally 

Resolving patch:
diff --git a/wagtail/images/signal_handlers.py b/wagtail/images/signal_handlers.py
--- a/wagtail/images/signal_handlers.py
+++ b/wagtail/images/signal_handlers.py
@@ -16,10 +16,12 @@ def post_delete_purge_rendition_cache(instance, **kwargs):
 
 def pre_save_image_feature_detection(instance, **kwargs):
     if getattr(settings, "WAGTAILIMAGES_FEATURE_DETECTION_ENABLED", False):
-        # Make sure the image doesn't already have a focal point
-        if not instance.has_focal_point():
-            # Set the focal point
-            instance.set_focal_point(instance.get_suggested_focal_point())
+        # Make sure the image is not from a fixture
+        if kwargs["raw"] is False:
+            # Make sure the image doesn't already have a focal point
+            if not instance.has_focal_point():
+                # Set the focal point
+                instance.set_focal_point(instance.get_suggested_focal_point())
 
 
 def register_signal_handlers():


Test patch:
diff --git a/wagtail/images/tests/test_signal_handlers.py b/wagtail/images/tests/test_signal_handlers.py
--- a/wagtail/images/tests/test_signal_handlers.py
+++ b/wagtail/images/tests/test_signal_handlers.py
@@ -1,10 +1,12 @@
 from django.db import transaction
-from django.test import TransactionTestCase, override_settings
+from django.test import TestCase, TransactionTestCase, override_settings
 
 from wagtail.images import get_image_model, signal_handlers
 from wagtail.images.tests.utils import get_test_image_file
 from wagtail.models import Collection
 
+from .utils import Image
+
 
 class TestFilesDeletedForDefaultModels(TransactionTestCase):
     """
@@ -80,3 +82,16 @@ def test_image_model(self):
         self.assertEqual(
             "%s.%s" % (cls._meta.app_label, cls.__name__), "tests.CustomImage"
         )
+
+
+@override_settings(WAGTAILIMAGES_FEATURE_DETECTION_ENABLED=True)
+class TestRawForPreSaveImageFeatureDetection(TestCase):
+    fixtures = ["test.json"]
+
+    # just to test the file is from a fixture doesn't actually exists.
+    # raw check in pre_save_image_feature_detection skips on the provided condition of this test
+    # hence avoiding an error
+
+    def test_image_does_not_exist(self):
+        bad_image = Image.objects.get(pk=1)
+        self.assertFalse(bad_image.file.storage.exists(bad_image.file.name))