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="Этот комментарий больше нельзя редактировать") 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() 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 register_view(cls, request, user, post): post_view, is_view_created = PostView.objects.get_or_create( user=user, post=post, defaults=dict( ipaddress=parse_ip_address(request), useragent=parse_useragent(request), )) # save last view timestamp to highlight comments last_view_at = post_view.last_view_at # increment view counter for new views or for re-opens after cooldown period if is_view_created or post_view.registered_view_at < datetime.utcnow( ) - settings.POST_VIEW_COOLDOWN_PERIOD: post_view.registered_view_at = datetime.utcnow() post.increment_view_count() # reset counters and store last view if not is_view_created: post_view.unread_comments = 0 post_view.last_view_at = datetime.utcnow() post_view.save() return post_view, 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 register_anonymous_view(cls, request, post): is_view_created = False post_view = PostView.objects.filter( post=post, ipaddress=parse_ip_address(request), ).first() if not post_view: post_view = PostView.objects.create( post=post, ipaddress=parse_ip_address(request), ) is_view_created = True # increment view counter for new views or for re-opens after cooldown period if is_view_created or post_view.registered_view_at < datetime.utcnow( ) - settings.POST_VIEW_COOLDOWN_PERIOD: post_view.registered_view_at = datetime.utcnow() post.increment_view_count() post_view.save() return post_view
def upvote(cls, user, post, request=None): if not user.is_god and user.id == post.author_id: return None, False post_vote, is_vote_created = PostVote.objects.get_or_create( user=user, post=post, defaults=dict( ipaddress=parse_ip_address(request) if request else None, useragent=parse_useragent(request) if request else None)) if is_vote_created: post.increment_vote_count() post.author.increment_vote_count() return post_vote, is_vote_created
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 upvote(cls, user, comment, request=None): if not user.is_god and user.id == comment.author_id: return None, False post_vote, is_vote_created = CommentVote.objects.get_or_create( user=user, comment=comment, defaults=dict( post=comment.post, ipaddress=parse_ip_address(request) if request else None, )) if is_vote_created: comment.increment_vote_count() comment.author.increment_vote_count() return post_vote, is_vote_created
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 })