Skip to content

Commit

Permalink
change html files
Browse files Browse the repository at this point in the history
  • Loading branch information
RustamovAkrom committed Nov 13, 2024
1 parent c174f86 commit 83bb8a5
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 93 deletions.
6 changes: 6 additions & 0 deletions apps/blog/choices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.db.models import TextChoices


class StatusChoice(TextChoices):
DRAFT = 'df', 'Draft'
PUBLISHED = 'pb', 'Published'
46 changes: 15 additions & 31 deletions apps/blog/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,40 @@
}


class PostCreateForm(forms.ModelForm):
class PostCreateUpdateForm(forms.ModelForm):

class Meta:
model = Post
fields = ("title", "content", "is_active")
fields = ("title", "description", "content", "status")

widgets = {
"title": forms.TextInput(
attrs={
"class": "form-control",
"name": "title",
"placeholder": "Enter you`r post title...",
}
),
"content": forms.Textarea(
"description": forms.Textarea(
attrs={
"class": "form-control",
"placeholder": "Enter you`r post content...",
"name": "description",
"placeholder": "Enter you`r post description...",
}
),
"is_active": forms.CheckboxInput(
attrs={"name": "is_active", "class": "form-check-input"}
),
}


class PostUpdateForm(forms.ModelForm):

class Meta:
model = Post
fields = ("title", "content", "is_active")

widgets = {
"title": forms.TextInput(
attrs={
"name": "title",
"class": "form-control",
"placeholder": "Title....",
},
),
"content": forms.Textarea(
attrs={
"name": "content",
"cols": "40",
"rows": "10",
"class": "form-control",
"placeholder": "Content....",
},
"name": "content",
"placeholder": "Enter you`r post content...",
}
),
"is_active": forms.CheckboxInput(
attrs={"name": "is_active", "class": "form-check-input"}
"status": forms.Select(
attrs={
"class": "form-select",
"name": "status",
"placeholder": "Enter you`r post status...",
}
),
}

Expand Down
9 changes: 9 additions & 0 deletions apps/blog/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import models
from .choices import StatusChoice


class PublishedManager(models.Manager):
def get_queryset(self) -> models.QuerySet:
return (
super().get_queryset().filter(status=StatusChoice.PUBLISHED, is_active=True)
)
24 changes: 24 additions & 0 deletions apps/blog/migrations/0002_post_description_post_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.1.1 on 2024-11-13 09:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='post',
name='description',
field=models.CharField(default=1, max_length=300, verbose_name='description'),
preserve_default=False,
),
migrations.AddField(
model_name='post',
name='status',
field=models.CharField(choices=[('df', 'Draft'), ('pb', 'Published')], default='df', max_length=2, verbose_name='status'),
),
]
18 changes: 18 additions & 0 deletions apps/blog/migrations/0003_alter_post_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.1 on 2024-11-13 10:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0002_post_description_post_status'),
]

operations = [
migrations.AlterField(
model_name='post',
name='description',
field=models.CharField(blank=True, max_length=300, null=True, verbose_name='description'),
),
]
18 changes: 18 additions & 0 deletions apps/blog/migrations/0004_alter_post_is_active.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.1 on 2024-11-13 10:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0003_alter_post_description'),
]

operations = [
migrations.AlterField(
model_name='post',
name='is_active',
field=models.BooleanField(default=True, verbose_name='active'),
),
]
15 changes: 14 additions & 1 deletion apps/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,30 @@
from django.utils.text import slugify
from apps.shared.models import TimestempedAbstractModel
from apps.shared.utils import get_random_text
from .managers import PublishedManager
from .choices import StatusChoice


class Post(TimestempedAbstractModel):

title = models.CharField(_("title"), max_length=120, db_index=True)
slug = models.SlugField(_("slug"), max_length=255, unique=True, db_index=True)
status = models.CharField(
_("status"),
max_length=2,
choices=StatusChoice.choices,
default=StatusChoice.DRAFT.value
)
description = models.CharField(_("description"), max_length=300, blank=True, null=True)
content = models.TextField(_("content"))
publisher_at = models.DateField(_("publisher at"))
is_active = models.BooleanField(_("active"), default=False)
is_active = models.BooleanField(_("active"), default=True)
author = models.ForeignKey("users.User", models.CASCADE, "posts", db_index=True)
watching = models.BigIntegerField(_("watching"), default=0)

objects = models.Manager()
published = PublishedManager()

def delete(self, *args, **kwargs):
print(self.post_comments.all().delete())
return super().delete(*args, **kwargs)
Expand Down
27 changes: 14 additions & 13 deletions apps/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

from apps.users.models import User
from .forms import (
PostUpdateForm,
PostCreateForm,
PostCreateUpdateForm,
SettingsUserForm,
SettingsUserProfileForm,
)
Expand Down Expand Up @@ -42,9 +41,9 @@ class HomePageView(TemplateView):

def get(self, request):
if request.user is not None and request.user.is_authenticated:
posts = Post.objects.exclude(author=request.user)
posts = Post.published.exclude(author=request.user)
else:
posts = Post.objects.all()
posts = Post.published.all()

search_query = request.GET.get("search_query", None)
page = request.GET.get("page", 1)
Expand Down Expand Up @@ -74,6 +73,7 @@ class PostDetailPageView(View):

def get(self, request, slug):
post = get_object_or_404(Post, slug=slug)

post_comments = post.post_comments.all().order_by("-created_at")
post.watching += 1
post.save()
Expand All @@ -88,17 +88,18 @@ class PostCreatePageView(LoginRequiredMixin, TemplateView):
template_name = "blog/post_create.html"

