-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add a icon serializer field.
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |