def handle(self, *args, **options): SearchIndex.objects.all().delete() indexed_comment_count = 0 indexed_post_count = 0 indexed_user_count = 0 for chunk in chunked_queryset( Comment.visible_objects().filter(is_deleted=False, post__is_visible=True) ): for comment in chunk: self.stdout.write(f"Indexing comment: {comment.id}") SearchIndex.update_comment_index(comment) indexed_comment_count += 1 for chunk in chunked_queryset( Post.visible_objects().filter(is_shadow_banned=False) ): for post in chunk: self.stdout.write(f"Indexing post: {post.slug}") SearchIndex.update_post_index(post) indexed_post_count += 1 for chunk in chunked_queryset( User.objects.filter(moderation_status=User.MODERATION_STATUS_APPROVED) ): for user in chunk: self.stdout.write(f"Indexing user: {user.slug}") SearchIndex.update_user_index(user) SearchIndex.update_user_tags(user) indexed_user_count += 1 self.stdout.write( f"Done 🥙 " f"Comments: {indexed_comment_count} Posts: {indexed_post_count} Users: {indexed_user_count}" )
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 edit_comment(request, comment_id): comment = get_object_or_404(Comment, id=comment_id) if not request.me.is_moderator: if comment.author != request.me: raise AccessDenied() if comment.is_deleted: raise AccessDenied( title="Нельзя редактировать удаленный комментарий", message="Сначала тот, кто его удалил, должен его восстановить" ) if not comment.is_editable: hours = int(settings.COMMENT_EDITABLE_TIMEDELTA.total_seconds() // 3600) raise AccessDenied( title="Время вышло", message=f"Комментарий можно редактировать только в течение {hours} часов после создания" ) if not comment.post.is_visible or not comment.post.is_commentable: raise AccessDenied(title="Комментарии к этому посту закрыты") post = comment.post if request.method == "POST": form = CommentForm(request.POST, instance=comment) if form.is_valid(): comment = form.save(commit=False) comment.is_deleted = False comment.html = None # flush cache comment.ipaddress = parse_ip_address(request) comment.useragent = parse_useragent(request) comment.save() SearchIndex.update_comment_index(comment) return redirect("show_comment", post.slug, comment.id) else: form = CommentForm(instance=comment) return render(request, "comments/edit.html", { "comment": comment, "post": post, "form": form })
def handle(self, *args, **options): SearchIndex.objects.all().delete() for comment in Comment.visible_objects().filter(is_deleted=False, post__is_visible=True): self.stdout.write(f"Indexing comment: {comment.id}") SearchIndex.update_comment_index(comment) for post in Post.visible_objects().filter(is_shadow_banned=False): self.stdout.write(f"Indexing post: {post.slug}") SearchIndex.update_post_index(post) for user in User.objects.filter(is_profile_complete=True, is_profile_reviewed=True, is_profile_rejected=False): self.stdout.write(f"Indexing user: {user.slug}") SearchIndex.update_user_index(user) self.stdout.write("Done 🥙")
def handle(self, *args, **options): SearchIndex.objects.all().delete() for comment in Comment.visible_objects().filter(is_deleted=False, post__is_visible=True): self.stdout.write(f"Indexing comment: {comment.id}") SearchIndex.update_comment_index(comment) for post in Post.visible_objects().filter(is_shadow_banned=False): self.stdout.write(f"Indexing post: {post.slug}") SearchIndex.update_post_index(post) for user in User.objects.filter( moderation_status=User.MODERATION_STATUS_APPROVED): self.stdout.write(f"Indexing user: {user.slug}") SearchIndex.update_user_index(user) SearchIndex.update_user_tags(user) self.stdout.write("Done 🥙")
def edit_comment(request, comment_id): comment = get_object_or_404(Comment, id=comment_id) if not request.me.is_moderator: if comment.author != request.me: raise AccessDenied() if not comment.is_editable: raise AccessDenied( title="Время вышло", message= "Комментарий можно редактировать только в первые 3 часа после создания" ) if not comment.post.is_visible or not comment.post.is_commentable: raise AccessDenied(title="Комментарии к этому посту закрыты") post = comment.post if request.method == "POST": form = CommentForm(request.POST, instance=comment) if form.is_valid(): comment = form.save(commit=False) comment.is_deleted = False comment.html = None # flush cache comment.ipaddress = parse_ip_address(request) comment.useragent = parse_useragent(request) comment.save() SearchIndex.update_comment_index(comment) return redirect("show_comment", post.slug, comment.id) else: form = CommentForm(instance=comment) return render(request, "comments/edit.html", { "comment": comment, "post": post, "form": form })