Skip to content

Commit

Permalink
feat(api): serialize ExperiencesCategory
Browse files Browse the repository at this point in the history
  • Loading branch information
MagneticNeedle committed Jul 20, 2023
1 parent dc2372c commit e016a5a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 27 deletions.
5 changes: 3 additions & 2 deletions bfportal/bfportal/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"wagtailautocomplete",
"ajax_select",
"markdownx",
"generic_chooser",
]

SITE_ID = 1
Expand Down Expand Up @@ -189,7 +190,7 @@

MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"

WAGTAILSVG_UPLOAD_FOLDER = "svg"
# Wagtail settings

WAGTAIL_SITE_NAME = "bfportal"
Expand Down Expand Up @@ -289,5 +290,5 @@ def setup_logging():

logger.remove()
logger.add(
sys.stdout, colorize=True, level="INFO", backtrace=True, format=LOGGING_FORMAT
sys.stdout, colorize=True, level="DEBUG", backtrace=True, format=LOGGING_FORMAT
)
28 changes: 7 additions & 21 deletions bfportal/core/api.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
from rest_framework.renderers import JSONRenderer
from wagtail.api.v2.router import WagtailAPIRouter
from wagtail.api.v2.views import BaseAPIViewSet, PagesAPIViewSet
from wagtail.api.v2.views import PagesAPIViewSet

from .models import ExperiencePage


class ExperiencePageAPIViewSet(BaseAPIViewSet):
"""Class that defines how page api works."""
class ExperiencePageAPIViewSet(PagesAPIViewSet):
"""
ViewSet used to define how the api for ExperiencePage will work.
# todo: serialize all field
"""

renderer_classes = [JSONRenderer]
model = ExperiencePage
filter_backends = PagesAPIViewSet.filter_backends
body_fields = [
"url",
"id",
"full_url",
"title",
"description",
"cover_img_url",
"code",
"exp_url",
"featured",
"bugged",
"xp_farm",
"broken",
"like_count",
"exp_creators",
]
listing_default_fields = body_fields


api_router = WagtailAPIRouter("wagtailapi")
Expand Down
32 changes: 29 additions & 3 deletions bfportal/core/models/experience.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
from modelcluster.fields import ParentalKey, ParentalManyToManyField
from taggit.models import TaggedItemBase
from wagtail.admin.panels import FieldPanel, MultiFieldPanel
from wagtail.api import APIField
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from wagtail.models import Page
from wagtailautocomplete.edit_handlers import AutocompletePanel

from bfportal.settings.base import LOGIN_URL

from .. import serializers


class UserApiResponse(TypedDict):
"""Type definition for user api response"""
Expand Down Expand Up @@ -186,6 +189,9 @@ class ExperiencePage(RoutablePageMixin, CustomBasePage):
default=False, null=False, help_text="Is this a mock experience ?"
)

parent_page_types = ["core.ExperiencesPage"]
subpage_types = []

content_panels = (
Page.content_panels
+ [
Expand Down Expand Up @@ -255,6 +261,29 @@ class ExperiencePage(RoutablePageMixin, CustomBasePage):
+ [CustomBasePage.content_panels[-1]]
)

api_fields = [
APIField("featured"),
APIField("trending"),
APIField("description"),
APIField("code"),
APIField("exp_url"),
APIField("tags"),
APIField("vid_url"),
APIField("cover_img_url"),
APIField("no_players"),
APIField("no_bots"),
APIField("category", serializer=serializers.ExperiencesCategorySerializer()),
APIField("sub_categories"),
APIField("bugged"),
APIField("broken"),
APIField("xp_farm"),
APIField("first_publish"),
APIField("liked_by"),
APIField("creators"),
APIField("allow_editing"),
APIField("is_mock"),
]

@property
def like_count(self):
"""Return the number of likes this experience has."""
Expand All @@ -269,9 +298,6 @@ def exp_creators(self):
)
return creators

parent_page_types = ["core.ExperiencesPage"]
subpage_types = []

@route(r"^edit/$")
def edit_page(self, request: HttpRequest): # noqa: D102
if request.user.is_authenticated:
Expand Down
19 changes: 19 additions & 0 deletions bfportal/core/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from rest_framework import serializers

from .models.categories import BaseCategory, ExperiencesCategory


class BaseCategorySerializer(serializers.ModelSerializer):
"""Serializer for BaseCategory."""

class Meta:
model = BaseCategory
exclude = ["icon"] # todo: serialize svg field


class ExperiencesCategorySerializer(BaseCategorySerializer):
"""Serializer for ExperiencesCategory."""

class Meta:
model = ExperiencesCategory
exclude = BaseCategorySerializer.Meta.exclude
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tinycss2 = "^1.2.1"
psycopg2 = "^2.9.6"
pre-commit = "^3.3.3"
gunicorn = "^20.1.0"
wagtail-generic-chooser = "^0.5.1"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.3.3"
Expand Down

0 comments on commit e016a5a

Please sign in to comment.