Skip to content

Commit 1d6b82e

Browse files
committed
Implement review suggestions
1 parent dbc80b3 commit 1d6b82e

File tree

5 files changed

+40
-46
lines changed

5 files changed

+40
-46
lines changed

pictures/models.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ def delete_all(self):
155155
import_string(conf.get_settings().PROCESSOR)(
156156
self.storage.deconstruct(),
157157
self.name,
158+
self.sender,
158159
[],
159160
[i.deconstruct() for i in self.get_picture_files_list()],
160-
self.instance_name,
161161
)
162162

163163
def update_all(self, other: PictureFieldFile | None = None):
@@ -170,14 +170,18 @@ def update_all(self, other: PictureFieldFile | None = None):
170170
import_string(conf.get_settings().PROCESSOR)(
171171
self.storage.deconstruct(),
172172
self.name,
173+
self.sender,
173174
[i.deconstruct() for i in new],
174175
[i.deconstruct() for i in old],
175-
self.instance_name,
176176
)
177177

178178
@property
179-
def instance_name(self):
180-
return f"{self.instance._meta.app_label}.{self.instance._meta.model_name}.{self.field.name}"
179+
def sender(self):
180+
return (
181+
self.instance._meta.app_label,
182+
self.instance._meta.model_name,
183+
self.field.name,
184+
)
181185

182186
@property
183187
def width(self):

pictures/signals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import django.dispatch
22

3-
process_picture_done = django.dispatch.Signal()
3+
picture_processed = django.dispatch.Signal()

pictures/tasks.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ def __call__(
1818
self,
1919
storage: tuple[str, list, dict],
2020
file_name: str,
21+
sender: tuple[str, str, str],
2122
new: list[tuple[str, list, dict]] | None = None,
2223
old: list[tuple[str, list, dict]] | None = None,
23-
field: str = "",
2424
) -> None: ...
2525

2626

