Skip to content

Commit

Permalink
refactor: dynamic quib component
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace committed Dec 20, 2024
1 parent de1ec72 commit 2585331
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 56 deletions.
23 changes: 11 additions & 12 deletions backend/apps/quib/api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
from rest_framework import serializers

from apps.quiblet.api.v1.serializers import QuibletSerializer, QuibletSlimSerializer
from apps.quiblet.api.v1.mixins import QuibletSlimMixin
from apps.user.api.v1.serializers import ProfileMinimalSerializer

from ...models import Quib


class QuibSerializer(serializers.ModelSerializer):
quiblet = QuibletSerializer(read_only=True)

class QuibSerializer(QuibletSlimMixin):
class Meta:
model = Quib
fields = '__all__'


class QuibSlimSerializer(serializers.ModelSerializer):
quiblet = QuibletSlimSerializer(read_only=True)

class QuibMinimalSerializer(QuibletSlimMixin):
class Meta:
model = Quib
exclude = ('quibber',)

def get_cover(self, obj):
request = self.context.get('request')
if obj.cover:
return request.build_absolute_uri(obj.cover) if request else obj.cover
return None

class QuibExtendedSerializer(QuibletSlimMixin):
quibber = ProfileMinimalSerializer(read_only=True)

class Meta:
model = Quib
fields = '__all__'


class QuibHighlightedSerializer(serializers.ModelSerializer):
Expand Down
4 changes: 2 additions & 2 deletions backend/apps/quib/api/v1/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
from apps.comment.api.v1.serializers import CommentSerializer

from ...models import Quib
from .serializers import QuibSerializer, QuibSlimSerializer
from .serializers import QuibMinimalSerializer, QuibSerializer


class QuibViewSet(viewsets.ModelViewSet):
queryset = Quib.objects.all()

def get_serializer_class(self): # pyright: ignore
if self.action == 'list':
return QuibSlimSerializer
return QuibMinimalSerializer
# if custom action: 'comment'
if self.action == 'comments':
return CommentSerializer
Expand Down
7 changes: 7 additions & 0 deletions backend/apps/quiblet/api/v1/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from rest_framework import serializers

from .serializers import QuibletMinimalSerializer


class QuibletSlimMixin(serializers.ModelSerializer):
quiblet = QuibletMinimalSerializer(read_only=True)
22 changes: 11 additions & 11 deletions backend/apps/quiblet/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ def get_name(self, obj) -> Optional[str]:
return None


class QuibletDetailSerializer(serializers.ModelSerializer):
rangers = RangerSerializer(many=True)
quibs = serializers.SerializerMethodField()

class QuibletSerializer(serializers.ModelSerializer):
class Meta:
model = Quiblet
fields = '__all__'

def get_quibs(self, obj) -> int:
return obj.quibs.count()


class QuibletSerializer(serializers.ModelSerializer):
class QuibletMinimalSerializer(serializers.ModelSerializer):
class Meta:
model = Quiblet
fields = '__all__'
fields = ('name', 'avatar')


class QuibletSlimSerializer(serializers.ModelSerializer):
class QuibletExtendedSerializer(serializers.ModelSerializer):
rangers = RangerSerializer(many=True)
quibs = serializers.SerializerMethodField()

class Meta:
model = Quiblet
fields = ('name', 'avatar')
fields = '__all__'

def get_quibs(self, obj) -> int:
return obj.quibs.count()


class QuibletExistsSerializer(serializers.Serializer):
Expand Down
15 changes: 9 additions & 6 deletions backend/apps/quiblet/api/v1/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
from rest_framework import exceptions, response, viewsets
from rest_framework.decorators import action

from apps.quib.api.v1.serializers import QuibHighlightedSerializer, QuibSlimSerializer
from apps.quib.api.v1.serializers import (
QuibExtendedSerializer,
QuibHighlightedSerializer,
)
from common.patches.request import PatchedHttpRequest

from ...models import Quiblet
from .serializers import (
QuibletDetailSerializer,
QuibletExistsSerializer,
QuibletExtendedSerializer,
QuibletSerializer,
)

Expand All @@ -25,10 +28,10 @@ class QuibletViewSet(viewsets.ModelViewSet):

# extra custom serializers
serializer_classes = {
'retrieve': QuibletDetailSerializer,
'retrieve': QuibletExtendedSerializer,
# extra actions
'exists': QuibletExistsSerializer,
'quibs': QuibSlimSerializer,
'quibs': QuibExtendedSerializer,
'highlighted_quibs': QuibHighlightedSerializer,
}

Expand Down Expand Up @@ -62,11 +65,11 @@ def exists(self, request, name=None):

return response.Response(res)

@extend_schema(responses=QuibSlimSerializer(many=True))
@extend_schema(responses=QuibExtendedSerializer(many=True))
@action(detail=True, methods=[HTTPMethod.GET])
def quibs(self, request, name=None):
quibs = self.get_object().quibs.all() # pyright: ignore
serializer = QuibSlimSerializer(quibs, many=True, context={'request': request})
serializer = QuibExtendedSerializer(quibs, many=True, context={'request': request})

return response.Response(serializer.data)

Expand Down
20 changes: 13 additions & 7 deletions backend/apps/user/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def create(self, validated_data):
return user


class AuthSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('email', 'password')


