99
1010from apps .users .models import User
1111from .forms import (
12- PostUpdateForm ,
13- PostCreateForm ,
12+ PostCreateUpdateForm ,
1413 SettingsUserForm ,
1514 SettingsUserProfileForm ,
1615)
@@ -42,9 +41,9 @@ class HomePageView(TemplateView):
4241
4342 def get (self , request ):
4443 if request .user is not None and request .user .is_authenticated :
45- posts = Post .objects .exclude (author = request .user )
44+ posts = Post .published .exclude (author = request .user )
4645 else :
47- posts = Post .objects .all ()
46+ posts = Post .published .all ()
4847
4948 search_query = request .GET .get ("search_query" , None )
5049 page = request .GET .get ("page" , 1 )
@@ -74,6 +73,7 @@ class PostDetailPageView(View):
7473
7574 def get (self , request , slug ):
7675 post = get_object_or_404 (Post , slug = slug )
76+
7777 post_comments = post .post_comments .all ().order_by ("-created_at" )
7878 post .watching += 1
7979 post .save ()
@@ -88,17 +88,18 @@ class PostCreatePageView(LoginRequiredMixin, TemplateView):
8888 template_name = "blog/post_create.html"
8989
9090 def get (self , request ):
91- return render (request , "blog/post_create.html" , {"form" : PostCreateForm ()})
91+ return render (request , "blog/post_create.html" , {"form" : PostCreateUpdateForm ()})
9292
9393 def post (self , request ):
94- form = PostCreateForm (request .POST )
94+ form = PostCreateUpdateForm (request .POST )
9595
9696 if form .is_valid ():
9797 cd = form .cleaned_data
9898 post = Post .objects .create (
9999 title = cd .get ("title" ),
100+ status = cd .get ("status" ),
101+ description = cd .get ("description" ),
100102 content = cd .get ("content" ),
101- is_active = cd .get ("is_active" ),
102103 author = request .user ,
103104 publisher_at = datetime .datetime .now ().strftime ("%Y-%m-%d" ),
104105 )
@@ -122,7 +123,7 @@ def get(self, request):
122123 search_query_for_user_posts = request .GET .get (
123124 "search_query_for_user_posts" , None
124125 )
125- posts = Post .objects .filter (author = request .user )
126+ posts = Post .objects .filter (author = request .user , is_active = True )
126127
127128 if search_query_for_user_posts is not None :
128129 posts = get_search_model_queryset (posts , search_query_for_user_posts )
@@ -134,13 +135,14 @@ class PostUpdateView(LoginRequiredMixin, View):
134135 template_name = "blog/post_update.html"
135136
136137 def get (self , request , slug ):
137- post = get_object_or_404 (Post , slug = slug )
138- form = PostUpdateForm (instance = post )
138+ post = get_object_or_404 (Post , slug = slug , is_active = True )
139+
140+ form = PostCreateUpdateForm (instance = post )
139141 return render (request , "blog/post_update.html" , {"form" : form , "post" : post })
140142
141143 def post (self , request , slug ):
142144 post = get_object_or_404 (Post , slug = slug )
143- form = PostUpdateForm (request .POST , instance = post )
145+ form = PostCreateUpdateForm (request .POST , instance = post )
144146 if form .is_valid ():
145147 form .save ()
146148 messages .success (request , "Post succsessfully updated" )
@@ -156,8 +158,8 @@ class PostDeletePageView(LoginRequiredMixin, DeleteView):
156158
157159 def post (self , request , slug ):
158160 post = get_object_or_404 (Post , slug = slug )
159- messages .success (request , "post successfully deleted" )
160161 post .delete ()
162+ messages .success (request , "post successfully deleted" )
161163 return redirect ("blog:user_posts" )
162164
163165
@@ -214,7 +216,6 @@ def post_message(request, slug):
214216 return redirect (reverse ("users:login" ))
215217
216218 post_message_input = request .GET .get ("post_message_input" , None )
217- print (post_message_input )
218219
219220 if post_message_input is not None :
220221 set_post_comment (request .user , slug , post_message_input )
0 commit comments