Skip to content

Commit

Permalink
add slug in post
Browse files Browse the repository at this point in the history
  • Loading branch information
RustamovAkrom committed Oct 7, 2024
1 parent 6be8a82 commit 30b916d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 23 deletions.
19 changes: 19 additions & 0 deletions apps/blog/migrations/0005_post_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.1.1 on 2024-10-07 12:30

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0004_alter_user_avatar'),
]

operations = [
migrations.AddField(
model_name='post',
name='slug',
field=models.SlugField(default=1, max_length=255, unique=True),
preserve_default=False,
),
]
24 changes: 14 additions & 10 deletions apps/blog/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
from django.db import models
from django.urls import reverse
from django.utils.text import slugify
from django.contrib.auth.models import AbstractUser
from apps.shared.models import TimestempedAbstractModel


class AbstractBaseModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
abstract = True


class User(AbstractUser, AbstractBaseModel):
class User(AbstractUser, TimestempedAbstractModel):
avatar = models.ImageField(
upload_to="avatars/", null=True, default="avatars/default/logo.png"
)
Expand All @@ -20,9 +15,18 @@ def post_count(self):
return self.posts.count


class Post(AbstractBaseModel):
class Post(TimestempedAbstractModel):
title = models.CharField(max_length=120)
slug = models.SlugField(max_length=255, unique=True, db_index=True)
content = models.TextField()
publisher_at = models.DateField()
is_active = models.BooleanField(default=False)
author = models.ForeignKey("User", models.CASCADE, "posts")

def get_absolute_url(self):
return reverse("post_detail", kwargs={'slug': self.slug})

def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
return super().save(*args, **kwargs)
6 changes: 3 additions & 3 deletions apps/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
path("register/", views.RegisterPageView.as_view(), name="register"),
path("logout/", views.LogoutView.as_view(), name="logout"),
path("login/", views.LoginPageView.as_view(), name="login"),
path("post/delete/<int:id>", views.PostDeleteView.as_view(), name="post_delete"),
path("post/<int:id>", views.PostDetailPageView.as_view(), name="post_detail"),
path("Post/update/<int:id>", views.PostUpdateView.as_view(), name="post_update"),
path("post/delete/<slug:slug>", views.PostDeleteView.as_view(), name="post_delete"),
path("post/<slug:slug>", views.PostDetailPageView.as_view(), name="post_detail"),
path("Post/update/<int:slug>", views.PostUpdateView.as_view(), name="post_update"),
path("post/create/", views.PostFormPageView.as_view(), name="post_form"),
path("post/user/", views.UserPostPageView.as_view(), name="user_posts"),
]
16 changes: 8 additions & 8 deletions apps/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ class UserPostPageView(CustomHtmxMixin, ListView):
class PostUpdateView(CustomHtmxMixin, LoginRequiredMixin, View):
template_name = "blog/post_update.html"

def get(self, request, id):
post = get_object_or_404(Post, id=id)
def get(self, request, slug):
post = get_object_or_404(Post, slug=slug)
form = PostUpdateForm(instance=post)
return render(request, "blog/post_update.html", {"form": form, "post": post})

def post(self, request, id):
post = get_object_or_404(Post, id=id)
def post(self, request, slug):
post = get_object_or_404(Post, slug=slug)
form = PostUpdateForm(request.POST, instance=post)
if form.is_valid():
messages.success(request, "Post succsessfully updated")
Expand All @@ -197,12 +197,12 @@ def post(self, request, id):
class PostDeleteView(CustomHtmxMixin, LoginRequiredMixin, View):
template_name = "blog/post_confirm_delete.html"

def get(self, request, id):
post = get_object_or_404(Post, id=id)
def get(self, request, slug):
post = get_object_or_404(Post, slug=slug)
return render(request, "blog/post_confirm_delete.html", {"post": post})

def post(self, request, id):
post = get_object_or_404(Post, id=id)
def post(self, request, slug):
post = get_object_or_404(Post, slug=slug)
messages.success(request, "post successfully deleted")
post.delete()
return redirect("profile")
11 changes: 10 additions & 1 deletion apps/shared/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
from django.db import models # noqa
from django.db import models


class TimestempedAbstractModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
abstract = True

Binary file modified db.sqlite3
Binary file not shown.
1 change: 0 additions & 1 deletion templates/blog/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.min.css' %}">

Expand Down

0 comments on commit 30b916d

Please sign in to comment.