def get(self, request):
return render(request, "blog/post_create.html", {"form": PostCreateForm()})
return render(request, "blog/post_create.html", {"form": PostCreateUpdateForm()})

def post(self, request):
form = PostCreateForm(request.POST)
form = PostCreateUpdateForm(request.POST)

if form.is_valid():
cd = form.cleaned_data
post = Post.objects.create(
title=cd.get("title"),
status=cd.get("status"),
description=cd.get("description"),
content=cd.get("content"),
is_active=cd.get("is_active"),
author=request.user,
publisher_at=datetime.datetime.now().strftime("%Y-%m-%d"),
)
Expand All @@ -122,7 +123,7 @@ def get(self, request):
search_query_for_user_posts = request.GET.get(
"search_query_for_user_posts", None
)
posts = Post.objects.filter(author=request.user)
posts = Post.objects.filter(author=request.user, is_active=True)

if search_query_for_user_posts is not None:
posts = get_search_model_queryset(posts, search_query_for_user_posts)
Expand All @@ -134,13 +135,14 @@ class PostUpdateView(LoginRequiredMixin, View):
template_name = "blog/post_update.html"

def get(self, request, slug):
post = get_object_or_404(Post, slug=slug)
form = PostUpdateForm(instance=post)
post = get_object_or_404(Post, slug=slug, is_active=True)

form = PostCreateUpdateForm(instance=post)
return render(request, "blog/post_update.html", {"form": form, "post": post})

def post(self, request, slug):
post = get_object_or_404(Post, slug=slug)
form = PostUpdateForm(request.POST, instance=post)
form = PostCreateUpdateForm(request.POST, instance=post)
if form.is_valid():
form.save()
messages.success(request, "Post succsessfully updated")
Expand All @@ -156,8 +158,8 @@ class PostDeletePageView(LoginRequiredMixin, DeleteView):

def post(self, request, slug):
post = get_object_or_404(Post, slug=slug)
messages.success(request, "post successfully deleted")
post.delete()
messages.success(request, "post successfully deleted")
return redirect("blog:user_posts")


Expand Down Expand Up @@ -214,7 +216,6 @@ def post_message(request, slug):
return redirect(reverse("users:login"))

post_message_input = request.GET.get("post_message_input", None)
print(post_message_input)

if post_message_input is not None:
set_post_comment(request.user, slug, post_message_input)
Expand Down
2 changes: 1 addition & 1 deletion apps/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class UserProfilePageView(View):

def get(self, request, username):
user = get_object_or_404(User, username=username)
posts = Post.objects.filter(author=user, is_active=True).all().order_by("id")
posts = Post.published.filter(author=user).all().order_by("-created_at")

search_query = request.GET.get("search_query_for_user_profile", None)

Expand Down
7 changes: 6 additions & 1 deletion templates/blog/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ <h6>{{ massage }}</h6>
</div>
<h3 class="mb-0">{{ post.title }}</h3>
<div class="mb-1 text-muted">{{ post.publisher_at }}</div>
<p class="card-text mb-auto">{{ post.content|truncatewords_html:30 }}</p>
<p class="card-text mb-auto">{{ post.description|truncatewords_html:30 }}</p>
<a href="{{ post.get_absolute_url }}" class="stretched-link">Continue reading</a>
<div class="d-flex">
<strong class="d-inline-block mb-2 col text-primary"><i class="bi bi-eye">{{ post.watching }}</i></strong>
<strong class="d-inline-block mb-2 col text-primary"><i class="bi bi-star">{{ post.like_count }}</i></strong>
<strong class="d-inline-block mb-2 col text-primary"><i class="bi bi-chat">{{ post.comment_count }}</i></strong>
</div>
</div>
</div>
</div>
Expand Down
70 changes: 38 additions & 32 deletions templates/blog/post_create.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@
{% block title %} New post {% endblock %}

{% block content %}
<main role="main" class="container">
<div class="row">
<div class="col-md-8">
<div class="content-section">
<form method="post">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Blog Post</legend>
<!-- {{ form.as_p }} -->
<div class="form-group">
<label for="title">Title</label>
{{ form.title }}
</div>

<div class="form-group">
<label for="content">Content</label>
{{ form.content }}
</div>
<div>
{{ form.is_active }}
Is active
</div>
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Post</button>
</div>
</form>
</div>
</div>
{% include 'components/latest_posts.html' %}
</div>
</main>
<main role="main" class="container">
<div class="row">
<div class="col-md-8">
<div class="content-section">
<form method="post">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Blog Post</legend>
<!-- {{ form.as_p }} -->

<div class="form-group">
<label for="title">Title</label>
{{ form.title }}
</div>

<div class="form-group">
<label for="description">Description</label>
{{ form.description }}
</div>

<div class="form-group">
<label for="content">Content</label>
{{ form.content }}
</div>
<div class="form-group">
<label for="status">Status</label>
{{ form.status }}
</div>
</fieldset>
<div class="form-group col-md-2 mt-3">
<button class="btn btn-outline-info" type="submit">Create</button>
</div>
</form>
</div>
</div>
{% include 'components/latest_posts.html' %}
</div>
</main>
{% endblock %}
6 changes: 5 additions & 1 deletion templates/blog/post_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
<article class="blog-post">
<h2 class="blog-post-title">{{ post.title }}</h2>
<p class="blog-post-meta">{{ post.publisher_at }}by <a href="{% url 'users:user_profile' post.author.username %}">{{ post.author }}</a></p>


<div class="text">
<p><small>{{ post.description }}</small></p>
</div>
<!-- Content -->
<p>{{ post.content|markdown|safe }}</p>

</article>
Expand Down
Loading

0 comments on commit 83bb8a5

Please sign in to comment.