diff --git a/docs/examples/integrations/dmr/model_file.py b/docs/examples/integrations/dmr/model_file.py deleted file mode 100644 index 8209130..0000000 --- a/docs/examples/integrations/dmr/model_file.py +++ /dev/null @@ -1,45 +0,0 @@ -from django.db import models -from django.shortcuts import get_object_or_404 -from dmr import Controller, Path -from dmr.plugins.pydantic import PydanticFastSerializer -from pydantic import BaseModel - -from imgproxykit.integrations.django import imgproxy_url - - -class Product(models.Model): - """Product with an optional uploaded image.""" - - image = models.ImageField(upload_to='products/', blank=True) - - class Meta: - app_label = 'products' - - -class ProductPath(BaseModel): - """Identify the requested product.""" - - product_id: int - - -class ProductResponse(BaseModel): - """Product with an optional image URL.""" - - id: int - image: str | None - - -class ProductController(Controller[PydanticFastSerializer]): - """Serve a product with its image URL.""" - - def get(self, parsed_path: Path[ProductPath]) -> ProductResponse: - """Return a product and transform its image when present.""" - product = get_object_or_404(Product, pk=parsed_path.product_id) - return ProductResponse( - id=product.pk, - image=( - imgproxy_url(product.image, 'thumbnail') - if product.image # (1)! - else None - ), - ) diff --git a/docs/examples/integrations/fastapi/multiple_clients.py b/docs/examples/integrations/fastapi/multiple_clients.py index aaab0d7..5e0ac38 100644 --- a/docs/examples/integrations/fastapi/multiple_clients.py +++ b/docs/examples/integrations/fastapi/multiple_clients.py @@ -9,7 +9,6 @@ _SOURCE_URL: Final = 'https://originals.example.com/photo.jpg' app = FastAPI() - get_signed_imgproxy = add_imgproxy( # (1)! app, Client( diff --git a/docs/examples/integrations/fastapi/variants.py b/docs/examples/integrations/fastapi/variants.py index 3602993..2d4bc57 100644 --- a/docs/examples/integrations/fastapi/variants.py +++ b/docs/examples/integrations/fastapi/variants.py @@ -8,13 +8,11 @@ from imgproxykit.integrations.fastapi import add_imgproxy _SOURCE_URL: Final = 'https://originals.example.com/photo.jpg' +_BASE: Final = Transform(format='webp') app = FastAPI() - get_imgproxy = add_imgproxy(app, Client('https://imgproxy.example.com')) -_BASE: Final = Transform(format='webp') - @dataclass(frozen=True, slots=True) class ProductImages: diff --git a/docs/pages/integrations/dmr.rst b/docs/pages/integrations/dmr.rst index 8d373d5..3c04f7e 100644 --- a/docs/pages/integrations/dmr.rst +++ b/docs/pages/integrations/dmr.rst @@ -87,14 +87,10 @@ for request parsing and handler definitions. Return variants --------------- -For several image sizes, use ``imgproxy_variants`` and a nested response model. -Keep the imports and ``_SOURCE_URL`` from the previous example, -replacing the shortcut import with -``from imgproxykit.integrations.django import imgproxy_variants``: +For several image sizes, use ``imgproxy_variants`` and a nested response model: .. literalinclude:: /examples/integrations/dmr/variants.py :language: python - :start-at: class ProductImages .. code-annotations:: @@ -110,27 +106,6 @@ See DMR's `OpenAPI guide `__ to publish the schema and interactive API documentation. -Model file fields ------------------ - -For a product loaded from your database, pass ``product.image`` directly to -``imgproxy_url`` in the controller. Here, ``Product`` is your Django model -with an optional image field. ``ProductPath`` declares a ``product_id: int`` -path parameter using a Pydantic model. Imports, the Django model, path model, -and the route are omitted from this snippet: - -.. literalinclude:: /examples/integrations/dmr/model_file.py - :language: python - :start-at: class ProductResponse - -.. code-annotations:: - - 1. Generate a URL only when the file field has an associated file; - otherwise return ``None``, serialized as JSON ``null``. - -Without the empty-field check, the default source -resolver raises ``ValueError`` for a field without an associated file. - Source resolution ----------------- diff --git a/docs/pages/integrations/drf.rst b/docs/pages/integrations/drf.rst index c8bdc26..b460481 100644 --- a/docs/pages/integrations/drf.rst +++ b/docs/pages/integrations/drf.rst @@ -109,6 +109,13 @@ fields used in the response: .. code-block:: python + from rest_framework import serializers + + from imgproxykit.integrations.drf import ( + ImgproxyURLField, + ImgproxyVariantsField, + ) + image = serializers.ImageField(write_only=True, required=False) # (1)! thumbnail = ImgproxyURLField(source='image', transform='thumbnail') images = ImgproxyVariantsField(source='image', variants='product')