11from __future__ import annotations
22
3+ import warnings
34from typing import Protocol
45
6+ import django
57from django .db import transaction
68from PIL import Image
79
@@ -46,12 +48,12 @@ def _process_picture(
4648
4749
4850try :
49- import dramatiq
51+ from dramatiq import actor
5052except ImportError :
5153 pass
5254else :
5355
54- @dramatiq . actor (queue_name = conf .get_settings ().QUEUE_NAME )
56+ @actor (queue_name = conf .get_settings ().QUEUE_NAME )
5557 def process_picture_with_dramatiq (
5658 storage : tuple [str , list , dict ],
5759 file_name : str ,
@@ -60,12 +62,19 @@ def process_picture_with_dramatiq(
6062 ) -> None :
6163 _process_picture (storage , file_name , new , old )
6264
63- def process_picture ( # noqa: F811
65+ def dramatiq_process_picture ( # noqa: F811
6466 storage : tuple [str , list , dict ],
6567 file_name : str ,
6668 new : list [tuple [str , list , dict ]] | None = None ,
6769 old : list [tuple [str , list , dict ]] | None = None ,
6870 ) -> None :
71+ if django .VERSION >= (6 , 0 ):
72+ warnings .warn (
73+ "The `dramatiq_process_picture`-processor deprecated in favor of Django's tasks framework."
74+ " Deletion is scheduled with Django 5.2 version support." ,
75+ PendingDeprecationWarning ,
76+ stacklevel = 2 ,
77+ )
6978 transaction .on_commit (
7079 lambda : process_picture_with_dramatiq .send (
7180 storage = storage ,
@@ -75,6 +84,8 @@ def process_picture( # noqa: F811
7584 )
7685 )
7786
87+ process_picture = dramatiq_process_picture # type: ignore[assignment]
88+
7889
7990try :
8091 from celery import shared_task
@@ -94,12 +105,19 @@ def process_picture_with_celery(
94105 ) -> None :
95106 _process_picture (storage , file_name , new , old )
96107
97- def process_picture ( # noqa: F811
108+ def celery_process_picture ( # noqa: F811
98109 storage : tuple [str , list , dict ],
99110 file_name : str ,
100111 new : list [tuple [str , list , dict ]] | None = None ,
101112 old : list [tuple [str , list , dict ]] | None = None ,
102113 ) -> None :
114+ if django .VERSION >= (6 , 0 ):
115+ warnings .warn (
116+ "The `celery_process_picture`-processor deprecated in favor of Django's tasks framework."
117+ " Deletion is scheduled with Django 5.2 version support." ,
118+ PendingDeprecationWarning ,
119+ stacklevel = 2 ,
120+ )
103121 transaction .on_commit (
104122 lambda : process_picture_with_celery .apply_async (
105123 kwargs = dict (
@@ -112,6 +130,8 @@ def process_picture( # noqa: F811
112130 )
113131 )
114132
133+ process_picture = celery_process_picture # type: ignore[assignment]
134+
115135
116136try :
117137 from django_rq import job
@@ -128,12 +148,19 @@ def process_picture_with_django_rq(
128148 ) -> None :
129149 _process_picture (storage , file_name , new , old )
130150
131- def process_picture ( # noqa: F811
151+ def rq_process_picture ( # noqa: F811
132152 storage : tuple [str , list , dict ],
133153 file_name : str ,
134154 new : list [tuple [str , list , dict ]] | None = None ,
135155 old : list [tuple [str , list , dict ]] | None = None ,
136156 ) -> None :
157+ if django .VERSION >= (6 , 0 ):
158+ warnings .warn (
159+ "The `rq_process_picture`-processor deprecated in favor of Django's tasks framework."
160+ " Deletion is scheduled with Django 5.2 version support." ,
161+ PendingDeprecationWarning ,
162+ stacklevel = 2 ,
163+ )
137164 transaction .on_commit (
138165 lambda : process_picture_with_django_rq .delay (
139166 storage = storage ,
@@ -142,3 +169,46 @@ def process_picture( # noqa: F811
142169 old = old ,
143170 )
144171 )
172+
173+ process_picture = rq_process_picture # type: ignore[assignment]
174+
175+
176+ try :
177+ from django .tasks import exceptions , task
178+ except ImportError :
179+ pass
180+ else :
181+ try :
182+
183+ @task (
184+ backend = conf .get_settings ().BACKEND ,
185+ queue_name = conf .get_settings ().QUEUE_NAME ,
186+ )
187+ def process_picture_with_django_tasks (
188+ storage : tuple [str , list , dict ],
189+ file_name : str ,
190+ new : list [tuple [str , list , dict ]] | None = None ,
191+ old : list [tuple [str , list , dict ]] | None = None ,
192+ ) -> None :
193+ _process_picture (storage , file_name , new , old )
194+
195+ def process_picture ( # noqa: F811
196+ storage : tuple [str , list , dict ],
197+ file_name : str ,
198+ new : list [tuple [str , list , dict ]] | None = None ,
199+ old : list [tuple [str , list , dict ]] | None = None ,
200+ ) -> None :
201+ transaction .on_commit (
202+ lambda : process_picture_with_django_tasks .enqueue (
203+ storage = storage ,
204+ file_name = file_name ,
205+ new = new ,
206+ old = old ,
207+ )
208+ )
209+
210+ except exceptions .InvalidTask as e :
211+ raise exceptions .ImproperlyConfigured (
212+ "Pictures are processed on a separate queue by default,"
213+ " please `TASKS` settings in accordance with Django-Pictures documentation."
214+ ) from e
0 commit comments