Repository: wagtail/wagtail
File: wagtail/images/models.py (Python)

Code before:

    def get_upload_to(self, filename):
        """
        Generates a file path within the "images" folder by combining the folder name and the validated filename.
        """
        folder_name = "images"
        filename = self.file.field.storage.get_valid_name(filename)
        return os.path.join(folder_name, filename)

    @classmethod
    def check(cls, **kwargs):
        errors = super().check(**kwargs)
        if not cls._meta.abstract:
            in_unique_together = any(
                set(constraint) == {"image", "filter_spec", "focal_point_key"}
                for constraint in cls._meta.unique_together
            )

            # We only include fields to check if they are a UniqueConstraint, and
            # they have no conditions on them.
            unique_constraint_fields = [
                constraint.fields
                for constraint in cls._meta.constraints
                if (
                    isinstance(constraint, UniqueConstraint)
                    and constraint.condition is None
                )
            ]
            in_unique_constraint = any(
                set(constraint) == {"image", "filter_spec", "focal_point_key"}
                for constraint in unique_constraint_fields
            )

            # We check for some kind of unique constraint, whether in unique together or
            # as part of the constraints.
            if not in_unique_together and not in_unique_constraint:
                errors.append(
                    checks.Error(
                        "Custom rendition model %r has an invalid unique_together setting"
                        % cls,
                        hint="Custom rendition models must include a unique constraint "
                        "on the 'image', 'filter_spec', and 'focal_point_key' fields.",
                        obj=cls,
                        id="wagtailimages.E001",
                    )
                )

        return errors

    @staticmethod
    def construct_cache_key(image, filter_cache_key, filter_spec):

Review comment (suggestion):
I think this can be simplified as a single-pass loop like the following:

```suggestion
        in_unique_constraint = any(
            isinstance(constraint, models.UniqueConstraint)
            and constraint.condition is None
            and set(constraint.fields) == {"image", "filter_spec", "focal_point_key"}
            for constraint in cls._meta.constraints
        )
```

Code after:

    def get_upload_to(self, filename):
        """
        Generates a file path within the "images" folder by combining the folder name and the validated filename.
        """
        folder_name = "images"
        filename = self.file.field.storage.get_valid_name(filename)
        return os.path.join(folder_name, filename)

    @classmethod
    def check(cls, **kwargs):
        errors = super().check(**kwargs)
        if not cls._meta.abstract:
            # Allow unique constraint to be defined via `unique_together` or
            # a `UniqueConstraint` in `constraints`.
            unique_constraint_fields = {"image", "filter_spec", "focal_point_key"}
            in_unique_together = any(
                set(constraint) == unique_constraint_fields
                for constraint in cls._meta.unique_together
            )
            in_unique_constraint = any(
                isinstance(constraint, models.UniqueConstraint)
                and constraint.condition is None
                and set(constraint.fields) == unique_constraint_fields
                for constraint in cls._meta.constraints
            )

            if not in_unique_together and not in_unique_constraint:
                errors.append(
                    checks.Error(
                        "Custom rendition model '%s' must include a unique constraint "
                        "on the 'image', 'filter_spec', and 'focal_point_key' fields."
                        % cls._meta.label,
                        hint=(
                            "Add models.UniqueConstraint(fields={"
                            '"image", "filter_spec", "focal_point_key"}, '
                            'name="unique_rendition") to %s.Meta.constraints.'
                            % (cls.__name__,)
                        ),
                        obj=cls,
                        id="wagtailimages.E001",
                    )
                )

        return errors

    @staticmethod
    def construct_cache_key(image, filter_cache_key, filter_spec):
        return "wagtail-rendition-" + "-".join(
            [str(image.id), image.file_hash, filter_cache_key, filter_spec]
        )