Skip to content

Commit

Permalink
Use DRF field_name not source as GET param prefix as documented
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Jun 9, 2023
1 parent 98d3934 commit f4acdfc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
8 changes: 4 additions & 4 deletions pictures/contrib/rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ def to_representation(self, obj: PictureFieldFile):
except KeyError:
pass
else:
ratio = query_params.get(f"{self.source}_ratio")
container = query_params.get(f"{self.source}_container")
ratio = query_params.get(f"{self.field_name}_ratio")
container = query_params.get(f"{self.field_name}_container")
try:
container = int(container)
except TypeError:
container = None
except ValueError as e:
raise ValueError(f"Container width is not a number: {container}") from e
breakpoints = {
bp: int(query_params.get(f"{self.source}_{bp}"))
bp: int(query_params.get(f"{self.field_name}_{bp}"))
for bp in get_settings().BREAKPOINTS
if f"{self.source}_{bp}" in query_params
if f"{self.field_name}_{bp}" in query_params
}
if ratio is not None:
try:
Expand Down
46 changes: 23 additions & 23 deletions tests/contrib/test_rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@


class ProfileSerializer(serializers.ModelSerializer):
picture = rest_framework.PictureField()
image = rest_framework.PictureField(source="picture")

class Meta:
model = models.Profile
fields = ["picture"]
fields = ["image"]


def test_default(settings):
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_to_representation(self, image_upload_file, settings):
profile = models.Profile.objects.create(picture=image_upload_file)
serializer = ProfileSerializer(profile)

assert serializer.data["picture"] == {
assert serializer.data["image"] == {
"url": "/media/testapp/profile/image.png",
"width": 800,
"height": 800,
Expand Down Expand Up @@ -121,12 +121,12 @@ def test_to_representation__with_aspect_ratios(
profile = models.Profile.objects.create(picture=image_upload_file)
request = rf.get("/")
request.GET._mutable = True
request.GET["picture_ratio"] = "1/1"
request.GET["picture_l"] = "3"
request.GET["picture_m"] = "4"
request.GET["image_ratio"] = "1/1"
request.GET["image_l"] = "3"
request.GET["image_m"] = "4"
serializer = ProfileSerializer(profile, context={"request": request})

assert serializer.data["picture"] == {
assert serializer.data["image"] == {
"url": "/media/testapp/profile/image.png",
"width": 800,
"height": 800,
Expand Down Expand Up @@ -158,13 +158,13 @@ def test_to_representation__raise_value_error(
profile = models.Profile.objects.create(picture=image_upload_file)
request = rf.get("/")
request.GET._mutable = True
request.GET["picture_ratio"] = "21/11"
request.GET["picture_l"] = "3"
request.GET["picture_m"] = "4"
request.GET["image_ratio"] = "21/11"
request.GET["image_l"] = "3"
request.GET["image_m"] = "4"
serializer = ProfileSerializer(profile, context={"request": request})

with pytest.raises(ValueError) as e:
serializer.data["picture"]
serializer.data["image"]

assert str(e.value) == "Invalid ratio: 21/11. Choices are: 1/1, 3/2, 16/9"

Expand All @@ -175,12 +175,12 @@ def test_to_representation__blank(self, rf, image_upload_file, settings):
profile = models.Profile.objects.create()
request = rf.get("/")
request.GET._mutable = True
request.GET["picture_ratio"] = "21/11"
request.GET["picture_l"] = "3"
request.GET["picture_m"] = "4"
request.GET["image_ratio"] = "21/11"
request.GET["image_l"] = "3"
request.GET["image_m"] = "4"
serializer = ProfileSerializer(profile, context={"request": request})

assert serializer.data["picture"] is None
assert serializer.data["image"] is None

@pytest.mark.django_db
def test_to_representation__with_container(self, rf, image_upload_file, settings):
Expand All @@ -189,10 +189,10 @@ def test_to_representation__with_container(self, rf, image_upload_file, settings
profile = models.Profile.objects.create(picture=image_upload_file)
request = rf.get("/")
request.GET._mutable = True
request.GET["picture_ratio"] = "16/9"
request.GET["picture_container"] = "1200"
request.GET["image_ratio"] = "16/9"
request.GET["image_container"] = "1200"
serializer = ProfileSerializer(profile, context={"request": request})
assert serializer.data["picture"] == {
assert serializer.data["image"] == {
"url": "/media/testapp/profile/image.png",
"width": 800,
"height": 800,
Expand Down Expand Up @@ -224,9 +224,9 @@ def test_to_representation__without_container(
profile = models.Profile.objects.create(picture=image_upload_file)
request = rf.get("/")
request.GET._mutable = True
request.GET["picture_ratio"] = "16/9"
request.GET["image_ratio"] = "16/9"
serializer = ProfileSerializer(profile, context={"request": request})
assert serializer.data["picture"] == {
assert serializer.data["image"] == {
"url": "/media/testapp/profile/image.png",
"width": 800,
"height": 800,
Expand Down Expand Up @@ -258,9 +258,9 @@ def test_to_representation__with_false_str_container(
profile = models.Profile.objects.create(picture=image_upload_file)
request = rf.get("/")
request.GET._mutable = True
request.GET["picture_ratio"] = "16/9"
request.GET["picture_container"] = "not_a_number"
request.GET["image_ratio"] = "16/9"
request.GET["image_container"] = "not_a_number"
serializer = ProfileSerializer(profile, context={"request": request})
with pytest.raises(ValueError) as e:
serializer.data["picture"]
serializer.data["image"]
assert str(e.value) == "Container width is not a number: not_a_number"
1 change: 1 addition & 0 deletions tests/testapp/migrations/0005_alter_profile_picture.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 3.2.19 on 2023-06-07 18:06

from django.db import migrations

import pictures.models


Expand Down

0 comments on commit f4acdfc

Please sign in to comment.