Skip to content

Commit c9c0b07

Browse files
committedFeb 2, 2021
added active field to post model and actions to activate posts in admin file for post.
1 parent fae624e commit c9c0b07

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed
 

‎blog/admin.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ class CustomUserAdmin(admin.ModelAdmin):
5050
class PostAdmin(admin.ModelAdmin):
5151
fieldsets = [
5252
('Post', {'fields' : ('datetime', 'category','author', 'title',
53-
'text', 'image', 'tag', 'show'),
53+
'text', 'image', 'tag', 'show', 'active'),
5454
}),
5555
]
5656
# fields = ('category','author', 'title', 'text', 'image', 'tag', 'datetime', 'show')
5757

5858
inlines = (CommentInline, LikeInline, TagPostInline)
5959
list_display = ('title', 'author', 'category', 'get_tags',
60-
'datetime', 'show', 'get_count_likes',
60+
'datetime', 'show', 'active', 'get_count_likes',
6161
'get_count_dislikes')
62-
list_filter = ('author', 'category', 'show', 'tag', 'datetime')
62+
list_filter = ('author', 'category', 'show','active', 'tag', 'datetime')
6363
search_fields = ('author', 'tag', 'category', 'title')
6464

6565
def show_posts(self, request, queryset):
@@ -73,7 +73,20 @@ def dont_show_posts(self, request, queryset):
7373
self.message_user(request, f'{updated} پست نمایش داده نمی شود.', messages.ERROR)
7474

7575
dont_show_posts.short_description = 'عدم نمایش پست ها'
76-
actions = (show_posts, dont_show_posts)
76+
77+
def active_posts(self, request, queryset):
78+
updated = queryset.update(active=True)
79+
self.message_user(request, f'{updated} پست فعال شد.', messages.SUCCESS)
80+
81+
active_posts.short_description = 'فعال کردن پست ها'
82+
83+
def deactive_posts(self, request, queryset):
84+
updated = queryset.update(active=False)
85+
self.message_user(request, f'{updated} پست غیرفعال شدند.', messages.ERROR)
86+
87+
deactive_posts.short_description = 'غیرفعال کردن پست ها'
88+
89+
actions = (show_posts, dont_show_posts, active_posts, deactive_posts)
7790

7891

7992
@admin.register(Comment)

‎blog/migrations/0008_post_active.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 3.1.4 on 2021-02-02 13:30
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('blog', '0007_auto_20210202_0411'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='post',
15+
name='active',
16+
field=models.BooleanField(default=True, verbose_name='فعال'),
17+
),
18+
]

‎blog/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Post(models.Model):
7272
tag = models.ManyToManyField(Tag, null=True, blank=True)
7373
category = models.ForeignKey(Category, on_delete=models.CASCADE)
7474
datetime = models.DateTimeField('زمان و تاریخ پست', null=True, blank=True)
75-
# active = models.BooleanField('فعال', default=False)
75+
active = models.BooleanField('فعال', default=True)
7676

7777
def get_tags(self):
7878
return ", ".join([tag.name for tag in self.tag.all()])

0 commit comments

Comments
 (0)
Please sign in to comment.