Guest User

Untitled

a guest
Jun 1st, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. class IndexView(View):
  2.  
  3.     def get(self, request, *args, **kwargs):
  4.         post_form = AddPostForm()
  5.         comment_form = AddCommentForm()
  6.         if request.user.is_authenticated:
  7.             logged_user = CustomUser.objects.get(id=request.user.id)
  8.             blocked_users = logged_user.blocked.all()
  9.             posts = Post.objects.exclude(author__in=blocked_users)
  10.             print(blocked_users)
  11.             posts = posts.order_by('-pub_date')
  12.         else:
  13.             posts = Post.objects.all()
  14.             posts = posts.order_by('-pub_date')
  15.  
  16.         comments = Comment.objects.all()
  17.         comments = comments.order_by("-pub_date")
  18.  
  19.         return render(request, 'mikroblog/index.html', {'posts': posts, 'comments': comments,
  20.                                                     'post_form': post_form, 'comment_form': comment_form})
  21.  
  22.     def post(self, request, *args, **kwargs):
  23.         if request.method == 'POST':
  24.             post_form = AddPostForm(request.POST)
  25.             comment_form = AddCommentForm(request.POST)
  26.             # FORMULARZ DODAWANIA POSTU
  27.             if post_form.is_valid():
  28.                 new_post = post_form.save(commit=False)
  29.                 new_post.author = request.user
  30.                 new_post.tags = ''
  31.                 content = ''
  32.                 # CHECK TAGS
  33.                 for word in new_post.content_post.split():
  34.                     if '#' in word:
  35.                         new_post.tags += f"{word} "
  36.                         content += f"{word} "
  37.                     else:
  38.                         content += f"{word} "
  39.                 # CHECK ADDED NOTIFICATION
  40.  
  41.                 new_post.content_post = content
  42.                 new_post.save()
  43.  
  44.                 for word in content.split():
  45.                     if '@' in word:
  46.                         print(word)
  47.                         user_to_notificate = CustomUser.objects.get(username=word[1:])
  48.  
  49.                         new_notification = TalkAbout()
  50.                         new_notification.where = new_post
  51.                         new_notification._from = new_post.author
  52.                         new_notification.to = user_to_notificate
  53.                         new_notification.sended = False
  54.                         print(new_notification)
  55.                         new_notification.save()
  56.  
  57.                 post_form = AddPostForm()
  58.  
  59.             # FORMULARZ DODAWANIA KOMENTARZA
  60.             if comment_form.is_valid():
  61.                 new_comment = comment_form.save(commit=False)
  62.                 new_comment.author = request.user
  63.                 new_comment.save()
  64.                 comment_form = AddCommentForm()
  65.  
  66.             return HttpResponseRedirect(reverse('index'))
Add Comment
Please, Sign In to add comment