Skip to content

Commit f3561d4

Browse files
author
Ahtesham Quraish
committed
add slug feature
1 parent d123e06 commit f3561d4

File tree

20 files changed

+61
-94
lines changed

20 files changed

+61
-94
lines changed

articles/migrations/0008_article_slug.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55

66
class Migration(migrations.Migration):
7-
87
dependencies = [
9-
('articles', '0007_add_editors_group'),
8+
("articles", "0007_add_editors_group"),
109
]
1110

1211
operations = [
1312
migrations.AddField(
14-
model_name='article',
15-
name='slug',
16-
field=models.SlugField(blank=True, max_length=60, null=True, unique=True),
13+
model_name="article",
14+
name="slug",
15+
field=models.SlugField(blank=True, max_length=255, null=True, unique=True),
1716
),
1817
]

articles/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Article(TimestampedModel):
2020
)
2121
content = models.JSONField(default={})
2222
title = models.CharField(max_length=255)
23-
slug = models.SlugField(max_length=60, unique=True, blank=True, null=True)
23+
slug = models.SlugField(max_length=255, unique=True, blank=True, null=True)
2424
is_published = models.BooleanField(default=False)
2525

2626

articles/serializers.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ class RichTextArticleSerializer(serializers.ModelSerializer):
3232
created_on = serializers.DateTimeField(read_only=True, required=False)
3333
updated_on = serializers.DateTimeField(read_only=True, required=False)
3434
content = serializers.JSONField(default={})
35-
slug = serializers.SlugField(
36-
max_length=60, required=False, allow_blank=True
37-
)
35+
slug = serializers.SlugField(max_length=60, required=False, allow_blank=True)
3836
title = serializers.CharField(max_length=255)
3937
user = UserSerializer(read_only=True)
4038

@@ -48,7 +46,7 @@ class Meta:
4846
"created_on",
4947
"updated_on",
5048
"is_published",
51-
"slug"
49+
"slug",
5250
]
5351

5452

articles/urls.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020
[
2121
# Existing router URLs for the ViewSet
2222
*v1_router.urls,
23-
2423
# Media upload endpoint
2524
path(
2625
"upload-media/",
2726
MediaUploadView.as_view(),
2827
name="api-media-upload",
2928
),
30-
3129
# New endpoint: retrieve article by ID or slug
3230
path(
3331
"articles/detail/<str:identifier>/",

articles/views.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class ArticleDetailByIdOrSlugAPIView(APIView):
9090

9191
@extend_schema(
9292
summary="Retrieve article by ID or slug",
93-
description="If the parameter is numeric, retrieve by ID. Otherwise, retrieve by slug.",
93+
description="If the parameter is numeric, retrieve by ID. Otherwise, slug.",
9494
parameters=[
9595
OpenApiParameter(
9696
name="identifier",
@@ -100,7 +100,10 @@ class ArticleDetailByIdOrSlugAPIView(APIView):
100100
location=OpenApiParameter.PATH,
101101
),
102102
],
103-
responses={200: RichTextArticleSerializer, 404: OpenApiResponse(description="Not found")},
103+
responses={
104+
200: RichTextArticleSerializer,
105+
404: OpenApiResponse(description="Not found"),
106+
},
104107
)
105108
def get(self, request, identifier):
106109
qs = Article.objects.all()
@@ -117,7 +120,8 @@ def get(self, request, identifier):
117120

118121
serializer = RichTextArticleSerializer(article, context={"request": request})
119122
return Response(serializer.data, status=status.HTTP_200_OK)
120-
123+
124+
121125
@extend_schema_view(
122126
post=extend_schema(
123127
# request: multipart/form-data with a binary file field

frontends/api/src/generated/v1/api.ts

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontends/api/src/hooks/articles/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ const useArticlePartialUpdate = () => {
8787
.then((response) => response.data),
8888
onSuccess: (article: Article) => {
8989
client.invalidateQueries({ queryKey: articleKeys.detail(article.id) })
90+
const identifier = article.slug || article.id.toString()
91+
client.invalidateQueries({
92+
queryKey: articleKeys.articlesDetailRetrieve(identifier),
93+
})
9094
},
9195
})
9296
}

frontends/api/src/hooks/articles/queries.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const articleKeys = {
88
list: (params: ArticleListRequest) => [...articleKeys.listRoot(), params],
99
detailRoot: () => [...articleKeys.root, "detail"],
1010
detail: (id: number) => [...articleKeys.detailRoot(), id],
11-
articlesDetailRetrieve: (identifier: string) => [...articleKeys.detailRoot(), identifier],
11+
articlesDetailRetrieve: (identifier: string) => [
12+
...articleKeys.detailRoot(),
13+
identifier,
14+
],
1215
}
1316

1417
const articleQueries = {
@@ -27,8 +30,10 @@ const articleQueries = {
2730
queryOptions({
2831
queryKey: articleKeys.articlesDetailRetrieve(identifier),
2932
queryFn: () =>
30-
articlesApi.articlesDetailRetrieve({ identifier }).then((res) => res.data),
31-
}),
33+
articlesApi
34+
.articlesDetailRetrieve({ identifier })
35+
.then((res) => res.data),
36+
}),
3237
}
3338

3439
export { articleQueries, articleKeys }

frontends/api/src/test-utils/urls.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ const articles = {
153153
list: (params?: Params<ArticlesApi, "articlesList">) =>
154154
`${API_BASE_URL}/api/v1/articles/${query(params)}`,
155155
details: (id: number) => `${API_BASE_URL}/api/v1/articles/${id}/`,
156+
articlesDetailRetrieve: (identifier: string) =>
157+
`${API_BASE_URL}/api/v1/articles/detail/${identifier}/`,
156158
}
157159

158160
const userSubscription = {

frontends/main/src/app-pages/Articles/ArticleDetailPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { notFound } from "next/navigation"
77
import { useFeatureFlagEnabled } from "posthog-js/react"
88
import { FeatureFlags } from "@/common/feature_flags"
99

10-
export const ArticleDetailPage = ({ articleId, isId }: { articleId: string, isId: boolean }) => {
10+
export const ArticleDetailPage = ({ articleId }: { articleId: string }) => {
1111
const {
1212
data: article,
1313
isLoading,

0 commit comments

Comments
 (0)