Skip to content

Commit

Permalink
Merge pull request #312 from HelloYeew/update-profile-api
Browse files Browse the repository at this point in the history
Add `tags` and `created_rulesets` to profile API
  • Loading branch information
HelloYeew authored Apr 10, 2022
2 parents 8ef410e + d2b6f47 commit 8362f98
Showing 1 changed file with 55 additions and 3 deletions.
58 changes: 55 additions & 3 deletions rurusetto/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers
from wiki.models import Ruleset, Subpage, RulesetStatus, RecommendBeatmap
from users.models import Profile
from users.models import Profile, Tag
from django.contrib.auth.models import User


Expand Down Expand Up @@ -33,6 +33,24 @@ def get_status(self, obj):
return {}


class RulesetProfileSerializer(serializers.ModelSerializer):
"""
Serializer for use in profile serializer that don't need owner detail.
"""
status = serializers.SerializerMethodField()

class Meta:
model = Ruleset
fields = ['id', 'name', 'slug', 'description', 'icon', 'light_icon', 'verified', 'archive',
'direct_download_link', 'can_download', 'status']

def get_status(self, obj):
try:
return StatusDetailSerializer(RulesetStatus.objects.get(ruleset=obj)).data
except RulesetStatus.DoesNotExist:
return {}


# Serializer for rulesets detail


Expand Down Expand Up @@ -85,8 +103,10 @@ def get_status(self, obj):
except RulesetStatus.DoesNotExist:
return {}


# Serializer for subpoge


class RulesetsSubpageSerializer(serializers.ModelSerializer):
"""
Serializer for Subpage model to get only detail that use in rulesets
Expand Down Expand Up @@ -150,7 +170,7 @@ def get_user_detail(self, obj):
return {}


# Serializer for user and profile
# Serializer for user and profile related fields

class UserSerializer(serializers.ModelSerializer):
class Meta:
Expand All @@ -175,8 +195,40 @@ class UserFullSerializer(serializers.ModelSerializer):
Serializer for full detail of users
"""
user = UserSerializer()
tags = serializers.SerializerMethodField()
created_rulesets = serializers.SerializerMethodField()

class Meta:
model = Profile
fields = ['id', 'user', 'image', 'cover', 'cover_light', 'about_me', 'osu_username']
fields = ['id', 'user', 'tags', 'image', 'cover', 'cover_light', 'about_me', 'osu_username', 'created_rulesets']
depth = 1

def get_tags(self, obj):
if obj.tag == '':
return []
tags_list = obj.tag.split(',')
tags_list_converted = []
for tag in tags_list:
try:
tags_list_converted.append(Tag.objects.get(id=int(tag)))
except Tag.DoesNotExist:
pass
try:
return TagSerializer(tags_list_converted, many=True).data
except Tag.DoesNotExist:
return []

def get_created_rulesets(self, obj):
try:
return RulesetProfileSerializer(Ruleset.objects.filter(owner=str(obj.id)), many=True).data
except Ruleset.DoesNotExist:
return []


class TagSerializer(serializers.ModelSerializer):
"""
Serializer for tag model
"""
class Meta:
model = Tag
fields = ['name', 'pills_color', 'font_color', 'description']

0 comments on commit 8362f98

Please sign in to comment.