From c7a804b13ba73f50e5aee652579d4e87adecefdc Mon Sep 17 00:00:00 2001 From: bearthatcares <149488155+bearthatcares@users.noreply.github.com> Date: Fri, 11 Oct 2024 09:04:53 -0400 Subject: [PATCH] Add slug generation, title uniqueness, seo description parameters (#802) * Add slug generation, title uniqueness, seo description parameters * int --- .../0003_evepost_state_alter_evepost_title.py | 31 +++++++++++++++++++ backend/posts/models.py | 21 ++++++++++++- backend/posts/router.py | 25 ++++++++++++--- 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 backend/posts/migrations/0003_evepost_state_alter_evepost_title.py diff --git a/backend/posts/migrations/0003_evepost_state_alter_evepost_title.py b/backend/posts/migrations/0003_evepost_state_alter_evepost_title.py new file mode 100644 index 00000000..c5710ddb --- /dev/null +++ b/backend/posts/migrations/0003_evepost_state_alter_evepost_title.py @@ -0,0 +1,31 @@ +# Generated by Django 5.1.1 on 2024-10-11 12:55 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("posts", "0002_evepost_seo_description"), + ] + + operations = [ + migrations.AddField( + model_name="evepost", + name="state", + field=models.CharField( + choices=[ + ("draft", "Draft"), + ("published", "Published"), + ("trash", "Trash"), + ], + default="draft", + max_length=10, + ), + ), + migrations.AlterField( + model_name="evepost", + name="title", + field=models.CharField(max_length=100, unique=True), + ), + ] diff --git a/backend/posts/models.py b/backend/posts/models.py index 83169ac1..8ac7fb26 100644 --- a/backend/posts/models.py +++ b/backend/posts/models.py @@ -11,7 +11,19 @@ def __str__(self): class EvePost(models.Model): - title = models.CharField(max_length=100) + """ + Model for the blog post + """ + + state_choices = [ + ("draft", "Draft"), + ("published", "Published"), + ("trash", "Trash"), + ] + state = models.CharField( + max_length=10, choices=state_choices, default="draft" + ) + title = models.CharField(max_length=100, unique=True) seo_description = models.CharField(max_length=300) slug = models.SlugField(max_length=100) content = models.TextField() @@ -21,6 +33,13 @@ class EvePost(models.Model): def __str__(self): return str(self.title) + @staticmethod + def generate_slug(title): + """ + Convert the title into a slug + """ + return title.lower().replace(" ", "-") + class EvePostImage(models.Model): post = models.ForeignKey(EvePost, on_delete=models.CASCADE) diff --git a/backend/posts/router.py b/backend/posts/router.py index 08bcde53..ec4c5e9e 100644 --- a/backend/posts/router.py +++ b/backend/posts/router.py @@ -13,6 +13,7 @@ class EvePostListResponse(BaseModel): post_id: int + state: str title: str seo_description: str slug: str @@ -23,6 +24,7 @@ class EvePostListResponse(BaseModel): class EvePostResponse(BaseModel): post_id: int + state: str title: str seo_description: str slug: str @@ -39,7 +41,8 @@ class EveTagesponse(BaseModel): class CreateEvePosRequest(BaseModel): title: str - slug: str + state: str + seo_description: str content: str @@ -58,6 +61,7 @@ def get_posts(request, user_id: int = None, tag_id: int = None): response.append( EvePostListResponse( post_id=post.id, + state=post.state, seo_description=post.seo_description, title=post.title, slug=post.slug, @@ -77,6 +81,7 @@ def get_post(request, post_id: int): return EvePostResponse( post_id=post.id, + state=post.state, seo_description=post.seo_description, title=post.title, slug=post.slug, @@ -89,16 +94,21 @@ def get_post(request, post_id: int): @router.post( "/posts", - response={403: ErrorResponse, 200: EvePostResponse}, + response={403: ErrorResponse, 400: ErrorResponse, 200: EvePostResponse}, auth=AuthBearer(), ) def create_post(request, payload: CreateEvePosRequest): if not request.user.has_perm("posts.add_evepost"): return 403, {"detail": "You do not have permission to create a post."} + if EvePost.objects.filter(title=payload.title).exists(): + return 400, {"detail": "A post with this title already exists."} + post = EvePost.objects.create( title=payload.title, - slug=payload.slug, + state=payload.state, + seo_description=payload.seo_description, + slug=EvePost.generate_slug(payload.title), content=payload.content, user=request.user, ) @@ -115,17 +125,22 @@ def create_post(request, payload: CreateEvePosRequest): @router.put( "/posts/{post_id}", - response={403: ErrorResponse, 200: EvePostResponse}, + response={403: ErrorResponse, 400: ErrorResponse, 200: EvePostResponse}, auth=AuthBearer(), ) def update_post(request, post_id: int, payload: CreateEvePosRequest): if not request.user.has_perm("posts.change_evepost"): return 403, {"detail": "You do not have permission to update a post."} + if EvePost.objects.filter(title=payload.title).exists(): + return 400, {"detail": "A post with this title already exists."} + post = EvePost.objects.get(id=post_id) post.title = payload.title - post.slug = payload.slug post.content = payload.content + post.seo_description = payload.seo_description + post.slug = EvePost.generate_slug(payload.title) + post.state = payload.state post.save() return EvePostResponse(