From 6f01068aaa9d809663534daf6d3c1e8dcb39a1e0 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Oct 2024 17:46:08 +0930 Subject: [PATCH] feat(core): Add a icon serializer field. ref: #345 #346 --- app/core/classes/icon.py | 29 +++++++++++++++++++++++++++++ app/core/fields/icon.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 app/core/classes/icon.py create mode 100644 app/core/fields/icon.py diff --git a/app/core/classes/icon.py b/app/core/classes/icon.py new file mode 100644 index 000000000..eac23a8c3 --- /dev/null +++ b/app/core/classes/icon.py @@ -0,0 +1,29 @@ + + + +class Icon: + + name: str + + style:str + + + def __init__(self, + name: str = None, + style: str = None, + url: str = None + ): + + self.name = name + + self.style = style + + self.url = url + + @property + def to_json(self): + + return { + 'name': self.name, + 'style': self.style + } diff --git a/app/core/fields/icon.py b/app/core/fields/icon.py new file mode 100644 index 000000000..1e561714e --- /dev/null +++ b/app/core/fields/icon.py @@ -0,0 +1,36 @@ +from rest_framework import serializers +from rest_framework.fields import empty + +from core.classes.icon import Icon + + + +class IconField(serializers.Field): + + source = '' + + label = '' + + def __init__(self, *, read_only=True, write_only=False, + required=None, default=empty, initial=empty, source=None, + label=None, help_text=None, style=None, + error_messages=None, validators=None, allow_null=False): + + super().__init__(read_only=read_only, write_only=write_only, + required=required, default=default, initial=initial, source=source, + label=label, help_text=help_text, style=style, + error_messages=error_messages, validators=validators, allow_null=allow_null) + + def to_representation(self, icons: list([Icon])): + + a_icons: list = [] + + for icon in icons: + + a_icons += [ icon.to_json ] + + return a_icons + + + def to_internal_value(self, data): + return Icon(data.icon,data.icon_style, data.url)