def show_post(request, post_type, post_slug): post = get_object_or_404(Post, slug=post_slug) # post_type can be changed by moderator if post.type != post_type: return redirect("show_post", post.type, post.slug) # don't show private posts into public if not post.is_public: access_denied = check_user_permissions(request, post=post) if access_denied: return access_denied # drafts are visible only to authors and moderators if not post.is_visible: if not request.me or (request.me != post.author and not request.me.is_moderator): raise Http404() # record a new view last_view_at = None if request.me: request.me.update_last_activity() post_view, last_view_at = PostView.register_view( request=request, user=request.me, post=post, ) else: PostView.register_anonymous_view( request=request, post=post, ) return render_post(request, post, {"post_last_view_at": last_view_at})
def create_comment(request, post_slug): post = get_object_or_404(Post, slug=post_slug) if not post.is_commentable and not request.me.is_moderator: raise AccessDenied(title="Комментарии к этому посту закрыты") if request.POST.get("reply_to_id"): ProperCommentForm = ReplyForm elif post.type == Post.TYPE_BATTLE: ProperCommentForm = BattleCommentForm else: ProperCommentForm = CommentForm if request.method == "POST": form = ProperCommentForm(request.POST) if form.is_valid(): is_ok = Comment.check_rate_limits(request.me) if not is_ok: raise RateLimitException( title="🙅♂️ Вы комментируете слишком часто", message= "Подождите немного, вы достигли нашего лимита на комментарии в день. " "Можете написать нам в саппорт, пожаловаться об этом.") comment = form.save(commit=False) comment.post = post if not comment.author: comment.author = request.me comment.ipaddress = parse_ip_address(request) comment.useragent = parse_useragent(request) comment.save() # update the shitload of counters :) request.me.update_last_activity() Comment.update_post_counters(post) PostView.increment_unread_comments(comment) PostView.register_view( request=request, user=request.me, post=post, ) SearchIndex.update_comment_index(comment) LinkedPost.create_links_from_text(post, comment.text) return redirect("show_comment", post.slug, comment.id) else: log.error(f"Comment form error: {form.errors}") return render( request, "error.html", { "title": "Какая-то ошибка при публикации комментария 🤷♂️", "message": f"Мы уже получили оповещение и скоро пофиксим. " f"Ваш коммент мы сохранили чтобы вы могли скопировать его и запостить еще раз:", "data": form.cleaned_data.get("text") }) raise Http404()
def show_post(request, post_type, post_slug): post = get_object_or_404(Post, slug=post_slug) # post_type can be changed by moderator if post.type != post_type: return redirect("show_post", post.type, post.slug) # don't show private posts into public if not post.is_public: access_denied = check_user_permissions(request, post=post) if access_denied: return access_denied # drafts are visible only to authors and moderators if not post.is_visible: if not request.me or (request.me != post.author and not request.me.is_moderator): raise Http404() # record a new view last_view_at = None if request.me: request.me.update_last_activity() post_view, last_view_at = PostView.register_view( request=request, user=request.me, post=post, ) else: PostView.register_anonymous_view( request=request, post=post, ) # find linked posts and sort them by upvotes linked_posts = sorted( { link.post_to if link.post_to != post else link.post_from for link in LinkedPost.links_for_post(post)[:50] }, key=lambda p: p.upvotes, reverse=True) # force cleanup deleted/hidden posts from linked linked_posts = [p for p in linked_posts if p.is_visible] return render_post(request, post, { "post_last_view_at": last_view_at, "linked_posts": linked_posts, })
def delete_comment(request, comment_id): comment = get_object_or_404(Comment, id=comment_id) if not request.me.is_moderator: # only comment author, post author or moderator can delete comments if comment.author != request.me and request.me != comment.post.author: raise AccessDenied( title="Нельзя!", message="Только автор комментария, поста или модератор может удалить комментарий" ) if not comment.is_deletable_by(request.me): raise AccessDenied( title="Время вышло", message="Комментарий можно удалять только в первые дни после создания. " "Потом только автор или модератор может это сделать." ) if not comment.post.is_visible: raise AccessDenied( title="Пост скрыт!", message="Нельзя удалять комментарии к скрытому посту" ) if not comment.is_deleted: # delete comment comment.delete(deleted_by=request.me) PostView.decrement_unread_comments(comment) else: # undelete comment if comment.deleted_by == request.me.id or request.me.is_moderator: comment.undelete() PostView.increment_unread_comments(comment) else: raise AccessDenied( title="Нельзя!", message="Только тот, кто удалил комментарий, может его восстановить" ) Comment.update_post_counters(comment.post) return redirect("show_comment", comment.post.slug, comment.id)