Skip to content
Snippets Groups Projects
Commit 78efaeb8 authored by Konrad Mohrfeldt's avatar Konrad Mohrfeldt :koala:
Browse files

fix: don’t output invalid PPOI format

The serializer would return an invalid format for the image.ppoi field
causing errors on save through the API even if the API consumer didn’t
modify the ppoi field value.

Apart from that the current implementation caused an internal server
error instead of rejecting invalid formats on save.
parent dacf2dfc
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import re
from typing import List, TypedDict
from rest_framework import serializers
......@@ -197,6 +198,22 @@ class HostLinkSerializer(serializers.ModelSerializer):
fields = ("type", "url")
class PPOIField(serializers.CharField):
def validate_format(self, value: str):
if not re.match(r"\d(?:\.\d+)?x\d(?:\.\d+)?", value):
raise serializers.ValidationError("PPOI must match format: ${float}x${float}")
def __init__(self, **kwargs):
kwargs["max_length"] = 20
kwargs.setdefault("validators", [])
kwargs["validators"].append(self.validate_format)
super().__init__(**kwargs)
def to_representation(self, value: tuple[float, float]):
[left, top] = value
return f"{left}x{top}"
class Thumbnail(TypedDict):
width: float
height: float
......@@ -204,7 +221,7 @@ class Thumbnail(TypedDict):
class ImageSerializer(serializers.ModelSerializer):
ppoi = serializers.CharField(max_length=20) # PPOIField max_length
ppoi = PPOIField()
thumbnails = serializers.SerializerMethodField()
@staticmethod
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment