Skip to content

Commit

Permalink
Merge pull request #310 from HelloYeew/recommend-beatmap-api
Browse files Browse the repository at this point in the history
Add open API for recommend beatmap
  • Loading branch information
HelloYeew authored Apr 9, 2022
2 parents a405dc9 + d12fd22 commit 8ef410e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 21 deletions.
60 changes: 41 additions & 19 deletions rurusetto/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
from rest_framework import serializers
from wiki.models import Ruleset, Subpage, RulesetStatus
from wiki.models import Ruleset, Subpage, RulesetStatus, RecommendBeatmap
from users.models import Profile
from django.contrib.auth.models import User


class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['username', 'email']


class ProfileMiniSerializer(serializers.ModelSerializer):
"""
Serializer that will only filter the profile of owner to use in RulesetListingSerializer.
"""
user = UserSerializer()

class Meta:
model = Profile
fields = ['id', 'user', 'image']
depth = 1


# Serializer for listing page


Expand Down Expand Up @@ -146,8 +128,48 @@ def get_last_edited_by_detail(self, obj):
return {}


# Serializer for recommend beatmap


class RecommendBeatmapSerializer(serializers.ModelSerializer):
"""
Serializer for recommend beatmap model to get full detail of recommend beatmap.
"""
user_detail = serializers.SerializerMethodField()

class Meta:
model = RecommendBeatmap
fields = ['user_detail', 'beatmap_id', 'beatmapset_id', 'title', 'artist', 'source', 'creator',
'approved', 'difficultyrating', 'bpm', 'version', 'url', 'beatmap_cover', 'beatmap_thumbnail',
'beatmap_card', 'beatmap_list', 'comment', 'created_at']

def get_user_detail(self, obj):
try:
return ProfileMiniSerializer(Profile.objects.get(id=obj.user_id)).data
except Profile.DoesNotExist:
return {}


# Serializer for user and profile

class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['username', 'email']


class ProfileMiniSerializer(serializers.ModelSerializer):
"""
Serializer that will only filter the profile of owner to use in RulesetListingSerializer.
"""
user = UserSerializer()

class Meta:
model = Profile
fields = ['id', 'user', 'image']
depth = 1


class UserFullSerializer(serializers.ModelSerializer):
"""
Serializer for full detail of users
Expand Down
5 changes: 4 additions & 1 deletion rurusetto/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
urlpatterns = [
path('rulesets', views.listing),
path('rulesets/<slug:slug>', views.ruleset_detail),
path('rulesets/<slug:rulesets_slug>/beatmaps', views.recommend_beatmap),
path('rulesets/<slug:rulesets_slug>/beatmaps/creator', views.recommend_beatmap_only_creator),
path('rulesets/<slug:rulesets_slug>/beatmaps/players', views.recommend_beatmap_other_players),
path('subpage/<slug:rulesets_slug>', views.all_ruleset_subpage),
path('subpage/<slug:rulesets_slug>/<slug:subpage_slug>', views.subpage),
path('profile/<int:user_id>', views.user)
path('profile/<int:user_id>', views.user),
]
59 changes: 58 additions & 1 deletion rurusetto/api/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .serializers import *
from django.views.decorators.csrf import csrf_exempt
from wiki.models import Ruleset, Subpage
from wiki.models import Ruleset, Subpage, RecommendBeatmap
from users.models import Profile
from django.contrib.auth.models import User
from django.http import HttpResponse, JsonResponse
Expand Down Expand Up @@ -105,3 +105,60 @@ def user(request, user_id):
if request.method == 'GET':
serializer = UserFullSerializer(profile)
return JsonResponse(serializer.data)

@csrf_exempt
def recommend_beatmap(request, rulesets_slug):
"""
View for return list of all ruleset's recommend beatmap
:param request: WSGI request from user
:param rulesets_slug: Ruleset slug in ruleset database model
:return: list of ruleset's recommend beatmap
"""
try:
ruleset = Ruleset.objects.get(slug=rulesets_slug)
except Ruleset.DoesNotExist:
return JsonResponse(make_404_json_response('The ruleset is not found'), status=404, content_type="application/json")

if request.method == 'GET':
all_approved_recommend_beatmap = RecommendBeatmap.objects.filter(ruleset_id=ruleset.id, owner_approved=True, owner_seen=True)
serializer = RecommendBeatmapSerializer(all_approved_recommend_beatmap, many=True)
return JsonResponse(serializer.data, safe=False)

@csrf_exempt
def recommend_beatmap_only_creator(request, rulesets_slug):
"""
View for return list of ruleset's recommend beatmap but filter only the beatmap that creator recommend.
:param request: WSGI request from user
:param rulesets_slug: Ruleset slug in ruleset database model
:return: list of ruleset's recommend beatmap but filter only the beatmap that creator recommend
"""
try:
ruleset = Ruleset.objects.get(slug=rulesets_slug)
except Ruleset.DoesNotExist:
return JsonResponse(make_404_json_response('The ruleset is not found'), status=404, content_type="application/json")

if request.method == 'GET':
recommend_beatmap_only_creator = RecommendBeatmap.objects.filter(user_id=ruleset.owner, ruleset_id=ruleset.id, owner_approved=True, owner_seen=True)
serializer = RecommendBeatmapSerializer(recommend_beatmap_only_creator, many=True)
return JsonResponse(serializer.data, safe=False)

@csrf_exempt
def recommend_beatmap_other_players(request, rulesets_slug):
"""
View for return list of ruleset's recommend beatmap but filter only the beatmap that recommend by other user except ruleset's creator.
:param request: WSGI request from user
:param rulesets_slug: Ruleset slug in ruleset database model
:return: list of ruleset's recommend beatmap but filter only the beatmap that recommend by other user except ruleset's creator
"""
try:
ruleset = Ruleset.objects.get(slug=rulesets_slug)
except Ruleset.DoesNotExist:
return JsonResponse(make_404_json_response('The ruleset is not found'), status=404, content_type="application/json")

if request.method == 'GET':
recommend_beatmap_only_users = RecommendBeatmap.objects.filter(ruleset_id=ruleset.id, owner_approved=True, owner_seen=True).exclude(user_id=ruleset.owner)
serializer = RecommendBeatmapSerializer(recommend_beatmap_only_users, many=True)
return JsonResponse(serializer.data, safe=False)

0 comments on commit 8ef410e

Please sign in to comment.