-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0090c0
commit 8cd0a80
Showing
6 changed files
with
256 additions
and
191 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,37 +1,56 @@ | ||
from worf.serializers import Serializer | ||
from worf.serializers import fields, Serializer | ||
|
||
|
||
class UserSerializer(Serializer): | ||
def read(self, model): | ||
return dict( | ||
username=model.username, | ||
lastLogin=model.last_login, | ||
dateJoined=model.date_joined, | ||
email=model.email, | ||
) | ||
|
||
def write(self): | ||
return [ | ||
last_login = fields.DateTime(dump_only=True) | ||
date_joined = fields.DateTime(dump_only=True) | ||
|
||
class Meta: | ||
fields = [ | ||
"username", | ||
"last_login", | ||
"date_joined", | ||
"email", | ||
] | ||
|
||
|
||
class ProfileSerializer(Serializer): | ||
def read(self, model): | ||
return dict( | ||
username=model.user.username, | ||
email=model.user.email, | ||
role=dict(name=model.role.name) if model.role else None, | ||
skills=[t.api() for t in model.ratedskill_set.all()], | ||
team=dict(name=model.team.name) if model.team else None, | ||
tags=[t.api() for t in model.tags.all()], | ||
) | ||
|
||
def write(self): | ||
return [ | ||
username = fields.Function(lambda obj: obj.user.username) | ||
email = fields.Function(lambda obj: obj.user.email) | ||
role = fields.Nested("RoleSerializer") | ||
skills = fields.Nested("RatedSkillSerializer", attribute="ratedskill_set", many=True) | ||
team = fields.Nested("TeamSerializer") | ||
tags = fields.Nested("TagSerializer", many=True) | ||
|
||
class Meta: | ||
fields = [ | ||
"username", | ||
"email", | ||
"role", | ||
"skills", | ||
"team", | ||
"tags", | ||
] | ||
|
||
|
||
class RatedSkillSerializer(Serializer): | ||
id = fields.Function(lambda obj: obj.skill.id) | ||
name = fields.Function(lambda obj: obj.skill.name) | ||
|
||
class Meta: | ||
fields = ["id", "name", "rating"] | ||
|
||
|
||
class RoleSerializer(Serializer): | ||
class Meta: | ||
fields = ["id", "name"] | ||
|
||
|
||
class TagSerializer(Serializer): | ||
class Meta: | ||
fields = ["id", "name"] | ||
|
||
|
||
class TeamSerializer(Serializer): | ||
class Meta: | ||
fields = ["id", "name"] |
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,11 @@ | ||
from django.db.models import Manager | ||
|
||
import marshmallow.fields | ||
from marshmallow.fields import * # noqa: F401, F403 | ||
|
||
|
||
class Nested(marshmallow.fields.Nested): | ||
def _serialize(self, nested_obj, attr, obj, **kwargs): | ||
if isinstance(nested_obj, Manager): | ||
nested_obj = nested_obj.all() | ||
return super()._serialize(nested_obj, attr, obj, **kwargs) |
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