class AuthTokenSerializer(serializers.Serializer):
token = serializers.CharField()


class ProfileSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True)

Expand All @@ -23,11 +33,7 @@ class Meta:
fields = '__all__'


class AuthSerializer(serializers.ModelSerializer):
class ProfileMinimalSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('email', 'password')


class AuthTokenSerializer(serializers.Serializer):
token = serializers.CharField()
model = Profile
fields = ('username', 'avatar')
52 changes: 39 additions & 13 deletions frontend/src/lib/clients/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ export interface components {
};
PatchedQuib: {
readonly id?: string;
readonly quiblet?: components['schemas']['Quiblet'];
readonly quiblet?: components['schemas']['QuibletMinimal'];
/**
* Create at
* Format: date-time
Expand Down Expand Up @@ -750,9 +750,15 @@ export interface components {
last_name?: string | null;
bio?: string | null;
};
ProfileMinimal: {
/** @description Required. 25 characters or fewer. Letters, digits and ./_ only. */
username: string;
/** Format: uri */
avatar?: string | null;
};
Quib: {
readonly id: string;
readonly quiblet: components['schemas']['Quiblet'];
readonly quiblet: components['schemas']['QuibletMinimal'];
/**
* Create at
* Format: date-time
Expand All @@ -770,6 +776,26 @@ export interface components {
downvotes?: number[];
comments?: number[];
};
QuibExtended: {
readonly id: string;
readonly quiblet: components['schemas']['QuibletMinimal'];
readonly quibber: components['schemas']['ProfileMinimal'];
/**
* Create at
* Format: date-time
*/
readonly created_at: string;
is_public?: boolean;
highlighted?: boolean;
title: string;
readonly slug: string;
content?: string;
/** Format: uri */
cover?: string | null;
upvotes?: number[];
downvotes?: number[];
comments?: number[];
};
QuibHighlighted: {
/** Format: uri */
cover?: string | null;
Expand All @@ -782,9 +808,9 @@ export interface components {
*/
readonly created_at: string;
};
QuibSlim: {
QuibMinimal: {
readonly id: string;
readonly quiblet: components['schemas']['QuibletSlim'];
readonly quiblet: components['schemas']['QuibletMinimal'];
/**
* Create at
* Format: date-time
Expand Down Expand Up @@ -819,7 +845,11 @@ export interface components {
members?: number[];
rangers?: number[];
};
QuibletDetail: {
QuibletExists: {
exists: boolean;
name: string;
};
QuibletExtended: {
readonly id: number;
rangers: components['schemas']['Ranger'][];
readonly quibs: number;
Expand All @@ -838,11 +868,7 @@ export interface components {
banner?: string | null;
members?: number[];
};
QuibletExists: {
exists: boolean;
name: string;
};
QuibletSlim: {
QuibletMinimal: {
name: string;
/** Format: uri */
avatar?: string | null;
Expand Down Expand Up @@ -2898,7 +2924,7 @@ export interface operations {
[name: string]: unknown;
};
content: {
'application/json': components['schemas']['QuibletDetail'];
'application/json': components['schemas']['QuibletExtended'];
};
};
404: {
Expand Down Expand Up @@ -3147,7 +3173,7 @@ export interface operations {
[name: string]: unknown;
};
content: {
'application/json': components['schemas']['QuibSlim'][];
'application/json': components['schemas']['QuibExtended'][];
};
};
404: {
Expand Down Expand Up @@ -3182,7 +3208,7 @@ export interface operations {
[name: string]: unknown;
};
content: {
'application/json': components['schemas']['QuibSlim'][];
'application/json': components['schemas']['QuibMinimal'][];
};
};
500: {
Expand Down
21 changes: 16 additions & 5 deletions frontend/src/lib/components/quib.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import readable from 'readable-numbers';
import { FormatDate } from '$lib/functions/date';
import { createAuthStore } from '$lib/stores/auth.svelte';
import { page } from '$app/stores';
type QuibSlimProps = components['schemas']['QuibSlim'];
type QuibExtendedProps = components['schemas']['QuibExtended'];
let quib: QuibSlimProps = $props();
let quib: QuibExtendedProps = $props();
const authStore = createAuthStore();
Expand All @@ -18,6 +19,16 @@
return false;
}
});
function get_avatar() {
return $page.url.pathname.includes('/q/') ? quib.quibber.avatar : quib.quiblet.avatar;
}
function get_name() {
return $page.url.pathname.includes('/q/')
? `u/${quib.quibber.username}`
: `q/${quib.quiblet.name}`;
}
</script>

<div class="flex flex-col overflow-hidden rounded-2xl border border-neutral bg-base-300">
Expand All @@ -31,11 +42,11 @@
></a>
<div class="flex items-center gap-2">
<a
href="/q/{quib.quiblet.name}"
href="/{get_name()}"
class="relative flex items-center gap-2 hover:text-accent hover:underline"
>
<Avatar src={quib.quiblet.avatar} />
<h3 class="text-xs font-semibold">q/{quib.quiblet.name}</h3>
<Avatar src={get_avatar()} />
<h3 class="text-xs font-semibold">{get_name()}</h3>
</a>
<coreicons-shape-circle variant="filled" class="size-0.5 text-base-content/75"
></coreicons-shape-circle>
Expand Down

0 comments on commit 2585331

Please sign in to comment.