Skip to content

Commit

Permalink
Add slug generation, title uniqueness, seo description parameters (mi…
Browse files Browse the repository at this point in the history
…nmatarfleet#802)

* Add slug generation, title uniqueness, seo description parameters

* int
  • Loading branch information
bearthatcares authored Oct 11, 2024
1 parent 03e42b2 commit c7a804b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
31 changes: 31 additions & 0 deletions backend/posts/migrations/0003_evepost_state_alter_evepost_title.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
21 changes: 20 additions & 1 deletion backend/posts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
25 changes: 20 additions & 5 deletions backend/posts/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

class EvePostListResponse(BaseModel):
post_id: int
state: str
title: str
seo_description: str
slug: str
Expand All @@ -23,6 +24,7 @@ class EvePostListResponse(BaseModel):

class EvePostResponse(BaseModel):
post_id: int
state: str
title: str
seo_description: str
slug: str
Expand All @@ -39,7 +41,8 @@ class EveTagesponse(BaseModel):

class CreateEvePosRequest(BaseModel):
title: str
slug: str
state: str
seo_description: str
content: str


Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
)
Expand All @@ -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(
Expand Down

0 comments on commit c7a804b

Please sign in to comment.