2727
def _process_picture(
2828
storage: tuple[str, list, dict],
2929
file_name: str,
30+
sender: tuple[str, str, str],
3031
new: list[tuple[str, list, dict]] | None = None,
3132
old: list[tuple[str, list, dict]] | None = None,
32-
field: str = "",
3333
) -> None:
3434
new = new or []
3535
old = old or []
@@ -44,19 +44,15 @@ def _process_picture(
4444
picture = utils.reconstruct(*picture)
4545
picture.delete()
4646

47-
if field:
48-
app_label, model_name, _ = field.split(".")
49-
sender = apps.get_model(app_label=app_label, model_name=model_name)
50-
else:
51-
sender = _process_picture
47+
app_label, model_name, field_name = sender
48+
model = apps.get_model(app_label=app_label, model_name=model_name)
49+
field = model._meta.get_field(field_name)
5250

53-
signals.process_picture_done.send(
54-
sender=sender,
55-
storage=storage.deconstruct(),
51+
signals.picture_processed.send(
52+
sender=field,
5653
file_name=file_name,
5754
new=new,
5855
old=old,
59-
field=field,
6056
)
6157

6258

@@ -73,26 +69,26 @@ def _process_picture(
7369
def process_picture_with_dramatiq(
7470
storage: tuple[str, list, dict],
7571
file_name: str,
72+
sender: tuple[str, str, str],
7673
new: list[tuple[str, list, dict]] | None = None,
7774
old: list[tuple[str, list, dict]] | None = None,
78-
field: str = "",
7975
) -> None:
80-
_process_picture(storage, file_name, new, old, field)
76+
_process_picture(storage, file_name, sender, new, old)
8177

8278
def process_picture( # noqa: F811
8379
storage: tuple[str, list, dict],
8480
file_name: str,
81+
sender: tuple[str, str, str],
8582
new: list[tuple[str, list, dict]] | None = None,
8683
old: list[tuple[str, list, dict]] | None = None,
87-
field: str = "",
8884
) -> None:
8985
transaction.on_commit(
9086
lambda: process_picture_with_dramatiq.send(
9187
storage=storage,
9288
file_name=file_name,
89+
sender=sender,
9390
new=new,
9491
old=old,
95-
field=field,
9692
)
9793
)
9894

@@ -110,27 +106,27 @@ def process_picture( # noqa: F811
110106
def process_picture_with_celery(
111107
storage: tuple[str, list, dict],
112108
file_name: str,
109+
sender: tuple[str, str, str],
113110
new: list[tuple[str, list, dict]] | None = None,
114111
old: list[tuple[str, list, dict]] | None = None,
115-
field: str = "",
116112
) -> None:
117-
_process_picture(storage, file_name, new, old, field)
113+
_process_picture(storage, file_name, sender, new, old)
118114

119115
def process_picture( # noqa: F811
120116
storage: tuple[str, list, dict],
121117
file_name: str,
118+
sender: tuple[str, str, str],
122119
new: list[tuple[str, list, dict]] | None = None,
123120
old: list[tuple[str, list, dict]] | None = None,
124-
field: str = "",
125121
) -> None:
126122
transaction.on_commit(
127123
lambda: process_picture_with_celery.apply_async(
128124
kwargs=dict(
129125
storage=storage,
130126
file_name=file_name,
127+
sender=sender,
131128
new=new,
132129
old=old,
133-
field=field,
134130
),
135131
queue=conf.get_settings().QUEUE_NAME,
136132
)
@@ -147,25 +143,25 @@ def process_picture( # noqa: F811
147143
def process_picture_with_django_rq(
148144
storage: tuple[str, list, dict],
149145
file_name: str,
146+
sender: tuple[str, str, str],
150147
new: list[tuple[str, list, dict]] | None = None,
151148
old: list[tuple[str, list, dict]] | None = None,
152-
field: str = "",
153149
) -> None:
154-
_process_picture(storage, file_name, new, old, field)
150+
_process_picture(storage, file_name, sender, new, old)
155151

156152
def process_picture( # noqa: F811
157153
storage: tuple[str, list, dict],
158154
file_name: str,
155+
sender: tuple[str, str, str],
159156
new: list[tuple[str, list, dict]] | None = None,
160157
old: list[tuple[str, list, dict]] | None = None,
161-
field: str = "",
162158
) -> None:
163159
transaction.on_commit(
164160
lambda: process_picture_with_django_rq.delay(
165161
storage=storage,
166162
file_name=file_name,
163+
sender=sender,
167164
new=new,
168165
old=old,
169-
field=field,
170166
)
171167
)

tests/test_signals.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from unittest.mock import Mock
22

33
import pytest
4-
from django.apps import apps
54
from django.dispatch import receiver
65

76
from pictures import signals, tasks
@@ -13,40 +12,37 @@ def test_process_picture_sends_process_picture_done(image_upload_file):
1312
obj = SimpleModel.objects.create(picture=image_upload_file)
1413

1514
handler = Mock()
16-
signals.process_picture_done.connect(handler)
15+
signals.picture_processed.connect(handler)
1716

1817
tasks._process_picture(
1918
obj.picture.storage.deconstruct(),
2019
obj.picture.name,
20+
obj.picture.sender,
2121
new=[i.deconstruct() for i in obj.picture.get_picture_files_list()],
2222
)
2323

2424
handler.assert_called_once_with(
25-
signal=signals.process_picture_done,
26-
sender=tasks._process_picture,
27-
storage=obj.picture.storage.deconstruct(),
25+
signal=signals.picture_processed,
26+
sender=SimpleModel._meta.get_field("picture"),
2827
file_name=obj.picture.name,
2928
new=[i.deconstruct() for i in obj.picture.get_picture_files_list()],
3029
old=[],
31-
field="",
3230
)
3331

3432

3533
@pytest.mark.django_db
3634
def test_process_picture_sends_process_picture_done_on_create(image_upload_file):
3735
handler = Mock()
38-
signals.process_picture_done.connect(handler)
36+
signals.picture_processed.connect(handler)
3937

4038
obj = SimpleModel.objects.create(picture=image_upload_file)
4139

4240
handler.assert_called_once_with(
43-
signal=signals.process_picture_done,
44-
sender=SimpleModel,
45-
storage=obj.picture.storage.deconstruct(),
41+
signal=signals.picture_processed,
42+
sender=SimpleModel._meta.get_field("picture"),
4643
file_name=obj.picture.name,
4744
new=[i.deconstruct() for i in obj.picture.get_picture_files_list()],
4845
old=[],
49-
field="testapp.simplemodel.picture",
5046
)
5147

5248

@@ -56,15 +52,12 @@ def test_processed_object_found(image_upload_file):
5652

5753
found_object = None
5854

59-
@receiver(signals.process_picture_done, sender=SimpleModel)
60-
def handler(*, file_name, field, **__):
55+
@receiver(signals.picture_processed, sender=SimpleModel._meta.get_field("picture"))
56+
def handler(*, sender, file_name, **__):
6157
nonlocal found_object
62-
app_label, model_name, field_name = field.split(".")
63-
model = apps.get_model(app_label=app_label, model_name=model_name)
6458

65-
# Users can now modify the object that process_picture_done
66-
# corresponds to
67-
found_object = model.objects.get(**{field_name: file_name})
59+
# Users can now modify the object that picture_processed corresponds to
60+
found_object = sender.model.objects.get(**{sender.name: file_name})
6861

6962
obj.picture.save("image.png", image_upload_file)
7063

tests/test_tasks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_process_picture__file_cannot_be_reopened(image_upload_file):
1818
tasks._process_picture(
1919
obj.picture.storage.deconstruct(),
2020
obj.picture.name,
21+
obj.picture.sender,
2122
new=[i.deconstruct() for i in obj.picture.get_picture_files_list()],
2223
)
2324

0 commit comments

Comments
 (0)