Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions blog/migrations/0019_blog_updated_at_alter_blog_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2 on 2026-04-30 07:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0018_reply'),
]

operations = [
migrations.AddField(
model_name='blog',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='blog',
name='status',
field=models.CharField(choices=[('draft', 'Draft'), ('published', 'Published')], default='draft', max_length=20),
),
]
9 changes: 5 additions & 4 deletions blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def __str__(self):

# Blog model
class Blog(models.Model):
status = (
('active','active'),
('pending','pending')
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)

title = models.CharField(max_length=200, null=True)
Expand All @@ -38,13 +38,14 @@ class Blog(models.Model):
#catagories = models.ManyToManyField(Catagory)
catagories = models.ForeignKey(Catagory,on_delete=models.DO_NOTHING, null=True)
tags = models.ManyToManyField(Tag, blank=True)
status = models.CharField(max_length=20, choices=status, default='pending')
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft')
#show_hide = models.CharField(max_length=5,choices=visibility, default='show')
author = models.ForeignKey(Author, on_delete=models.CASCADE)
featured = models.BooleanField(default=False)
visit_count = models.IntegerField(default=0)
visible = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
verbose_name_plural = 'Blog'
Expand Down
29 changes: 19 additions & 10 deletions blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ def get(self,request,*args,**kwargs):

