Guest User

Untitled

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