# }
'''
featured_obj = Blog.objects.all().filter(status='active', visible=True, featured=True).order_by('catagories','-created_at')[:5]
post_obj = Blog.objects.all().filter(status='active', visible=True).order_by('catagories','-created_at')
featured_obj = Blog.objects.all().filter(status='published', visible=True, featured=True).order_by('catagories','-created_at')[:5]
featured_list = list(featured_obj)
post_obj = Blog.objects.all().filter(status='published', visible=True).order_by('catagories','-created_at')
# As per Templates Views
first_post = featured_obj.first()
s_post = featured_obj[1]
last_post = featured_obj[2:]
first_post = featured_list[0] if len(featured_list) > 0 else None
s_post = featured_list[1] if len(featured_list) > 1 else None
last_post = featured_list[2:] if len(featured_list) > 2 else []
context={
'post':post_obj,
'f_post':featured_obj,
Expand All @@ -58,9 +59,17 @@ def get(self,request,*args,**kwargs):
class SingleBlogView(View):
def get(self,request,id,*args,**kwargs):
post_obj = get_object_or_404(Blog, id=id)

# 检查文章状态,如果是草稿,只有作者才能查看
if post_obj.status == 'draft':
if not request.user.is_authenticated or request.user.author != post_obj.author:
# 非作者用户尝试访问草稿,显示404或重定向
messages.warning(request, 'This post is not available')
return redirect('home')

post_obj.visit_count = post_obj.visit_count + 1
post_obj.save()
releted_post = Blog.objects.filter(author=post_obj.author).exclude(id=id).order_by('-id')[:4]
releted_post = Blog.objects.filter(author=post_obj.author, status='published', visible=True).exclude(id=id).order_by('-id')[:4]
# As per templates views
first_post = releted_post.first()
last_post = releted_post[1:]
Expand All @@ -79,10 +88,10 @@ def get(self,request,slug,*args,**kwargs):
catagory_obj = get_object_or_404(Catagory, slug=slug)
#post = catagory_obj.blog_set.all().order_by('-id')
post = Blog.objects.filter(catagories= catagory_obj,\
status='active',visible=True)\
status='published',visible=True)\
.order_by('-created_at')
popular = Blog.objects.filter(catagories= catagory_obj,\
status='active',visible=True)\
status='published',visible=True)\
.annotate(post_count=Count('visit_count'))\
.order_by('-visit_count')
# as Per templates views
Expand All @@ -104,7 +113,7 @@ def get(self,request,slug,*args,**kwargs):
class TagView(View):
def get(self,request,id,*args,**kwargs):
tag_obj = get_object_or_404(Tag, id=id)
post = tag_obj.blog_set.all().order_by('-id')
post = tag_obj.blog_set.filter(status='published', visible=True).order_by('-id')
tag_count = post.count()
context={
'tag':tag_obj,
Expand All @@ -131,7 +140,7 @@ def post(self, request,*args,**kwargs):
class SearchView(View):
def get(self,request,*args,**kwargs):
search = request.GET['q']
post = Blog.objects.filter(status='active',visible=True)
post = Blog.objects.filter(status='published', visible=True)
if len(search) > 100:
posts = post.none()
else:
Expand Down
2 changes: 2 additions & 0 deletions dashboard/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
path('post_listing_pending/', views.PostListingPending.as_view(), name='post_listing_pending'),
path('visible/post/<str:id>', views.VisiblePost.as_view(), name='visible'),
path('hidden/post/<str:id>', views.HidePost.as_view(),name='hidden'),
path('publish/post/<str:id>', views.PublishPost.as_view(), name='publish'),
path('draft/post/<str:id>', views.MakeDraft.as_view(), name='draft'),
# Author
path('profile/',views.AuthorProfile.as_view(), name='profile'),
path('profile/edit/', views.EditAuthor.as_view(), name="edit"),
Expand Down
54 changes: 43 additions & 11 deletions dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ def get(self,request,*args,**kwargs):
user = request.user
post = user.author.blog_set.all()
post_count = post.count()
post_active = user.author.blog_set.filter(status='active')
post_active_count = post_active.count()
post_pending = user.author.blog_set.filter(status='pending')
post_pending_count = post_pending.count()
post_published = user.author.blog_set.filter(status='published')
post_published_count = post_published.count()
post_draft = user.author.blog_set.filter(status='draft')
post_draft_count = post_draft.count()
# showing the sum of visit count of spacific users
post_visit_count = post.aggregate(Sum('visit_count'))['visit_count__sum']
context= {
'user':user,
'post':post,
'post_count':post_count,
'post_active':post_active,
'post_pending':post_pending,
'post_active_count':post_active_count,
'post_pending_count':post_pending_count,
'post_published':post_published,
'post_draft':post_draft,
'post_published_count':post_published_count,
'post_draft_count':post_draft_count,
'count':post_visit_count

}
Expand Down Expand Up @@ -157,7 +157,7 @@ def dispatch(self,request,*args,**kwargs):

def get(self,request,*args,**kwargs):
user = request.user
post_active = user.author.blog_set.filter(status='active').order_by('-id')
post_active = user.author.blog_set.filter(status='published').order_by('-id')
context={
'post_active':post_active
}
Expand All @@ -171,7 +171,7 @@ def dispatch(self,request,*args,**kwargs):

def get(self,request,*args,**kwargs):
user = request.user
post_pending = user.author.blog_set.filter(status='Pending').order_by('-id')
post_pending = user.author.blog_set.filter(status='draft').order_by('-id')
context={
'post_pending':post_pending
}
Expand Down Expand Up @@ -310,8 +310,9 @@ def post(self,request):
#tag_obj = Tag.objects.get(name=tag)
category = request.POST.get('category')
cat_obj = Catagory.objects.get(name=category)
status = request.POST.get('status', 'draft')

post_obj = Blog(author=author,title=title, detail=detail,image=image,catagories=cat_obj)
post_obj = Blog(author=author,title=title, detail=detail,image=image,catagories=cat_obj, status=status)
post_obj.save(post_obj)
messages.success(request,'created Post Successfully')
return redirect('all_post')
Expand Down Expand Up @@ -367,6 +368,8 @@ def post(self,request,id):
obj.image = request.FILES.get('image')
category = request.POST.get('category')
obj.cat_obj = Catagory.objects.get(name=category)
status = request.POST.get('status', obj.status)
obj.status = status
obj.save()
messages.success(request,'Post has been Updated')
return redirect('all_post')
Expand Down Expand Up @@ -400,6 +403,35 @@ def get(self,request,id):
# Redirect To the Same Page
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

# Publish Post
class PublishPost(View):
@method_decorator(login_required(login_url='login'))
def dispatch(self,request,*args,**kwargs):
return super().dispatch(request,*args,**kwargs)

def get(self,request,id):
obj = Blog.objects.get(id=id)
obj.status = 'published'
obj.save()
messages.success(request,'Post has been Published')
# Redirect To the Same Page
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

# Make Draft
class MakeDraft(View):
@method_decorator(login_required(login_url='login'))
def dispatch(self,request,*args,**kwargs):
return super().dispatch(request,*args,**kwargs)

def get(self,request,id):
obj = Blog.objects.get(id=id)
obj.status = 'draft'
obj.save()
messages.success(request,'Post is now a Draft')

# Redirect To the Same Page
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))


# Delete Posts
class DeletePost(View):
Expand Down
Binary file modified db.sqlite3
Binary file not shown.
1 change: 0 additions & 1 deletion src/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

from django.contrib import admin
from django.urls import path, include
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
from dashboard import views
Expand Down
13 changes: 12 additions & 1 deletion templates/dashboard/post/all_post.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,19 @@
<td><a href="{% url 'post' p.id %}">{{ p.title }}</a> </td>
<td> {{ p.catagories }} </td>
<td> {{ p.visit_count }} </td>
<td> {{ p.status }} </td>
<td>
{% if p.status == 'published' %}
<span class="badge badge-success">Published</span>
{% else %}
<span class="badge badge-warning">Draft</span>
{% endif %}
</td>
<td><a href="{% url 'edit_post' p.id %}"><i class="far fa-edit"></i></a>
{% if p.status == 'draft' %}
<a href="{% url 'publish' p.id %}" title="Publish"><i class="fas fa-upload"></i></a>
{% else %}
<a href="{% url 'draft' p.id %}" title="Make Draft"><i class="fas fa-file-alt"></i></a>
{% endif %}
{% if p.visible == False %}
<a href="{% url 'visible' p.id %}"><i class="fas fa-eye"></i></a>
{% else %}
Expand Down
7 changes: 7 additions & 0 deletions templates/dashboard/post/create_post.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ <h6 class="m-0 font-weight-bold text-primary">Add Blog Post</h6>
<option>{{ cat }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="status">Status</label>
<select class="form-control" id="status" name="status">
<option value="draft" selected>Draft</option>
<option value="published">Published</option>
</select>
</div>
<input type="submit" value="submit">
</form>
Expand Down
7 changes: 7 additions & 0 deletions templates/dashboard/post/edit_post.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ <h6 class="m-0 font-weight-bold text-primary">Edit : {{obj.title}}</h6>
{% endfor %}
</select>
</div>
<div class="form-group col">
<label for="status">Status</label>
<select class="form-control" id="status" name="status">
<option value="draft" {% if obj.status == 'draft' %}selected{% endif %}>Draft</option>
<option value="published" {% if obj.status == 'published' %}selected{% endif %}>Published</option>
</select>
</div>
</div>
<input type="submit" value="submit" class="btn btn-primary">
</form>
Expand Down
6 changes: 4 additions & 2 deletions templates/home/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block title %} MultiBlogs {% endblock %}
{% block body %}


{% if first %}
<div class="container">
<div class="jumbotron jumbotron-fluid mb-3 pt-0 pb-0 bg-lightblue position-relative">
<div class="pl-4 pr-0 h-100 tofront">
Expand All @@ -21,9 +21,10 @@ <h1 class="secondfont mb-3 font-weight-bold">{{ first.title }}</h1>
</div>
</div>
</div>
{% endif %}


{% if s_post %}
<div class="container pt-4 pb-4 " style="margin-bottom: 70px;">
<div class="row">
<div class="col-lg-6">
Expand Down Expand Up @@ -77,6 +78,7 @@ <h2 class="mb-2 h6 font-weight-bold">

</div>
</div>
{% endif %}



Expand Down